-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
280 lines (217 loc) · 7.48 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import math
import random
import tkinter as tk
import pygame
from pygame import mixer
import time
# Intialize the pygame
pygame.init()
#Creating the first start up screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("CLICK TO START")
font = pygame.font.Font(None, 50)
play_image = pygame.image.load("play.png").convert_alpha()
quit_image = pygame.image.load('quit.png').convert_alpha()
restart_image = pygame.image.load('restart.png').convert_alpha()
play_rect = play_image.get_rect()
play_rect.centerx = screen.get_rect().centerx - 100
play_rect.centery = screen.get_rect().centery
quit_rect = quit_image.get_rect()
quit_rect.centerx = screen.get_rect().centerx + 100
quit_rect.centery = screen.get_rect().centery
restart_rect = restart_image.get_rect()
restart_rect.centerx = screen.get_rect().centerx + 100
restart_rect.centery = screen.get_rect().centery
#caption and icon for the first screen
pygame.display.set_caption("Haggai's space fighter")
icon = pygame.image.load('spaceship.png')
pygame.display.set_icon(icon)
background = pygame.image.load('bg3.png')
play_bn = pygame.image.load('play.png').convert_alpha()
restart_bn = pygame.image.load('restart.png').convert_alpha()
quit_bn = pygame.image.load('quit.png').convert_alpha()
font1 = pygame.font.Font('freesansbold.ttf', 64)
# create the 2nd screen for the game
screen = pygame.display.set_mode((800, 600))
# Background
background = pygame.image.load('bg3.png')
# Sound
mixer.music.load("background.wav")
mixer.music.play(-1)
# Caption and Icon for the 2nd screen
pygame.display.set_caption("Space Invader")
icon = pygame.image.load('spaceship.png')
pygame.display.set_icon(icon)
# Score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
textX = 10
testY = 10
# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)
def show_score(x, y):
score = font.render("Score : " + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))
def start_game():
# code to start the game goes here
#loop for the first screen
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
mouse_pos=pygame.mouse.get_pos()
if play_rect.collidepoint(mouse_pos):
running = False
elif quit_rect.collidepoint(mouse_pos):
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
# Start the game here
pass
screen.blit(background, (0, 0))
screen.blit(play_image, play_rect)
screen.blit(quit_image, quit_rect)
pygame.display.update()
root = tk.Tk()
root.title("Click to Start Game")
start_button = tk.Button(root, text="Start Game", command=start_game())
start_button.pack()
# def game_over_text():
# # Clear the screen
# screen.fill((255, 255, 255))
# # Render the text "Game Over"
# font = pygame.font.Font(None, 36)
# text = font.render("Game Over", True, (0, 0, 0))
# text_rect = text.get_rect()
# text_rect.centerx = screen.get_rect().centerx
# text_rect.centery = screen.get_rect().centery
# # Blit the text onto the screen
# screen.blit(text, text_rect)
# # Update the screen
# pygame.display.update()
def game_over():
while True:
screen.fill((255, 255, 255))
screen.blit(background, (0, 0))
for i in range(3):
text = font1.render('Game over', True, (255, 255, 255))
screen.blit(text, (200, 250))
time.sleep(1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
break
pygame.display.update()
# Player
playerImg = pygame.image.load('ufo.png')
playerX = 370
playerY = 480
playerX_change = 0
def player(x, y):
screen.blit(playerImg, (x, y))
# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 20
for i in range(num_of_enemies):
enemyImg.append(pygame.image.load('enemy.png'))
enemyX.append(random.randint(0, 736))
enemyY.append(random.randint(50, 150))
enemyX_change.append(7)
enemyY_change.append(40)
def enemy(x, y, i):
screen.blit(enemyImg[i], (x, y))
# Bullet
# Ready - You can't see the bullet on the screen
# Fire - The bullet is currently moving
bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 8
bullet_state = "ready"
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt(math.pow(enemyX - bulletX, 2) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
# Game Loop
running = True
while running:
# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
playerX_change = 1
if event.key == pygame.K_SPACE:
if bullet_state is "ready":
bulletSound = mixer.Sound("laser.wav")
bulletSound.play()
# Get the current x cordinate of the spaceship
bulletX = playerX
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
#player change increment
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# Enemy Movement
for i in range(num_of_enemies):
# Game Over
if enemyY[i] > 440:
for j in range(num_of_enemies):
enemyY[j] = 2000
playerY=2000
game_over()
break
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 1
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 736:
enemyX_change[i] = -1
enemyY[i] += enemyY_change[i]
# Collision
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
explosionSound = mixer.Sound("explosion.wav")
explosionSound.play()
bulletY = 480
bullet_state = "ready"
score_value += 1
enemyX[i] = random.randint(0, 736)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state is "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
show_score(textX, testY)
pygame.display.update()
pygame.display.update()