前言:贪吃蛇是一款经典的电子游戏,最早可以追溯到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 messagebox
WHITE= (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_direction
elif new_direction == 'LEFT' and self.direction!= 'RIGHT':
self.direction = new_direction
elif new_direction == 'UP' and self.direction!= 'DOWN':
self.direction = new_direction
elif new_direction == 'DOWN' and self.direction!= 'UP':
self.direction = new_direction
def 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 True
for segment in snake.body[1:]:
if head == segment:
return True
if head == food.position:
snake.grow()
food.respawn()
return False
def main():
snake = Snake()
food = Food()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif 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 = False
messagebox.showwarning('Game Over', '游戏结束!')
draw(snake, food)
clock.tick(8)
pygame.quit()
if __name__ == '__main__':
main()
运行效果展示:
第三步:玩法介绍
上,下,左,右方向键控制移动方向
(后续我还会更新哦)