手把手教你用Python製作貪吃蛇小遊戲
阿新 • • 發佈:2021-12-14
最近有很多的同學問,能不能用Python做出一個小遊戲來,而且最好要講清楚每一段幹嘛是用來幹嘛的
那行,今天將來講解一下用Python pygame做一個貪吃蛇的小遊戲
據說是貪吃蛇遊戲是1976年,Gremlin公司推出的經典街機遊戲,那我們今天用Python製作的這個貪吃蛇小遊戲是一個畫素版的,雖然簡陋,但還是可以玩起來的
很多人學習蟒蛇,不知道從何學起。 很多人學習python,掌握了基本語法之後,不知道在哪裡尋找案例上手。 很多已經做了案例的人,卻不知道如何去學習更多高深的知識。 那麼針對這三類人,我給大家提供一個好的學習平臺,免費獲取視訊教程,電子書,以及課程的原始碼! QQ群:101677771 歡迎加入,一起討論一起學習!
我們主要要做的內容
- 建立遊戲視窗
- 繪製貪吃蛇與食物
- 蛇吃食物
貪吃蛇的棋盤模型
現在就開始我們的程式碼,首先,還是匯入模組
import pygame
import random
import copy
1. 建立遊戲視窗
1.1 遊戲初始化
pygame.init() clock = pygame.time.Clock() # 設定遊戲時鐘 pygame.display.set_caption("貪吃蛇-解答、原始碼、相關資料可私信我") # 初始化標題 screen = pygame.display.set_mode((500, 500)) # 初始化視窗 窗體的大小為 500 500
1.2 初始化蛇的位置 蛇的長度 10 10 也就是蛇的 X Y 座標
snake_list = [[10, 10]]
首先設定蛇的一個執行方向 接下來判斷鍵盤事件在決定蛇的執行方向
蛇可以執行起來了,那麼接下來就是,吃食物增加自己的長度和不吃食物在不同的位置顯示
初始小蛇方向
move_up = False move_down = False move_left = False move_right = True
1.3 初始化食物的位置
x = random.randint(10, 490) y = random.randint(10, 490) food_point = [x, y]
1.4 開啟遊戲迴圈
running = True while running: # 遊戲時鐘 重新整理頻率 clock.tick(20)
1.5 填充背景為白色
screen.fill([255, 255, 255])
1.6 繪製背景
for x in range(0, 501, 10): pygame.draw.line(screen, (195, 197, 199), (x, 0), (x, 500), 1) pygame.draw.line(screen, (195, 197, 199), (0, x), (500, x), 1) food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0)
1.7繪製蛇
snake_rect = [] for pos in snake_list: # 1.7.1 繪製蛇的身子 snake_rect.append(pygame.draw.circle(screen, [255, 0, 0], pos, 5, 0))
2. 繪製貪吃蛇與食物
2.1 獲取蛇的長度,移動蛇的身子
pos = len(snake_list) - 1 while pos > 0: snake_list[pos] = copy.deepcopy(snake_list[pos - 1]) pos -= 1
2.2 更改蛇頭位置
if move_up: snake_list[pos][1] -= 10 if snake_list[pos][1] < 0: snake_list[pos][1] = 500 if move_down: snake_list[pos][1] += 10 if snake_list[pos][1] > 500: snake_list[pos][1] = 0 if move_left: snake_list[pos][0] -= 10 if snake_list[pos][0] < 0: snake_list[pos][0] = 500 if move_right: snake_list[pos][0] += 10 if snake_list[pos][0] > 500: snake_list[pos][0] = 0
2.3 鍵盤控制移動職位
for event in pygame.event.get(): # print(event) # 判斷按下的按鍵 if event.type == pygame.KEYDOWN: # 上鍵 if event.key == pygame.K_UP: move_up = True move_down = False move_left = False move_right = False # 下鍵 if event.key == pygame.K_DOWN: move_up = False move_down = True move_left = False move_right = False # 左鍵 if event.key == pygame.K_LEFT: move_up = False move_down = False move_left = True move_right = False # 右鍵 if event.key == pygame.K_RIGHT: move_up = False move_down = False move_left = False move_right = True
2.4 獲取蛇的長度,移動蛇的身子
pos = len(snake_list) - 1 while pos > 0: snake_list[pos] = copy.deepcopy(snake_list[pos - 1]) pos -= 1
3. 蛇吃食物
3.1 碰撞檢測 如果蛇吃掉食物
if food_rect.collidepoint(pos): # 貪吃蛇吃掉食物 snake_list.append(food_point) # 重置食物位置 food_point = [random.randint(10, 490), random.randint(10, 490)] food_rect = pygame.draw.circle(screen, [255, 0, 0], food_point, 15, 0) break
3.2 如果蛇吃掉了自己
head_rect = snake_rect[0] count = len(snake_rect) while count > 1: if head_rect.colliderect(snake_rect[count - 1]): running = False count -= 1 pygame.display.update()