-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
191 lines (157 loc) · 6.11 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
import pygame
import sys
import os
def terminate():
pygame.quit()
sys.exit()
def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
if not os.path.isfile(fullname):
print(f"Файл с изображением '{fullname}' не найден")
sys.exit()
image = pygame.image.load(fullname)
return image
def start_screen(screen):
intro_text = ["Перемещение героя", "",
"Герой двигается",
"Карта на месте"]
WIDTH, HEIGHT = screen.get_size()
fon = pygame.transform.scale(load_image('fon.jpg'), (WIDTH, HEIGHT))
screen.blit(fon, (0, 0))
font = pygame.font.Font(None, 30)
text_coord = 50
for line in intro_text:
string_rendered = font.render(line, 1, pygame.Color('black'))
intro_rect = string_rendered.get_rect()
text_coord += 10
intro_rect.top = text_coord
intro_rect.x = 10
text_coord += intro_rect.height
screen.blit(string_rendered, intro_rect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
elif event.type == pygame.KEYDOWN or \
event.type == pygame.MOUSEBUTTONDOWN:
# terminate()
return # начинаем игру
pygame.display.flip()
def load_level(filename):
filename = "data/" + filename
try:
with open(filename, 'r') as mapFile:
level_map = [line.strip() for line in mapFile]
except Exception:
print(f"Файл с изображением '{filename}' не найден")
sys.exit()
return level_map
def correct_level(level_map, filename):
max_width = max(map(len, level_map))
level_map = list(map(lambda x: x.ljust(max_width, '.'), level_map))
filename = "data/" + filename
with open(filename, 'w') as mapFile:
for line in level_map:
print(line, file=mapFile)
return level_map
class Tile(pygame.sprite.Sprite):
def __init__(self, tile_type, pos_x, pos_y):
if tile_type == "wall":
super().__init__(tiles_group, box_group, all_sprites)
elif tile_type == "empty":
super().__init__(tiles_group, grass_group, all_sprites)
else:
super().__init__(tiles_group, all_sprites)
self.image = tile_images[tile_type]
self.rect = self.image.get_rect().move(
tile_width * pos_x, tile_height * pos_y)
class Player(pygame.sprite.Sprite):
def __init__(self, pos_x, pos_y):
super().__init__(player_group, all_sprites)
self.image = player_image
self.pos_x = pos_x
self.pos_y = pos_y
self.rect = self.image.get_rect().move(
tile_width * pos_x + 15, tile_height * pos_y + 5)
def move(self, dx, dy):
self.rect = pygame.rect.Rect(self.rect[0] + dx * tile_width, self.rect[1] + dy * tile_height, self.rect[2],
self.rect[3])
if not pygame.sprite.spritecollideany(self, grass_group):
self.rect = pygame.rect.Rect(self.rect[0] - dx * tile_width, self.rect[1] - dy * tile_height, self.rect[2],
self.rect[3])
def generate_level(level):
new_player, x, y = None, None, None
for y in range(len(level)):
for x in range(len(level[y])):
if level[y][x] == '.':
Tile('empty', x, y)
elif level[y][x] == '#':
Tile('wall', x, y)
elif level[y][x] == '@':
Tile('empty', x, y)
new_player = Player(x, y)
# вернем игрока, а также размер поля в клетках
return new_player, x, y
class Camera:
# зададим начальный сдвиг камеры
def __init__(self):
self.dx = 0
self.dy = 0
# сдвинуть объект obj на смещение камеры
def apply(self, obj):
obj.rect.x += self.dx
obj.rect.y += self.dy
# позиционировать камеру на объекте target
def update(self, target):
self.dx = -(target.rect.x + target.rect.w // 2 - width // 2)
self.dy = -(target.rect.y + target.rect.h // 2 - height // 2)
if __name__ in '__main__':
filename = 'level1.txt'
level_map = load_level(filename)
level_map = correct_level(level_map, filename)
pygame.init()
size = width, height = 500, 500
screen = pygame.display.set_mode(size, vsync=1)
pygame.display.set_caption('Перемещение героя')
start_screen(screen)
camera = Camera()
tile_images = {
'wall': load_image('box.png'),
'empty': load_image('grass.png')
}
player_image = load_image('mar.png')
tile_width = tile_height = 50
# основной персонаж
player = None
# группы спрайтов
all_sprites = pygame.sprite.Group()
tiles_group = pygame.sprite.Group()
player_group = pygame.sprite.Group()
box_group = pygame.sprite.Group()
grass_group = pygame.sprite.Group()
player, x, y = generate_level(level_map)
running = True
while running:
screen.fill(pygame.Color('black'))
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player.move(0, -1)
elif event.key == pygame.K_DOWN:
player.move(0, 1)
elif event.key == pygame.K_RIGHT:
player.move(1, 0)
elif event.key == pygame.K_LEFT:
player.move(-1, 0)
all_sprites.update()
all_sprites.draw(screen)
player_group.draw(screen)
# изменяем ракурс камеры
camera.update(player)
# обновляем положение всех спрайтов
for sprite in all_sprites:
camera.apply(sprite)
pygame.display.flip()
pygame.quit()