【问题标题】:The y value of my rectangle is not changing in pygame我的矩形的 y 值在 pygame 中没有改变
【发布时间】:2022-01-22 01:59:11
【问题描述】:

我正在使用 pygame 模块来制作一个简单的游戏。目前,我正在尝试让一个矩形在我的 500x500 屏幕上移动,但它只在 x 值(左右)中移动。

这是我的代码:

import pygame

pygame.init()

# Colors
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)

# Game Screen Dimensions
game_layout_length = 500
game_layout_width = 500

# Character Attributes
character_length = 10
character_width = 10
character_x = 0
character_y = 0

game_screen = pygame.display.set_mode((game_layout_width, game_layout_length))

game_close = False
game_lost = False

while not game_close:

    while not game_lost:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                game_close = True
                game_lost = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    character_x -= 10

                elif event.key == pygame.K_RIGHT:
                    character_x += 10

                elif event.key == pygame.K_DOWN:
                    character_y -= 10

                elif event.key == pygame.K_UP:
                    character_y += 10

        pygame.draw.rect(game_screen, blue, [character_x, character_y, character_length, character_width])
        pygame.display.update()

print(f'{character_x, character_y}')
pygame.quit()
quit()

print(f'{character_x, character_y}') 行是调试行,它显示“character_y”的值正在改变,所以我一直在试图找出问题所在。

【问题讨论】:

  • 1) 无需更新不变的值。例如那些“ += 0 ”行。 2)你把打印线放在哪里?就在抽奖电话之前?如果您发布包括调试行在内的完整代码,其他人可能更容易提供帮助。
  • 谢谢,我将删除不变的值。并且打印线放在绘图调用之前。包括在内,这就是整个代码。解决此问题后,我将添加更多代码。

标签: python pygame


【解决方案1】:

向下移动需要增大 y 而不是减小它,向上移动需要减小 y。您还可以在每次渲染时填充屏幕以避免出现拖尾矩形。

import pygame

pygame.init()

# Colors
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)

# Game Screen Dimensions
game_layout_length = 500
game_layout_width = 500

# Character Attributes
character_length = 10
character_width = 10
character_x = 0
character_y = 0

game_screen = pygame.display.set_mode((game_layout_width, game_layout_length))

game_close = False
game_lost = False

while not game_close:

    while not game_lost:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                game_close = True
                game_lost = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    character_x -= 10

                elif event.key == pygame.K_RIGHT:
                    character_x += 10

                elif event.key == pygame.K_DOWN:
                    character_y += 10

                elif event.key == pygame.K_UP:
                    character_y -= 10
        game_screen.fill(black)
        pygame.draw.rect(game_screen, blue, [character_x, character_y, character_length, character_width])
        pygame.display.update()

print(f'{character_x, character_y}')
pygame.quit()
quit()

【讨论】:

  • 哇,非常感谢!它起作用了,但我只是想知道为什么减少 y 值会使它上升,而增加它会使它下降。以及为什么同样的逻辑不适用于 x 值。
  • 这是因为坐标系的工作原理。左上角是 (0, 0) [in (x, y) 形式] 所以增加 x 将使其向右移动,增加 y 使其向下。
  • 好的,非常感谢!
猜你喜欢
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多