1- import pygame , sys , time , random
2- from pygame .locals import *
31
4- # 定义颜色变量
5- redColour = pygame .Color (255 , 0 , 0 )
6- blackColour = pygame .Color (0 , 0 , 0 )
7- whiteColour = pygame .Color (255 , 255 , 255 )
8- greyColour = pygame .Color (150 , 150 , 150 )
92
103
11- def gameOver (playSurface , score ):
12- gameOverFont = pygame .font .SysFont ('arial.ttf' , 54 )
13- gameOverSurf = gameOverFont .render ('Game Over!' , True , greyColour )
14- gameOverRect = gameOverSurf .get_rect ()
15- gameOverRect .midtop = (300 , 10 )
16- playSurface .blit (gameOverSurf , gameOverRect )
17- scoreFont = pygame .font .SysFont ('arial.ttf' , 54 )
18- scoreSurf = scoreFont .render ('Score:' + str (score ), True , greyColour )
19- scoreRect = scoreSurf .get_rect ()
20- scoreRect .midtop = (300 , 50 )
21- playSurface .blit (scoreSurf , scoreRect )
22- pygame .display .flip ()
23- time .sleep (5 )
24- pygame .quit ()
25- sys .exit ()
26-
27-
28- def main ():
29- # 初始化pygame
30- pygame .init ()
31- fpsClock = pygame .time .Clock ()
32- # 创建pygame显示层
33- playSurface = pygame .display .set_mode ((600 , 460 ))
34- pygame .display .set_caption ('Snake Game' )
35- # 初始化变量
36- snakePosition = [100 , 100 ] # 贪吃蛇 蛇头的位置
37- snakeSegments = [[100 , 100 ]] # 贪吃蛇 蛇的身体,初始为一个单位
38- raspberryPosition = [300 , 300 ] # 树莓的初始位置
39- raspberrySpawned = 1 # 树莓的个数为1
40- direction = 'right' # 初始方向为右
41- changeDirection = direction
42- score = 0 # 初始得分
43- while True :
44- # 检测例如按键等pygame事件
45- for event in pygame .event .get ():
46- if event .type == QUIT :
47- pygame .quit ()
48- sys .exit ()
49- elif event .type == KEYDOWN :
50- # 判断键盘事件
51- if event .key == K_RIGHT or event .key == ord ('d' ):
52- changeDirection = 'right'
53- if event .key == K_LEFT or event .key == ord ('a' ):
54- changeDirection = 'left'
55- if event .key == K_UP or event .key == ord ('w' ):
56- changeDirection = 'up'
57- if event .key == K_DOWN or event .key == ord ('s' ):
58- changeDirection = 'down'
59- if event .key == K_ESCAPE :
60- pygame .event .post (pygame .event .Event (QUIT ))
61- # 判断是否输入了反方向
62- if changeDirection == 'right' and not direction == 'left' :
63- direction = changeDirection
64- if changeDirection == 'left' and not direction == 'right' :
65- direction = changeDirection
66- if changeDirection == 'up' and not direction == 'down' :
67- direction = changeDirection
68- if changeDirection == 'down' and not direction == 'up' :
69- direction = changeDirection
70- # 根据方向移动蛇头的坐标
71- if direction == 'right' :
72- snakePosition [0 ] += 20
73- if direction == 'left' :
74- snakePosition [0 ] -= 20
75- if direction == 'up' :
76- snakePosition [1 ] -= 20
77- if direction == 'down' :
78- snakePosition [1 ] += 20
79- # 增加蛇的长度
80- snakeSegments .insert (0 , list (snakePosition ))
81- # 判断是否吃掉了树莓
82- if snakePosition [0 ] == raspberryPosition [0 ] and snakePosition [1 ] == raspberryPosition [1 ]:
83- raspberrySpawned = 0
84- else :
85- snakeSegments .pop ()
86- # 如果吃掉树莓,则重新生成树莓
87- if raspberrySpawned == 0 :
88- x = random .randrange (1 , 30 )
89- y = random .randrange (1 , 23 )
90- raspberryPosition = [int (x * 20 ), int (y * 20 )]
91- raspberrySpawned = 1
92- score += 1
93- # 绘制pygame显示层
94- playSurface .fill (blackColour )
95- for position in snakeSegments :
96- pygame .draw .rect (playSurface , whiteColour , Rect (position [0 ], position [1 ], 20 , 20 ))
97- pygame .draw .rect (playSurface , redColour , Rect (raspberryPosition [0 ], raspberryPosition [1 ], 20 , 20 ))
98- # 刷新pygame显示层
99- pygame .display .flip ()
100- # 判断是否死亡
101- if snakePosition [0 ] > 600 or snakePosition [0 ] < 0 :
102- gameOver (playSurface , score )
103- if snakePosition [1 ] > 460 or snakePosition [1 ] < 0 :
104- gameOver (playSurface , score )
105- for snakeBody in snakeSegments [1 :]:
106- if snakePosition [0 ] == snakeBody [0 ] and snakePosition [1 ] == snakeBody [1 ]:
107- gameOver (playSurface , score )
108- # 控制游戏速度
109- fpsClock .tick (5 )
110-
4+ def xx ():
5+ pass
1116
1127if __name__ == "__main__" :
113- main ()
8+ # main()
9+ xx (1 ,2 )
0 commit comments