• 智能AI问答!更聪明、更完整、原创作!
  • 一款基于GPT的超级AI机器人,可以回答问题、聊天、创作、写文案!

python写一个贪吃蛇游戏

发布于 2023-09-22 23:08:27

当然可以!以下是一个简单的贪吃蛇游戏的Python代码示例:

import pygame
import random

# 初始化游戏
pygame.init()

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)

# 设置游戏窗口大小和标题
screen_width = 800
screen_height = 600
game_display = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Greedy Snake')

# 设置帧率(游戏速度)
clock = pygame.time.Clock()
snake_speed = 15

snake_block_size = 20
snake_block_width = snake_block_size
snake_block_height = snake_block_size

# 设置字体
font_style = pygame.font.SysFont(None, 30)
score_font = pygame.font.SysFont(None, 50)

# 定义计分函数
def our_snake(snake_block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(game_display, green, [x[0], x[1], snake_block_width, snake_block_height])

# 显示得分函数
def your_score(score):
    value = score_font.render("Your Score: " + str(score), True, white)
    game_display.blit(value, [10, 10])

# 主程序函数
def game_loop():
    game_over = False
    game_close = False

    # 设置初始位置
    x1 = screen_width / 2
    y1 = screen_height / 2
    x1_change = 0
    y1_change = 0

    # 创建蛇身列表
    snake_List = []
    Length_of_snake = 1

    # 随机生成食物位置
    foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
    foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0

    while not game_over:
        while game_close == True:
            game_display.fill(black)
            message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, white)
            game_display.blit(message, [screen_width/6, screen_height/3])
            your_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_block_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block_size
                    x1_change = 0

        # 判断是否碰到边界
        if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        game_display.fill(black)
        pygame.draw.rect(game_display, red, [foodx, foody, snake_block_width, snake_block_height])
        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

        our_snake(snake_block_size, snake_List)
        your_score(Length_of_snake - 1)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
            foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
            Length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()

# 启动游戏
game_loop()

这是一个使用pygame库编写的简单贪吃蛇游戏代码。执行代码后,你可以通过键盘上下左右箭头控制蛇的移动,吃到食物后蛇的长度会增加。如果碰到边界或者蛇自己咬到自己,游戏就会结束。

请确保已经安装pygame库,并将代码保存为.py文件,然后运行即可开始游戏。希望对你有所帮助!

0 条评论

发布
问题

在线
客服