-
Notifications
You must be signed in to change notification settings - Fork 0
/
Перемещение героя.py
121 lines (100 loc) · 3.31 KB
/
Перемещение героя.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
import pygame
import sys
import os
pygame.init()
x, y = 1000, 500
size = width, height = x, y
screen = pygame.display.set_mode(size)
screen.fill((0, 0, 0))
FPS = 50
clock = pygame.time.Clock()
def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print('Cannot load image:', name)
raise SystemExit(message)
image = image.convert_alpha()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey)
return image
def terminate():
pygame.quit()
sys.exit()
def start_screen():
intro_text = ["Чтобы начать, нажмите на любую клавишу"]
fon = pygame.transform.scale(load_image('fon.jpg'), (1000, 700))
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:
return
pygame.display.flip()
clock.tick(FPS)
def load_level(filename):
filename = "data/" + filename
with open(filename, 'r') as mapFile:
level_map = [line.strip() for line in mapFile]
max_width = max(map(len, level_map))
return list(map(lambda x: x.ljust(max_width, '.'), level_map))
level1 = load_level('map')
tile_images = {
'wall': load_image('box.png'),
'empty': load_image('grass.png')
}
tile_width = tile_height = 50
player = None
all_sprites = pygame.sprite.Group()
tiles_group = pygame.sprite.Group()
player_group = pygame.sprite.Group()
player_image = load_image('mar.png')
class Tile(pygame.sprite.Sprite):
def __init__(self, tile_type, pos_x, pos_y):
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.rect = self.image.get_rect().move(
tile_width * pos_x + 15, tile_height * pos_y + 5)
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
running = True
start_screen()
generate_level(level1)
while running:
tiles_group.draw(screen)
player_group.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
pygame.display.flip()