Skip to content

Commit f35eaa1

Browse files
committed
v 0.6
Step 8 Collissons
1 parent 5730284 commit f35eaa1

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

__pycache__/alien.cpython-311.pyc

1.48 KB
Binary file not shown.

__pycache__/game.cpython-311.pyc

2 KB
Binary file not shown.

alien.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pygame
1+
import pygame, random
22

33
class Alien(pygame.sprite.Sprite):
44
def __init__(self, type, x, y):
@@ -8,4 +8,23 @@ def __init__(self, type, x, y):
88
self.rect = self.image.get_rect(topleft = (x, y))
99

1010
def update(self, direction):
11-
self.rect.x += direction
11+
self.rect.x += direction
12+
13+
class MysteryShip(pygame.sprite.Sprite):
14+
def __init__(self, screen_width):
15+
super().__init__()
16+
self.screen_width = screen_width
17+
self.image = pygame.image.load("Graphics/mystery.png")
18+
x = random.choice([0, screen_width - self.image.get_width()])
19+
if x == 0:
20+
self.speed = 3
21+
else:
22+
self.speed = -3
23+
self.rect = self.image.get_rect(topleft = (x, 20))
24+
25+
def update(self):
26+
self.rect.x += self.speed
27+
if self.rect.x > self.screen_width:
28+
self.kill()
29+
elif self.rect.x < - 200:
30+
self.kill()

game.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
import pygame
1+
import pygame, random
22
from spaceship import Spaceship
33
from alien import Alien
4+
from alien import MysteryShip
45
from obstacle import Obstacle
6+
from laser import Laser
57

68
class Game():
79
def __init__(self, screen_width, screen_height):
810
self.screen_width = screen_width
11+
self.screen_height = screen_height
912
self.spaceship = pygame.sprite.GroupSingle()
1013
self.spaceship.add(Spaceship(screen_width, screen_height))
1114
self.obstacle_1 = Obstacle(screen_width/4 - 128,screen_height - 100)
1215
self.obstacle_2 = Obstacle((screen_width/4)*2 - 128,screen_height - 100)
1316
self.obstacle_3 = Obstacle((screen_width/4)*3 - 128,screen_height - 100)
1417
self.obstacle_4 = Obstacle((screen_width/4)*4 - 128,screen_height - 100)
1518
self.aliens = pygame.sprite.Group()
16-
self.alien_direction = 1
19+
self.alien_lasers = pygame.sprite.Group()
20+
self.mystery_ship = pygame.sprite.GroupSingle()
21+
self.alien_direction = 1
1722
self.create_aliens()
1823

24+
def create_mystery_ship(self):
25+
self.mystery_ship.add(MysteryShip(self.screen_width))
26+
27+
def check_for_collisions(self):
28+
if self.spaceship.sprite.lasers:
29+
for laser in self.spaceship.sprite.lasers:
30+
for obstacle in [self.obstacle_1, self.obstacle_2, self.obstacle_3, self.obstacle_4]:
31+
if pygame.sprite.spritecollide(laser, obstacle.blocks, True):
32+
laser.kill()
33+
print("Collision")
34+
1935
def create_aliens(self):
2036
for row in range(5):
2137
for column in range(11):
@@ -43,3 +59,9 @@ def alien_move_down(self, distance):
4359
if self.aliens:
4460
for alien in self.aliens.sprites():
4561
alien.rect.y += distance
62+
63+
def alien_shoot_laser(self):
64+
if self.aliens.sprites():
65+
random_alien = random.choice(self.aliens.sprites())
66+
laser_sprite = Laser(random_alien.rect.center, -6, self.screen_height)
67+
self.alien_lasers.add(laser_sprite)

main.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pygame, sys, shelve
1+
import pygame, sys, random
22
from spaceship import Spaceship
33
from obstacle import *
44
from alien import Alien
@@ -15,6 +15,11 @@
1515
pygame.display.set_caption("Space Invaders")
1616

1717
clock = pygame.time.Clock()
18+
ALIENLASER = pygame.USEREVENT
19+
pygame.time.set_timer(ALIENLASER, 500)
20+
21+
MYSTERYSHIP = pygame.USEREVENT + 1
22+
pygame.time.set_timer(MYSTERYSHIP, random.randint(5000,10000))
1823

1924
game = Game(SCREEN_WIDTH, SCREEN_HEIGHT)
2025

@@ -23,9 +28,17 @@
2328
if event.type == pygame.QUIT:
2429
pygame.quit()
2530
sys.exit()
31+
if event.type == ALIENLASER:
32+
game.alien_shoot_laser()
33+
if event.type == MYSTERYSHIP:
34+
game.create_mystery_ship()
2635

2736
screen.fill(GREY)
37+
38+
game.alien_lasers.update()
2839
game.spaceship.update()
40+
game.mystery_ship.update()
41+
game.check_for_collisions()
2942

3043
game.alien_position_checker()
3144
game.aliens.update(game.alien_direction)
@@ -37,6 +50,8 @@
3750
game.obstacle_3.blocks.draw(screen)
3851
game.obstacle_4.blocks.draw(screen)
3952
game.aliens.draw(screen)
53+
game.alien_lasers.draw(screen)
54+
game.mystery_ship.draw(screen)
4055

4156
#pygame.draw.rect(screen, YELLOW, (10, 10 ,780, 780), 2, 0, 60, 60, 60, 60)
4257
#pygame.draw.line(screen, YELLOW, (25, 800 - 70), (800 -25, 800 - 70), 3)

0 commit comments

Comments
 (0)