Space Invaders further
This commit is contained in:
commit
b3e55c7073
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
7
.idea/misc.xml
Normal file
7
.idea/misc.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.11 (spaceinvaders)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (spaceinvaders)" project-jdk-type="Python SDK" />
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/spaceinvaders.iml" filepath="$PROJECT_DIR$/.idea/spaceinvaders.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
10
.idea/spaceinvaders.iml
Normal file
10
.idea/spaceinvaders.iml
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
149
main.py
Normal file
149
main.py
Normal file
@ -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))
|
BIN
spr_patrone.png
Normal file
BIN
spr_patrone.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 285 B |
BIN
spr_space_enemy.png
Normal file
BIN
spr_space_enemy.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
BIN
spr_space_himmel.png
Normal file
BIN
spr_space_himmel.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.7 KiB |
BIN
spr_spaceship.png
Normal file
BIN
spr_spaceship.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
Loading…
Reference in New Issue
Block a user