好的,我刚刚开始了一个pygame程序。而且图像没有移动?我99%肯定我做的一切都是正确的......但我可能犯了一个愚蠢的错误。这是代码。
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
window_width = 800
window_height = 600
gameDisplay = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
def gameLoop():
while True:
characterimg = pygame.image.load('racecar.png')
characterx = 500
charactery = 500
for event in pygame.event.get():
if event.type == pygame.QUIT:
False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
characterx += -10
charactery += 0
if event.key == pygame.K_RIGHT:
characterx += 10
charactery += 0
if event.key == pygame.K_UP:
charactery += -10
characterx += 0
if event.key == pygame.K_DOWN:
charactery += 10
characterx += 0
gameDisplay.fill(white)
gameDisplay.blit(characterimg, (characterx,charactery))
pygame.display.update()
clock.tick(15)
gameLoop()
pygame.quit
quit()
如果你能帮助我,我会非常感激。请告诉我我做错了什么:)
答案 0 :(得分:0)
你的错误就在这里
while True:
characterimg = pygame.image.load('racecar.png')
characterx = 500
charactery = 500
# ... code ...
在每个循环中,您将图像移回位置(500, 500)
。
您必须在while True
characterimg = pygame.image.load('racecar.png')
characterx = 500
charactery = 500
while True:
# ... code ...
我的版本:
import pygame
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# --- classes --- (CamelCaseNames)
# empty
# --- functions --- (lower_case_names)
# empty
# --- main --- (lower_case_names)
# - init -
pygame.init()
display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# - objects -
character_image = pygame.image.load('racecar.png')
character_x = 500
character_y = 500
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif 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_UP:
character_y -= 10
elif event.key == pygame.K_DOWN:
character_y += 10
# - updates -
# empty
# - draws -
display.fill(WHITE)
display.blit(character_image, (character_x, character_y))
pygame.display.update()
# - FPS -
clock.tick(15)
# - end -
pygame.quit()