1
1
import pygame , random
2
2
from spaceship import Spaceship
3
- from alien import Alien
4
- from alien import MysteryShip
5
3
from obstacle import Obstacle
4
+ from obstacle import grid
5
+ from alien import Alien
6
6
from laser import Laser
7
+ from alien import MysteryShip
7
8
8
- class Game ():
9
- def __init__ (self , screen_width , screen_height , offset ):
10
-
11
- self .lives = 3
12
- self .lives_surface = pygame .image .load ("Graphics/ship.png" ).convert_alpha ()
13
- self .score = 0
14
- self .highscore = 0
15
- self .load_highscore ()
16
-
17
- self .screen_width = screen_width
18
- self .screen_height = screen_height
19
- self .offset = offset
20
- self .spaceship = pygame .sprite .GroupSingle ()
21
- self .spaceship .add (Spaceship (screen_width , screen_height , offset ))
22
- self .obstacle_1 = Obstacle (screen_width / 4 - 110 ,screen_height - 100 )
23
- self .obstacle_2 = Obstacle ((screen_width / 4 )* 2 - 110 ,screen_height - 100 )
24
- self .obstacle_3 = Obstacle ((screen_width / 4 )* 3 - 110 ,screen_height - 100 )
25
- self .obstacle_4 = Obstacle ((screen_width / 4 )* 4 - 110 ,screen_height - 100 )
26
- self .aliens = pygame .sprite .Group ()
27
- self .alien_lasers = pygame .sprite .Group ()
28
- self .mystery_ship = pygame .sprite .GroupSingle ()
29
- self .alien_direction = 1
30
- self .run = True
31
- self .create_aliens ()
32
-
33
- def create_mystery_ship (self ):
34
- self .mystery_ship .add (MysteryShip (self .screen_width , self .offset ))
35
-
36
- def check_for_collisions (self ):
37
- #Spaceship Laser
38
- if self .spaceship .sprite .lasers :
39
- for laser in self .spaceship .sprite .lasers :
40
- for obstacle in [self .obstacle_1 , self .obstacle_2 , self .obstacle_3 , self .obstacle_4 ]:
41
- if pygame .sprite .spritecollide (laser , obstacle .blocks , True ):
42
- laser .kill ()
43
-
44
- aliens_hit = pygame .sprite .spritecollide (laser , self .aliens , True )
45
- if aliens_hit :
46
- for alien in aliens_hit :
47
- self .score += alien .type * 100
48
- self .check_for_highscore ()
49
- laser .kill ()
50
-
51
- if pygame .sprite .spritecollide (laser , self .mystery_ship , True ):
52
- self .score += 500
53
- self .check_for_highscore ()
54
- laser .kill ()
55
-
56
- #Aliens Laser
57
- if self .alien_lasers :
58
- for laser in self .alien_lasers :
59
- for obstacle in [self .obstacle_1 , self .obstacle_2 , self .obstacle_3 , self .obstacle_4 ]:
60
- if pygame .sprite .spritecollide (laser , obstacle .blocks , True ):
61
- laser .kill ()
62
- if pygame .sprite .spritecollide (laser , self .spaceship , False ):
63
- laser .kill ()
64
- self .lives -= 1
65
- if self .lives == 0 :
66
- self .game_over ()
67
-
68
- if self .aliens :
69
- for alien in self .aliens :
70
- pygame .sprite .spritecollide (alien , self .obstacle_1 .blocks , True )
71
- pygame .sprite .spritecollide (alien , self .obstacle_2 .blocks , True )
72
- pygame .sprite .spritecollide (alien , self .obstacle_3 .blocks , True )
73
- pygame .sprite .spritecollide (alien , self .obstacle_4 .blocks , True )
74
-
75
- if pygame .sprite .spritecollide (alien , self .spaceship , False ):
76
- self .game_over ()
77
-
78
- def create_aliens (self ):
79
- for row in range (5 ):
80
- for column in range (11 ):
81
- x = column * 55
82
- y = row * 55
83
- if row == 0 :
84
- alien_sprite = Alien (3 , 75 + x + self .offset / 2 , 80 + y + self .offset )
85
- elif row == 1 or row == 2 :
86
- alien_sprite = Alien (2 , 75 + x + self .offset / 2 , 80 + y + self .offset )
87
- else :
88
- alien_sprite = Alien (1 , 75 + x + self .offset / 2 , 80 + y + self .offset )
89
- self .aliens .add (alien_sprite )
90
-
91
- def alien_position_checker (self ):
92
- alien_sprites = self .aliens .sprites ()
93
- for alien in alien_sprites :
94
- if alien .rect .right >= self .screen_width + self .offset / 2 :
95
- self .alien_direction = - 1
96
- self .alien_move_down (2 )
97
- elif alien .rect .left <= self .offset / 2 :
98
- self .alien_direction = 1
99
- self .alien_move_down (2 )
100
-
101
- def alien_move_down (self , distance ):
102
- if self .aliens :
103
- for alien in self .aliens .sprites ():
104
- alien .rect .y += distance
105
-
106
- def alien_shoot_laser (self ):
107
- if self .aliens .sprites ():
108
- random_alien = random .choice (self .aliens .sprites ())
109
- laser_sprite = Laser (random_alien .rect .center , - 6 , self .screen_height )
110
- self .alien_lasers .add (laser_sprite )
111
-
112
- def game_over (self ):
113
- self .run = False
114
-
115
- def reset (self ):
116
- self .run = True
117
- self .lives = 3
118
- self .spaceship .sprite .reset ()
119
- self .aliens .empty ()
120
- self .create_aliens ()
121
- self .spaceship .sprite .lasers .empty ()
122
- self .alien_lasers .empty ()
123
-
124
- # Recreate the obstacles
125
- self .obstacle_1 = Obstacle (self .screen_width / 4 - 110 , self .screen_height - 100 )
126
- self .obstacle_2 = Obstacle ((self .screen_width / 4 ) * 2 - 110 , self .screen_height - 100 )
127
- self .obstacle_3 = Obstacle ((self .screen_width / 4 ) * 3 - 110 , self .screen_height - 100 )
128
- self .obstacle_4 = Obstacle ((self .screen_width / 4 ) * 4 - 110 , self .screen_height - 100 )
129
-
130
- self .score = 0
131
-
132
- def check_for_highscore (self ):
133
- if self .score > self .highscore :
134
- self .highscore = self .score
135
-
136
- # Save the highscore to a file
137
- with open ('highscore.txt' , 'w' ) as file :
138
- file .write (str (self .highscore ))
139
-
140
- def load_highscore (self ):
141
- try :
142
- with open ('highscore.txt' , 'r' ) as file :
143
- self .highscore = int (file .read ())
144
- except FileNotFoundError :
145
- # If the file doesn't exist, set a default highscore
146
- self .highscore = 0
9
+ class Game :
10
+ def __init__ (self , screen_width , screen_height , offset ):
11
+ self .screen_width = screen_width
12
+ self .screen_height = screen_height
13
+ self .offset = offset
14
+ self .spaceship_group = pygame .sprite .GroupSingle ()
15
+ self .spaceship_group .add (Spaceship (self .screen_width , self .screen_height , self .offset ))
16
+ self .obstacles = self .create_obstacles ()
17
+ self .aliens_group = pygame .sprite .Group ()
18
+ self .create_aliens ()
19
+ self .aliens_direction = 1
20
+ self .alien_lasers_group = pygame .sprite .Group ()
21
+ self .mystery_ship_group = pygame .sprite .GroupSingle ()
22
+ self .lives = 3
23
+ self .run = True
24
+ self .score = 0
25
+ self .highscore = 0
26
+ self .explosion_sound = pygame .mixer .Sound ("Sounds/explosion.ogg" )
27
+ self .load_highscore ()
28
+ pygame .mixer .music .load ("Sounds/music.ogg" )
29
+ pygame .mixer .music .play (- 1 )
30
+
31
+ def create_obstacles (self ):
32
+ obstacle_width = len (grid [0 ]) * 3
33
+ gap = (self .screen_width + self .offset - (4 * obstacle_width ))/ 5
34
+ obstacles = []
35
+ for i in range (4 ):
36
+ offset_x = (i + 1 ) * gap + i * obstacle_width
37
+ obstacle = Obstacle (offset_x , self .screen_height - 100 )
38
+ obstacles .append (obstacle )
39
+ return obstacles
40
+
41
+ def create_aliens (self ):
42
+ for row in range (5 ):
43
+ for column in range (11 ):
44
+ x = 75 + column * 55
45
+ y = 110 + row * 55
46
+
47
+ if row == 0 :
48
+ alien_type = 3
49
+ elif row in (1 ,2 ):
50
+ alien_type = 2
51
+ else :
52
+ alien_type = 1
53
+
54
+ alien = Alien (alien_type , x + self .offset / 2 , y )
55
+ self .aliens_group .add (alien )
56
+
57
+ def move_aliens (self ):
58
+ self .aliens_group .update (self .aliens_direction )
59
+
60
+ alien_sprites = self .aliens_group .sprites ()
61
+ for alien in alien_sprites :
62
+ if alien .rect .right >= self .screen_width + self .offset / 2 :
63
+ self .aliens_direction = - 1
64
+ self .alien_move_down (2 )
65
+ elif alien .rect .left <= self .offset / 2 :
66
+ self .aliens_direction = 1
67
+ self .alien_move_down (2 )
68
+
69
+ def alien_move_down (self , distance ):
70
+ if self .aliens_group :
71
+ for alien in self .aliens_group .sprites ():
72
+ alien .rect .y += distance
73
+
74
+ def alien_shoot_laser (self ):
75
+ if self .aliens_group .sprites ():
76
+ random_alien = random .choice (self .aliens_group .sprites ())
77
+ laser_sprite = Laser (random_alien .rect .center , - 6 , self .screen_height )
78
+ self .alien_lasers_group .add (laser_sprite )
79
+
80
+ def create_mystery_ship (self ):
81
+ self .mystery_ship_group .add (MysteryShip (self .screen_width , self .offset ))
82
+
83
+ def check_for_collisions (self ):
84
+ #Spaceship
85
+ if self .spaceship_group .sprite .lasers_group :
86
+ for laser_sprite in self .spaceship_group .sprite .lasers_group :
87
+
88
+ aliens_hit = pygame .sprite .spritecollide (laser_sprite , self .aliens_group , True )
89
+ if aliens_hit :
90
+ self .explosion_sound .play ()
91
+ for alien in aliens_hit :
92
+ self .score += alien .type * 100
93
+ self .check_for_highscore ()
94
+ laser_sprite .kill ()
95
+
96
+ if pygame .sprite .spritecollide (laser_sprite , self .mystery_ship_group , True ):
97
+ self .score += 500
98
+ self .explosion_sound .play ()
99
+ self .check_for_highscore ()
100
+ laser_sprite .kill ()
101
+
102
+ for obstacle in self .obstacles :
103
+ if pygame .sprite .spritecollide (laser_sprite , obstacle .blocks_group , True ):
104
+ laser_sprite .kill ()
105
+
106
+ #Alien Lasers
107
+ if self .alien_lasers_group :
108
+ for laser_sprite in self .alien_lasers_group :
109
+ if pygame .sprite .spritecollide (laser_sprite , self .spaceship_group , False ):
110
+ laser_sprite .kill ()
111
+ self .lives -= 1
112
+ if self .lives == 0 :
113
+ self .game_over ()
114
+
115
+ for obstacle in self .obstacles :
116
+ if pygame .sprite .spritecollide (laser_sprite , obstacle .blocks_group , True ):
117
+ laser_sprite .kill ()
118
+
119
+ if self .aliens_group :
120
+ for alien in self .aliens_group :
121
+ for obstacle in self .obstacles :
122
+ pygame .sprite .spritecollide (alien , obstacle .blocks_group , True )
123
+
124
+ if pygame .sprite .spritecollide (alien , self .spaceship_group , False ):
125
+ self .game_over ()
126
+
127
+ def game_over (self ):
128
+ self .run = False
129
+
130
+ def reset (self ):
131
+ self .run = True
132
+ self .lives = 3
133
+ self .spaceship_group .sprite .reset ()
134
+ self .aliens_group .empty ()
135
+ self .alien_lasers_group .empty ()
136
+ self .create_aliens ()
137
+ self .mystery_ship_group .empty ()
138
+ self .obstacles = self .create_obstacles ()
139
+ self .score = 0
140
+
141
+ def check_for_highscore (self ):
142
+ if self .score > self .highscore :
143
+ self .highscore = self .score
144
+
145
+ with open ("highscore.txt" , "w" ) as file :
146
+ file .write (str (self .highscore ))
147
+
148
+ def load_highscore (self ):
149
+ try :
150
+ with open ("highscore.txt" , "r" ) as file :
151
+ self .highscore = int (file .read ())
152
+ except FileNotFoundError :
153
+ self .highscore = 0
0 commit comments