python贪吃蛇游戏代码

时间:2021-07-17 19:10:33

本文实例为大家分享了python贪吃蛇游戏的具体代码,供大家参考,具体内容如下

贪吃蛇游戏截图:

python贪吃蛇游戏代码

首先安装pygame,可以使用pip安装pygame:

pip install pygame

运行以下代码即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
import pygame,sys,time,random
from pygame.locals import *
# 定义颜色变量
redcolour = pygame.color(255,0,0)
blackcolour = pygame.color(0,0,0)
whitecolour = pygame.color(255,255,255)
greycolour = pygame.color(150,150,150)
 
# 定义gameover函数
def gameover(playsurface):
 gameoverfont = pygame.font.font('arial.ttf',72)
 gameoversurf = gameoverfont.render('game over', true, greycolour)
 gameoverrect = gameoversurf.get_rect()
 gameoverrect.midtop = (320, 10)
 playsurface.blit(gameoversurf, gameoverrect)
 pygame.display.flip()
 time.sleep(5)
 pygame.quit()
 sys.exit()
 
# 定义main函数
def main():
 # 初始化pygame
 pygame.init()
 fpsclock = pygame.time.clock()
 # 创建pygame显示层
 playsurface = pygame.display.set_mode((640,480))
 pygame.display.set_caption('raspberry snake')
 
 # 初始化变量
 snakeposition = [100,100]
 snakesegments = [[100,100],[80,100],[60,100]]
 raspberryposition = [300,300]
 raspberryspawned = 1
 direction = 'right'
 changedirection = direction
 while true:
 # 检测例如按键等pygame事件
 for event in pygame.event.get():
  if event.type == quit:
  pygame.quit()
  sys.exit()
  elif event.type == keydown:
  # 判断键盘事件
  if event.key == k_right or event.key == ord('d'):
   changedirection = 'right'
  if event.key == k_left or event.key == ord('a'):
   changedirection = 'left'
  if event.key == k_up or event.key == ord('w'):
   changedirection = 'up'
  if event.key == k_down or event.key == ord('s'):
   changedirection = 'down'
  if event.key == k_escape:
   pygame.event.post(pygame.event.event(quit))
 # 判断是否输入了反方向
 if changedirection == 'right' and not direction == 'left':
  direction = changedirection
 if changedirection == 'left' and not direction == 'right':
  direction = changedirection
 if changedirection == 'up' and not direction == 'down':
  direction = changedirection
 if changedirection == 'down' and not direction == 'up':
  direction = changedirection
 # 根据方向移动蛇头的坐标
 if direction == 'right':
  snakeposition[0] += 20
 if direction == 'left':
  snakeposition[0] -= 20
 if direction == 'up':
  snakeposition[1] -= 20
 if direction == 'down':
  snakeposition[1] += 20
 # 增加蛇的长度
 snakesegments.insert(0,list(snakeposition))
 # 判断是否吃掉了树莓
 if snakeposition[0] == raspberryposition[0] and snakeposition[1] == raspberryposition[1]:
  raspberryspawned = 0
 else:
  snakesegments.pop()
 # 如果吃掉树莓,则重新生成树莓
 if raspberryspawned == 0:
  x = random.randrange(1,32)
  y = random.randrange(1,24)
  raspberryposition = [int(x*20),int(y*20)]
  raspberryspawned = 1
 # 绘制pygame显示层
 playsurface.fill(blackcolour)
 for position in snakesegments:
  pygame.draw.rect(playsurface,whitecolour,rect(position[0],position[1],20,20))
  pygame.draw.rect(playsurface,redcolour,rect(raspberryposition[0], raspberryposition[1],20,20))
 
 # 刷新pygame显示层
 pygame.display.flip()
 # 判断是否死亡
 if snakeposition[0] > 620 or snakeposition[0] < 0:
  gameover(playsurface)
 if snakeposition[1] > 460 or snakeposition[1] < 0:
  for snakebody in snakesegments[1:]:
  if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
   gameover(playsurface)
 # 控制游戏速度
 fpsclock.tick(5)
 
if __name__ == "__main__":
 main()

操作方法:

上下左右键或wsad键控制

esc键退出游戏

下载代码:贪吃蛇游戏代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/qiu2013/p/6087627.html