我的矩形的 y 值在 pygame 中没有变化
The y value of my rectangle is not changing in pygame
我正在使用 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”的值正在改变,所以我一直在试图找出问题所在。
要向下移动,您需要增加而不是减小 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()
我正在使用 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”的值正在改变,所以我一直在试图找出问题所在。
要向下移动,您需要增加而不是减小 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()