本文實例為大家分享了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