前言:貪吃蛇是一款經典的電子游戲,最早可以追溯到1976年的街機游戲Blockade。隨著諾基亞手機的普及,貪吃蛇游戲在1990年代變得廣為人知。它是一款休閑益智類游戲,適合所有年齡段的玩家,其最初為單機模式,后來隨著技術發展,出現了多種版本和玩法,包括團戰模式、賞金模式和挑戰模式等。隨著互聯網和智能手機的普及,多人在線對戰的版本也出現了,大大增加了游戲的社交性和競技性。那么好,今天我們就用pygame等編寫一個簡單的貪吃蛇小游戲,來感受一下童年游戲的快樂。
編程思路:本次我們將會用到pygame,random,tkinter等庫。
第一步:準備必要的庫
本次編程中我們所需導入的庫:pygame,random,tkinter中只有pygame屬于第三方庫。因此,在運行/編寫代碼前我們需先準備好pygame庫。
具體步驟:在PyCharm終端輸入"pip install pygame"并回車等待一段時間。(如下所示)
pip install pygame
第二步:完整代碼展示
import pygame
import random
from tkinter import messageboxWHITE= (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)pygame.init()
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('貪吃蛇游戲')
BLOCK_SIZE = 20
clock = pygame.time.Clock()class Snake:def __init__(self):self.body = [(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)]self.direction = 'RIGHT'def move(self):head_x, head_y = self.body[0]if self.direction == 'RIGHT':new_head = (head_x + BLOCK_SIZE, head_y)elif self.direction == 'LEFT':new_head = (head_x - BLOCK_SIZE, head_y)elif self.direction == 'UP':new_head = (head_x, head_y - BLOCK_SIZE)else:new_head = (head_x, head_y + BLOCK_SIZE)self.body.insert(0, new_head)self.body.pop()def change_direction(self, new_direction):if new_direction == 'RIGHT' and self.direction!= 'LEFT':self.direction = new_directionelif new_direction == 'LEFT' and self.direction!= 'RIGHT':self.direction = new_directionelif new_direction == 'UP' and self.direction!= 'DOWN':self.direction = new_directionelif new_direction == 'DOWN' and self.direction!= 'UP':self.direction = new_directiondef grow(self):tail = self.body[-1]self.body.append(tail)class Food:def __init__(self):self.position = (random.randint(0, (WINDOW_WIDTH - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE,random.randint(0, (WINDOW_HEIGHT - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE)def respawn(self):self.position = (random.randint(0, (WINDOW_WIDTH - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE,random.randint(0, (WINDOW_HEIGHT - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE)def draw(snake, food):window.fill(WHITE)for segment in snake.body:pygame.draw.rect(window, GREEN, [segment[0], segment[1], BLOCK_SIZE, BLOCK_SIZE])pygame.draw.rect(window, RED, [food.position[0], food.position[1], BLOCK_SIZE, BLOCK_SIZE])pygame.display.update()def check_collision(snake, food):head = snake.body[0]if head[0] < 0 or head[0] >= WINDOW_WIDTH or head[1] < 0 or head[1] >= WINDOW_HEIGHT:return Truefor segment in snake.body[1:]:if head == segment:return Trueif head == food.position:snake.grow()food.respawn()return Falsedef main():snake = Snake()food = Food()running = Truewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_UP:snake.change_direction('UP')elif event.key == pygame.K_DOWN:snake.change_direction('DOWN')elif event.key == pygame.K_LEFT:snake.change_direction('LEFT')elif event.key == pygame.K_RIGHT:snake.change_direction('RIGHT')snake.move()if check_collision(snake, food):running = Falsemessagebox.showwarning('Game Over', '游戲結束!')draw(snake, food)clock.tick(8)pygame.quit()if __name__ == '__main__':main()
運行效果展示:
第三步:玩法介紹
上,下,左,右方向鍵控制移動方向
(后續我還會更新哦)