-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonJump.py
More file actions
183 lines (149 loc) · 5.78 KB
/
pythonJump.py
File metadata and controls
183 lines (149 loc) · 5.78 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
import arcade
class player():
def __init__(self, x_pos, y_pos, x_vel, y_vel, x_size, y_size):
self.x_pos = x_pos
self.y_pos = y_pos
self.x_vel = x_vel
self.y_vel = y_vel
self.x_size = x_size
self.y_size = y_size
def check_collision(self, collisions_x, collisions_y):
if round(self.x_pos) in collisions_x or round(self.x_pos) + self.x_size in collisions_x:
return True
else:
return False
class MyGame(arcade.Window):
def __init__(self, width, height):
super().__init__(width, height, "Not Game", resizable=True)
arcade.set_background_color(arcade.color.BLUE)
self.width = width
self.height = height
<<<<<<< HEAD
self.player = [[0, 10], [0, 0]]
self.pressed = {"a": False, "d": False}
=======
self.player = player(0, 0, 0, 0, 50, 50)
self.a_pressed = False
self.d_pressed = False
>>>>>>> 4e33841a10474dc1804440e8d0f9cf065acafaa0
self.friction = 0.3 # How much per update you slow down on the ground
self.gravity = 1 # How much you get pulled down per update
self.air_resistance_modifier = 4 # Division, higher is less resistance
self.jump_height = 15 # Speed up when you jump
self.speed = 0.8 # Speed increase per update when you hold left or right
self.max_speed = 30 # Max speed
self.air_jumps = 0 # Temp, not used in set-up
self.max_air_jumps = 0 # Number of air jumps you get in total
self.max_vertical_speed = 10 # Max vertical speed
self.wall_jumps = 0
self.max_wall_jumps = 1
self.x_collisions = []
self.y_collisions = []
def on_draw(self):
arcade.start_render()
# Draw the player
arcade.draw_rectangle_filled(self.player.x_pos + 25, self.player.y_pos + 25, 50, 50, arcade.color.RED)
# Draw the grass at the bottom
arcade.draw_rectangle_filled(self.width / 2, 5, self.width, 10, arcade.color.GREEN)
# Draw drawn collisions
for i in range(0, len(self.x_collisions) - 1):
arcade.draw_rectangle_filled(self.x_collisions[i], self.y_collisions[i], 1, 1, arcade.color.GREEN)
def update(self, delta_time):
<<<<<<< HEAD
if self.pressed["a"]:
if self.player[0][1] > 10: # If in the air add a third of the speed instead
self.player[1][0] += -self.speed / 3
=======
if self.a_pressed:
if self.player.y_pos > 10: # If in the air add less speed
self.player.x_vel += -self.speed / 1.5
>>>>>>> 4e33841a10474dc1804440e8d0f9cf065acafaa0
else:
self.player.x_vel += -self.speed
<<<<<<< HEAD
if self.pressed["d"]:
if self.player[0][1] > 10: # If in the air add a third of the speed instead
self.player[1][0] += self.speed / 3
=======
if self.d_pressed:
if self.player.y_pos > 10: # If in the air add less speed
self.player.x_vel += self.speed / 1.5
>>>>>>> 4e33841a10474dc1804440e8d0f9cf065acafaa0
else:
self.player.x_vel += self.speed
# Increase the speeds in both directions
self.player.x_pos += self.player.x_vel
self.player.y_pos += self.player.y_vel
# If your speed is higher than the max speed, set it to the max speed
if self.player.x_vel > self.max_speed:
self.player.x_vel = self.max_speed
elif self.player.x_vel < -self.max_speed:
self.player.x_vel = -self.max_speed
# If the player is above ground, draw them down with gravity
if self.player.y_pos > 10:
self.player.y_vel += -self.gravity
# Makes player never go under 10 pixels, and removes downward momentum when hitting the floor
if self.player.y_pos < 10:
self.player.y_vel = 0
self.player.y_pos = 10
# if you are on the ground add friction in different directions
if self.player.y_pos == 10:
if self.player.x_vel < 0:
self.player.x_vel += self.friction
if self.player.x_vel > 0:
self.player.x_vel = 0
elif self.player.x_vel > 0:
self.player.x_vel += -self.friction
if self.player.x_vel < 0:
self.player.x_vel = 0
# Otherwise you are in the air and add air resistance
else:
if self.player.x_vel < 0:
self.player.x_vel += self.friction / self.air_resistance_modifier
if self.player.x_vel > 0:
self.player.x_vel = 0
elif self.player.x_vel > 0:
self.player.x_vel += -self.friction / self.air_resistance_modifier
if self.player.x_vel < 0:
self.player.x_vel = 0
# If past the wall, set them to the wall and reverse x speed
if self.player.x_pos < 0:
self.player.x_pos = 0
self.player.x_vel = -self.player.x_vel
elif self.player.x_pos > self.width - 50:
self.player.x_pos = self.width - 50
self.player.x_vel = -self.player.x_vel
if self.player.check_collision(self.x_collisions, self.y_collisions):
self.player.x_vel = 0
self.player.y_vel = 0
def on_key_press(self, key, modifier):
if key == ord("w") or key == ord(" "):
# If the player is on the ground, launch them and reset the air and wall jumps
# Not tracked if held because it's worse
if self.player.y_pos == 10:
self.player.y_vel += self.jump_height
self.air_jumps = self.max_air_jumps
self.wall_jumps = self.max_wall_jumps
# If you have wall jumps left and is touching a wall remove one jump
elif (self.player.x_pos == 0 or self.player.x_pos == self.width - 50) and self.wall_jumps > 0:
self.wall_jumps += -1
self.player.y_vel += self.jump_height
# If you have air jumps, use one and jump
elif self.air_jumps > 0:
self.air_jumps += -1
self.player.y_vel += self.jump_height
# Allows me to track if button is pressed, to make smooth movement
# Tracks all buttons to allow future expansion
self.pressed[chr(key)] = True
def on_key_release(self, key, modifiers):
# Tracks all buttons to allow future expansion
self.pressed[chr(key)] = False
def on_resize(self, width, height):
super().on_resize(width, height)
self.width = width
self.height = height
def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
self.x_collisions.append(round(x))
self.y_collisions.append(round(y))
game = MyGame(1000, 500)
arcade.run()