【问题标题】:PyGame Character not moving leftPyGame角色不向左移动
【发布时间】:2021-02-21 01:14:30
【问题描述】:

我有以下问题:角色没有正确地向左移动。我尝试调试并看到在 drawfrantz 函数中 moveLeft 不是真的,但在 while 游戏循环中它是。我已经尝试了很多,比如设置全局等等,但它似乎根本不起作用。我遵循了一个 yt 教程,这是链接 https://www.youtube.com/watch?v=9Kh9s9__ywo

import pygame
pygame.init()

display_width = 1280
display_height = 720

x = 250
y = 250
velocity = 10
moveRIGHT = False
moveLeft = False
stepIndex = 0

screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Frantz Reichts!")
clock = pygame.time.Clock()

frantz_stationary = pygame.image.load("assets/frantz/trash_ting3.png")

frantz_going_left = [None]*10
for indexofpic in range(1,9):
    frantz_going_left[indexofpic-1] = pygame.image.load("assets/frantz/L" + str(indexofpic) + ".png")
    indexofpic = (indexofpic + 1)

frantz_going_right = [None]*10
for picindex in range(1,9):
    frantz_going_right[picindex-1] = pygame.image.load("assets/frantz/R" + str(picindex) + ".png")
    picindex = (picindex + 1)





# Images #
stage1full = pygame.image.load("assets/WholeStage.png")
karavane = pygame.image.load("assets/Karavane.png")

def DrawFrantz():
    global stepIndex
    if stepIndex >= 8:
        stepIndex = 0
    if moveLeft == True:
        screen.blit(frantz_going_left[stepIndex], (x, y))
        stepIndex += 1
    elif moveRIGHT == True:
        screen.blit(frantz_going_right[stepIndex], (x, y))
        stepIndex += 1
    else:
        screen.blit(frantz_stationary, (x, y))

while(True): #Game Loop
    screen.fill((0, 0, 0))
    screen.blit(stage1full, (0, 0))
    screen.blit(karavane, (0,0))

    DrawFrantz()

    userInput = pygame.key.get_pressed()
    if userInput[pygame.K_a]:
        x -= velocity
        moveLeft = True
        moveRIGHT = False
        
    if userInput[pygame.K_d]:
        x += velocity
        moveLeft = False
        moveRIGHT = True
    else:
        moveLeft = False
        moveRIGHT = False
        stepIndex = 0


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.time.delay(100)
    pygame.display.update()
    clock.tick(60)

【问题讨论】:

  • 不确定,但只是一个简短的说明:您不需要在开始时更新那些“for”循环中的计数器,也许不试试?
  • 在您的“DrawFrantz”函数中,您调用了全局 stepIndex,但也许您也错过了调用 moveLeft 和 moveRight 全局?

标签: python pygame pygame-surface


【解决方案1】:

我还没有测试过,但我猜你需要elif 而不是if

userInput = pygame.key.get_pressed()
if userInput[pygame.K_a]:
    x -= velocity
    moveLeft = True
    moveRIGHT = False
    
elif userInput[pygame.K_d]: # <- here elif instead of if
    x += velocity
    moveLeft = False
    moveRIGHT = True
else:
    moveLeft = False
    moveRIGHT = False
    stepIndex = 0

否则,即使您按下K_a 按钮,最后的else 也会将布尔值moveLeft 重置为False

【讨论】:

  • 谢谢你成功了,没想到
【解决方案2】:

@Valentino 的回答是正确的。不过,我建议简化代码。

设置一个move_x 变量,当按下right 时为1,按下left 时为-1。被按下。如果一个或两个按钮都没有按下,则变量为 0。使用move_x 变量设置moveLeftmoveRIGHT 并更改xstepIndex

userInput = pygame.key.get_pressed()
move_x = userInput[pygame.K_d] - userInput[pygame.K_a]

moveLeft = move_x < 0
moveRIGHT = move_x > 0
x += move_x * velocity
if move_x = 0:
    stepIndex = 0

【讨论】:

    猜你喜欢
    • 2019-05-09
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    相关资源
    最近更新 更多