整体的思路:
1.游戏里面有俩个角色: 食物 蛇
我们就定义了俩个类来初始化它们的设置与属性(包括蛇的运动规则等)(我这里写的蛇的颜色,长度就没有在类里面写)
2.游戏逻辑:(1)蛇吃了食物要长大,速度变快
(2)蛇会跟随指令动
(3)不能碰到边界
(4)如果碰到就会重新开始
3.加载背景和声音
下面是代码:
import pygame
import random
# import time
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
STEP = 20 # 蛇的步长
SNAKE_SIZE = 20 # 蛇身体的尺寸
INIT_SNAKE_LENGTH = 100 # 蛇的初始长度
#
# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
# 初始化 Pygame 库
pygame.init()
# 设置频率,防止贪吃蛇跑的太快
clock = pygame.time.Clock()
# 设置游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("贪吃蛇")
#加载音效和图像
beijing_sound = pygame.mixer.Sound("D:\\python\\project\\project 1\\beijingyinyue.wav")
background = pygame.image.load("D:\\python\\project\\project 1\\beijingtu.jpg")
background = pygame.transform.scale(background, (SCREEN_WIDTH, SCREEN_HEIGHT))#改变图形大小,适应窗口大小
# 重新开始游戏的设置 有问题
def game_again():
print("调用了函数1")
global snake, food, FPS, game_over
FPS = 10
snake = Snake()
food = Food()
FPS = 10
game_over = False
print("重新开始")
# for event in pygame.event.get():#出现了问题
# print("判断判断")
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_RETURN:
# game_again1()
# elif event.key == pygame.K_SPACE:
# pygame.quit()
# quit()
# print("结束游戏")
def game_again1():
print("调用了函数2")
global snake, food, FPS, game_over
FPS = 10
snake.positions = [(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2), (SCREEN_WIDTH // 2 - SNAKE_SIZE, SCREEN_HEIGHT // 2),
(SCREEN_WIDTH // 2 - 2 * SNAKE_SIZE, SCREEN_HEIGHT // 2)]
snake.direction = 'right'
food.randomize_position()
game_over = False
print("重新开始")
# 定义蛇的类
class Snake:
def __init__(self):
self.length = INIT_SNAKE_LENGTH
self.positions = [(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)]
self.color = RED
self.direction = random.choice(['left', 'right', 'up', 'down'])
def control(self, xy):
if xy == 'left' and self.direction != 'right':
self.direction = 'left'
elif xy == 'right' and self.direction != 'left':
self.direction = 'right'
elif xy == 'up' and self.direction != 'down':
self.direction = 'up'
elif xy == 'down' and self.direction != 'up':
self.direction = 'down'
def move(self):
x, y = self.positions[0]
if self.direction == 'left':
x -= STEP
elif self.direction == 'right':
x += STEP
elif self.direction == 'up':
y -= STEP
elif self.direction == 'down':
y += STEP
self.positions = [(x, y)] + self.positions[:-1]
def grow(self):
x, y = self.positions[0]
if self.direction == 'left':
x -= STEP
elif self.direction == 'right':
x += STEP
elif self.direction == 'up':
y -= STEP
elif self.direction == 'down':
y += STEP
self.positions = [(x, y)] + self.positions[:]
self.length += 1
def draw(self):
for position in self.positions:
pygame.draw.rect(screen, self.color, (position[0], position[1], SNAKE_SIZE, SNAKE_SIZE))
# 定义食物的类
class Food:
def __init__(self):
self.position = (0, 0)
self.color = WHITE
self.randomize_position()
def randomize_position(self):
self.position = (
random.randint(0, SCREEN_WIDTH // STEP - 1) * STEP, random.randint(0, SCREEN_HEIGHT // STEP - 1) * STEP)
def draw(self):
pygame.draw.rect(screen, self.color, (self.position[0], self.position[1], SNAKE_SIZE, SNAKE_SIZE))
# self.position[0], self.position[1]为矩形左上角的坐标 后俩个参数代表了宽和高
# 初始化蛇和食物和速度
snake = Snake()
food = Food()
FPS = 10
# 配置蛇的速度
def speed(sudu):
sudu = sudu + 10
print(sudu)
return sudu
# 游戏开始
game_over = False
# 游戏循环
while not game_over:
#screen.fill(BLACK) # 填充游戏背景为黑色
screen.blit(background, (0, 0))
#beijing_sound.play(loops = -1) #可以使用,但是音频里面有一定杂音
snake.draw() # 绘制小蛇
food.draw() # 绘制食物
pygame.display.update() # 更新整个屏幕
# 检测事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake.control('left')
elif event.key == pygame.K_RIGHT:
snake.control('right')
elif event.key == pygame.K_UP:
snake.control('up')
elif event.key == pygame.K_DOWN:
snake.control('down')
# 获取蛇头和食物坐标
x, y = snake.positions[0]
fx, fy = food.position
# 判断蛇是否吃到了食物
if x <= fx < x + SNAKE_SIZE and y <= fy < y + SNAKE_SIZE:
food.randomize_position()
snake.grow()
FPS = speed(FPS)
# eat_sound.play()
# FPS = FPS + 10
# 蛇移动
snake.move()
# 判断蛇是否碰到了边界
if x < 0 or x > SCREEN_WIDTH or y < 0 or y > SCREEN_HEIGHT:
game_over = True
game_again()
# 判断蛇是否碰到了自己的身体
if x in snake.positions or y in snake.positions:
game_over = True
game_again()
clock.tick(FPS)
print("代码完")
代码的不足之处:
1.这里的蛇是简单化的用方块组成
2.这里的游戏结束和重新开始的时间间距很短,没有给玩家进行判断是否要重新开 始