Space Invaders Aliens zurückschicken lassen
Verfasst: Samstag 11. März 2023, 21:45
Hey, ich programmiere gerade space invaders und möchte die "SpeedEnemy" Objekte jetzt zurückschießen lassen. Aber iwie klappt das nicht so wie ich es mir vorgestellt habe. Wäre top wenn ihr mal einen Blick drüber werfen könntet.
(Zeile 88-93 liegt der Fehler glaube ich irgendwo)
(Zeile 88-93 liegt der Fehler glaube ich irgendwo)
Code: Alles auswählen
import pygame
import random
import math
class Game:
def __init__(self, width, height):
pygame.init()
self.width = width # 7
self.height = height # 7
self.screen = pygame.display.set_mode((self.width, self.height)) # 7
pygame.display.set_caption("Space Invaders") # 5
self.running = True # 4
self.clock = pygame.time.Clock() # 6
self.background_img = pygame.image.load("background123.peg").convert() #20
self.background_img = pygame.transform.scale(self.background_img, (width, height))
self.spaceship = Spaceship(self, 368, 515) # 8
self.y = 600
self.hpu = []
self.boss = []
self.hearts = []
self.enemies = [] # 15
self.speed_enemy = []
self.score = 0
self.counter = 0
self.enemy_shoot_counter = 0
self.clock = pygame.time.Clock()
self.speed_lives = 2
self.heart_lives = 3
self.pixel = 64
self.pu_pixel = 32
self.hearts.append(Hearts(self, 20, 30))
self.hearts.append(Hearts(self, 36, 30))
self.hearts.append(Hearts(self, 52, 30))
self.hpu.append(PowerUps(self, random.randint(0, 736), random.randint(-130, -60)))
for i in range(2):
self.speed_enemy.append(SpeedEnemy(self, random.randint(0, 736), random.randint(-130, -60)))
for i in range(2):
self.boss.append(Boss(self, random.randint(0, 736), random.randint(-130, -60)))
for i in range(24): # 15
self.enemies.append(Enemy(self, random.randint(0, 736), random.randint(-130, -60))) # 15
while self.running: # 4
self.clock.tick(60) # 6 setzt die Frames per second
self.spaceship.check_collision()
# Beweglicher Bildschirm
self.rel_y = self.y % self.background_img.get_rect().height # 20
self.screen.blit(self.background_img, (0, self.rel_y - self.background_img.get_rect().height))
if self.rel_y < height:
self.screen.blit(self.background_img, (0, self.rel_y))
self.y += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN: # 10
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
self.spaceship.move(-10) # 10.4
if event.key == pygame.K_SPACE: # 13
self.spaceship.fire_bullet()
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self.spaceship.move(10)
if event.key == pygame.K_UP or event.key == pygame.K_w:
self.spaceship.move_y(-10)
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
self.spaceship.move_y(10)
if event.type == pygame.KEYUP: # 10.5
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
self.spaceship.move(10)
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self.spaceship.move(-10)
if event.key == pygame.K_UP or event.key == pygame.K_w:
self.spaceship.move_y(10)
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
self.spaceship.move_y(-10)
for element in self.speed_enemy:
self.enemy_shoot_counter += 1
if self.enemy_shoot_counter >= 300:
element.fire_bullet() # fügt element zur l hinzu und setzt das letzte element auf true
for element1 in element.bullets:
element1.update()
self.spaceship.update() # 9
if len(self.spaceship.bullets) > 0: # 13.3
for bullet in self.spaceship.bullets:
if bullet.is_fired:
bullet.bullet_update()
else: # 14.1
self.spaceship.bullets.remove(bullet)
for enemy_speed in self.speed_enemy:
enemy_speed.update()
enemy_speed.check_collision()
if enemy_speed.y >= 536:
self.change_position_g_o()
self.print_game_over()
break
for end_boss in self.boss:
end_boss.update()
end_boss.check_collision_bullet()
if end_boss.y >= 536:
self.change_position_g_o()
self.print_game_over()
break
for enemy in self.enemies:
enemy.update()
enemy.check_collision()
if enemy.y >= 536: # 17
self.change_position_g_o()
self.print_game_over()
break
#23 Powerup einfügen
if len(self.hearts) < 3:
self.counter += 1
if self.counter >= 600:
for element in self.hpu:
element.update()
self.spaceship.check_collision_hpu()
if element.y > 600:
self.counter = 0
self.hpu.remove(element)
self.hpu.append(PowerUps(self, random.randint(0, 736), random.randint(-130, -60)))
for heart in self.hearts:
heart.update()
self.print_score()
pygame.display.update()
def change_position_g_o(self):
for i in self.enemies:
i.y = 1000
for i in self.speed_enemy:
i.y = 1000
for i in self.boss:
i.y = 1000
def print_game_over(self):
go_font = pygame.font.Font("freesansbold.ttf", 64)
go_text = go_font.render("GAME OVER", True, (255, 255, 255))
score_font1 = pygame.font.Font("freesansbold.ttf", 16)
go_text_score = score_font1.render("Dein Punktestand: " + str(self.score), True, (100, 200, 10))
self.screen.blit(go_text, (200, 250))
self.screen.blit(go_text_score, (325, 350))
def print_score(self):
score_font = pygame.font.Font("freesansbold.ttf", 16)
score_text = score_font.render("Punkte: " + str(self.score), True, (155, 200, 255))
self.screen.blit(score_text, (8, 8))
########################################################################################################################
class Spaceship:
def __init__(self, game, x, y): # 8
self.x = x # 8
self.y = y # 8
self.change_x = 0 # 10
self.change_y = 0
self.game = game
self.spaceship_img = pygame.image.load("battleship.png") # 9
self.bullets = []
def fire_bullet(self): # 13
self.bullets.append(Bullet(self.game, self.x, self.y))
self.bullets[len(self.bullets) - 1].fired() # 13.2
def move(self, speed): # 10.2
self.change_x += speed
def move_y(self, speed):
self.change_y += speed
def update(self): # 11
self.x += self.change_x # 10.3
self.y += self.change_y
if self.x < 0: # 12
self.x = 0
elif self.x > 736:
self.x = 736
elif self.y < 0:
self.y = 0
elif self.y > 536:
self.y = 536
self.game.screen.blit(self.spaceship_img, (self.x, self.y)) # 9
def check_collision(self):
for enemy in self.game.enemies: #21
if self.y < (enemy.y + self.game.pixel) and self.y + self.game.pixel > (enemy.y + self.game.pixel)\
or ((self.y + self.game.pixel) > enemy.y and (self.y + self.game.pixel) < enemy.y + self.game.pixel):
if ((self.x > enemy.x and self.x < (enemy.x + self.game.pixel))
or ((self.x + self.game.pixel) > enemy.x and self.x + self.game.pixel < (enemy.x + self.game.pixel))):
enemy.x = random.randint(0, 736)
enemy.y = random.randint(-130, -60)
self.game.hearts.pop()
if len(self.game.hearts) <= 0:
self.game.change_position_g_o()
self.game.print_game_over()
def check_collision_hpu(self):
for hpu in self.game.hpu: # Health- PowerUp auf kollision checken
if self.y < (hpu.y + self.game.pixel) and self.y + self.game.pixel > (hpu.y + self.game.pixel) \
or ((self.y + self.game.pixel) > hpu.y and (self.y + self.game.pixel) < hpu.y + self.game.pixel):
if ((self.x > hpu.x and self.x < (hpu.x + self.game.pixel))
or ((self.x + self.game.pixel) > hpu.x and self.x + self.game.pixel < (
hpu.x + self.game.pixel))):
if len(self.game.hearts) == 2: #Wenn 2 Herzen: an letzter Stelle hinzufügen und hpu entfernen
self.game.hpu.pop()
self.game.hearts.append(Hearts(self.game, 52, 30))
self.game.hpu.append(PowerUps(self.game, random.randint(0, 736), random.randint(-130, -60)))
self.game.counter = 0
if len(self.game.hearts) == 1:
self.game.hpu.pop()
self.game.hearts.append(Hearts(self.game, 36, 30))
self.game.hpu.append(PowerUps(self.game, random.randint(0, 736), random.randint(-130, -60)))
self.game.counter = 0
class Bullet: # 13
def __init__(self, game, x, y):
self.x = x
self.y = y
self.is_fired = False
self.bullet_speed = 10
self.game = game
self.bullet_img = pygame.image.load("laser.png")
def fired(self): # 13.2
self.is_fired = True
def bullet_update(self):
self.y -= self.bullet_speed # 14
if self.y < -20:
self.is_fired = False
self.game.screen.blit(self.bullet_img, (self.x + 30, self.y))
########################################################################################################################
class SpeedEnemy:
def __init__(self, game, x, y):
self.x = x
self.y = y
self.change_x = 10
self.change_y = 60
self.game = game
self.speed_enemy_img = pygame.image.load("t-fighter.png")
self.speed_enemy_lives = 2
self.bullets = []
def fire_bullet(self):
self.bullets.append(EnemyBullet(self.game, self.x, self.y))
self.bullets[len(self.bullets) - 1].fired()
def update(self):
self.x += self.change_x
if self.x > 736:
self.y += self.change_y
self.change_x = -10
elif self.x < 0:
self.y += self.change_y
self.change_x = 10
self.game.screen.blit(self.speed_enemy_img, (self.x, self.y))
def check_collision(self):
for bullet in self.game.spaceship.bullets:
distance = math.sqrt(math.pow(self.x - bullet.x, 2) + math.pow(self.y - bullet.y, 2))
if distance < 35:
bullet.is_fired = False
self.speed_enemy_lives -= 1
if self.speed_enemy_lives <= 0:
self.game.score += 2
self.x = random.randint(0, 736)
self.y = random.randint(-130, 60)
self.speed_enemy_lives += 2
class EnemyBullet:
def __init__(self, game, x, y):
self.x = x
self.y = y
self.bullet_is_fired = False
self.bullet_speed = 10
self.game = game
self.enemy_bullet_img = pygame.image.load("laser_grey.png")
def fired(self):
self.bullet_is_fired = True
def update(self):
self.y += self.bullet_speed
if self.y < 600:
self.bullet_is_fired = False
self.game.screen.blit(self.enemy_bullet_img, (self.x + 20, self.y))
class Enemy: # 15
def __init__(self, game, x, y):
self.x = x
self.y = y
self.game = game
self.change_x = 10 # 15.2
self.change_y = 60
self.ufo_img = pygame.image.load("ufo.png")
def check_collision(self): # 16
for bullet in self.game.spaceship.bullets:
distance = math.sqrt(math.pow(self.x - bullet.x, 2) + math.pow(self.y - bullet.y, 2)) # 16
if distance < 35:
bullet.is_fired = False
self.game.score += 1
self.x = random.randint(0, 736)
self.y = random.randint(-130, -60)
def update(self):
self.x += self.change_x # 15.2
if self.x >= 736:
self.y += self.change_y
self.change_x = -5
elif self.x <= 0:
self.y += self.change_y
self.change_x = 5
self.game.screen.blit(self.ufo_img, (self.x, self.y))
class Boss:
def __init__(self, game, x, y):
self.x = x
self.y = y
self.game = game
self.endboss_img = pygame.image.load("death-star.png")
self.change_x = 5
self.change_y = 60
self.boss_lives = 3
self.rect = self.endboss_img.get_rect()
def update(self):
self.x += self.change_x
if self.x > 736:
self.change_x = -5
self.y += self.change_y
elif self.x < 0:
self.change_x = 5
self.y += self.change_y
self.game.screen.blit(self.endboss_img, (self.x, self.y))
def check_collision_bullet(self):
for bullet in self.game.spaceship.bullets:
distance = math.sqrt(math.pow(self.x - bullet.x, 2) + math.pow(self.y - bullet.y, 2))
if distance < 35:
self.boss_lives -= 1
bullet.is_fired = False
if self.boss_lives <= 3:
self.game.score += 5
self.x = random.randint(0, 736)
self.y = random.randint(-130, 60)
self.boss_lives += 3
class Hearts:
def __init__(self, game, x, y):
self.x = x
self.y = y
self.game = game
self.heart_img = pygame.image.load("heart.png")
def update(self):
self.game.screen.blit(self.heart_img, (self.x, self.y))
# Powerups ertellen
class PowerUps:
def __init__(self, game, x, y):
self.x = x
self.y = y
self.change_y = 5
self.game = game
self.health_pu_img = pygame.image.load("passion.png")
def update(self):
self.y += self.change_y
self.game.screen.blit(self.health_pu_img, (self.x, self.y))
if __name__ == "__main__":
game = Game(800, 600)
# ToDo
# - Nur ein enemy bei kollision --> bei remove wenn level implementiert
# - level implementieren
# - bei terminierung kleine explosion
# - Aliens sollen sich nicht überlappen
# - bosse grüner laserstrahl
# - t- fighter schießen 2 schuss auf einmal
# - bei Mauskick schießen
# - Vollbild implementieren
# - Pause implementieren
# - wenn alle 3 herzen weg, spaceship weg, riesen explosion, game over