随笔 - 3  文章 - 0  评论 - 0  阅读 - 55

贪吃蛇小游戏代码

import pygame
import time
import random

初始化pygame

pygame.init()

定义颜色

white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

定义屏幕大小

width = 600
height = 400

创建游戏窗口

game_window = pygame.display.set_mode((width, height))
pygame.display.set_caption('贪吃蛇游戏')

定义时钟

clock = pygame.time.Clock()

定义蛇的大小和速度

snake_block = 10
snake_speed = 15

定义字体样式

font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)

显示得分

def display_score(score):
value = score_font.render("得分: " + str(score), True, yellow)
game_window.blit(value, [0, 0])

绘制蛇

def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(game_window, green, [x[0], x[1], snake_block, snake_block])

显示消息

def display_message(msg, color):
mesg = font_style.render(msg, True, color)
game_window.blit(mesg, [width / 6, height / 3])

游戏循环

def game_loop():
game_over = False
game_close = False

# 蛇的初始位置
x1 = width / 2
y1 = height / 2

# 蛇的移动变化量
x1_change = 0
y1_change = 0

# 蛇的身体
snake_list = []
snake_length = 1

# 食物的位置
food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0

while not game_over:

    while game_close:
        game_window.fill(blue)
        display_message("你输了! 按Q退出游戏或C重新开始", red)
        display_score(snake_length - 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_block
                y1_change = 0
            elif event.key == pygame.K_RIGHT:
                x1_change = snake_block
                y1_change = 0
            elif event.key == pygame.K_UP:
                y1_change = -snake_block
                x1_change = 0
            elif event.key == pygame.K_DOWN:
                y1_change = snake_block
                x1_change = 0

    # 如果蛇撞到边界,游戏结束
    if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
        game_close = True
    x1 += x1_change
    y1 += y1_change
    game_window.fill(black)
    pygame.draw.rect(game_window, red, [food_x, food_y, snake_block, snake_block])
    snake_head = []
    snake_head.append(x1)
    snake_head.append(y1)
    snake_list.append(snake_head)
    if len(snake_list) > snake_length:
        del snake_list[0]

    # 如果蛇撞到自己,游戏结束
    for x in snake_list[:-1]:
        if x == snake_head:
            game_close = True

    draw_snake(snake_block, snake_list)
    display_score(snake_length - 1)

    pygame.display.update()

    # 如果蛇吃到食物,增加长度
    if x1 == food_x and y1 == food_y:
        food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
        food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
        snake_length += 1

    clock.tick(snake_speed)

pygame.quit()
quit()

启动游戏

game_loop()

image

posted on   髙貴的腋毛  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示