-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_objects.py
222 lines (168 loc) · 6.32 KB
/
game_objects.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from constants import *
import math
import pymunk as pm
import pymunk.constraints as pmc
import arcade as arc
import numpy as np
from perlin_noise import PerlinNoise
class Player():
def __init__(self, x: float, y: float, space: pm.Space):
self.sprites = arc.SpriteList()
self.space = space
width = 60
height = 20
self.car = Car(x, y, width, height, space)
self.sprites.append(self.car)
self.b_wheel = Wheel(x-width/2+10, y-height, self.space)
self.f_wheel = Wheel(x+width/2-10, y-height, self.space)
self.sprites.append(self.b_wheel)
self.sprites.append(self.f_wheel)
back_spring = pmc.PinJoint(
self.car.body, self.b_wheel.body, (-width / 2+10, -height), (0, 0))
front_spring = pmc.PinJoint(
self.car.body, self.f_wheel.body, (width / 2-10, -height), (0, 0))
self.space.add(front_spring, back_spring)
def key_press(self, key: int):
self.b_wheel.key_press(key)
self.f_wheel.key_press(key)
self.car.key_press(key)
def key_release(self, key: int):
self.b_wheel.key_release(key)
self.f_wheel.key_release(key)
self.car.key_release(key)
def update(self):
self.sprites.update()
def draw(self):
self.sprites.draw()
# arc.draw_rectangle_outline(self.car.center_x, self.car.center_y, 60, 40, arc.color.GREEN, 2)
def destroy(self):
self.car.destroy()
self.b_wheel.destroy()
self.f_wheel.destroy()
def move(self, x: float, y: float):
self.car.body.position = (x, y)
self.b_wheel.body.position = (x-self.car.width/2+10, y-self.car.height)
self.f_wheel.body.position = (x+self.car.width/2-10, y-self.car.height)
class Car(arc.Sprite):
def __init__(self, x: float, y: float, width: float, height: float, space: pm.Space):
super().__init__("assets/truck/Body.png", 0.1)
mass = 16
self.angular_velocity = 0
moment = pm.moment_for_box(mass, (width, height))
self.body = pm.Body(mass, moment)
self.body.position = (x, y)
vertices_box_up = [
(-width/3, 0),
(-width/3, height/2),
(width/3, height/2),
(width/3, 0)
]
vertices_box_down = [
(-width/2, -height/2),
(-width/2, 0),
(width/2, 0),
(width/2, -height/2)
]
# self.shape = pm.Poly.create_box(self.body, (width, height))
self.shape_up = pm.Poly(self.body, vertices_box_up)
self.shape_down = pm.Poly(self.body, vertices_box_down)
self.shape_up.friction = 100
self.space = space
self.space.add(self.body, self.shape_up, self.shape_down)
def key_press(self, key: int):
if key == arc.key.W:
self.angular_velocity = 1
if key == arc.key.S:
self.angular_velocity = -1
def key_release(self, key: int):
if key in (arc.key.W, arc.key.S):
self.angular_velocity = 0
def update(self):
self.center_x = self.body.position.x
self.center_y = self.body.position.y
self.body._set_torque(200000*self.angular_velocity)
self.angle = math.degrees(self.body.angle)
def destroy(self):
self.space.remove(self.body, self.shape_up, self.shape_down)
class Wheel(arc.Sprite):
def __init__(self, x: float, y: float, space: pm.Space) -> None:
super().__init__("assets/truck/Wheel.png", 0.1)
self.speed = 0
mass = 4
radius = 10
moment = pm.moment_for_circle(mass, 0, radius)
self.body = pm.Body(mass, moment)
self.body.position = (x, y)
self.shape = pm.Circle(self.body, radius)
self.shape.elasticity = 20
self.shape.friction = 200
self.space = space
self.space.add(self.body, self.shape)
def key_press(self, key: int):
match key:
case arc.key.D:
self.speed = 1
case arc.key.A:
self.speed = -1
case arc.key.LSHIFT:
if self.speed > 0:
self.speed = 2
def key_release(self, key: int):
if key in (arc.key.D, arc.key.A):
self.speed = 0
if key == arc.key.LSHIFT and self.speed > 0:
self.speed = 1
def update(self):
self.center_x = self.body.position.x
self.center_y = self.body.position.y
self.angle = math.degrees(self.shape.body.angle)
self.body._set_torque(-20000*self.speed)
def destroy(self):
self.space.remove(self.body, self.shape)
class Terrain:
def __init__(self, space: pm.Space) -> None:
self.space = space
self.segments = []
self.generate_terrain()
def generate_terrain(self):
noise = PerlinNoise()
self.sprites = arc.SpriteList()
for s in self.segments:
self.space.remove(s)
self.segments = []
length = 20
prev_y = 250
frequency = 120
amplitude = 16
for x in range(0, WINDOW_WIDTH+1, length):
noise_value = noise([x / frequency])
noise_value = amplitude * noise_value
y = int(prev_y + noise_value)
segment = pm.Segment(self.space.static_body,
(x-length, prev_y), (x, y), 6)
segment.friction = 200
self.segments.append(segment)
sprite = arc.Sprite(
"assets/terrain/terrain-forest-surface-unit.png", 0.35)
sprite.width += abs(y-prev_y)/2
sprite.center_x = x-length/2
sprite.center_y = (y+prev_y)/2
sprite.angle = math.degrees(np.arctan((y-prev_y)/20))
self.sprites.append(sprite)
prev_y = y
amplitude += 1.6
# frequency += 5
for s in self.segments:
self.space.add(s)
def generate_coins(self):
coins = arc.SpriteList()
coins_pos = self.segments[7::5]
for cp in coins_pos:
coins.append(Coin(cp.center_of_gravity.x,
cp.center_of_gravity.y+30))
return coins
def draw(self):
self.sprites.draw()
class Coin(arc.Sprite):
def __init__(self, x: float, y: float):
super().__init__("assets/coin.png", 0.05, center_x=x, center_y=y)