I am making a game in Pygame which requires the player to defend the Earth from aliens that are attacking it. The player is controlled with the arrow keys and the spacebar is used to shoot. So far everything is working fine except for the lasers. When I fire the laser, I wish for them to move in the direction the player is facing.
Here is my lazer
class:
class lazer(pygame.sprite.Sprite):
def __init__(self):
super(lazer, self).__init__()
self.image = pygame.image.load('lazer.png')
self.rect = self.image.get_rect()
def update1(self):
keyy = pygame.key.get_pressed()
speed = 30
if keyy[pygame.K_UP]:
self.rect.y -= speed
elif keyy[pygame.K_DOWN]:
self.rect.y += speed
if keyy[pygame.K_RIGHT]:
self.rect.x += speed
elif keyy[pygame.K_LEFT]:
self.rect.x -= speed
So far the laser only moves when the player presses an arrow key. If the player stops pressing the arrow key, the laser will also stop moving and if the player moves in a different direction, the laser will also move in that direction.
Here is my full game code:
while GameOn == True:
Player1.background(gameDisplay)
if len(EnemyAliens) < 1:
pygame.time.delay(3000)
wave = wave + 1
diff = diff + wave
for i in range(diff):
block = Aliens()
block.rect.y = random.randrange(200, DisplayHeight-10)
block.rect.x = random.randrange(110, DisplayWidth-10)
EnemyAliens.add(block)
all_sprites.add(block)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
keyy = pygame.key.get_pressed()
print(direction)
lazer1 = lazer()
lazer1.rect.y = Player1.rect.y
lazer1.rect.x = Player1.rect.x
all_sprites.add(lazer1)
lazer_list.add(lazer1)
for lazer1 in lazer_list:
block_hit_list = pygame.sprite.spritecollide(lazer1, EnemyAliens, True)
for block in block_hit_list:
lazer_list.remove(lazer1)
all_sprites.remove(lazer1)
score += 1
print(score)
if lazer1.rect.y<-10 or lazer1.rect.y > DisplayHeight + 10 or lazer1.rect.x < -10 or lazer1.rect.x > DisplayWidth +10:
lazer_list.remove(lazer1)
all_sprites.remove(lazer1)
for block in EnemyAliens:
speed = 3
if block.rect.x > 115:
block.rect.x -= speed
elif block.rect.x < 115:
block.rect.x += speed
if block.rect.y < 91:
block.rect.y += speed
elif block.rect.y > 91:
block.rect.y -= speed
col = pygame.sprite.collide_rect(Earth1,block)
if col == True:
all_sprites.remove(block)
EnemyAliens.remove(block)
earth_health -= 10
for Pl in PlayerList:
key = pygame.key.get_pressed()
speed = 6
direction = "w"
if key[pygame.K_UP]:
Pl.rect.y -= speed
direction = "U"
elif key[pygame.K_DOWN]:
Pl.rect.y += speed
direction = "D"
if key[pygame.K_RIGHT]:
Pl.rect.x += speed
direction = "R"
elif key[pygame.K_LEFT]:
Pl.rect.x -= speed
direction = "L"
if earth_health == 0 or earth_health < 0:
if score > Highscore:
saveHs(score)
GameOn = False
HealthBars(earth_health)
all_sprites.update()
all_sprites.draw(gameDisplay)
PlayerList.draw(gameDisplay)
pygame.display.update()
How can I get the laser to move in the direction the player was moving, and not move along with the player?