赞
踩
贪吃蛇是一个很常见的小游戏,我们如何用Python去实现呢。
- pip install pygame
- pip install keyboard
- pip install pickledb
通过命令提示符安装所需模块。(以上非Python代码)
- #导入
- import pygame, keyboard, random, threading, time, pickledb
这个程序用到了pygame作为显示模块,keyboard捕获键盘操,pickledb记录最高纪录。
- #显示规则
- print()
- print('方向键控制方向')
- print('\'esc\'退出')
- print('\'alt\'暂停')
- print('\'space\'继续')
- print('每吃掉一个莓果,蛇身长长一格')
- print('撞到蛇身或窗口边缘即为死亡')
- print('死亡后\'esc\'退出,\'enter\'再来一局')
- #初始化
- pygame.init()
-
- #设定窗口
- screen = pygame.display.set_mode((500, 500))
- pygame.display.set_caption('Greedy Snake')
执行以上代码就设定好了贪吃蛇的窗口。
- #设定蛇身
- body = pygame.Surface((20, 20))
- body.fill('yellow')
-
- #设定莓果
- berries = pygame.Surface((20, 20))
- berries.fill('red')
-
- #设定数字方块(用于显示数字)
- block = pygame.Surface((4, 4))
- #设定变量
- T = 0
- db = pickledb.load('Mark.db', False)
- HI = db.get('HiMark')
- B = False
- def keyname(key) :
- #键盘捕获
- global KEY
- KEY = key.name
- keyboard.on_press(keyname)
用on_press绑定了keyname后,当键盘按下时会执行keyname函数。(key.name是按键的名称)
- def snake() :
- #显示蛇身
- global path
- for xy in path :
- screen.blit(body, xy)
-
- def food() :
- #显示食物
- global fx, fy
- screen.blit(berries, (fx, fy))
-
- def eat() :
- #检测是否吃掉食物
- if (x, y) == (fx, fy) :
- return True
- else :
- return False
-
- def crash() :
- #检测是否碰撞
- if path.count((x, y)) != 0 :
- return True
- elif x < 0 or x > 480 or y < 0 or y > 480 :
- return True
- else :
- return False
-
- def count() :
- #计时线程函数
- global T
- while True :
- if B :
- time.sleep(1)
- T += 1
-
- def timer() :
- #显示时间
- global T, number
- a = T // 100
- b = T % 100 // 10
- c = T % 10
- a = number[a]
- b = number[b]
- c = number[c]
- X, Y = 6, 10
- block.fill('white')
- for n in (a, b, c) :
- for xy in n :
- x = X + xy[0] * 4
- y = Y + xy[1] * 4
- screen.blit(block, (x, y))
- X += 32
-
- def mark() :
- #显示分数
- global M, number
- a = M // 100
- b = M % 100 // 10
- c = M % 10
- a = number[a]
- b = number[b]
- c = number[c]
- X, Y = 395, 10
- block.fill('white')
- for n in (a, b, c) :
- for xy in n :
- x = X + xy[0] * 4
- y = Y + xy[1] * 4
- screen.blit(block, (x, y))
- X += 32
-
- def hi() :
- #显示最高纪录
- global HI, number
- a = HI // 100
- b = HI % 100 // 10
- c = HI % 10
- a = number[a]
- b = number[b]
- c = number[c]
- X, Y = 263, 10
- block.fill('grey')
- for n in (a, b, c) :
- for xy in n :
- x = X + xy[0] * 4
- y = Y + xy[1] * 4
- screen.blit(block, (x, y))
- X += 32
- #启动计时线程
- time_count_thread = threading.Thread(target = count)
- time_count_thread.start()
线程可以在主线程的运行过程中运行,起到计时的作用。
以下就是程序的主要部分,实现了对按键名称进行判断、蛇的移动、蛇吃掉食物后的生长和判断死亡。
- while True :
- #变量初始化
- KEY = 'up'
- lKEY = KEY
- x, y = 240, 240
- path = []
- path.append((x, y))
- fx, fy = random.randrange(20, 460, 20), random.randrange(20, 460, 20)
- while path.count((fx, fy)) != 0 :
- fx, fy = random.randrange(20, 460, 20), random.randrange(20, 460, 20)
- T = 0
- M = 0
- B = True
-
- while True :
- time.sleep(0.1)
-
- #检测是否按下退出按钮
- for event in pygame.event.get() :
- if event.type == pygame.QUIT :
- pygame.quit()
- exit()
-
- #检测键盘按键
- if (KEY == 'up' and lKEY != 'down') or (KEY == 'w' and lKEY != 's') :
- y -= 20
- lKEY = KEY
- elif (KEY == 'down' and lKEY != 'up') or (KEY == 's' and lKEY != 'w') :
- y += 20
- lKEY = KEY
- elif (KEY == 'left' and lKEY != 'right') or (KEY == 'a' and lKEY != 'd') :
- x -= 20
- lKEY = KEY
- elif (KEY == 'right' and lKEY != 'left') or (KEY == 'd' and lKEY != 'a') :
- x += 20
- lKEY = KEY
- elif KEY == 'alt' or KEY == 'right alt' :
- B = False
- while True :
- pygame.event.wait()
- if KEY == 'space' :
- KEY = lKEY
- B = True
- break
- elif KEY == 'esc' :
- pygame.quit()
- if M > HI :
- HI = M
- db.set('HiMark', HI)
- db.dump()
- exit()
- continue
- elif KEY == 'esc' :
- pygame.quit()
- if M > HI :
- HI = M
- db.set('HiMark', HI)
- db.dump()
- exit()
- else :
- KEY = lKEY
- continue
-
- #检测是否碰撞或吃掉食物
- if crash() :
- break
- if eat() :
- M += 1
- fx, fy = random.randrange(20, 460, 20), random.randrange(20, 460, 20)
- while path.count((fx, fy)) != 0 :
- fx, fy = random.randrange(20, 460, 20), random.randrange(20, 460, 20)
- else :
- del path[0]
-
- #更新屏幕内容
- path.append((x, y))
- screen.fill('black')
- snake()
- food()
- timer()
- mark()
- hi()
- pygame.display.flip()
-
- #保存最高纪录
- if M > HI :
- HI = M
- db.set('HiMark', HI)
- db.dump()
-
- B = False
-
- #检测是否退出或再来一局
- while True :
- event = pygame.event.wait()
- if event.type == pygame.QUIT :
- pygame.quit()
- exit()
- elif KEY == 'esc' :
- pygame.quit()
- exit()
- elif KEY == 'return' or KEY == 'enter' :
- break
如果你将以上所有的代码按顺序编写在一些,就可以运行完整的贪吃蛇了。这些代码都很简单,适合初学者学习。
希望这篇文章对您有帮助,感谢浏览。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。