我在 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])
我是一名优秀的程序员,十分优秀!