-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving_objects.py
More file actions
83 lines (68 loc) · 2.71 KB
/
Copy pathmoving_objects.py
File metadata and controls
83 lines (68 loc) · 2.71 KB
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
import pygame
import tilemap
class MovingObject():
def __init__(self):
self.width = tilemap.tile_size
self.height = tilemap.tile_size
self.img = pygame.image.load('images/yellow_dot.png')
self.img_width, self.img_height = self.img.get_rect().size
self.x = tilemap.tile_size * 17
self.y = tilemap.tile_size * 17
self.x_vec = 0
self.y_vec = 0
self.speed = 4
self.marked_tiles = []
def draw(self):
img_x = self.x + (tilemap.tile_size - self.img_width) / 2
img_y = self.y + (tilemap.tile_size - self.img_height) / 2
tilemap.screen.blit(self.img, (img_x, img_y))
def move(self):
# check_move() is used only if object is in the middle of a tile
if self.x % tilemap.tile_size == 0 and self.y % tilemap.tile_size == 0:
self.check_move()
self.x += self.speed * self.x_vec
self.y += self.speed * self.y_vec
def check_move(self):
x_ind = tilemap.row_num(self.x + self.width / 2)
y_ind = tilemap.col_num(self.y + self.height / 2)
key = pygame.key.get_pressed()
# pacman can stop only if is on occupied tile
if tilemap.tile_map[y_ind][x_ind]:
direction = (0, 0)
else:
direction = (self.x_vec, self.y_vec)
if key[pygame.K_RIGHT]:
direction = (1, 0)
elif key[pygame.K_LEFT]:
direction = (-1, 0)
elif key[pygame.K_DOWN]:
direction = (0, 1)
elif key[pygame.K_UP]:
direction = (0, -1)
(self.x_vec, self.y_vec) = direction
# ensures, that character stays inside the map
if tilemap.tile_map[y_ind][x_ind] == 2:
if y_ind == 0:
self.y_vec = max(0, self.y_vec)
elif y_ind == tilemap.height - 1:
self.y_vec = min(0, self.y_vec)
if x_ind == 0:
self.x_vec = max(0, self.x_vec)
elif x_ind == tilemap.width - 1:
self.x_vec = min(0, self.x_vec)
# jedna z opcji zaznaczania śladu pacmana
self.mark_tile(x_ind, y_ind)
def mark_tile(self, x_ind, y_ind):
if tilemap.tile_map[y_ind][x_ind] == 0:
tilemap.tile_map[y_ind][x_ind] = 3
self.marked_tiles.append((x_ind, y_ind))
elif self.marked_tiles:
tilemap.mark_area()
while self.marked_tiles:
for x, y in self.marked_tiles:
if tilemap.tile_map[y][x] == 3: # ten if wyleci - uderzenie duszka ma zabić pacmana
tilemap.tile_map[y][x] = 1
self.marked_tiles.remove((x, y))
def action(self):
self.move()
self.draw()