-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
236 lines (179 loc) · 6.98 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import sys
import time
import pygame
import config as cfg
from paddle import Paddle
from ball import Ball
from brick import Brick
from game_menu import *
from pygame.locals import *
pygame.init()
count=1
score=cfg.SCORE
time_duration_count=cfg.TIME_DURATION_COUNT
font = pygame.font.SysFont(None, 30)
# set window size
size=(cfg.WINDOW_WIDTH,cfg.WINDOW_HEIGHT)
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Shooter Aircraft Breakout Game")
balls=[]
# this will be a list that will contain all the sprites we intend
all_sprites_list=pygame.sprite.Group()
all_bricks=pygame.sprite.Group()
def paddle_and_ball_initialization():
# Create the paddle
paddle=Paddle(cfg.LIGHTBLUE,100,50)
paddle.rect.x=350
paddle.rect.y=560
# Create the ball sprite
ball=Ball(cfg.LIGHTBLUE,10,10)
ball.rect.x=400
ball.rect.y=560
balls.append(ball)
all_sprites_list.add(paddle)
return paddle, ball
def brick_design_on_display():
colors=[cfg.RED,cfg.ORANGE,cfg.YELLOW,cfg.GREEN]
for i in range(7):
for j in range(4):
brick = Brick(colors[j],80,30)
brick.rect.x = 60 + i* 100
brick.rect.y = 100+j*40
all_sprites_list.add(brick)
all_bricks.add(brick)
return all_bricks
def score_and_time_duration_calucation(score,time_duration_count):
font=pygame.font.Font(None,34)
text=font.render("Score: "+str(score),1,cfg.WHITE)
screen.blit(text,(20,10))
text=font.render("Time: "+str(time_duration_count),1,cfg.WHITE)
screen.blit(text,(650,10))
def game_win_function(score,time_duration_count):
font=pygame.font.Font(None,65)
text=font.render("Win, Congratulations!",1,cfg.WHITE)
screen.blit(text,(200,200))
pygame.display.flip()
player_score_and_time_show_in_display(score,time_duration_count)
pygame.time.wait(3000)
def player_score_and_time_show_in_display(score,time_duration_count):
font=pygame.font.Font(None,45)
text=font.render('Player score: '+str(score),1,cfg.GREEN)
screen.blit(text,(250,300))
pygame.display.flip()
text=font.render('Total time: '+str(time_duration_count),1,cfg.GREEN)
screen.blit(text,(250,350))
pygame.display.flip()
def game_over_function(score,time_duration_count):
font=pygame.font.Font(None,74)
text=font.render("You Lost",1,cfg.WHITE)
screen.blit(text,(250,150))
pygame.display.flip()
player_score_and_time_show_in_display(score,time_duration_count)
pygame.time.wait(3000)
def brick_and_ball_collision_detection(ball,brick,score,time_duration_count):
ball.bounce()
ball.kill()
brick.kill()
balls.remove(ball)
if len(all_bricks) ==0:
game_win_function(score,time_duration_count)
carryOn=False
return carryOn
def ball_fire_to_bricks_from_paddle(ball,paddle,score,time_duration_count):
# ball=Ball(WHITE,10,10)
all_sprites_list.add(ball)
ball.rect.x=paddle.rect.x+50
ball.rect.y=paddle.rect.y
balls.append(ball)
for brick in all_bricks:
brick.rect.y += 1
if brick.rect.y > 550:
game_over_function(score,time_duration_count)
carryOn=False
return carryOn
def game_end_display_function(clock,score,time_duration_count):
screen.fill(cfg.DARKBLUE)
pygame.draw.line(screen,cfg.WHITE,[0,38],[800,38],2)
all_sprites_list.draw(screen)
score_and_time_duration_calucation(score,time_duration_count)
pygame.display.flip()
clock.tick(100)
def aircraft_shooter_game_screen(carryOn,all_sprites_list,all_bricks,paddle,ball,balls,clock,score,time_duration_count):
while carryOn:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
carryOn=False
keys=pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
paddle.moveLeft(5)
if keys[pygame.K_RIGHT]:
paddle.moveRight(5)
if keys[pygame.K_DOWN]:
paddle.moveDown(5)
if keys[pygame.K_UP]:
paddle.moveUp(5)
if keys[pygame.K_SPACE]:
game_end=ball_fire_to_bricks_from_paddle(ball,paddle,score,time_duration_count)
if game_end == False:
carryOn=False
all_sprites_list.update()
for ball in balls:
brick_collision_list=pygame.sprite.spritecollide(ball,all_bricks,False)
for brick in brick_collision_list:
score +=1
game_end=brick_and_ball_collision_detection(ball,brick,score,time_duration_count)
if game_end == False:
carryOn=False
time_duration_count+=1
game_end_display_function(clock,score,time_duration_count)
pygame.quit()
if __name__ == "__main__":
# make a game start menu program
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 30)
screen = pygame.display.set_mode((cfg.WINDOW_WIDTH, cfg.WINDOW_HEIGHT),0,32)
click = False
while True:
screen.fill((0,190,255))
draw_text('Shooter Aircraft Breakout Game', pygame.font.SysFont(None, 45), (0,0,255), screen, 200, 100)
mx, my = pygame.mouse.get_pos()
#creating buttons
button_1 = pygame.Rect(300, 200, 200, 50)
button_2 = pygame.Rect(300, 300, 200, 50)
button_3 = pygame.Rect(300,400, 200, 50)
#defining functions when a certain button is pressed
if button_1.collidepoint((mx, my)):
if click:
carryOn=True
# clock=pygame.time.Clock()
all_bricks=brick_design_on_display()
paddle,ball=paddle_and_ball_initialization()
aircraft_shooter_game_screen(carryOn,all_sprites_list,all_bricks,paddle,ball,balls,clock,score,time_duration_count)
if button_2.collidepoint((mx, my)):
if click:
options(screen,font,clock,click)
if button_3.collidepoint((mx, my)):
if click:
pygame.quit()
sys.exit()
pygame.draw.rect(screen, (255, 0, 0), button_1)
pygame.draw.rect(screen, (255, 0, 0), button_2)
pygame.draw.rect(screen, (255, 0, 0), button_3)
#writing text on top of button
draw_text('PLAY', font, (255,255,255), screen, 370, 220)
draw_text('Game Rules', font, (255,255,255), screen, 350, 315)
draw_text('Exit', font, (255,255,255), screen, 380, 420)
click = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
clock.tick(60)