-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovement.py
94 lines (65 loc) · 1.97 KB
/
movement.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
import pyxel
import math
CHARA_WIDTH = 8
CHARA_HEIGHT = 8
TILE_SPACE = (0, 0)
TILE_BLOCK = (1, 0)
TILE_FLOOR = (2, 0)
TILE_SPAWN = (0, 1)
TILE_ENEMY = (0, 1)
TILE_Tortoise = (1, 1)
TILE_Flower = (2, 1)
character = None
def get_tilemap(x, y):
return pyxel.tilemap(0).pget(x, y)
def check_tilemap_collision(x, y, dx, dy):
x1 = x // 8
y1 = y // 8
x2 = (x + CHARA_WIDTH - 1) // 8
y2 = (y + CHARA_HEIGHT - 1) // 8
for i in range(y1, y2 + 1):
for j in range(x1, x2 + 1):
if get_tilemap(j, i) == TILE_BLOCK:
return True
if dy > 0 and y % 8 == 1:
for i in range(x1, x2 + 1):
if get_tilemap(i, y1 + 1) == TILE_FLOOR:
return True
return False
def react_on_collision(x, y, dx, dy):
abs_dx = abs(dx)
abs_dy = abs(dy)
if abs_dx > abs_dy:
sign = 1 if dx > 0 else -1
for i in range(abs_dx):
if check_tilemap_collision(x + sign, y, dx, dy):
break
x += sign
sign = 1 if dy > 0 else -1
for i in range(abs_dy):
if check_tilemap_collision(x, y + sign, dx, dy):
break
y += sign
else:
sign = 1 if dy > 0 else -1
for i in range(abs_dy):
if check_tilemap_collision(x, y + sign, dx, dy):
break
y += sign
sign = 1 if dx > 0 else -1
for i in range(abs_dx):
if check_tilemap_collision(x + sign, y, dx, dy):
break
x += sign
return x, y, dx, dy
def check_floor(x, y):
tile = get_tilemap(x // 8, y // 8)
return tile == TILE_BLOCK or tile == TILE_FLOOR
def cleanup_list(list):
i = 0
while i < len(list):
elem = list[i]
if not elem.alive:
list.pop(i)
else:
i += 1