gpt4 book ai didi

Python/Pygame - 跳转键并不总是注册

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:37 25 4
gpt4 key购买 nike

我在 pygame 中跳跃时遇到问题。基本上,当我快速点击跳跃键时,我的 Sprite 有时会拒绝跳跃。这是一款平台游戏,因此快速点击跳跃是游戏机制的关键。我不认为这是事件队列的问题,因为即使 Sprite 不跳跃,跳跃声音仍然会播放。

完整代码:http://pastebin.com/D1B5yuEx

主循环:

        while 1:

timer.tick(30)

for e in pygame.event.get():
if e.type == KEYDOWN:
if e.key == K_ESCAPE:
pygame.quit()
sys.exit()

if e.type == KEYDOWN and e.key == K_w:
jump.play()
up = True
if e.type == KEYDOWN and e.key == K_s:
down = True
if e.type == KEYDOWN and e.key == K_a:
left = True
last_key_pressed = "LEFT"
if e.type == KEYDOWN and e.key == K_d:
right = True
last_key_pressed = "RIGHT"
if e.type == KEYDOWN and e.key == K_e:
select = True
if e.type == KEYDOWN and e.key == K_SPACE:
if inventory.count("PISTOL") == 1:

bullet = Bullet()
shoot_fx.play()
bullet.rect.x = player.rect.x +20
bullet.rect.y = player.rect.y +20
bullets.add(bullet)
if bullet.rect.x == player.rect.x +100:
print "hi"

if right or last_key_pressed =="RIGHT":
bullet.xvel = 7
if left or last_key_pressed =="LEFT":
bullet.xvel = -7

if pygame.joystick.get_count() != 0:
if inventory.count("PISTOL") == 1:
if my_joystick.get_button(2) == 1:

bullet = Bullet()
shoot_fx.play()
bullet.rect.x = player.rect.x +20
bullet.rect.y = player.rect.y +20
bullets.add(bullet)

if right or last_key_pressed =="RIGHT":
bullet.xvel = 7
if left or last_key_pressed =="LEFT":
bullet.xvel = -7


if e.type == KEYUP and e.key == K_w:
up = False
if e.type == KEYUP and e.key == K_s:
down = False
if e.type == KEYUP and e.key == K_a:
left = False
if e.type == KEYUP and e.key == K_d:
right = False



if pygame.joystick.get_count() != 0:

if my_joystick.get_axis(0) == -1:
last_key_pressed = "LEFT"#left
left = True
else:
left = False

if my_joystick.get_axis(0) > .9:
last_key_pressed = "RIGHT"
right = True
else:
right = False

if my_joystick.get_button(1) == True:

up = True
else:
up = False

if my_joystick.get_button(3) == True:
select = True
else:
select = False








for y in range(20):
for x in range(40):
screen.blit(bg, (x * 64, y * 64))
bullets.update(left,right)

camera.update(player)


player.update(shoot,up, down, left, right, select,pickups,platforms,inventory,player,bullets)
for e in entities:
screen.blit(e.image,camera.apply(e))
for b in bullets:
screen.blit(b.image,camera.apply(b))

pygame.display.update()

玩家等级:

class Player(Entity):

walking_frames_l = []
walking_frames_r = []

jumping_r = []
jumping_l = []

run_frames_r = []
run_frames_l = []


def __init__(self, x, y):
image = pygame.image.load("C:\Users\Tom\Data\Art\Player1.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player2.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player22.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player3.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player33.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player4.png")
self.walking_frames_r.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player44.png")
self.walking_frames_r.append(image)

image = pygame.image.load("C:\Users\Tom\Data\Art\Player1.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player11.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\Player2.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\player22.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\player3.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\player33.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\player4.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)
image = pygame.image.load("C:\Users\Tom\Data\Art\player44.png")
image = pygame.transform.flip(image,True,False)
self.walking_frames_l.append(image)

self.frame_r = 0
self.frame_l = 0

self.last_key_pressed = None

Entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.onGround = False
self.image = self.walking_frames_r[self.frame_r]
self.rect = Rect(x,y,23,31)



def update(self,shoot,up, down, left, right,select, pickups, platforms, inventory, player,bullets):

if shoot:
pass


if up:
if self.onGround: self.yvel -= 7
if down:
pass
if left:

self.last_key_pressed = "LEFT"
self.xvel = -4
self.frame_l += 1
self.image = self.walking_frames_l[self.frame_l]
if self.frame_l == 8: self.frame_l = 0
if right:

self.last_key_pressed = "RIGHT"
self.xvel = 4
self.frame_r += 1
self.image = self.walking_frames_r[self.frame_r]
if self.frame_r == 8: self.frame_r = 0

if not (left or right):
if self.last_key_pressed == "LEFT":
self.facing = 1
self.image = self.walking_frames_l[1]

if self.last_key_pressed == "RIGHT":
self.facing = 0
self.image = self.walking_frames_r[1]


if not self.onGround:

self.yvel += 0.3

if self.yvel > 30: self.yvel = 30
if not(left or right):
self.xvel = 0

self.rect.left += self.xvel

self.collide(self.xvel, 0,pickups, platforms,select,inventory)

self.rect.top += self.yvel

self.onGround = False;

self.collide(0, self.yvel, pickups, platforms,select,inventory)

def collide(self, xvel, yvel,pickups, platforms,select,inventory):
for p in platforms:
if sprite.collide_rect(self, p):
if isinstance(p, ExitBlock):
pygame.quit
if xvel > 0: self.rect.right = p.rect.left
if xvel < 0: self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0: self.rect.top = p.rect.bottom
if yvel < 0: self.yvel = 3
for p in pickups:
if sprite.collide_rect(self,p) and select == True:
if inventory.count("PISTOL") < 1:
inventory.append(ITEMS.item.items[0])

最佳答案

在事件处理程序中,您正在处理队列中的所有事件。如果 keydown 和 keyup 事件足够接近,它们将被一个接一个地处理,并且全局 up 变量将不会被其余事件视为 True你的程序的。您需要捕获 up 变量的上升沿,并确保在调用玩家的 update 方法之前它不会被设置回 False .

发布时是否需要将up设置为False?如果是这样,您可能需要另一个 jump 变量,该变量可以在处理跳转时设置为 False

我注意到您的操纵杆跳跃按钮处理不会播放声音。您可能需要考虑使用函数来处理按钮按下,而不是直接修改全局状态。它也可以让您只拥有一次 PISTOL 处理代码。

关于Python/Pygame - 跳转键并不总是注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21468050/

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