-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.py
67 lines (56 loc) · 2.73 KB
/
level.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
import pygame
import json
import button
class Level(pygame.sprite.Sprite):
def __init__(self, screen_width, screen_height, username):
self.screen_width = screen_width
self.screen_height = screen_height
self.levels = ['1', '2', '3','4','5']
self.level_selected = 0
self.username = username
self.scores = self.load_scores()
def level_select(self, screen, menu_state):
font = pygame.font.Font('fonts/SVN-Determination Sans.otf', 50)
background_img = pygame.image.load('images/background2.png').convert()
background_img = pygame.transform.scale(background_img, (self.screen_width, self.screen_height))
thoat_img = pygame.image.load('images/btn_thoat.png').convert_alpha()
thoat_button = button.Button(375, 500, thoat_img, 0.3)
levelrun = True
while levelrun:
screen.blit(background_img, (0, 0))
title = font.render("Chọn màn chơi", True, (255, 255, 255))
screen.blit(title, (self.screen_width // 2 - title.get_width() // 2, 50))
for i, level in enumerate(self.levels):
score = self.scores.get(str(i), 0)
level_text = font.render(f"Màn {level} - Điểm {score}", True, (255, 255, 255))
level_rect = level_text.get_rect(center=(self.screen_width // 2, 150 + i * 60))
screen.blit(level_text, level_rect)
if i == self.level_selected:
pygame.draw.rect(screen, (176, 252, 248), level_rect.inflate(10, 10), 3)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.level_selected = (self.level_selected - 1) % len(self.levels)
if event.key == pygame.K_DOWN:
self.level_selected = (self.level_selected + 1) % len(self.levels)
if event.key == pygame.K_RETURN:
menu_state[0] = "start"
levelrun = False
if thoat_button.draw(screen):
menu_state[0] = "choose"
levelrun = False
pygame.display.update()
return self.level_selected
def load_scores(self):
try:
with open('user_data.json', 'r') as f:
data = json.load(f)
if self.username in data:
user_data = data[self.username]
return user_data.get('levels', {})
else:
return {str(i): 0 for i in range(5)}
except FileNotFoundError:
return {str(i): 0 for i in range(5)}