-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyGame 7_4.py
170 lines (145 loc) · 5.46 KB
/
PyGame 7_4.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
import pygame
import sys
import os
file_name = input() + '.txt'
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 load_level(filename):
filename = "data/" + filename
try:
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))
except OSError:
print('Такого файла не существует')
exit(0)
fps = 50
pygame.init()
pygame.display.set_caption('Перемещение героя. Новый уровень')
size = width, height = 500, 500
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
tile_images = {
'wall': load_image('box.png'),
'empty': load_image('grass.png')
}
player_image = load_image('mario.png')
tile_width = tile_height = 50
player = None
all_sprites = pygame.sprite.Group()
tiles_group = pygame.sprite.Group()
player_group = pygame.sprite.Group()
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)
class Camera:
def __init__(self):
self.dx = 0
self.dy = 0
def apply(self, obj):
obj.rect.x += self.dx
obj.rect.y += self.dy
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)
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
def terminate():
pygame.quit()
sys.exit()
def start_screen():
fon = pygame.transform.scale(load_image('fon.jpg'), (width, height))
screen.blit(fon, (0, 0))
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)
LEVEL = load_level(file_name)
for i in range(len(LEVEL)):
if '@' in LEVEL[i]:
x, y = i, LEVEL[i].index('@')
start_screen()
player, level_x, level_y = generate_level(LEVEL)
camera = Camera()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if LEVEL[x - 1][y] == '.':
if x == 4:
LEVEL.insert(0, LEVEL[-1])
del LEVEL[-1]
else:
x -= 1
LEVEL[x + 1] = LEVEL[x + 1][:y] + '.' + LEVEL[x + 1][y + 1:]
LEVEL[x] = LEVEL[x][:y] + '@' + LEVEL[x][y + 1:]
elif event.key == pygame.K_DOWN:
if LEVEL[x + 1][y] == '.':
if x == len(LEVEL) - 5:
LEVEL.append(LEVEL[0])
del LEVEL[0]
else:
x += 1
LEVEL[x - 1] = LEVEL[x - 1][:y] + '.' + LEVEL[x - 1][y + 1:]
LEVEL[x] = LEVEL[x][:y] + '@' + LEVEL[x][y + 1:]
elif event.key == pygame.K_LEFT:
if LEVEL[x][y - 1] == '.':
if y == 4:
for i in range(len(LEVEL)):
j = LEVEL[i]
del LEVEL[i]
LEVEL.insert(i, j[-1] + j[:-1])
else:
y -= 1
LEVEL[x] = LEVEL[x][:y] + '@.' + LEVEL[x][y + 2:]
else:
if LEVEL[x][y + 1] == '.':
if y == len(LEVEL) - 5:
for i in range(len(LEVEL)):
j = LEVEL[i]
del LEVEL[i]
LEVEL.insert(i, j[1:] + j[0])
else:
y += 1
LEVEL[x] = LEVEL[x][:y - 1] + '.@' + LEVEL[x][y + 1:]
all_sprites = pygame.sprite.Group()
player, level_x, level_y = generate_level(LEVEL)
camera.update(player)
for sprite in all_sprites:
camera.apply(sprite)
screen.fill((0, 0, 0))
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()