当前位置: 首页 > news >正文

重庆企业网站如何推广交换链接适用于哪些网站

重庆企业网站如何推广,交换链接适用于哪些网站,wordpress自定义界面,凡科 做网站准备步骤 项目开发使用【Mu 编辑器】 1.新建项目,并导入游戏图片 游戏编写 1.创建场景 SIZE 15 # 每个格子的大小 WIDTH SIZE * 30 # 游戏场景总宽度 HEIGHT SIZE * 30 # 游戏场景总高度def draw():screen…

准备步骤

项目开发使用【Mu 编辑器】

1.新建项目,并导入游戏图片

在这里插入图片描述

游戏编写

1.创建场景

SIZE = 15                       # 每个格子的大小
WIDTH = SIZE * 30               # 游戏场景总宽度
HEIGHT = SIZE * 30              # 游戏场景总高度def draw():screen.fill((255,255,255))  # 设置背景色为白色

运行游戏,可见一个简单的场景被绘制出来

在这里插入图片描述

2.创建主角

snake_head = Actor("snake_head",(30, 30))   # 绘制蛇头图标,初始坐标为(30,30)def draw():screen.fill((255,255,255)) snake_head.draw()						# 绘制蛇头

运行后,场景中可见多了一个蛇头图标

在这里插入图片描述

3.移动蛇头

direction = "R"                 			# 蛇头初始移动方向
counter = 0                     			# 循环标识,控制蛇头位置变更频率
# 监测按下的按键
def check_keys():global direction# ←键被按下,且当前方向不为向右if keyboard.left and direction != "R":direction = "L"# →键被按下,且当前方向不为向左elif keyboard.right and direction != "L":direction = "R"# ↑键被按下,且当前方向不为向下elif keyboard.up and direction != "D":direction = "U"# ↓键被按下,且当前方向不为向上elif keyboard.down and direction != "U":direction = "D"
# 蛇头移动位置逻辑        
def update_snake():    global countercounter += 1# 每执行10次update函数,才执行一次下方代码,减缓蛇头位置变更速度if counter < 10:returnelse:counter = 0# 蛇头移动逻辑,改变蛇头位置实现移动效果if direction == "L":snake_head.x -= SIZEelif direction == "R":snake_head.x += SIZEelif direction == "U":snake_head.y -= SIZEelif direction == "D":snake_head.y += SIZE
def update():check_keys()update_snake()

此刻开始游戏,蛇头就会开始移动,按下方向键,便会改变蛇头移动方向

在这里插入图片描述

可使用angle属性,控制图标翻转,实现蛇头朝向效果

# 监测按下的按键
def check_keys():global direction# ←键被按下,且当前方向不为向右if keyboard.left and direction != "R":direction = "L"snake_head.angle = 0# →键被按下,且当前方向不为向左elif keyboard.right and direction != "L":direction = "R"snake_head.angle = 180# ↑键被按下,且当前方向不为向下elif keyboard.up and direction != "D":direction = "U"snake_head.angle = 90# ↓键被按下,且当前方向不为向上elif keyboard.down and direction != "U":direction = "D"snake_head.angle = -90

4.添加食物

food = Actor("snake_food")
# 在窗口内生成随机坐标,作为食物出现的位置
food.x = random.randint(2,WIDTH // SIZE - 2) * SIZE     
food.y = random.randint(2,HEIGHT // SIZE - 2) * SIZEdef draw():screen.fill((255,255,255)) snake_head.draw()food.draw()								# 绘制食物

此时运行游戏,窗口中会随机生成食物,但蛇头触碰,并不会被吃掉

在这里插入图片描述

5.吃食物

length = 1                                          # 贪吃蛇当前的初始长度
body = []                                           # 贪吃蛇蛇身各部位位置,不含蛇头
# draw函数中添加蛇身绘制方法
def draw():screen.fill((255,255,255))                      snake_head.draw()                               food.draw()                                     # 通过循环列表绘制出所有蛇身for b in body:b.draw()
# 蛇头移动位置逻辑中增加蛇身移动逻辑
def update_snake():global countercounter += 1if counter < 10:returnelse:counter = 0# 如果蛇身数量等于蛇的总长度,则移除列表记录的第一个蛇身,实际是移除蛇尾if len(body) == length:body.remove(body[0])# 在列表后追加当前蛇头的坐标,用于绘制新的蛇身;配合上面删除实现蛇身位置变更效果body.append(Actor("snake_body",(snake_head.x, snake_head.y)))if direction == "L":snake_head.x -= SIZEelif direction == "R":snake_head.x += SIZEelif direction == "U":snake_head.y -= SIZEelif direction == "D":snake_head.y += SIZE
# 吃食物逻辑    
def eat_food():global length# 如果当前食物坐标与蛇头坐标位置重叠if food.x == snake_head.x and food.y == snake_head.y:# 在窗口内随机坐标位置重新生成食物food.x = random.randint(2,WIDTH // SIZE - 2) * SIZEfood.y = random.randint(2,HEIGHT // SIZE - 2) * SIZElength += 1                                 # 每吃一个食物,贪吃蛇长度+1   
# 将eat_food方法写入update中
def update():check_keys()update_snake()eat_food()

6.游戏结束判定

finished = False                                    # 游戏结束标识
# draw函数中添加失败弹窗
def draw():screen.fill((255,255,255))                      snake_head.draw()                               food.draw()                                     for b in body:b.draw()# 绘制失败提示图像if finished:screen.draw.text("Game Over!",center=(WIDTH // 2,HEIGHT // 2),fontsize = 50,color = "red")
# 检测游戏是否结束
def check_gameover():global finished# 如果蛇头的位置超出窗口,则判定失败if snake_head.left < 0 or snake_head.right > WIDTH or snake_head.top < 0 or snake_head.bottom > HEIGHT:finished = True# 遍历蛇身,判断是否存在与蛇头重叠的蛇身,有,则判定失败for n in range(len(body) - 1):if(body[n].x == snake_head.x and body[n].y == snake_head.y):finished = True  
# update函数中调用检测方法     
def update():# 如果游戏已结束,则不再更新游戏,即暂停游戏 if finished:returncheck_gameover()check_keys()eat_food()update_snake()

运行游戏后,当蛇头碰到蛇身或墙壁,则会暂停游戏,弹窗游戏失败提示语

在这里插入图片描述

完整代码

import random
SIZE = 15                                           # 每个格子的大小
WIDTH = SIZE * 30                                   # 游戏场景总宽度
HEIGHT = SIZE * 30                                  # 游戏场景总高度
direction = "R"                                     # 蛇头初始移动方向
counter = 0                                         # 循环标识
snake_head = Actor("snake_head",(30, 30))           # 绘制蛇头图标,初始坐标为(30,30)
length = 1                                          # 贪吃蛇当前的初始长度
body = []                                           # 贪吃蛇蛇身各部位位置,不含蛇头
finished = False                                    # 游戏结束标识food = Actor("snake_food")
# 在窗口内生成随机坐标,作为食物出现的位置
food.x = random.randint(2,WIDTH // SIZE - 2) * SIZE
food.y = random.randint(2,HEIGHT // SIZE - 2) * SIZEdef draw():screen.fill((255,255,255))                      # 设置背景色为白色snake_head.draw()                               # 绘制蛇头food.draw()                                     # 绘制食物# 通过循环列表绘制出所有蛇身for b in body:b.draw()# 绘制失败提示图像if finished:screen.draw.text("Game Over!",center=(WIDTH // 2,HEIGHT // 2),fontsize = 50,color = "red")# 监测按下的按键
def check_keys():global direction# ←键被按下,且当前方向不为向右if keyboard.left and direction != "R":direction = "L"snake_head.angle = 180# →键被按下,且当前方向不为向左elif keyboard.right and direction != "L":direction = "R"snake_head.angle = 0# ↑键被按下,且当前方向不为向下elif keyboard.up and direction != "D":direction = "U"snake_head.angle = 90# ↓键被按下,且当前方向不为向上elif keyboard.down and direction != "U":direction = "D"snake_head.angle = -90# 贪吃蛇移动位置各部位变化逻辑
def update_snake():global countercounter += 1# 每执行10次update函数,才执行一次下方代码,减缓蛇头位置变更速度if counter < 10:returnelse:counter = 0# 如果蛇身数量等于蛇的总长度,则移除列表记录的第一个蛇身,实际是移除蛇尾if len(body) == length:body.remove(body[0])# 在列表后追加当前蛇头的坐标,用于绘制新的蛇身;配合上面删除实现蛇身位置变更效果body.append(Actor("snake_body",(snake_head.x, snake_head.y)))# 蛇头移动逻辑,改变蛇头位置实现移动效果if direction == "L":snake_head.x -= SIZEelif direction == "R":snake_head.x += SIZEelif direction == "U":snake_head.y -= SIZEelif direction == "D":snake_head.y += SIZE# 吃食物
def eat_food():global length# 如果当前食物坐标与蛇头坐标位置重叠if food.x == snake_head.x and food.y == snake_head.y:# 在窗口内随机坐标位置重新生成食物food.x = random.randint(2,WIDTH // SIZE - 2) * SIZEfood.y = random.randint(2,HEIGHT // SIZE - 2) * SIZElength += 1                                 # 每吃一个食物,贪吃蛇长度+1
# 检测游戏是否结束
def check_gameover():global finished# 如果蛇头的位置超出窗口,则判定失败if snake_head.left < 0 or snake_head.right > WIDTH or snake_head.top < 0 or snake_head.bottom > HEIGHT:finished = True# 遍历蛇身,判断是否存在与蛇头重叠的蛇身,有,则判定失败for n in range(len(body) - 1):if(body[n].x == snake_head.x and body[n].y == snake_head.y):finished = True     def update():# 如果游戏已结束,则不再更新游戏,即暂停游戏 if finished:returncheck_gameover()check_keys()eat_food()update_snake()
http://www.hotlads.com/news/3687.html

相关文章:

  • 网站都有什么类型朋友圈信息流广告投放价格
  • 成为网站建设人员措施网站百度收录查询
  • 北京网站备案公司广州优化营商环境条例
  • wordpress注册无法设置密码东莞百度推广优化公司
  • 怎样做外贸网站建设怎么做一个网站的步骤
  • 网站 建设 现状企业培训方案
  • js网站登录怎么做网络广告投放方案
  • 装备可以卖人民币的手游长沙网站优化方法
  • 品牌型网站的设计网站查询器
  • asp网站表格代码网站排名点击工具
  • 360百度网站怎么做长春网络优化哪个公司在做
  • wordpress站点添加skype网络服务公司
  • wordpress客户端有什么用厦门站长优化工具
  • 做网站维护的是什么公司seo教程书籍
  • 网站后门怎么去除云南疫情最新情况
  • 厦门建设银行招聘网站万网创始人
  • 资讯网站想学销售去哪培训
  • 网站如何做页数百度旅游官网
  • 衡阳网站建设icp备网站关键词优化推广哪家好
  • 珠海手机网站建设seo优化网站优化排名
  • 商贸公司注册需要多少钱武汉抖音seo搜索
  • 网站无法连接服务器搜索引擎优化实训
  • 东莞做公司网站西安网络科技有限公司
  • 深圳网站制作培训制作网站大概多少钱
  • 河北网站备案注销网站收录一键提交
  • 为中国移动做网站的公司叫什么友情链接互换
  • wordpress 说说 主题关键词优化公司电话
  • 网站首页的动态视频怎么做的网站排名怎么优化
  • springboot做音乐网站动态网站的制作与设计
  • 长沙模板网站建设互动营销策略