-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
310 lines (255 loc) · 11.6 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import pygame, random
import button
import choose
import level
import sys
from game import Game
import inputbox
from authSystem import AuthSystem
pygame.init()
pygame.mixer.music.load("Sounds/music.ogg")
pygame.mixer.music.play(-1)
SCREEN_WIDTH = 770
SCREEN_HEIGHT = 620
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('GAME NHOM 7')
music_playing = True
# Tải ảnh lên
background_img = pygame.image.load('images/background2.png').convert()
background_img = pygame.transform.scale(background_img, (SCREEN_WIDTH, SCREEN_HEIGHT))
batdau_img = pygame.image.load('images/btn_batdau.png').convert_alpha()
luachon_img = pygame.image.load('images/btn_luachon.png').convert_alpha()
thoat_img = pygame.image.load('images/btn_thoat.png').convert_alpha()
quaylai_img = pygame.image.load('images/btn_quaylai.png').convert_alpha()
bat_nhac_img = pygame.image.load('images/batnhac.png').convert_alpha()
tat_nhac_img = pygame.image.load('images/tatnhac.png').convert_alpha()
choilai_img = pygame.image.load('images/btn_choilai.png').convert_alpha()
tieptuc_img = pygame.image.load('images/btn_tieptuc.png').convert_alpha()
chonman_img = pygame.image.load('images/btn_chonman.png').convert_alpha()
# Tạo button
batdau_button = button.Button(375, 270, batdau_img, 0.5)
luachon_button = button.Button(375, 350, luachon_img, 0.5)
thoat_button = button.Button(375, 430, thoat_img, 0.5)
quaylai_button = button.Button(375, 500, quaylai_img, 0.3)
batnhac_button = button.Button(375,350, bat_nhac_img, 3.0)
tatnhac_button= button.Button(375,430, tat_nhac_img, 3.0)
choilai_button= button.Button(375,300, choilai_img, 0.5)
tieptuc_button= button.Button(375,300, tieptuc_img, 0.5)
chonman_button= button.Button(375,370, chonman_img, 0.5)
# Trạng thái game
menu_state = ["main"]
# Font
font_path = 'fonts/SVN-Determination Sans.otf'
font = pygame.font.Font(font_path, 5)
font_tieude = pygame.font.Font(font_path, 40)
font2 = pygame.font.Font(font_path, 20)
# Màu sắc
Black = (1, 1, 1)
White = (250, 250, 250)
Red = (250, 0, 0)
def draw_text(text, path, size, text_color, x, y):
font = pygame.font.Font(path, size)
img = font.render(text, True, text_color)
screen.blit(img, (x, y))
#Thông tin người dùng
FILE_NAME = "user_data.json"
current_user = None #người dùng hiện tại
# Tạo các ô nhập liệu
username_box = inputbox.InputBox(330, 150, 200, 32)
password_box = inputbox.InputBox(330, 200, 200, 32)
auth_system = AuthSystem(FILE_NAME)
# Biến trạng thái
message = ""
mode = "register"
#Kết quả game
resultGame = None
max_level_index = 4
# Vẽ score
score_text_surface = font.render("SCORE", True, White)
highscore_text_surface = font.render("HIGH SCORE",True, White)
# Main game loop
run = True
character_selected = False
selected_character_index = None
level_selected = False
selected_level_index = None
clock = pygame.time.Clock()
SHOOT_LASER = pygame.USEREVENT
pygame.time.set_timer(SHOOT_LASER, 300)
BONUSSHIP = pygame.USEREVENT + 1
pygame.time.set_timer(BONUSSHIP, random.randint(4000, 8000))
BOSS_SHOOT = pygame.USEREVENT + 2
pygame.time.set_timer(BOSS_SHOOT, 1000)
while run:
screen.blit(background_img, (0, 0)) # Vẽ nền
if menu_state[0] == "main":
draw_text("CHEO", font_path, 120, White, 260, 20)
draw_text("GAME", font_path, 120, White, 260, 97)
if batdau_button.draw(screen):
if current_user:
menu_state[0] = "choose"
else:
menu_state[0] = "register"
if luachon_button.draw(screen):
menu_state[0] = "options"
if thoat_button.draw(screen):
run = False
elif menu_state[0] == "choose":
shooter_choose = choose.Choose(SCREEN_WIDTH, SCREEN_HEIGHT)
selected_character_index = shooter_choose.character_select(screen, menu_state)
if selected_character_index is not None:
character_selected = True
level_selected = False
else:
character_selected = False
elif menu_state[0] == "level":
screen.blit(background_img, (0, 0))
level_choose = level.Level(SCREEN_WIDTH, SCREEN_HEIGHT, current_user)
selected_level_index = level_choose.level_select(screen, menu_state)
if selected_level_index is not None:
level_selected = True
else:
level_selected = False
elif menu_state[0] == "start":
if selected_character_index is not None and selected_level_index is not None:
game = Game(SCREEN_WIDTH, SCREEN_HEIGHT, menu_state, selected_character_index, selected_level_index, current_user)
while menu_state[0] == "start":
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == SHOOT_LASER and game.run:
game.invader_shoot_laser()
if event.type == BONUSSHIP and game.run:
game.create_bonus_ship()
pygame.time.set_timer(BONUSSHIP, random.randint(4000, 8000))
if event.type == BOSS_SHOOT and game.run:
game.boss_shoot_laser()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] and not game.run:
game.reset()
# Cập nhật
if game.run:
game.shooter_group.update()
game.move_invaders()
game.invader_lasers_group.update()
game.bonus_ship_group.update()
game.boss_group.update()
game.boss_lasers_group.update()
result = game.check_for_collisions()
if result:
resultGame = result
print("Kết quả: ", result)
menu_state[0] = "result"
# Vẽ
screen.blit(background_img, (0, 0))
game.shooter_group.draw(screen)
if game.shooter_group.sprite:
game.shooter_group.sprite.lasers_group.draw(screen)
for object in game.object:
object.blocks_group.draw(screen)
life_image = pygame.transform.scale(game.shooter_group.sprite.image, (52, 52))
x = 550
for life in range(game.lives):
screen.blit(life_image, (x, 7)) # surface, position
x += 50
score_text = f"Score: {game.score}"
draw_text(score_text, font_path, 22, White, 100, 9)
score_high = f"High Score: {game.highscore}"
draw_text(score_high, font_path, 22, White, 350,9)
game.invaders_group.draw(screen)
game.invader_lasers_group.draw(screen)
game.draw_level(screen)
game.bonus_ship_group.draw(screen)
game.boss_group.draw(screen)
game.boss_lasers_group.draw(screen)
pygame.display.update()
clock.tick(60)
elif menu_state[0] == "options":
draw_text("CÀI ĐẶT", font_path, 40, White, 310, 70)
if batnhac_button.draw(screen):
if not music_playing: # Chỉ bật nhạc nếu nhạc chưa được bật
music_playing = True
pygame.mixer.music.play(-1)
if tatnhac_button.draw(screen):
if music_playing:
pygame.mixer.music.stop()
music_playing = False
if quaylai_button.draw(screen):
menu_state[0] = "main"
elif menu_state[0] == "result":
if resultGame == "win":
draw_text("Bạn đã thắng!", font_path, 70, White, 200, 70)
if selected_level_index < max_level_index:
if tieptuc_button.draw(screen):
selected_level_index += 1
menu_state[0] = "start"
else:
draw_text("Bạn đã hoàn thành tất cả cấp độ!", font_path, 40, White, 100, 180)
if chonman_button.draw(screen):
menu_state[0] = "level"
if quaylai_button.draw(screen):
menu_state[0] = "main"
if resultGame == "lose":
draw_text("Bạn đã thua!", font_path, 70, White, 200, 70)
if choilai_button.draw(screen):
menu_state[0] = "start"
if chonman_button.draw(screen):
menu_state[0] = "level"
if quaylai_button.draw(screen):
menu_state[0] = "main"
elif menu_state[0] == "register":
draw_text("ĐĂNG KÝ" if mode == "register" else "ĐĂNG NHẬP",font_path, 40, White, 300, 50)
draw_text("Tên người dùng:", font_path, 20, White, 160, 150)
draw_text("Mật khẩu:", font_path, 20, White, 160, 200)
for event in pygame.event.get():
if event.type == pygame.QUIT:
menu_state[0] = "main"
# Xử lý sự kiện nhập liệu
username_box.handle_event(event)
password_box.handle_event(event)
if event.type == pygame.MOUSEBUTTONDOWN:
if login_button.collidepoint(event.pos):
username = username_box.get_text()
password = password_box.get_text()
if username and password:
if mode == "register":
message, current_user = auth_system.register(username, password)
elif mode == "login":
message, current_user = auth_system.login(username, password)
username_box.clear_text()
password_box.clear_text()
elif mode_button.collidepoint(event.pos):
mode = "login" if mode == "register" else "register"
# Cập nhật
username_box.update()
password_box.update()
# Vẽ
username_box.draw(screen)
password_box.draw(screen)
draw_text(message, font_path, 20, Red, 250, 270)
if current_user:
draw_text(f"Tên: {current_user}", font_path, 13, White, 5, 5)
menu_state[0] = "choose"
login_button = pygame.Rect(300, 330, 150, 40)
pygame.draw.rect(screen, White, login_button, border_radius=10)
login_text = font2.render("Đăng ký" if mode == "register" else "Đăng nhập", True, Black)
screen.blit(login_text, (login_button.x + (login_button.width - login_text.get_width()) // 2,
login_button.y + (login_button.height - login_text.get_height()) // 2))
# Nút chuyển chế độ
mode_button = pygame.Rect(220, 380, 300, 40)
mode_text = font2.render("Bạn đã có tài khoản? ĐĂNG NHẬP" if mode == "register" else "Bạn chưa có tài khoản? ĐĂNG KÝ", True, White)
screen.blit(mode_text, (mode_button.x + (mode_button.width - mode_text.get_width()) // 2,
mode_button.y + (mode_button.height - mode_text.get_height()) // 2))
if quaylai_button.draw(screen):
menu_state[0] = "main"
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
print("Menu State:", menu_state)
print("Selected Character Index:", selected_character_index + 1 if selected_character_index is not None else "None")
print("Selected Level Index:", selected_level_index + 1 if selected_level_index is not None else "None")
pygame.display.update()
clock.tick(60)
pygame.quit()