貪吃蛇大家都玩過吧,今天手把手教你們自己寫一個貪吃蛇游戲,
1.有關游戲的專案我們需要用到pygame庫,用win+s輸入cmd視窗,用 pip install pygame 命令安裝就好了,

2.如果出現了這種版本過低的警告,可以輸入 easy_install -U pip 命令進行版本升級,等待一段時間就可以了,

3.輸入 pip list 可以查看當前版本,

4.下面就是完整的代碼:
import pygame, sys, random, time
# 從pygame模塊匯入常用的函式和常量
from pygame.locals import *
# 定義顏色變數
black_colour = pygame.Color(0, 0, 0)
white_colour = pygame.Color(255, 255, 255)
red_colour = pygame.Color(255, 0, 0)
grey_colour = pygame.Color(150, 150, 150)
# 定義游戲結束函式
def GameOver(gamesurface):
# 設定提示字體的格式
GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)
# 設定提示字體的顏色
GameOver_colour = GameOver_font.render('Game Over', True, grey_colour)
# 設定提示位置
GameOver_location = GameOver_colour.get_rect()
GameOver_location.midtop = (320, 10)
# 系結以上設定到句柄
gamesurface.blit(GameOver_colour, GameOver_location)
# 提示運行資訊
pygame.display.flip()
# 休眠5秒
time.sleep(5)
# 退出游戲
pygame.quit()
# 退出程式
sys.exit()
# 定義主函式
def main():
# 初始化pygame,為使用硬體做準備
pygame.init()
pygame.time.Clock()
ftpsClock = pygame.time.Clock()
# 創建一個視窗
gamesurface = pygame.display.set_mode((640, 480))
# 設定視窗的標題
pygame.display.set_caption('tanchishe snake')
# 初始化變數
# 初始化貪吃蛇的起始位置
snakeposition = [100, 100]
# 初始化貪吃蛇的長度
snakelength = [[100, 100], [80, 100], [60, 100]]
# 初始化目標方塊的位置
square_purpose = [300, 300]
# 初始化一個數來判斷目標方塊是否存在
square_position = 1
# 初始化方向,用來使貪吃蛇移動
derection = "right"
change_derection = derection
# 進行游戲主回圈
while True:
# 檢測按鍵等pygame事件
for event in pygame.event.get():
if event.type == QUIT:
# 接收到退出事件后,退出程式
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
# 判斷鍵盤事件,用w,s,a,d來表示上下左右
if event.key == K_RIGHT or event.key == ord('d'):
change_derection = "right"
if event.key == K_LEFT or event.key == ord('a'):
change_derection = "left"
if event.key == K_UP or event.key == ord('w'):
change_derection = "up"
if event.key == K_DOWN or event.key == ord('s'):
change_derection = "down"
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
# 判斷移動的方向是否相反
if change_derection == 'left' and not derection == 'right':
derection = change_derection
if change_derection == 'right' and not derection == 'left':
derection = change_derection
if change_derection == 'up' and not derection == 'down':
derection = change_derection
if change_derection == 'down' and not derection == 'up':
derection = change_derection
# 根據方向,改變坐標
if derection == 'left':
snakeposition[0] -= 20
if derection == 'right':
snakeposition[0] += 20
if derection == 'up':
snakeposition[1] -= 20
if derection == 'down':
snakeposition[1] += 20
# 增加蛇的長度
snakelength.insert(0, list(snakeposition))
# 判斷是否吃掉目標方塊
if snakeposition[0] == square_purpose[0] and snakeposition[1] == square_purpose[1]:
square_position = 0
else:
snakelength.pop()
# 重新生成目標方塊
if square_position == 0:
# 隨機生成x,y,擴大二十倍,在視窗范圍內
x = random.randrange(1, 32)
y = random.randrange(1, 24)
square_purpose = [int(x * 20), int(y * 20)]
square_position = 1
# 繪制pygame顯示層
gamesurface.fill(black_colour)
for position in snakelength:
pygame.draw.rect(gamesurface, white_colour, Rect(position[0], position[1], 20, 20))
pygame.draw.rect(gamesurface, red_colour, Rect(square_purpose[0], square_purpose[1], 20, 20))
# 重繪pygame顯示層
pygame.display.flip()
# 判斷是否死亡
if snakeposition[0] < 0 or snakeposition[0] > 620:
GameOver(gamesurface)
if snakeposition[1] < 0 or snakeposition[1] > 460:
GameOver(gamesurface)
for snakebody in snakelength[1:]:
if snakeposition[0] == snakebody[0] and snakeposition[1] == snakebody[1]:
GameOver(gamesurface)
# 控制游戲速度
ftpsClock.tick(5)
if __name__ == "__main__":
main()
5.下面是運行結果:


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290754.html
標籤:其他
