-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinteraction.py
159 lines (143 loc) · 5.81 KB
/
interaction.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
class Interaction(object):
def __init__(self, pos, size):
self.dR = 0 # to separate Mario class and others
# pos = [x, y] in background
# size = [width, height] of the object
self.pos = pos
self.size = size
top = self.pos[1] - self.size[1] / 2
bottom = top + self.size[1]
left = self.pos[0] - self.size[0] / 2
right = left + self.size[0]
self.square = (left, top, right, bottom)
self.previousSquare = self.square
self.previousPos = self.pos
def collide(self, other=None, others=None):
offset = 3
if (other is None and others is None) or (
other is not None and others is not None):
raise ValueError("collide method must have one optional argument.")
pointUL = (self.square[0], self.square[1])
pointUR = (self.square[2], self.square[1])
pointDL = (self.square[0], self.square[3])
pointDR = (self.square[2], self.square[3])
points = [pointUL, pointUR, pointDL, pointDR]
self.hitTop = False
self.hitBrickOnLeft = False
if isinstance(other, Interaction):
# #######space for debug
#
# #######collideOnLeftOfA
#
# Because Mario will only move at 1pixel/tick(relative with background),
# bricks are stationary and rocks move 1.5p/tick(relative with background),
# we just write a simple version with offset of 3 pixeles
#
# one condition:
# a = rock and b = brick
if (not (not (other.square[0] - offset <= self.square[2] <=
other.square[0]) or not (
other.square[1] - self.size[1] < self.square[
1] <
other.square[3]))):
self.collideOtherOnLeft(other)
elif (
not (not (other.square[2] - offset <= self.square[0] <=
other.square[2]) or not (
other.square[1] - self.size[1] <
self.square[
1] <
other.square[
3]))):
self.collideOtherOnRight(other)
# #######collide on downside
#
# we will count previous pos of mario into consideration,
# cause mario sometimes will jump out our judgement
#
# one condition:
# self = mario, other = self
elif (not ((
not (other.square[0] - self.size[0] <=
self.square[0]) or not (
other.square[2] + self.size[0] >=
self.square[2])) or not (
self.square[1] < other.square[3] <
self.previousSquare[
1]))):
self.collideOtherOnBottom(other)
elif (other.square[0] - self.size[0] <= self.square[0]
and other.square[2] + self.size[0] >= self.square[2]
and self.previousSquare[3] - 20 <= other.square[1] <=
self.square[3] + 4.9):
self.collideOtherOnTop(other)
# #######not collideOnOther in every possibility
#
# many conditions:
# 1. rock hit mario
# 2. rock hit earth
# 3. mario hit earth
# 4. rock hit brick(no effect)
# 5. rock hit brick(no effect)
# 6. brick hit mario(no effect)
# etc.
for point in points:
if self.overLap(self, other):
self.collideOnOther(other)
return
self.collideNothing(other)
# ######not collide on others in every possibility
#
# again, we don't consider the situation that objects right throw the other in one time,
# cause we only set jump height as 5pixels/tick.
#
# There are two conditions:
# 1. self = mario, others = bricks
# 2. self = rock, others = bricks
elif isinstance(others, list):
newBrickList = []
for brick in others:
if not brick.hit:
newBrickList += [brick]
for brick in newBrickList:
# if brick.col == 3:
# print brick.square
if self.dR == 5:
offset = 3
if self.overLap(self, brick, offset=offset):
return
self.collideNothing(others)
@staticmethod
def overLap(a, b, offset=0):
# have same line included
if not a.square[0] - offset > b.square[2] and not a.square[2] + offset < \
b.square[0] \
and not a.square[1] - offset > b.square[3] and not a.square[
3] + offset < b.square[1]:
return True
@staticmethod
def squareToPos(square):
left = square[0]
top = square[1]
right = square[2]
bottom = square[3]
cx = (left + right) / 2.0
cy = (top + bottom) / 2.0
return [cx, cy]
def collideOtherOnLeft(self, other):
pass
def collideOtherOnRight(self, other):
pass
def collideOtherOnBottom(self, other):
pass
def collideOtherOnTop(self, other):
pass
def collideOnOther(self, other):
pass
def collideNothing(self, other):
if isinstance(other, list):
# not collide on bricks
pass
elif isinstance(other, Interaction):
# not collide on single object
pass