The cursor keys move the sprite (well left and right only really change the horizontal speed), space shoots. I can move in every direction and shoot at the same time except for up+right. When I move right, shoot and press up, the sprite keeps moving right instead of up-right. If I move right first, shoot and move up, it moves right while shooting. If I move up-right and then start to shoot, no shots happen. In each of the three cases a moment or so later I get a speaker beep until I release one of the keys.
I have no problems with up-left, down-left or down-right.
This is the only place where I ever use keypresses:
Code: Select all
keys=pygame.key.get_pressed() ### Get keyboard input ###
### check if player swings to left or right ###
if keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT]:
if player.tilt > -(player.speed):
player.tilt -= player.acc
if player.tilt < -player.speed:
player.tilt = -player.speed
elif keys[pygame.K_RIGHT] and not keys[pygame.K_LEFT]:
if player.tilt < player.speed:
player.tilt += player.acc
if player.tilt > player.speed:
player.tilt = player.speed
else: ### if not, gradually normalise the momentum ###
if player.tilt != 0:
player.tilt += int(float(abs(player.tilt)) / float(-player.tilt))
### calculate sprite rotation ### irrelevant for this problem
sin = float(player.tilt) / float(player.speed)
player.head = int(float(math.degrees(math.asin(sin))) / 2.0)
# player.sprite.set_child(Transform(child=player.basesprite, rotate=player.head))
### up&down movement, slower if also moving sideways ###
# changing this reveals it's indeed the key combination and not the direction
hspeed = player.tilt
if keys[pygame.K_DOWN]:
vspeed = int(player.speed - (0.5*abs(player.tilt)))
elif keys[pygame.K_UP]:
vspeed = -int(player.speed - (0.5*abs(player.tilt)))
else:
vspeed = 0
### make sure not to leave the screen! ###
if player.x + hspeed > config.screen_width-40:
hspeed = config.screen_width-40 - player.x
if player.x + hspeed < 40:
hspeed = 40 - player.x
if player.y + vspeed > config.screen_height-40:
vspeed = config.screen_height-40 - player.y
if player.y + vspeed < 40:
vspeed = 40 - player.y
player.x += hspeed
player.y += vspeed
### check if player can shoot and if he wants to shoot ###
if player.cooldownleft > 0:
player.cooldownleft -= 1
if keys[pygame.K_SPACE] and player.cooldownleft == 0:
player.shoot()
player.cooldownleft = player.cooldown