【问题标题】:Why is the apple not spawning?为什么苹果不产卵?
【发布时间】:2022-11-24 00:21:47
【问题描述】:

我的问题

作为一名新程序员,我尝试用 Python 编写 Snake,这是使用 Pygame 的最直接的编程语言(除了 Scratch)。直到现在我才明白这个问题,食物(和蛇的死亡点)超出了游戏显示范围。我应该怎么办?

我的代码

# Importing libraries
import pygame
import random
# Pygame initialization
pygame.init()
dis_width = 600
dis_height = 500
dis = pygame.display.set_mode((dis_width, dis_height))
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 10
font_style = pygame.font.SysFont("comicsansms", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
pygame.display.set_caption('Snake REVAMPED')
white = (255, 255, 255)
blue = (50, 153, 213)
red = (213, 50, 80)
green = (0, 255, 102)
yellow = (255, 255, 102)
black = (0, 0, 0)


# Defining functions
def Your_score(score):
    Returning_value = score_font.render("Score: " + str(score), True, black)
    dis.blit(Returning_value, [0, 0])


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):
    m = font_style.render(msg, True, color)
    dis.blit(m, [dis_width / 6, dis_height / 3])


def Game_loop():
    game_over = False
    game_close = False
    x1 = dis_width / 2
    y1 = dis_height / 2
    x1_change = 0
    y1_change = 0
    snake_list = []
    Snake_length = 1
    Food_x = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
    Food_y = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
    while not game_over:
        while game_close == True:
            dis.fill(black)
            message("Ouch! Press Enter to try again, or Shift to quit.", white)
            Your_score(Snake_length - 1)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN:
                        Game_loop()
                    if event.key == pygame.K_LSHIFT:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_RSHIFT:
                        game_over = True
                        game_close = False
        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_UP:
                    x1_change = 0
                    y1_change = -snake_block
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_DOWN:
                    x1_change = 0
                    y1_change = snake_block
        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,
                         [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
        our_snake(snake_block, snake_list)
        Your_score(Snake_length - 1)
        pygame.display.update()
        if x1 == Food_x and y1 == Food_y:
            Food_x = round(
                random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
            Food_y = round(
                random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
            Snake_length += 1
        clock.tick(snake_speed)
    pygame.quit()
    quit()


Game_loop()

我试图做什么

为了解决这个问题,我尝试更改变量“snake_block”。但是,它只会让 sprite(如果你能称它们为 sprite 的话......)变得更大,而 hitboxes 变得一团糟。我似乎不明白问题出在哪里,而且我不知道应该如何为我的游戏添加屏幕边界。

【问题讨论】:

  • 我无法重现或者我不明白你的问题出在哪里。刚刚测试了您发布的代码,它按预期工作。在蛇出界和游戏结束屏幕出现之间有一个稍微令人困惑的延迟,但这被计入了循环结束时的 clock.tick。那么你的问题在哪里?

标签: python pygame


【解决方案1】:

正如所评论的那样,游戏对我来说没问题,但是,我会交换以下几行:

        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change

这样更有意义:

        x1 += x1_change
        y1 += y1_change
        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            game_close = True

您首先要更新位置,然后才检查新位置是否越界。

我在玩的时候没有注意到这个问题,因为当你出界时,不可能在下一步内回到界内,但错误的顺序让延迟是游戏结束屏幕前预期的两倍出现了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    相关资源
    最近更新 更多