12
12
SCREEN_HEIGHT = 700
13
13
OFFSET = 50
14
14
15
+ title_font = pygame .font .Font ("Font/monogram.ttf" , 40 )
16
+ level_surface = title_font .render ("LEVEL 01" , False , YELLOW )
17
+ game_over_surface = title_font .render ("GAME OVER" , False , YELLOW )
18
+ score_text_surface = title_font .render ("SCORE" , False , YELLOW )
19
+ high_score_text_surface = title_font .render ("HIGH-SCORE" , False , YELLOW )
20
+
15
21
screen = pygame .display .set_mode ((SCREEN_WIDTH + OFFSET , SCREEN_HEIGHT + 2 * OFFSET ))
16
22
pygame .display .set_caption ("Space Invaders" )
17
23
25
31
game = Game (SCREEN_WIDTH , SCREEN_HEIGHT , OFFSET )
26
32
27
33
while True :
28
- for event in pygame .event .get ():
29
- if event .type == pygame .QUIT :
30
- pygame .quit ()
31
- sys .exit ()
32
- if event .type == ALIENLASER :
33
- game .alien_shoot_laser ()
34
- if event .type == MYSTERYSHIP :
35
- game .create_mystery_ship ()
36
-
37
- screen .fill (GREY )
38
- pygame .draw .rect (screen , YELLOW , (10 , 10 ,775 , 775 ), 2 , 0 , 60 , 60 , 60 , 60 )
39
- pygame .draw .line (screen , YELLOW , (25 , 800 - 70 ), (800 - 25 , 800 - 70 ), 3 )
40
-
41
- game .alien_lasers .update ()
42
- game .spaceship .update ()
43
- game .mystery_ship .update ()
44
- game .check_for_collisions ()
45
-
46
- game .alien_position_checker ()
47
- game .aliens .update (game .alien_direction )
48
-
49
- game .spaceship .draw (screen )
50
- game .spaceship .sprite .lasers .draw (screen )
51
- game .obstacle_1 .blocks .draw (screen )
52
- game .obstacle_2 .blocks .draw (screen )
53
- game .obstacle_3 .blocks .draw (screen )
54
- game .obstacle_4 .blocks .draw (screen )
55
- game .aliens .draw (screen )
56
- game .alien_lasers .draw (screen )
57
- game .mystery_ship .draw (screen )
58
-
59
- pygame .display .update ()
60
- clock .tick (60 )
34
+ for event in pygame .event .get ():
35
+ if event .type == pygame .QUIT :
36
+ pygame .quit ()
37
+ sys .exit ()
38
+ if event .type == ALIENLASER :
39
+ game .alien_shoot_laser ()
40
+ if event .type == MYSTERYSHIP and game .run :
41
+ game .create_mystery_ship ()
42
+
43
+ keys = pygame .key .get_pressed ()
44
+ if keys [pygame .K_SPACE ] and game .run == False :
45
+ game .reset ()
46
+
47
+ screen .fill (GREY )
48
+ pygame .draw .rect (screen , YELLOW , (10 , 10 ,775 , 775 ), 2 , 0 , 60 , 60 , 60 , 60 )
49
+ pygame .draw .line (screen , YELLOW , (25 , 800 - 70 ), (800 - 25 , 800 - 70 ), 3 )
50
+
51
+ if game .run :
52
+ game .alien_lasers .update ()
53
+ game .spaceship .update ()
54
+ game .mystery_ship .update ()
55
+ game .check_for_collisions ()
56
+ game .alien_position_checker ()
57
+ game .aliens .update (game .alien_direction )
58
+
59
+ game .spaceship .draw (screen )
60
+ game .spaceship .sprite .lasers .draw (screen )
61
+ game .obstacle_1 .blocks .draw (screen )
62
+ game .obstacle_2 .blocks .draw (screen )
63
+ game .obstacle_3 .blocks .draw (screen )
64
+ game .obstacle_4 .blocks .draw (screen )
65
+ game .aliens .draw (screen )
66
+ game .alien_lasers .draw (screen )
67
+ game .mystery_ship .draw (screen )
68
+ screen .blit (score_text_surface , (50 , 15 , 50 , 50 ))
69
+ screen .blit (high_score_text_surface , (550 , 15 , 50 , 50 ))
70
+
71
+ formatted_high_score = str (game .highscore ).zfill (5 ) # Pad with leading zeros to make it 4 digits
72
+ formatted_score = str (game .score ).zfill (5 ) # Pad with leading zeros to make it 4 digits
73
+ score_surface = title_font .render (formatted_score , False , YELLOW )
74
+ high_score_surface = title_font .render (formatted_high_score , False , YELLOW )
75
+ screen .blit (score_surface , (50 , 40 , 50 , 50 ))
76
+ screen .blit (high_score_surface , (625 , 40 , 50 , 50 ))
77
+
78
+ if game .run :
79
+ screen .blit (level_surface , (570 , SCREEN_HEIGHT + 38 , 50 , 50 ))
80
+ else :
81
+ screen .blit (game_over_surface , (570 , SCREEN_HEIGHT + 38 , 50 , 50 ))
82
+
83
+ x_start = 50 # Adjust this based on your desired starting x-coordinate
84
+ y = SCREEN_HEIGHT + OFFSET - 5
85
+
86
+ for _ in range (game .lives ):
87
+ screen .blit (game .lives_surface , (x_start , y ))
88
+ x_start += 50 # Adjust this value to control the spacing between lives
89
+
90
+ pygame .display .update ()
91
+ clock .tick (60 )
0 commit comments