用python制作一个贪吃蛇的程序
下面是一个使用Python编写的简单贪吃蛇程序示例:
import pygame
import time
import random
# 初始化pygame
pygame.init()
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 窗口大小
window_width = 800
window_height = 600
# 定义蛇的大小和速度
snake_size = 10
snake_speed = 15
# 设置窗口大小
game_display = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('贪吃蛇')
# 定义时钟
clock = pygame.time.Clock()
# 字体设置
font_style = pygame.font.SysFont(None, 50)
score_font = pygame.font.SysFont(None, 35)
# 显示得分
def show_score(score):
value = score_font.render("得分: " + str(score), True, BLACK)
game_display.blit(value, [10, 10])
# 绘制蛇
def draw_snake(snake_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_display, GREEN, [x[0], x[1], snake_size, snake_size])
# 游戏结束时显示信息
def message(msg, color):
mesg = font_style.render(msg, True, color)
game_display.blit(mesg, [window_width / 6, window_height / 3])
# 游戏循环
def game_loop():
game_over = False
game_close = False
# 蛇的初始位置
x1 = window_width / 2
y1 = window_height / 2
# 蛇在x和y轴上移动的初始速度
x1_change = 0
y1_change = 0
# 初始化蛇的长度
snake_list = []
length_of_snake = 1
# 随机生成食物的位置
foodx = round(random.randrange(0, window_width - snake_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - snake_size) / 10.0) * 10.0
while not game_over:
while game_close:
game_display.fill(WHITE)
message("游戏结束! 按 Q-退出, C-重新开始", RED)
show_score(length_of_snake - 1)
pygame.display.update()
# 处理游戏结束时的事件
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
# 处理游戏中的事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_size
x1_change = 0
# 判断蛇是否超出窗口边界
if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
game_display.fill(WHITE)
pygame.draw.rect(game_display, BLUE, [foodx, foody, snake_size, snake_size])
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
# 控制蛇的长度
if len(snake_list) > length_of_snake:
del snake_list[0]
# 判断蛇是否碰到自身
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
draw_snake(snake_size, snake_list)
show_score(length_of_snake - 1)
pygame.display.update()
# 判断蛇是否吃到食物
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, window_width - snake_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - snake_size) / 10.0) * 10.0
length_of_snake += 1
# 控制蛇的速度
clock.tick(snake_speed)
pygame.quit()
# 运行游戏循环
game_loop()
运行这个程序,将在一个窗口中显示贪吃蛇游戏。你可以使用方向键来控制蛇的移动方向,吃到食物后蛇的长度会增加,如果蛇碰到窗口边界或自身,则游戏结束。游戏结束后,你可以按下 Q 键退出游戏,按下 C 键重新开始游戏
原文地址: https://www.cveoy.top/t/topic/h2uR 著作权归作者所有。请勿转载和采集!