Python貪吃蛇小游戲實例分享
發(fā)布日期:2022-01-24 15:30 | 文章來源:源碼中國
本文實例為大家分享了Python實現(xiàn)貪吃蛇小游戲的具體代碼,供大家參考,具體內容如下
使用的庫
pygame 、random 、pyautogui
流程簡述
1.設置初始參數(shù)
設置每個網(wǎng)格大小為 20 px ,size可以隨意修改但最好為20的倍數(shù),設置初始方向向右,初始蛇長為 3 。
# 初始化參數(shù)
size = 320
screen = pygame.display.set_mode([size,size],0 , 32)
pygame.display.set_caption("貪吃蛇")
cell_size = 20
cell_num = int(size/20)
x , y = [60,0]
# 初始方向向右
dir_snake = 'R'
body_snake = [[0,0],[20,0],[40,0],[60,0]]
# 初始蛇長為3
len_snake = 3
# 初始食物坐標
food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
# 蛇移動速度
clock = pygame.time.Clock()
2.鍵盤控制
鍵盤上下左右控制蛇方向,禁止反向。
3.食物
蛇頭吃到食物后,蛇長加一 ,random 一個隨機坐標,如果坐標再蛇身上則繼續(xù) random。
if [x,y] == food_coor[:2]: food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20] if food_coor[:2] in body_snake[-2:-len_snake -1 :-1]: food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20] len_snake +=1
4. 勝負判斷
蛇頭與蛇身碰撞判輸,彈出游戲結束窗口,說明最終蛇長為多長。
if body_snake[-1] in body_snake[-2:-len_snake -1 :-1]:
pyautogui.alert(text='游戲結束,最終蛇長為{}'.format(len_snake))
exit()
代碼及結果
代碼
import pygame , random ,pyautogui
from pygame.locals import *
# 初始化參數(shù)
size = 500
screen = pygame.display.set_mode([size,size],0 , 32)
pygame.display.set_caption("貪吃蛇")
cell_size = 20
cell_num = int(size/20)
x , y = [60,0]
# 初始方向向右
dir_snake = 'R'
body_snake = [[0,0],[20,0],[40,0],[60,0]]
# 初始蛇長為3
len_snake = 3
# 初始食物坐標
food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
# 蛇移動速度
clock = pygame.time.Clock()
# bg
def bg():
for i in range(0, size , int(cell_size)):
pygame.draw.line(screen,[72,72,72],[i,0],[i,size])
for i in range(0, size, int(cell_size)):
pygame.draw.line(screen, [72, 72, 72], [0, i], [size,i])
# 蛇
def snake(K):
for x,y in K[:-len_snake - 1:-1]:
pygame.draw.rect(screen,[255,255,255],[x,y,20,20],0)
# 食物
def food():
pygame.draw.rect(screen,[255,0,0],food_coor,0)
# 主循環(huán)
def running():
global x ,y , dir_snake ,food_coor , len_snake
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.fill((40, 43, 46))
# 方向規(guī)則
if dir_snake == 'U':
y -= 20
elif dir_snake == 'R':
x += 20
elif dir_snake == 'D':
y += 20
elif dir_snake == 'L':
x -= 20
# 鍵盤方向控制
if event.type == KEYDOWN:
if event.key == K_LEFT and dir_snake != 'R':
dir_snake = 'L'
elif event.key == K_DOWN and dir_snake != 'U':
dir_snake = 'D'
elif event.key == K_RIGHT and dir_snake != 'L':
dir_snake = 'R'
elif event.key == K_UP and dir_snake != 'D':
dir_snake = 'U'
# 撞墻之后
if x < 0:
x += size
elif x >= size:
x -= size
elif y < 0:
y += size
elif y >= size:
y -= size
# draw蛇體
body_snake.append([x,y])
snake(body_snake)
food()
# 吃到食物后food換位置
if [x,y] == food_coor[:2]:
food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
if food_coor[:2] in body_snake[-2:-len_snake -1 :-1]:
food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
len_snake +=1
# 游戲結束
if body_snake[-1] in body_snake[-2:-len_snake -1 :-1]:
pyautogui.alert(text='游戲結束,最終蛇長為{}'.format(len_snake))
exit()
# 格線
bg()
# 右下角顯示蛇長
font = pygame.font.SysFont("simsunnsimsun", 40)
text_surface = font.render("{}".format(len_snake), True, (255,255, 255))
screen.blit(text_surface , (size-40,size-40))
pygame.display.update()
# 蛇的移動速度隨著蛇的長度而越來越快
clock.tick(len_snake * 2)
if __name__ == '__main__':
pygame.init()
running()
輸出結果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章
上一篇:
DHCP服務器與NIS服務器
下一篇:
關注官方微信