commit b3e55c70735a7472490c8152d1f30e354a5a5925 Author: punix Date: Thu Jul 25 21:15:03 2024 +0200 Space Invaders further diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..be5bc53 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..bdf3413 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/spaceinvaders.iml b/.idea/spaceinvaders.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/spaceinvaders.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ae26442 --- /dev/null +++ b/main.py @@ -0,0 +1,149 @@ +import pygame +import random +import math + + +class Game: + def __init__(self, width, height): + pygame.init() + self.width = width + self.height = height + self.screen = pygame.display.set_mode((self.width, self.height)) + pygame.display.set_caption("Space Invaders") + self.clock = pygame.time.Clock() + self.background_image = pygame.image.load("spr_space_himmel.png") + self.running = True + self.spaceship = Spaceship(self, 370, 515) + self.enemies = [] + self.score = 0 + for i in range(12): + self.enemies.append(Enemy(self, random.randint(0, 736), random.randint(30, 130))) + + while self.running: + self.clock.tick(60) + self.screen.blit(self.background_image, (0, 0)) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.running = False + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + self.spaceship.move(-10) + if event.key == pygame.K_RIGHT: + self.spaceship.move(10) + if event.key == pygame.K_SPACE: + self.spaceship.fire_bullet() + + if event.type == pygame.KEYUP: + if event.key == pygame.K_LEFT: + self.spaceship.move(10) + if event.type == pygame.KEYUP: + if event.key == pygame.K_RIGHT: + self.spaceship.move(-10) + + self.spaceship.update() + if len(self.spaceship.bullets) > 0: + for bullet in self.spaceship.bullets: + if bullet.is_fired == True: + bullet.update() + else: + self.spaceship.bullets.remove(bullet) + + for enemy in self.enemies: + enemy.update() + enemy.check_collision() + if enemy.y > 460: + for i in self.enemies: + i.y = 1000 + self.print_game_over() + break + self.print_score() + pygame.display.update() + + def print_game_over(self): + go_font = pygame.font.Font("freesansbold.ttf", 64) + go_text = go_font.render("GAME OVER", True, (255, 255, 255)) + self.screen.blit(go_text, (200, 250)) + + def print_score(self): + score_font = pygame.font.Font("freesansbold.ttf", 24) + score_text = score_font.render("Punkte: " + str(self.score), True, (255, 255, 255)) + self.screen.blit(score_text, (8, 8)) + + +class Spaceship: + def __init__(self, game, x, y): + self.x = x + self.y = y + self.change_x = 0 + self.game = game + self.spaceship_img = pygame.image.load("spr_spaceship.png") + self.bullets = [] + + def fire_bullet(self): + self.bullets.append(Bullet(self.game, self.x, self.y)) + self.bullets[len(self.bullets) - 1].fire() + + def move(self, speed): + self.change_x += speed + + def update(self): + self.x += self.change_x + if self.x < 0: + self.x = 0 + elif self.x > 736: + self.x = 736 + + self.game.screen.blit(self.spaceship_img, (self.x, self.y)) + + +class Bullet: + def __init__(self, game, x, y): + self.x = x + self.y = y + self.game = game + self.is_fired = False + self.bullet_speed = 10 + self.bullet_img = pygame.image.load("spr_patrone.png") + + def fire(self): + self.is_fired = True + + def update(self): + self.y -= self.bullet_speed + if self.y <= 0: + self.is_fired = False + self.game.screen.blit(self.bullet_img, (self.x, self.y)) + + +class Enemy: + def __init__(self, game, x, y): + self.x = x + self.y = y + self.change_x = 5 + self.change_y = 60 + self.game = game + self.enemy_img = pygame.image.load("spr_space_enemy.png") + + 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.game.score += 1 + self.x = random.randint(0, 736) + self.y = random.randint(50, 150) + + def update(self): + self.x += self.change_x + 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.enemy_img, (self.x, self.y)) + + +if __name__ == '__main__': + game = Game(800, 600) + print(len(game.spaceship.bullets)) diff --git a/spr_patrone.png b/spr_patrone.png new file mode 100644 index 0000000..f95b6b1 Binary files /dev/null and b/spr_patrone.png differ diff --git a/spr_space_enemy.png b/spr_space_enemy.png new file mode 100644 index 0000000..ba612fd Binary files /dev/null and b/spr_space_enemy.png differ diff --git a/spr_space_himmel.png b/spr_space_himmel.png new file mode 100644 index 0000000..52a0816 Binary files /dev/null and b/spr_space_himmel.png differ diff --git a/spr_spaceship.png b/spr_spaceship.png new file mode 100644 index 0000000..a6197d1 Binary files /dev/null and b/spr_spaceship.png differ