This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
280 lines (265 loc) · 12.4 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 pygame
import random
import components.button as button
import sys
from game import Game
from interface.leaderboard import Leaderboard
leaderboard = Leaderboard("./utils/leaderboard.txt")
# init pygame.
pygame.init()
# load game icon.
icon = pygame.image.load("./img/icon.png")
# set game icon.
pygame.display.set_icon(icon)
# set game name.
pygame.display.set_caption("ʙᴀsᴋᴇᴛ ɢᴏ !")
# define window's size.
window = pygame.display.set_mode((1024, 640))
fond = pygame.image.load('./img/main_menu/background/bg.png')
pygame.mouse.set_pos((fond.get_width()/2, fond.get_height()/2))
# load button images
start_img = pygame.image.load('./img/main_menu/buttons/start_btn.png')
exit_img = pygame.image.load('./img/main_menu/buttons/exit_btn.png')
start_img_hov = pygame.image.load('./img/main_menu/buttons/start_btn_hov.png')
exit_img_hov = pygame.image.load('./img/main_menu/buttons/exit_btn_hov.png')
Leader_img = pygame.image.load('./img/main_menu/buttons/Leaderboard_btn.png')
Leader_img_hov = pygame.image.load(
'./img/main_menu/buttons/Leaderboard_btn_hov.png')
shop_img = pygame.image.load('./img/main_menu/buttons/shop_btn.png')
shop_img_hov = pygame.image.load('./img/main_menu/buttons/shop_btn_hov.png')
gear_img = pygame.image.load('./img/main_menu/buttons/gear_btn.png')
gear_img_hov = pygame.image.load('./img/main_menu/buttons/gear_btn_hov.png')
back_img = pygame.image.load('./img/option_menu/buttons/back_btn.png')
back_img_hov = pygame.image.load('./img/option_menu/buttons/back_btn_hov.png')
save_img = pygame.image.load('./img/option_menu/buttons/save_btn.png')
save_img_hov = pygame.image.load('./img/option_menu/buttons/save_btn_hov.png')
# create button instances
start_button = button.Button(397, 390, start_img, start_img_hov, 1)
exit_button = button.Button(412, 500, exit_img, exit_img_hov, 1)
leader_btn = button.Button(920, 540, Leader_img, Leader_img_hov, 1)
shop_btn = button.Button(840, 550, shop_img, shop_img_hov, 1)
gear_btn = button.Button(17, 550, gear_img, gear_img_hov, 1)
back_btn = button.Button(612, 500, back_img, back_img_hov, 1)
back_btn2 = button.Button(412, 500, back_img, back_img_hov, 1)
save_btn = button.Button(212, 500, save_img, save_img_hov, 1)
clock = pygame.time.Clock()
# game variables
menu_state = "main"
is_running = True
# set leaderboard
leaderboard.import_player_from_txt()
leaderboard_data = leaderboard.get_leaderboard_rank()
##########################################################
# basic font for user typed
# Define the police
font = pygame.font.Font('fonts/Comicy.ttf', 43)
font2 = pygame.font.Font('fonts/Comicy.ttf', 70)
font3 = pygame.font.Font('fonts/Comicy.ttf', 60)
base_font = pygame.font.Font('fonts/Comicy.ttf', 26)
base_font2 = pygame.font.Font('fonts/Comicy.ttf', 12)
user_text = ''
key_text = ''
# create rectangle
input_rect2 = pygame.Rect(530, 260, 500, 30)
input_rect = pygame.Rect(450, 320, 500, 30)
text_on_user = "Maximum 6 characters"
text_on_key = ""
# color_active stores color(lightskyblue3) which
# gets active when input box is clicked by user
color_active = pygame.Color((60, 60, 60))
# color_passive store color(chartreuse4) which is
# color of input box.
color_passive = pygame.Color((0, 0, 0))
color = color_passive
color2 = color_passive
active = False
active2 = False
cursor = "img/cursor.png"
cursor_init = pygame.image.load(cursor).convert_alpha()
pygame.mouse.set_visible(False)
dict_pos = {"main1": [(513, 438), (513, 547)], "main2": [(58, 593), (887, 589), (971, 590)],
"option": [(320, 550), (712, 550)], "leaderboard": [(513, 547)], "shop": [], }
# set sound
sound_img = pygame.image.load('./img/option_menu/buttons/sound_btn.png')
sound_img_hov = pygame.image.load(
'./img/option_menu/buttons/sound_btn_hov.png')
sound_minus_img = pygame.image.load(
'./img/option_menu/buttons/sound_minus_btn.png')
sound_minus_img_hov = pygame.image.load(
'./img/option_menu/buttons/sound_minus_btn_hov.png')
sound_btn = button.Button(490, 428, sound_img, sound_img_hov, 0.5)
sound2_btn = button.Button(780, 428, sound_img, sound_img_hov, 0.5)
sound_minus_btn = button.Button(
305, 438, sound_minus_img, sound_minus_img_hov, 0.5)
sound2_minus_btn = button.Button(
595, 438, sound_minus_img, sound_minus_img_hov, 0.5)
sound = pygame.Rect(337, 434, 75, 20)
sound2 = pygame.Rect(627, 434, 75, 20)
while is_running:
MOUSE_POS = pygame.mouse.get_pos()
x, y = pygame.mouse.get_pos()
x -= cursor_init.get_width()/2-5
y -= cursor_init.get_height()/2-8
# blit background
window.blit(fond, (0, 0))
# check if game is paused
if menu_state == "main":
fond = pygame.image.load('./img/main_menu/background/bg.png')
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
button.Button.parcourir(dict_pos, "main1", MOUSE_POS, event)
if leader_btn.draw_and_clicked(window, event):
menu_state = "options"
Leader = True
if shop_btn.draw_and_clicked(window, event):
pass
# if start_button was clicked or selected and press enter
if start_button.draw_and_clicked(window, event) == True:
image = ["flame", "mountains", "pink",
"basket-ball", "smile", "military"]
image_terrain = ["IMG", "IMG2", "IMG3"]
i_bis = random.randint(0, 2)
i = random.randint(0, 5)
# create a new instance of the Game.
# create a new instance of the Game.
game = Game(window, "img/", image[i], image_terrain[i_bis], None, [
(152, 255, 152), (135, 206, 235), (255, 105, 180), (255, 215, 0), (64, 224, 208), (218, 112, 214)])
# register dummy player.
game.register_player(user_text, 0)
game.register_player("Test", 0)
# setup the game.
game.setup()
if exit_button.draw_and_clicked(window, event):
pygame.quit()
sys.exit()
if gear_btn.draw_and_clicked(window, event):
menu_state = "options"
Leader = False
# check if the options menu is open
if menu_state == "options":
# draw the different options buttons
if Leader == False:
fond = pygame.image.load('./img/option_menu/background/bg.png')
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
button.Button.parcourir(
dict_pos, "option", MOUSE_POS, event)
if event.type == pygame.MOUSEBUTTONDOWN:
if input_rect.collidepoint(event.pos):
active = True
else:
active = False
if input_rect2.collidepoint(event.pos):
active2 = True
else:
active2 = False
if active == True:
color = color_active
if event.type == pygame.KEYDOWN:
# Check for backspace
if event.key == pygame.K_BACKSPACE:
# get text input from 0 to -1 i.e. end.
user_text = user_text[:-1]
# Unicode standard is used for string
# formation
else:
if event.key <= 122 and event.key >= 97:
if len(user_text) < 6:
user_text += event.unicode
else:
color = color_passive
if active2 == True:
color2 = color_active
if event.type == pygame.KEYDOWN:
# Check for backspace
if event.key == pygame.K_BACKSPACE:
# get text input from 0 to -1 i.e. end.
key_text = key_text[:-1]
# Unicode standard is used for string
# formation
else:
if len(key_text) < 1:
key_text += event.unicode
else:
color2 = color_passive
if back_btn.draw_and_clicked(window, event):
menu_state = "main"
if save_btn.draw_and_clicked(window, event):
menu_state = "main"
sound_music = ((sound.w * 2)/3)/100
sound_effect = ((sound2.w * 2)/3)/100
print(sound_music, sound_effect)
# option(key_text,user_text,sound_music,sound_effect)
if sound_btn.draw_and_clicked(window, event):
if 150 > sound.w >= 0:
sound.w += 15
print(sound.w)
if sound_minus_btn.draw_and_clicked(window, event):
if 150 >= sound.w >= 10:
sound.w -= 15
print(sound.w)
if sound2_btn.draw_and_clicked(window, event):
if 150 > sound2.w >= 0:
sound2.w += 15
print(sound2.w)
if sound2_minus_btn.draw_and_clicked(window, event):
if 150 >= sound2.w >= 10:
sound2.w -= 15
print(sound2.w)
pygame.draw.rect(window, (255, 255, 255), sound)
pygame.draw.rect(window, (255, 255, 255), sound2)
# draw rectangle and argument passed which should
# be on screen
pygame.draw.rect(window, color, input_rect)
pygame.draw.rect(window, color2, input_rect2)
text_surface = base_font.render(user_text, True, (255, 255, 255))
text_surface2 = base_font.render("R", True, (255, 255, 255))
# render at position stated in arguments
window.blit(text_surface, (input_rect.x+5, input_rect.y+3))
window.blit(text_surface2, (input_rect2.x+90, input_rect2.y+3))
if not active == True and not len(user_text) != 0:
window.blit(base_font2.render(text_on_user, True,
(255, 255, 255)), (input_rect.x+12, input_rect.y+10))
if not active2 == True and not len(key_text) != 0:
window.blit(base_font2.render(
text_on_key, True, (255, 255, 255)), (input_rect2.x+12, input_rect2.y+10))
# set width of textfield so that text cannot get
# outside of user's text input
input_rect.w = max(200, text_surface.get_width()+10)
input_rect2.w = max(200, text_surface.get_width()+10)
else:
fond = pygame.image.load(
'./img/leaderboard_menu/background/bg.png')
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
button.Button.parcourir(
dict_pos, "leaderboard", MOUSE_POS, event)
# get all player by score in a dict
textLeader = font2.render("LEADERBOARD", True, (255, 255, 255))
textPlayer = font3.render("PLAYER", True, (45, 130, 211))
textScore = font3.render("SCORE", True, (45, 130, 211))
# Print the leaderboard from leaderboard_data
for i in range(0, len(leaderboard_data)):
text = font.render(
leaderboard_data[i][0], True, (255, 255, 255))
window.blit(text, (262, 290+i*50))
text = font.render(
str(leaderboard_data[i][1]), True, (255, 255, 255))
window.blit(text, (620, 290+i*50))
window.blit(textPlayer, (220, 230))
window.blit(textScore, (578, 230))
window.blit(textLeader, (240, 90))
if back_btn2.draw_and_clicked(window, event):
menu_state = "main"
window.blit(cursor_init, (x, y))
pygame.display.update()
clock.tick(60)