gpt4 book ai didi

python - 如何让玩家角色移动?

转载 作者:行者123 更新时间:2023-12-01 06:53:27 30 4
gpt4 key购买 nike

当我遇到障碍时,我开始改进我的代码。我的玩家角色可以跳跃,但不能左右移动。程序运行时就好像没有语法错误一样。主要目标是尝试让角色左右移动

这里是定义其属性和函数的玩家类

class player:
def __init__(self,x,y):
self.x = x
self.y = y
self.width = 64
self.height = 64
self.standing = True
self.left = False
self.right = True
self.vel = 15
self.jumping = False
self.jumpCount = 10
self.k = pygame.key.get_pressed()
def move(self,x,y):
if not self.standing:
if self.k[pygame.K_LEFT] and self.x > 0 - 150:
self.left = True
self.right = False
self.x -= self.vel
elif self.k[pygame.K_RIGHT] and self.x < 500 - 150 :
self.right = True
self.left = False
self.x += self.vel
else:
self.standing = True

主循环

run = True
wizard = player(25,320)
while run:#main game loop
pygame.time.delay(15)
for event in pygame.event.get():#loops through a list of keyboard or mouse events
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
wizard.jumping = True
wizard.move(wizard.x,wizard.y)
win.blit(bg,(0,0))
wizard.jump(wizard.y)
wizard.draw(win)
pygame.display.update()
pygame.quit()

最佳答案

好吧,有些代码看起来有点困惑。我认为如果您的 player 类只是处理为一个向导(这本身就是一个足够大的任务),并且您的主事件循环应该处理用户输入,那就更好了。 p>

主循环使用pygame.KEYDOWN事件。如果您想要那种按键按下、按键抬起的“打字” Action ,这很好。但更自然的方法是简单地检查 pygame.key.get_pressed() ,它返回所有按钮的状态。由于您的播放器已经保持了速度,因此请使用按键状态来调整速度。

FPS=20
clock = pygame.time.Clock() # clock to limit the FPS
while run: #main game loop
#pygame.time.delay(15) # <-- No need to delay here, see FPS limit below
for event in pygame.event.get(): #loops through event list
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
wizard.goJump()

# Handle keys pressed
if ( keys[pygame.K_LEFT] ):
wizard.goLeft()
elif ( keys[pygame.K_RIGHT] ):
wizard.goRight()

# Update the player's position
#wizard.move(wizard.x,wizard.y)
wizard.update()

# redraw the screen
win.blit( bg, (0, 0) )
wizard.draw( win )
pygame.display.update()

# Update the window, but at a useful FPS
clock.tick_busy_loop( FPS )

pygame.quit()

所以这意味着对播放器进行一些更改。我尝试将所有“玩家处理”功能保留在播放器内部,同时将所有用户输入处理代码移到类之外。

class player:
def __init__(self,x,y):
self.x = x
self.y = y
self.width = 64
self.height = 64
#self.standing = True <-- Standing is "not self.jumping"
#self.left = False
#self.right = True
self.vel = 15 # current player speed
self.max_vel = 20 # Limit the player speed extremes
self.mix_vel = -20
self.jumping = False
self.jumpCount = 10
# self.k = pygame.key.get_pressed() <-- Don't handle events inside the player

def goLeft( self ):
""" Handle the user-input to move left """
self.vel -= 1
if ( self.vel <= self.min_vel ):
self.vel = self.min_vel

def goRight( self ):
""" Handle the user-input to move right """
self.vel += 1
if ( self.vel >= self.max_vel ):
self.vel = self.max_vel

def goJump( self ):
""" Handle the user-input to jump """
if ( self.jumping == False ):
self.jumping = True
print( "TODO: Handle Jumping" )
else:
print( "TODO: Already Jumping" )

def update( self ):
# Move the character left/right
self.x += self.vel # (+) velocity is right, (-) is left
# handle the progress of the jump
if ( self.jumping == True ):
print( "TODO: jump wizard - GO!" )

跳跃未实现。实现此目的的一种方法是,不要简单地将 self.jumping 记录为 bool 值,而是存储跳转开始的毫秒时间。然后在 player.update() 期间,使用实时差异使玩家沿着其(抛物线?)路径向上和向后移动。一旦 player.jumptime 重置为零,用户就可以让向导再次跳跃。

关于python - 如何让玩家角色移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58903479/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com