程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PyGame 角色不向左移动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决PyGame 角色不向左移动?

开发过程中遇到PyGame 角色不向左移动的问题如何解决?下面主要结合日常开发的经验,给出你关于PyGame 角色不向左移动的解决方法建议,希望对你解决PyGame 角色不向左移动有所启发或帮助;

我有以下问题:角色没有正确向左移动。我尝试调试并看到在 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],y))
        stepIndex += 1
    else:
        screen.blit(frantz_stationary,y))

while(true): #Game Loop
    screen.fill((0,0))
    screen.blit(stage1full,(0,0))
    screen.blit(karavane,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)

解决方法

我还没有测试过,但我猜你需要一个 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

否则,即使您按下 else 按钮,末尾的 @H_961_5@moveLeft 也会将布尔值 false 重置为 K_a

,

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

设置一个 @H_961_5@move_x 变量,当按下 right 时为 1,按下 left 时为 -1。被按下。如果没有或同时按下两个按钮,则变量为 0。使用 @H_961_5@move_x 变量设置 @H_961_5@moveLeft、@H_961_5@moveRIGHT 并更改 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

大佬总结

以上是大佬教程为你收集整理的PyGame 角色不向左移动全部内容,希望文章能够帮你解决PyGame 角色不向左移动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: