python贪吃蛇小游戏

贪吃蛇小游戏 python

使用python代码写了一个贪吃蛇小游戏,闲来无事。纯属瞎玩

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置游戏窗口大小
window_width = 800
window_height = 600
game_display = pygame.display.set_mode((window_width, window_height))

# 设置游戏标题
pygame.display.set_caption('贪吃蛇游戏')

# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

# 设置蛇的大小和速度
block_size = 10
snake_speed = 7

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


# 绘制蛇
def draw_snake(block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(game_display, black, [x[0], x[1], block_size, block_size])


# 显示消息
def message(msg, color):
    message_text = font_style.render(msg, True, color)
    game_display.blit(message_text, [window_width/6, window_height/3])


# 游戏循环
def game_loop():
    game_over = False
    game_close = False

    # 初始化蛇的位置
    x1 = window_width / 2
    y1 = window_height / 2
    x1_change = 0
    y1_change = 0

    # 初始化食物的位置
    foodx = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
    foody = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0

    # 初始化蛇的长度
    snake_list = []
    length_of_snake = 1

    # 游戏循环
    while not game_over:

        # 游戏结束
        while game_close:
            game_display.fill(white)
            message("game over!enter Q quit,enter C restart.", red)
            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 = -block_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = block_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -block_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = block_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
        print(x1, y1)

        game_display.fill(white)
        pygame.draw.rect(game_display, red, [foodx, foody, block_size, block_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 segment in snake_list[:-1]:
            if segment == snake_head:
                game_close = True

        draw_snake(block_size, snake_list)
        pygame.display.update()

        # 判断是否吃到食物
        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
            foody = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
            length_of_snake += 1

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

    # 退出 Pygame
    pygame.quit()

    # 退出 Python
    quit()


# 开始游戏
game_loop()

运行结果:

python贪吃蛇小游戏_第1张图片

你可能感兴趣的:(游戏,pygame,python,开发语言)