-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
165 lines (142 loc) · 5.99 KB
/
models.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
import pygame
from pygame.locals import *
class Map:
def __init__(self, display, width: int = 50, height: int = 50):
self.path_map = "maps/map1.txt"
self.path_layer = "maps/layer1.txt"
self.width = width
self.height = height
self.map = []
self.layer = []
self.move_x = 0
self.move_y = 0
self.display = display
self.choosed_layer = 1
self.choosed_tile = 0
def create_new_board(self, path):
with open(path, "w") as f:
row = " ".join(["0" for x in range(self.width)]) + "\n"
f.write(row)
for y in range(self.height - 2):
row = "0 " + " ".join(["1" for x in range(self.width - 2)]) + " 0\n"
f.write(row)
row = " ".join(["0" for x in range(self.width)]) + "\n"
f.write(row)
def import_board(self):
with open(self.path_map, "r") as f:
rows = []
lines = f.readlines()
for line in lines:
row = line.replace("\n", "").split()
row = [int(x) for x in row]
rows.append(row)
self.map = rows
with open(self.path_layer, "r") as f:
rows = []
lines = f.readlines()
for line in lines:
row = line.replace("\n", "").split()
row = [int(x) for x in row]
rows.append(row)
self.layer = rows
def export_map(self):
with open(self.path_map, "w") as f:
for row in self.map:
f.write(" ".join([str(x) for x in row]) + "\n")
with open(self.path_layer, "w") as f:
for row in self.layer:
f.write(" ".join([str(x) for x in row]) + "\n")
def draw(self, tiles):
for y in range(self.height):
for x in range(self.width):
left = self.move_x + x * 32
top = self.move_y + y * 32
if 0 <= left <= 800 and 0 <= top <= 600:
if self.map[y][x] != -1:
tile = tiles[self.map[y][x]]
self.display.blit(tile, pygame.Rect(left, top, 32, 32))
if self.layer[y][x] != -1:
tile = tiles[self.layer[y][x]]
self.display.blit(tile, pygame.Rect(left, top, 32, 32))
def move(self, keys):
if keys[K_d]:
self.move_x -= 32
elif keys[K_a]:
self.move_x += 32
elif keys[K_w]:
self.move_y += 32
elif keys[K_s]:
self.move_y -= 32
def update_tile(self, x_pos, y_pos):
x = (x_pos - self.move_x) // 32
y = (y_pos - self.move_y) // 32
if 0 <= y <= self.height - 1 and 0 <= x <= self.width - 1:
if self.choosed_layer == 1:
self.map[y][x] = self.choosed_tile
else:
self.layer[y][x] = self.choosed_tile
class Menu:
def __init__(self, display, tiles):
self.window_bg = pygame.Surface((740, 540)) # the size of your rect
self.window_bg.set_alpha(200) # alpha level
self.window_bg.fill((74, 58, 35)) # this fills the entire surface
self.display = display
self.tiles = tiles
self.invisible_rects = self.create_invisible_rects()
def draw(self, procrutka):
self.display.blit(self.window_bg, (30, 30)) # (0,0) are the top-left coordinates
for i in range(procrutka, 14 + procrutka):
part_tiles = self.tiles[i * 19:(i + 1) * 19]
for x, tile in enumerate(part_tiles):
left = 52 + x * 32 + x * 5
top = 52 + (i - procrutka) * 32 + (i - procrutka) * 5
self.display.blit(tile, pygame.Rect(left, top, 32, 32))
def create_invisible_rects(self):
count_tiles = len(self.tiles)
tiles = [x for x in range(count_tiles)]
blocks = []
for i in range(count_tiles // 19 + 1):
part_tiles = tiles[i * 19:(i + 1) * 19]
for x, tile in enumerate(part_tiles):
left = 52 + x * 32 + x * 5
top = 52 + i * 32 + i * 5
blocks.append(pygame.Rect(left, top, 32, 32))
return blocks
class Button:
def __init__(self, text:str, rect:pygame.Rect, color:tuple, font_name:str, font_size:int):
self.rect = rect
self.font = pygame.font.Font(font_name, font_size)
self.text = self.font.render(text, True, color)
self.text_rect = self.text.get_rect()
self.text_rect.center = self.rect.center
def draw(self, display, bg):
pygame.draw.rect(display, bg, self.rect, border_radius=5)
display.blit(self.text, self.text_rect)
def is_pressed(self, x, y):
if self.rect.collidepoint(x, y):
return True
return False
class Input:
def __init__(self, rect:pygame.Rect, color_text:tuple, color_bg:tuple, label:str, font:str, main_font_size:int, label_font_size:int):
self.rect = rect
self.color_bg = color_bg
self.color_text = color_text
self.main_font = pygame.font.Font(font, main_font_size)
self.label_font = pygame.font.Font(font, label_font_size)
self.label_text = self.label_font.render(label, True, self.color_text)
self.label_rect = self.label_text.get_rect()
self.label_rect.top = self.rect.top - 20
self.label_rect.left = self.rect.left + 5
self.input = self.main_font.render("299", True, self.color_text)
self.input_rect = self.input.get_rect()
self.input_rect.center = self.rect.center
self.active = False
def draw(self, display, value):
self.input = self.main_font.render(str(value), True, self.color_text)
pygame.draw.rect(display, self.color_bg, self.rect, border_radius=5)
display.blit(self.input, self.input_rect)
display.blit(self.label_text, self.label_rect)
def is_pressed(self, x, y):
if self.rect.collidepoint(x, y):
return True
return False