python贪吃蛇游戏代码详解,python贪吃蛇最简单代码

大家好,小编为大家解答python贪吃蛇游戏代码详解外加中文的问题。很多人还不知道python贪吃蛇游戏代码详解,现在让我们一起来看看吧!

简单的贪吃蛇小游戏的 Python 代码:

import pygame
import random

# 初始化
pygame.init()

# 游戏窗口大小
width = 800
height = 600

# 设置游戏窗口
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")

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

# 蛇的大小
block_size = 10

# 字体
font_style = pygame.font.SysFont(None, 30)

# 显示消息
def message(msg, color):
    message = font_style.render(msg, True, color)
    screen.blit(message, [width / 6, height / 3])

# 绘制蛇
def draw_snake(block_size, snake_List):
    for x in snake_List:
        pygame.draw.rect(screen, green, [x[0], x[1], block_size, block_size])

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

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

    # 蛇的移动距离
    x1_change = 0
    y1_change = 0

    # 初始长度
    snake_List = []
    Length_of_snake = 1

    # 食物的位置
    foodx = round(random.randrange(0, width - block_size) / 10.0) * 10.0
    foody = round(random.randrange(0, height - block_size) / 10.0) * 10.0

    # 游戏循环
    while not game_over:

        # 游戏结束
        while game_close == True:
            screen.fill(white)
            message("You Lost! Press Q-Quit or C-Play Again", 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:
                        gameLoop()

        # 判断按键
        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 >= width or x1 < 0 or y1 >= height or y1 < 0:
            game_close = True

        # 移动蛇
        x1 += x1_change
        y1 += y1_change

        # 绘制食物
        screen.fill(white)
        pygame.draw.rect(screen, 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 x in snake_List[:-1]:
            if x == 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, width - block_size) / 10.0) * 10.0
            foody = round(random.randrange(0, height - block_size) / 10.0) * 10.0
            Length_of_snake += 1

        # 刷新屏幕
        pygame.display.update()

    # 退出游戏
    pygame.quit()
    quit()

# 运行游戏
gameLoop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值