Skip to content
This repository
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
43 collisions/PlayerEnvCollision.py
@@ -13,6 +13,10 @@ def begin(space, arbiter, jukebox):
13 13 player_shape, env_shape = arbiter.shapes
14 14 player_shape.gameobject.normal = arbiter.contacts[0].normal
15 15
  16 + #~ if arbiter.contacts[0].normal.y > 0:
  17 + #~ # If moving downwards from above
  18 + #~ player_shape.gameobject.ground_collision()
  19 +
16 20 return True
17 21
18 22 @staticmethod
@@ -20,43 +24,6 @@ def pre_solve(space, arbiter, jukebox):
20 24 #~ a, b = arbiter.shapes
21 25 player_shape, env_shape = arbiter.shapes
22 26
23   - #~ print "==== Collision ===="
24   - #~ print player_shape.gameobject
25   - #~ print arbiter.contacts[0].normal
26   - #~ print "=========="
27   -
28   -
29   - #~ print "collide"
30   - #~ if(isinstance(a, Player) and isinstance(b, StaticObject)):
31   - #~ print "proper"
32   - #~ player_shape = a
33   - #~ env_shape = b
34   -
35   - #~ if(player_shape.body.velocity.y < 0 and env_shape.point_query(player_shape.body.position)):
36   -
37   -
38   - #~ print "===contacts"
39   - #~ for p in arbiter.contacts:
40   - #~ print p
41   -
42   -
43   - #~ print arbiter.contacts[0].normal
44   -
45   - # Orient the player in the direction of the normal
46   - #~ normal = arbiter.contacts[0].normal
47   - #~ if(normal.y < 0): # Insure y component is positive
48   - #~ normal.x *= -1
49   - #~ normal.y *= -1
50   - #~ angle = normal.get_angle()
51   - #~ if(angle < 0):
52   - #~ angle + math.pi
53   - #~ print "=="
54   - #~ print angle
55   - #~ player_shape.body.angle = angle
56   - #~ print player_shape.body.angle
57   - #~ player_shape.gameobject.rotate(player_shape.body.angle)
58   -
59   -
60 27 return True
61 28
62 29 @staticmethod
@@ -74,6 +41,4 @@ def post_solve(space, arbiter, jukebox):
74 41 def separate(space, arbiter, jukebox):
75 42 #~ player_shape, env_shape = arbiter.shapes
76 43
77   - #~ arbiter.shapes[0].body.angle = 0
78   -
79 44 return True
6 gameobjects/Player.py
@@ -100,9 +100,11 @@ def let_go(self, space):
100 100
101 101 def ground_collision(self):
102 102 #~ print "ground"
103   - self._animation.transition_to('walk_loop')
104 103
105   - self.in_air = False
  104 +
  105 + if self.in_air:
  106 + self.in_air = False
  107 + self._animation.transition_to('stand')
106 108
107 109 self.jump_count = 0
108 110
127 utilities/Animation.py
@@ -50,22 +50,11 @@ def __init__(self):
50 50 def update(self, velocity=None):
51 51 # Set the image to the next frame, and update the rect as well
52 52 if self.next_state != self.state:
53   - self.state = self.next_state
54   - print "new state: {}".format(self.state)
55   - self._frame_count = 0
56   -
57   - # Transition to spritesheet for new state
58   - self.spritesheet = self.animations[self.state][0]
59   - self.frame_rects = self.animations[self.state][1]
60   -
61   - # Create new frame surface
62   - current_frame_rect = self.frame_rects[self._frame_count]
63   -
64   - self.current_frame = pygame.Surface((current_frame_rect[2], current_frame_rect[3]),
65   - pygame.SRCALPHA)
66   -
67   - self.current_frame.blit(self.spritesheet, (0,0), current_frame_rect)
  53 + self._transition_to_new_state()
68 54 else:
  55 + movement_threshold = 2
  56 + run_velocity = 350
  57 +
69 58 #~ print "update"
70 59 #~ self._frame_count += 1
71 60
@@ -74,16 +63,32 @@ def update(self, velocity=None):
74 63 if velocity:
75 64 vx = velocity.x
76 65
77   - if vx < 0:
  66 + if vx > movement_threshold:
  67 + self.direction = 'right'
  68 + else:
78 69 vx *= -1
79 70 self.direction = 'left'
80   - else:
81   - self.direction = 'right'
  71 +
82 72
83 73 self.tick += 1
84 74
85   - if self.state == 'walk_loop':
86   - if vx > 100:
  75 +
  76 +
  77 + if self.state == 'stand':
  78 + if vx > movement_threshold:
  79 + self.transition_to('walk_start')
  80 + elif self.state == 'walk_start':
  81 + # NOTE: This animation is a frame short of a full set
  82 + if self.tick >= 2:
  83 + self.tick = 0
  84 + self._frame_count += 1
  85 +
  86 + if self._frame_count > 4:
  87 + self.transition_to('walk_loop')
  88 + elif self.state == 'walk_loop':
  89 + if vx > run_velocity:
  90 + self.transition_to('run')
  91 + elif vx > 100:
87 92 #~ print "high speed walking!!"
88 93 if self.tick >= 2:
89 94 self.tick = 0
@@ -94,20 +99,50 @@ def update(self, velocity=None):
94 99 self.tick = 0
95 100 self._frame_count += 1
96 101 else:
  102 + self.transition_to('stand')
  103 + elif self.state == 'walk_to_run':
  104 + if self.tick >= 1:
97 105 self.tick = 0
98   - self._frame_count = 0
  106 + self._frame_count += 1
  107 +
  108 + if self.is_last_frame():
  109 + print "RUN!"
  110 + self.transition_to('run')
  111 +
  112 + elif self.state == 'run':
  113 + if self.tick >= 3:
  114 + self.tick = 0
  115 + self._frame_count += 1
  116 +
  117 + if vx < run_velocity:
  118 + self.transition_to('slide')
  119 + elif self.state == 'slide':
  120 + if self.tick >= 2:
  121 + self.tick = 0
  122 + self._frame_count += 1
  123 +
  124 + if vx < movement_threshold:
  125 + self.transition_to('slide_to_stand')
  126 + elif self.state == 'slide_to_stand':
  127 + # NOTE: This animation is a frame short of a full set
  128 + if self.tick >= 2:
  129 + self.tick = 0
  130 + self._frame_count += 1
  131 +
  132 + if self._frame_count > 5:
  133 + self.transition_to('stand')
99 134 elif self.state == 'jump':
100   - if self.tick >= 1:
  135 + if self.tick >= 2:
101 136 self.tick = 0
102 137 self._frame_count += 1
103 138
104   -
105   -
106   - if self.state == 'jump':
107   - if self._frame_count >= self.animations[self.state][2]:
  139 + if self.is_last_frame():
  140 + if self.state == 'jump' or self.state == 'slide':
  141 + # Stick on last frame
108 142 self._frame_count = self.animations[self.state][2]-1
109   - elif self._frame_count >= self.animations[self.state][2]:
110   - self._frame_count = 0
  143 + else:
  144 + # Loop animation
  145 + self._frame_count = 0
111 146
112 147 # Prep current frame
113 148 current_frame_rect = self.frame_rects[self._frame_count]
@@ -133,6 +168,9 @@ def get_width(self):
133 168 def get_height(self):
134 169 return self.current_frame.get_rect().height
135 170
  171 + def is_last_frame(self):
  172 + return self._frame_count >= self.animations[self.state][2]
  173 +
136 174 def _load(self, name, frame_size):
137 175 fullname = os.path.join('sprites', name)
138 176 image = pygame.image.load(fullname)
@@ -141,24 +179,39 @@ def _load(self, name, frame_size):
141 179
142 180 frame_rects = []
143 181
144   - i = 0
145   - x = 0
  182 + frame_count = 0
  183 +
146 184 y = 0
147   - #~ print "width: {} height: {}".format(image.get_width(), image.get_height())
148   - #~ print "frame size: {}x{}".format(frame_size[0], frame_size[1])
149 185 while y < image.get_height():
150   - #~ print "y cut"
151 186 x = 0
152 187 while x < image.get_width():
153   - i += 1
154   - #~ print "x cut"
155   - #~ print "new frame {}".format(name)
  188 + frame_count += 1
  189 +
156 190 # Cut out sprite
157 191 frame_rects.append(pygame.Rect((x, y), frame_size))
158 192
159 193 x += frame_size[0]
160 194 y += frame_size[1]
161 195
162   - return image, frame_rects, i
  196 + return image, frame_rects, frame_count
  197 +
  198 + def _transition_to_new_state(self):
  199 + self._frame_count = 0
  200 +
  201 + self.state = self.next_state
  202 + print "new state: {}".format(self.state)
  203 +
  204 +
  205 + # Transition to spritesheet for new state
  206 + self.spritesheet = self.animations[self.state][0]
  207 + self.frame_rects = self.animations[self.state][1]
  208 +
  209 + # Create new frame surface
  210 + current_frame_rect = self.frame_rects[self._frame_count]
  211 +
  212 + self.current_frame = pygame.Surface((current_frame_rect[2], current_frame_rect[3]),
  213 + pygame.SRCALPHA)
  214 +
  215 + self.current_frame.blit(self.spritesheet, (0,0), current_frame_rect)
163 216
164 217

No commit comments for this range

Something went wrong with that request. Please try again.