python如何实现简单贪吃蛇小游戏
短信预约 -IT技能 免费直播动态提醒
本篇内容介绍了“python如何实现简单贪吃蛇小游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1. 导入游戏库
import pgzrunimport random
2.游戏初始化
# 窗口大小WIDTH = 600HEIGHT = 480# 贪吃蛇bodys = [ [100,100],[80,100], [60,100], [40,100], [20,100]]head = [100,100]d = 'right'# 食物food = [290,290]# 得分score=0
3.游戏窗口绘制
def draw():#清空屏幕screen.clear()# 绘制网格for i in range(1,24):screen.draw.line((0,i*20),(600,i*20),'gray')for i in range(1,30):screen.draw.line((i*20,0),(i*20,480),'gray')#绘制蛇for body in bodys:rect = Rect(body,(20,20))screen.draw.filled_rect(rect,(0,0,255))inner = [body[0]+2,body[1]+2]rect = Rect(inner, (15, 15),center='center')screen.draw.filled_rect(rect, (0, 0, 180))# 绘制头rect = Rect(head, (20, 20))screen.draw.filled_rect(rect, (0, 200,0))inner = [head[0] + 2, head[1] + 2]rect = Rect(inner, (15, 15))screen.draw.filled_rect(rect, (0, 255, 12))# 绘制食物screen.draw.filled_circle(food,10, '#ffddee')# 绘制得分screen.draw.text('score:'+str(score),(20, 20), color="orange",fontsize=30)
4.蛇的移动功能
def run():global food,d,head,bodys,score# 新增一个格子的身体if d=='right':head[0] += 20elif d=='left':head[0] -= 20elif d=='up':head[1] -= 20else:head[1] += 20bodys.insert(0,list(head))if head[0] == food[0]-10 and head[1] == food[1] - 10:food = [random.randint(1,30)*20-10,random.randint(1,20)*20-10]score+=1if score>3:clock.unschedule(run)clock.schedule_interval(run, 0.1)else:bodys.pop()# 撞墙后重新开始if head[0]<0 or head[0]>580 or head[1]<0 or head[1]>480 or head inbodys[1:]:# 蛇回到初始位置bodys = [[100, 100], [80, 100], [60, 100], [40, 100], [20, 100]]head = [100, 100]# 方向向右d = 'right'# 得分清零score=0clock.unschedule(run)clock.schedule_interval(run, 0.3)
5.按键控制蛇的行走方向
# 按键控制蛇的行走方向def on_key_down(key):global d# 改变方向if key == keys.DOWN and d != 'up':d = 'down'if key == keys.UP and d != 'down':d = 'up'if key == keys.LEFT and d != 'right':d = 'left'if key == keys.RIGHT and d != 'left':d = 'right'
6.启动游戏
# 定时设置clock.schedule_interval(run, 0.3)# 播放音乐music.play('music.mp3')# 启动游戏pgzrun.go()
完整代码
# 1.导入库import pgzrunimport random# 2.初始化# 窗口大小WIDTH = 600HEIGHT = 480# 贪吃蛇bodys = [[100, 100], [80, 100], [60, 100], [40, 100], [20, 100]]head = [100, 100]d = 'right'# 食物food = [290, 290]# 得分score = 0# 3.游戏窗口绘制def draw(): # 清空屏幕 screen.clear() # 绘制网格 for i in range(1, 24): screen.draw.line((0, i * 20), (600, i * 20), 'gray') for i in range(1, 30): screen.draw.line((i * 20, 0), (i * 20, 480), 'gray') # 绘制蛇 for body in bodys: rect = Rect(body, (20, 20)) screen.draw.filled_rect(rect, (0, 0, 255)) inner = [body[0] + 2, body[1] + 2] rect = Rect(inner, (15, 15), center='center') screen.draw.filled_rect(rect, (128, 0, 128)) # 紫色 # 绘制头 rect = Rect(head, (20, 20)) screen.draw.filled_rect(rect, (0, 200, 0)) inner = [head[0] + 2, head[1] + 2] rect = Rect(inner, (15, 15)) screen.draw.filled_rect(rect, (0, 255, 12)) # 绘制食物 颜色为红色 screen.draw.filled_circle(food, 10, '#ff0000') # 绘制得分 screen.draw.text('score:' + str(score), (20, 20), color="red", fontsize=30)# 4.蛇的移动功能def run(): global food, d, head, bodys, score # 新增一个格子的身体 if d == 'right': head[0] += 20 elif d == 'left': head[0] -= 20 elif d == 'up': head[1] -= 20 else: head[1] += 20 bodys.insert(0, list(head)) if head[0] == food[0] - 10 and head[1] == food[1] - 10: food = [random.randint(1, 30) * 20 - 10, random.randint(1, 20) * 20 - 10] score += 1 if score > 10: clock.unschedule(run) clock.schedule_interval(run, 0.1) else: bodys.pop() # 撞墙后重新开始 if head[0] < 0 or head[0] > 580 or head[1] < 0 or head[1] > 480 or head in bodys[1:]: # 蛇回到初始位置 bodys = [[100, 100], [80, 100], [60, 100], [40, 100], [20, 100]] head = [100, 100] # 方向向右 d = 'right' # 得分清零 score = 0 clock.unschedule(run) clock.schedule_interval(run, 0.3)# 按键控制蛇的行走方向def on_key_down(key): global d # 改变方向 if key == keys.DOWN and d != 'up': d = 'down' if key == keys.UP and d != 'down': d = 'up' if key == keys.LEFT and d != 'right': d = 'left' if key == keys.RIGHT and d != 'left': d = 'right'# 6.启动游戏# 定时设置clock.schedule_interval(run, 0.3)# 播放音乐music.play('music.mp3')# 启动游戏pgzrun.go()
“python如何实现简单贪吃蛇小游戏”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341