-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathninja.py
280 lines (240 loc) · 8.03 KB
/
ninja.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 pygame
import random
import math
import time
pygame.init()
pygame.mixer.init()
# constants
from ninja.constants import *
from ninja.Button import Button
# pygame setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
pygame.display.set_caption("FRUIT NINJA")
start_btn = Button(start_img, 400, 300, False, screen)
pygame.mixer.music.play(-1)
# game variables
score = 0
lives = 5
level = 1
paused = True
new_level = True
current_score = 0
game_over = False
label_no = 0
letter_objects = []
input_letter = ""
text_font = pygame.font.Font("./AldotheApache.ttf", 50)
high_score_file = open(file_loc, "r")
read = high_score_file.readlines()
highscore = int(read[0])
high_score_file.close()
def drawText(text, font, text_colour, x, y):
img = font.render(text, True, text_colour)
screen.blit(img, (x, y))
def draw_screen():
screen.blit(background, (0, 0))
pygame.draw.rect(screen, "white", [0, 0, WIDTH, HEIGHT], 5)
drawText(f"Score: {score}", text_font, "black", 30, HEIGHT - 700)
drawText(f"LIVE {lives}", text_font, "black", WIDTH // 2 - 160, HEIGHT - 700)
drawText(f"BEST {highscore}", text_font, "black", WIDTH // 2 + 100, HEIGHT - 700)
pause_btn = Button(pause_img, WIDTH - 100, HEIGHT - 720, False, screen)
pause_btn.draw()
return pause_btn.clicked
def draw_pause():
surf = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
pygame.draw.rect(
surf, (0, 0, 0, 100), [(WIDTH - 600) / 2, (HEIGHT - 300) / 2, 600, 300], 0, 5
)
pygame.draw.rect(
surf, (0, 0, 0, 200), [(WIDTH - 600) / 2, (HEIGHT - 300) / 2, 600, 300], 5, 5
)
# define btns
resume_btn = Button(
play_img, (WIDTH - 600) / 2 + 50, (HEIGHT - 300) / 2 + 50, False, surf
)
resume_btn.draw()
quit_btn = Button(
quit_img, (WIDTH - 600) / 2 + 350, (HEIGHT - 300) / 2 + 50, False, surf
)
quit_btn.draw()
surf.blit(
text_font.render("Play", True, "white"),
((WIDTH - 600) / 2 + 180, (HEIGHT - 300) / 2 + 75),
)
surf.blit(
text_font.render("Quit", True, "white"),
((WIDTH - 600) / 2 + 480, (HEIGHT - 300) / 2 + 75),
)
surf.blit(
text_font.render(f"Score: {current_score}", True, "white"),
((WIDTH - 600) / 2 + 20, (HEIGHT - 300) / 2 + 200),
)
surf.blit(
text_font.render(f"HighScore: {highscore}", True, "white"),
((WIDTH - 600) / 2 + 250, (HEIGHT - 300) / 2 + 200),
)
screen.blit(surf, (0, 0))
return resume_btn.clicked, quit_btn.clicked
def check_highscore():
global highscore, score
if score > highscore:
highscore = score
file = open(file_loc, "w")
file.write(str(int(highscore)))
file.close()
class Letter:
def __init__(self, letter, speed, x_pos, y_pos, fruit, isBomb):
self.x_pos = x_pos
self.speed = speed
self.letter = letter
self.y_pos = y_pos
self.wid = x_pos
self.angle = 80 * 3.14 / 180
self.time = 0
self.gravity = 0.03999
self.isBomb = isBomb
self.fruit_img_full = fruit[0]
self.fruit_img_cut = fruit[1]
self.surf = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
self.pressed = False
self.scored = False
if self.y_pos >= HEIGHT + 150:
self.angle = 90 * 3.14 / 180
def draw(self):
if self.pressed:
self.remove()
else:
screen.blit(self.fruit_img_full, (self.x_pos - 60, self.y_pos - 60))
screen.blit(
text_font.render(self.letter, True, "white"), (self.x_pos, self.y_pos)
)
def remove(self):
screen.blit(self.fruit_img_cut, (self.x_pos - 60, self.y_pos - 60))
screen.blit(
text_font.render(self.letter, True, "green"),
(self.x_pos, self.y_pos),
)
def update(self):
# Calculate the new x and y positions
if self.wid > WIDTH / 2:
self.x_pos = self.x_pos - self.speed * math.cos(self.angle) * self.time
else:
self.x_pos = self.x_pos + self.speed * math.cos(self.angle) * self.time
self.y_pos = self.y_pos - (
self.speed * math.sin(self.angle) * self.time
- 0.5 * self.gravity * self.time**2
)
# Increase the time
self.time += 1
def generate_levels():
global input_letter
input_letter = ''
letter_objs = []
letter_copy = letters.copy()
horizontal_spacing = (WIDTH - 150) // level
for i in range(level):
speed_tup = random.choice(speed_arr)
x_pos = random.randint(
10 + (i * horizontal_spacing), (i + 1) * horizontal_spacing
)
speed = speed_tup[0]
y_pos = speed_tup[1]
letter = random.choice(letter_copy)
letter_copy.remove(letter)
fruit = random.choice(fruits)
if fruit[0] == fruit[1]:
isBomb = True
else:
isBomb = False
new_letter = Letter(letter, speed, x_pos, y_pos, fruit, isBomb)
letter_objs.append(new_letter)
return letter_objs
def main():
global new_level, paused, lives, level, input_letter, letter_objects, score, current_score, game_over, label_no
clock = pygame.time.Clock()
running = True
while running:
screen.fill("white")
screen.blit(background, (0, 0))
pausebtn_status = draw_screen()
if paused and game_over:
game_over = False
if paused:
resume_btt, quit_butt = draw_pause()
if resume_btt:
ui_sound.play()
paused = False
if quit_butt:
ui_sound.play()
check_highscore()
running = False
if new_level and not paused:
letter_objects = generate_levels()
new_level = False
else:
for l in letter_objects:
l.draw()
if not paused:
l.update()
if l.y_pos >= HEIGHT + 200:
if not l.pressed and not l.isBomb:
score -= 1
label_no += 1
if score <=0:
score = 0
letter_objects.remove(l)
if len(letter_objects) <= 0 and not paused:
input_letter = ""
if label_no == 0:
level += 1
label_no = 0
if level >= 4:
level = 4
new_level = True
for l in letter_objects:
if l.letter == input_letter and not l.scored:
input_letter = ""
if not l.isBomb:
score += level
fruit_sound1.play()
else:
bomb_sound.play()
lives -= 1
l.pressed = True
l.scored = True
# /1
for event in pygame.event.get():
if event.type == pygame.QUIT:
check_highscore()
running = False
if event.type == pygame.KEYDOWN:
if not paused:
if event.unicode.lower() in letters:
input_letter = event.unicode.lower()
if event.key == pygame.K_SPACE or event.key == pygame.K_ESCAPE:
if paused:
paused = False
ui_sound.play()
else:
paused = True
ui_sound.play()
if pausebtn_status:
paused = True
if lives <= 0:
game_over = True
paused = True
input_letter = ""
lives = 5
level = 1
letter_objects = []
new_level = True
check_highscore()
current_score = score
score = 0
if paused:
draw_pause()
clock.tick(60)
pygame.display.update()
pygame.quit()
main()