-
Notifications
You must be signed in to change notification settings - Fork 23
/
player_utility.py
170 lines (154 loc) · 7.03 KB
/
player_utility.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 arcade
import logging
from constants import (
DEFAULT_FRICTION,
PLAYER_FRICTION,
DEFAULT_MASS,
SPRITE_SIZE,
SPRITE_SCALING,
TEXTURE_LEFT,
TEXTURE_RIGHT,
TEXTURE_JUMP_RIGHT,
TEXTURE_JUMP_LEFT,
TEXTURE_JUMP_UP,
TEXTURE_IDLE,
TEXTURE_PUNCH_LEFT,
TEXTURE_PUNCH_RIGHT,
TEXTURE_IDLE_2,
TEXTURE_LEFT_2,
TEXTURE_RIGHT_2,
TEXTURE_LEFT_3,
TEXTURE_RIGHT_3,
TEXTURE_LEFT_4,
TEXTURE_RIGHT_4,
SCREEN_HEIGHT,
SCREEN_WIDTH
)
import pymunk
import math
class Player(arcade.Sprite):
def __init__(self,
filename,
center_x=0,
center_y=0,
scale=SPRITE_SCALING,
mass=DEFAULT_MASS * 2,
moment=None,
friction=PLAYER_FRICTION, # seeing if its better to use non-default value
body_type=pymunk.Body.DYNAMIC):
super().__init__(filename, scale=scale, center_x=center_x, center_y=center_y)
width = self.texture.width * scale
height = self.texture.height * scale
if moment is None:
moment = pymunk.moment_for_box(mass, (width, height))
self.body = pymunk.Body(mass, moment, body_type=body_type)
self.body.position = pymunk.Vec2d(center_x, center_y)
self.shape = pymunk.Poly.create_box(self.body, (width * 0.90, height * 0.90,), radius=0.8) # was 0.8
self.shape.friction = friction
self.shape.HITCOUNT = -100
self.shape.name = "Player"
self.punching = False
self.set_hit_box([[-22, -60], [22, -60], [22, 50], [-22, 50]])
# self._collision_radius = 0.5
# Load in monkey textures
try:
self.textures = []
# TEXTURE_LEFT
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_1.png", mirrored=True)
self.textures.append(texture)
# TEXTURE_RIGHT
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_1.png")
self.textures.append(texture)
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_jump_4.png")
self.textures.append(texture)
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_jump_4.png", mirrored=True)
self.textures.append(texture)
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_armsup.png")
self.textures.append(texture)
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_faceforward.png")
self.textures.append(texture)
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_jump_swing_2.png", mirrored=True)
self.textures.append(texture)
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_jump_swing_2.png")
self.textures.append(texture)
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_armsup_happy.png")
self.textures.append(texture)
# TEXTURE_LEFT_2
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_2.png", mirrored=True)
self.textures.append(texture)
# TEXTURE_RIGHT_2
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_2.png")
self.textures.append(texture)
# TEXTURE_LEFT_3
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_3.png", mirrored=True)
self.textures.append(texture)
# TEXTURE_RIGHT_3
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_3.png")
self.textures.append(texture)
# TEXTURE_LEFT_4
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_4.png", mirrored=True)
self.textures.append(texture)
# TEXTURE_RIGHT_4
texture = arcade.load_texture("./images/Char_Monkey_Free_Images/Animations/monkey_walk_4.png")
self.textures.append(texture)
except:
logging.error("Unable to load textures")
quit(1)
self.scale = SPRITE_SCALING
def update(self, FRAME_COUNT):
''' Updates player animations '''
# If punching
if self.punching == True and self.body.velocity[0] < 0:
# print("punched left")
# Play punching sound?
self.texture = self.textures[TEXTURE_PUNCH_LEFT]
# self.punching = False
return
elif self.punching == True and self.body.velocity[0] >= 0:
# print("punched right")
# Play punching sound?
self.texture = self.textures[TEXTURE_PUNCH_RIGHT]
# self.punching = False
return
# If not moving, set idle
if self.body.velocity[0] == 0 and self.body.velocity[1] == 0:
# print("Idle")
if FRAME_COUNT % 30 == 0:
self.texture = self.textures[TEXTURE_IDLE]
elif FRAME_COUNT % 15 == 0:
self.texture = self.textures[TEXTURE_IDLE_2]
return
# Figure out if we should animate walking left or right
if self.body.velocity[0] < -20:
if FRAME_COUNT % 60 == 0:
self.texture = self.textures[TEXTURE_LEFT]
elif FRAME_COUNT % 45 == 0:
self.texture = self.textures[TEXTURE_LEFT_2]
elif FRAME_COUNT % 30 == 0:
self.texture = self.textures[TEXTURE_LEFT_3]
elif FRAME_COUNT % 15 == 0:
self.texture = self.textures[TEXTURE_LEFT_4]
# return
elif self.body.velocity[0] > 20:
if FRAME_COUNT % 60 == 0:
self.texture = self.textures[TEXTURE_RIGHT]
elif FRAME_COUNT % 45 == 0:
self.texture = self.textures[TEXTURE_RIGHT_2]
elif FRAME_COUNT % 30 == 0:
self.texture = self.textures[TEXTURE_RIGHT_3]
elif FRAME_COUNT % 15 == 0:
self.texture = self.textures[TEXTURE_RIGHT_4]
# return
# Detect jumping
if self.body.velocity[1] > 5 and self.body.velocity[0] < 0: # Jumping left
# print("left jump")
self.texture = self.textures[TEXTURE_JUMP_LEFT]
elif self.body.velocity[1] > 5 and self.body.velocity[0] > 0: # Jumping right
# print("right jump")
self.texture = self.textures[TEXTURE_JUMP_RIGHT]
elif self.body.velocity[1] > 5 and self.body.velocity[0] == 0: # Jumping straight up
# print("up jump")
self.texture = self.textures[TEXTURE_JUMP_UP]
elif self.body.velocity[1] < 5 and self.body.velocity[0] == 0: # Falling down
self.texture = self.textures[TEXTURE_JUMP_UP]
#TODO: Replace with a falling/downward jump texture