pygame小游戏
pygame小游戏
import pygame
import random
初始化pygame
pygame.init()
定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
设置显示窗口
dis_width = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('贪吃蛇游戏 by Python')
设置游戏时钟
clock = pygame.time.Clock()
蛇的大小和速度
snake_block = 10
snake_speed = 15
设置字体 - 确保使用系统存在的字体
try:
font_style = pygame.font.SysFont("arial", 25)
score_font = pygame.font.SysFont("arial", 35)
except:
font_style = pygame.font.SysFont(None, 25)
score_font = pygame.font.SysFont(None, 35)
def your_score(score):
"""显示当前得分"""
value = score_font.render("得分: " + str(score), True, black)
dis.blit(value, [10, 10])
def our_snake(snake_block, snake_list):
"""绘制蛇"""
for x in snake_list:
pygame.draw.rect(dis, green, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
"""显示消息"""
# 使用多行消息显示
lines = msg.split('\n')
for i, line in enumerate(lines):
mesg = font_style.render(line, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3 + i*30])
def gameLoop():
"""游戏主循环"""
game_over = False
game_close = False
# 初始化蛇的位置
x1 = dis_width / 2
y1 = dis_height / 2
# 初始化蛇的移动方向
x1_change = 0
y1_change = 0
# 初始化蛇的身体
snake_List = []
Length_of_snake = 1
# 随机生成食物位置
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
dis.fill(white)
message("游戏结束!\n按Q键退出 或 按C键重新开始", red)
your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_close = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop() # 重新开始游戏
return # 确保退出当前游戏循环
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 and x1_change <= 0: # 防止180度转弯
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT and x1_change >= 0:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP and y1_change <= 0:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN and y1_change >= 0:
y1_change = snake_block
x1_change = 0
# 检查是否撞墙
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True
# 更新蛇的位置
x1 += x1_change
y1 += y1_change
dis.fill(white)
# 绘制食物
pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
# 更新蛇的身体
snake_Head = [x1, 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
# 绘制蛇和分数
our_snake(snake_block, snake_List)
your_score(Length_of_snake - 1)
pygame.display.update()
# 检查是否吃到食物
if abs(x1 - foodx) < snake_block and abs(y1 - foody) < snake_block:
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
# 控制游戏速度
clock.tick(snake_speed)
# 退出游戏
pygame.quit()
quit()
启动游戏
gameLoop()