-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAR.py
783 lines (615 loc) · 28 KB
/
CAR.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import arcade
import math
SPRITE_SCALING = 0.3
TILE_SCALING = 1
SCREEN_HEIGHT = 720
SCREEN_WIDTH = round(SCREEN_HEIGHT * (16/9))
SCREEN_TITLE = "test drift"
CAMERA_SPEED = 0.15
FRICTION = 0
NON_DRIFT_FRICTION = 0.05
DRIFT_FRICTION = 0.1
ACCELERATIONF = 1.8
ACCELERATIONB = 0.3
TEMP_ACCELERATION = 0
TEMP_ACCELERATION_ADD = 0.06
ANGLE_SPEED_MAX = 5
ANGLE_SPEED_MIN = 1E-2
ANGLE_ACCELERATION = 1
MAX_SPEED = 999
MIN_SPEED = 1E-2
position = None
# color
WHITE = (255, 255, 255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
class Drift(arcade.Window):
global anglerad
"""
Main application class.
"""
def __init__(self, width, height, title):
"""
Initializer
"""
# Call the parent class initializer
super().__init__(width, height, title, fullscreen = True, vsync=True, resizable=True, update_rate = 1/60)
width, height = self.get_size()
self.set_viewport(0, width, 0, height)
# Our Scene Object
self.scene = None
self.physics_engine = None
# Set up the player info
self.player_sprite = None
self.player_anglerad = None
self.player_gearing = None
self.gear = None
self.state = None
self.portx = None
self.porty = None
self.angleport = None
self.speed = None
self.time = None
self.poscarx = None
self.poscary = None
# background
self.background = None
# GUI
self.GUI = None
# camera
self.camera = None
# MOOV
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.shift_pressed = False
# camera
self.camera = None
# Our TileMap Object
self.tile_map = None
def setup(self):
""" Set up the game and initialize the variables. """
# setup GUI
self.GUI = arcade.Camera(self.width, self.height)
# setup camera
self.camera = arcade.Camera(self.width, self.height)
# Name of map file to load
map_name = "assets/car map.tmj"
# Layer specific options are defined based on Layer names in a dictionary
# Doing this will make the SpriteList for the platforms layer
# use spatial hashing for detection.
layer_options = {
"Terrain": {
"use_spatial_hash": True
},
"Water": {
"use_spatial_hash": True
},
"Port":{
"use_spatial_hash": True
},
"Ground": {
"use_spatial_hash": True
},
"Boundary": {
"use_spatial_hash": True
},
"Runway": {
"use_spatial_hash": True
}
}
# Read in the tiled map
self.tile_map = arcade.load_tilemap(map_name, TILE_SCALING, layer_options)
# Initialize Scene with our TileMap, this will automatically add all layers
# from the map as SpriteLists in the scene in the proper order.
self.scene = arcade.Scene.from_tilemap(self.tile_map)
# Set up the player
self.car = arcade.load_texture("assets/car.png")
self.boat = arcade.load_texture("assets/boat.png")
self.plane = arcade.load_texture("assets/plane.png")
self.human = arcade.load_texture("assets/human.png")
self.player_sprite = arcade.Sprite("assets/car.png", SPRITE_SCALING)
self.player_sprite.angle = 0
self.player_sprite.center_x = 100
self.player_sprite.center_y = 100
self.player_anglerad = math.radians(self.player_sprite.angle)
self.player_gearing = 3.5
self.gear = 0
self.state = 0
self.portx = 0
self.porty = 0
self.speed = 0
self.angleport = 0
self.time = 0
self.poscarx = 0
self.poscary = 0
self.poscarang = 0
self.scene.add_sprite("Player", self.player_sprite)
#ghost player
self.ghost = arcade.Sprite("assets/car.png", SPRITE_SCALING)
self.ghost.angle = 0
self.ghost.center_x = -1000000
self.ghost.center_y = -1000000
self.scene.add_sprite("Ghost", self.ghost)
#human player
self.player_sprite2 = arcade.Sprite("assets/human.png", SPRITE_SCALING)
self.player_sprite2.angle = 0
self.player_sprite2.center_x = 1000000
self.player_sprite2.center_y = 1000000
self.scene.add_sprite("Human", self.player_sprite2)
# Set the background color
if self.tile_map.background_color:
arcade.set_background_color(self.tile_map.background_color)
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, walls=self.scene["Terrain"])
def on_draw(self):
global position
"""
Render the screen.
"""
# This command has to happen before we start drawing
self.clear()
# Get viewport dimensions
left, screen_width, bottom, screen_height = self.get_viewport()
# activate camera
self.camera.use()
# Draw our Screen
self.scene.draw()
# activate GUI
self.GUI.use()
#draw car gearing
gear_text = ""
if self.gear == 0:
gear_text = f'Vitesse: N'
elif self.gear == -1:
gear_text = f'Vitesse: R'
else:
gear_text = f'Vitesse: {self.gear}'
gear_text = arcade.Text(gear_text, 10, self.height - 20, GREEN,18)
gear_text.draw()
#draw speed
speed_text = f"{self.speed} km/h"
speed_text = arcade.Text(speed_text, 10, self.height - 40, GREEN, 18)
speed_text.draw()
"""
#debug
#draw speed
speedx_text = f"speed x = {self.player_sprite.change_x}"
speedy_text = f"speed y = {self.player_sprite.change_y}"
acceleration= f"acce = {ACCELERATIONF}"
info = "Tout ce qui est écrit en blanc sert pour le debug"
arcade.draw_text(info,SCREEN_WIDTH - 10,SCREEN_HEIGHT - 10,WHITE,18)
arcade.draw_text(speedx_text,10,10,WHITE,18)
arcade.draw_text(speedy_text,10,30,WHITE, 18)
arcade.draw_text(acceleration,10,50,WHITE, 18)
#draw coordonates
x_text = f"x = {self.player_sprite.center_x}"
y_text = f"y = {self.player_sprite.center_y}"
arcade.draw_text(y_text,10,70,WHITE,18)
arcade.draw_text(x_text,10,90,WHITE,18)
#draw cam info
position_text = f'pos = {position}'
arcade.draw_text(position_text,10,110,WHITE,18)
"""
def on_update(self, delta_time):
""" Movement and game logic """
self.scroll_to_player()
self.physics_engine.update()
global ACCELERATIONF
global ACCELERATIONB
global TEMP_ACCELERATION
global TEMP_ACCELERATION_ADD
global FRICTION
self.speed = round(math.sqrt((self.player_sprite.change_x ** 2) + (self.player_sprite.change_y ** 2)) * 7.5)
self.player_anglerad = math.radians(self.player_sprite.angle)
port_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.scene["Port"])
if len(port_hit_list) > 1:
if self.state == 0:
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.scene["Ground"])
self.player_sprite.texture = self.boat
self.angleport = self.player_sprite.angle
self.portx = self.player_sprite.center_x
self.porty = self.player_sprite.center_y
self.player_sprite.change_y = 0
self.player_sprite.change_x = 0
self.player_sprite.forward(-10)
self.state = 1
self.player_gearing = 3.5
TEMP_ACCELERATION_ADD = 0.05
self.gear = 0
self.time = 0
elif self.state == 1:
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, walls=self.scene["Terrain"])
self.player_sprite.angle = self.angleport
self.player_sprite.center_x = self.portx
self.player_sprite.center_y = self.porty
self.player_sprite.change_y = 0
self.player_sprite.change_x = 0
self.player_sprite.texture = self.car
self.player_sprite.forward(-10)
self.state = 0
self.gear = 0
self.time = 0
port_hit_list = []
runway_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.scene["Runway"])
if len(runway_hit_list) > 1:
if self.state == 2:
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, walls=self.scene["Terrain"])
self.player_sprite.angle = self.angleport
self.player_sprite.center_x = self.portx
self.player_sprite.center_y = self.porty
self.player_sprite.change_y = 0
self.player_sprite.change_x = 0
self.player_sprite.texture = self.car
self.player_sprite.forward(-10)
self.state = 0
self.gear = 0
self.time = 0
elif self.state == 0:
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.scene['Boundary'])
self.player_sprite.texture = self.plane
self.angleport = self.player_sprite.angle
self.portx = self.player_sprite.center_x
self.porty = self.player_sprite.center_y
self.player_sprite.change_y = 0
self.player_sprite.change_x = 0
self.player_sprite.forward(20)
self.player_sprite.angle = 90
self.state = 2
self.gear = 0
self.player_gearing = 1
TEMP_ACCELERATION_ADD = 0.05
self.time = 0
# Apply acceleration based on the keys pressed
if self.state == 0:
ACCELERATIONB = 0.3
if self.shift_pressed:
FRICTION = DRIFT_FRICTION
else:
FRICTION = NON_DRIFT_FRICTION
if self.up_pressed and not self.down_pressed:
if self.gear == -1:
self.player_sprite.forward(-ACCELERATIONB / (FRICTION + 1))
if TEMP_ACCELERATION - 0.1 <= 0:
TEMP_ACCELERATION = 0
else:
TEMP_ACCELERATION -= 0.1
else:
if TEMP_ACCELERATION + TEMP_ACCELERATION_ADD >= ACCELERATIONF:
TEMP_ACCELERATION = ACCELERATIONF
else:
TEMP_ACCELERATION += TEMP_ACCELERATION_ADD
if self.player_sprite.change_x + (TEMP_ACCELERATION / self.player_gearing) / (FRICTION + 1) * -math.sin(self.player_anglerad) > MAX_SPEED:
self.player_sprite.forward(MAX_SPEED)
elif self.player_sprite.change_x + (TEMP_ACCELERATION / self.player_gearing) / (FRICTION + 1) * -math.sin(self.player_anglerad) < -MAX_SPEED:
self.player_sprite.forward(MAX_SPEED)
else:
self.player_sprite.forward((TEMP_ACCELERATION / self.player_gearing) / (FRICTION + 1))
elif self.down_pressed and not self.up_pressed:
self.player_sprite.change_x /= 1.05
self.player_sprite.change_y /= 1.05
elif not self.down_pressed and not self.up_pressed:
if TEMP_ACCELERATION - 0.1 <= 0:
TEMP_ACCELERATION = 0
else:
TEMP_ACCELERATION -= 0.1
self.player_sprite.change_x /= FRICTION + 1
self.player_sprite.change_y /= FRICTION + 1
if self.left_pressed:
self.player_sprite.change_angle += ANGLE_ACCELERATION
elif self.right_pressed:
self.player_sprite.change_angle -= ANGLE_ACCELERATION
if self.player_sprite.change_angle > ANGLE_SPEED_MAX:
self.player_sprite.change_angle = ANGLE_SPEED_MAX
elif self.player_sprite.change_angle < -ANGLE_SPEED_MAX:
self.player_sprite.change_angle = -ANGLE_SPEED_MAX
if not self.left_pressed and not self.right_pressed:
self.player_sprite.change_angle /= 5
if ANGLE_SPEED_MIN > self.player_sprite.change_angle > 0:
self.player_sprite.change_angle = 0
elif -ANGLE_SPEED_MIN < self.player_sprite.change_angle < 0:
self.player_sprite.change_angle = 0
if self.shift_pressed:
FRICTION = DRIFT_FRICTION
ACCELERATIONF = 1.5
else:
FRICTION = NON_DRIFT_FRICTION
ACCELERATIONF = 1.2
if MIN_SPEED > self.player_sprite.change_x > 0:
self.player_sprite.change_x = 0
if MIN_SPEED > self.player_sprite.change_y > 0:
self.player_sprite.change_y = 0
if -MIN_SPEED < self.player_sprite.change_x < 0:
self.player_sprite.change_x = 0
if -MIN_SPEED < self.player_sprite.change_y < 0:
self.player_sprite.change_y = 0
if self.gear == 1:
self.player_gearing = 3.5
TEMP_ACCELERATION_ADD = 0.05
elif self.gear == 2:
self.player_gearing = 2.2
TEMP_ACCELERATION_ADD = 0.02
elif self.gear == 3:
self.player_gearing = 1.8
TEMP_ACCELERATION_ADD = 0.008
elif self.gear == 4:
self.player_gearing = 1.4
TEMP_ACCELERATION_ADD = 0.004
elif self.gear == 5:
self.player_gearing = 1.2
TEMP_ACCELERATION_ADD = 0.002
elif self.gear == 6:
self.player_gearing = 1
TEMP_ACCELERATION_ADD = 0.001
elif self.gear == 0:
TEMP_ACCELERATION = 0
TEMP_ACCELERATION_ADD = 0
self.player_gearing = 3.5
elif self.gear == -1:
TEMP_ACCELERATION_ADD = 0
self.player_gearing = 3.5
water_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.scene["Water"])
for water in water_hit_list:
self.player_sprite.angle = 0
self.player_sprite.center_x = 100
self.player_sprite.center_y = 100
self.player_anglerad = math.radians(self.player_sprite.angle)
self.player_gearing = 3.5
self.gear = 0
self.state = 0
self.player_sprite.texture = self.car
elif self.state == 1:
if self.up_pressed and not self.down_pressed:
if self.gear == -1:
self.player_sprite.forward(-ACCELERATIONB / (FRICTION + 1))
if TEMP_ACCELERATION - 0.1 <= 0:
TEMP_ACCELERATION = 0
else:
TEMP_ACCELERATION -= 0.1
else:
if TEMP_ACCELERATION + TEMP_ACCELERATION_ADD >= ACCELERATIONF:
TEMP_ACCELERATION = ACCELERATIONF
else:
TEMP_ACCELERATION += TEMP_ACCELERATION_ADD
if self.player_sprite.change_x + (TEMP_ACCELERATION / self.player_gearing) / (
FRICTION + 1) * -math.sin(self.player_anglerad) > MAX_SPEED:
self.player_sprite.forward(MAX_SPEED)
elif self.player_sprite.change_x + (TEMP_ACCELERATION / self.player_gearing) / (
FRICTION + 1) * -math.sin(self.player_anglerad) < -MAX_SPEED:
self.player_sprite.forward(MAX_SPEED)
else:
self.player_sprite.forward((TEMP_ACCELERATION / self.player_gearing) / (FRICTION + 1))
elif self.down_pressed and not self.up_pressed:
self.player_sprite.change_x /= 1.05
self.player_sprite.change_y /= 1.05
elif not self.down_pressed and not self.up_pressed:
if TEMP_ACCELERATION - 0.1 <= 0:
TEMP_ACCELERATION = 0
else:
TEMP_ACCELERATION -= 0.1
self.player_sprite.change_x /= FRICTION + 1
self.player_sprite.change_y /= FRICTION + 1
if self.left_pressed:
self.player_sprite.change_angle += ANGLE_ACCELERATION
elif self.right_pressed:
self.player_sprite.change_angle -= ANGLE_ACCELERATION
if self.player_sprite.change_angle > ANGLE_SPEED_MAX:
self.player_sprite.change_angle = ANGLE_SPEED_MAX
elif self.player_sprite.change_angle < -ANGLE_SPEED_MAX:
self.player_sprite.change_angle = -ANGLE_SPEED_MAX
if not self.left_pressed and not self.right_pressed:
self.player_sprite.change_angle /= 5
if ANGLE_SPEED_MIN > self.player_sprite.change_angle > 0:
self.player_sprite.change_angle = 0
elif -ANGLE_SPEED_MIN < self.player_sprite.change_angle < 0:
self.player_sprite.change_angle = 0
if self.shift_pressed:
FRICTION = DRIFT_FRICTION
ACCELERATIONF = 1
else:
FRICTION = NON_DRIFT_FRICTION
ACCELERATIONF = 1
if MIN_SPEED > self.player_sprite.change_x > 0:
self.player_sprite.change_x = 0
if MIN_SPEED > self.player_sprite.change_y > 0:
self.player_sprite.change_y = 0
if -MIN_SPEED < self.player_sprite.change_x < 0:
self.player_sprite.change_x = 0
if -MIN_SPEED < self.player_sprite.change_y < 0:
self.player_sprite.change_y = 0
elif self.state == 2:
if self.speed == 0:
self.time += 1
else:
self.time = 0
if self.time >= 180:
self.player_sprite.change_y = 0
self.player_sprite.change_x = 0
self.player_sprite.center_x = 6000
self.player_sprite.center_y = 875
self.player_sprite.angle = 90
self.state = 2
self.gear = 0
self.player_gearing = 1
self.time = 0
if self.up_pressed and not self.down_pressed:
if self.gear == -1:
self.player_sprite.forward(-ACCELERATIONB / (FRICTION + 1))
if TEMP_ACCELERATION - 0.1 <= 0:
TEMP_ACCELERATION = 0
else:
TEMP_ACCELERATION -= 0.1
else:
if TEMP_ACCELERATION + TEMP_ACCELERATION_ADD >= ACCELERATIONF:
TEMP_ACCELERATION = ACCELERATIONF
else:
TEMP_ACCELERATION += TEMP_ACCELERATION_ADD
if self.player_sprite.change_x + (TEMP_ACCELERATION / self.player_gearing) / (
FRICTION + 1) * -math.sin(self.player_anglerad) > MAX_SPEED:
self.player_sprite.forward(MAX_SPEED)
elif self.player_sprite.change_x + (TEMP_ACCELERATION / self.player_gearing) / (
FRICTION + 1) * -math.sin(self.player_anglerad) < -MAX_SPEED:
self.player_sprite.forward(MAX_SPEED)
else:
self.player_sprite.forward((TEMP_ACCELERATION / self.player_gearing) / (FRICTION + 1))
elif self.down_pressed and not self.up_pressed:
self.player_sprite.change_x /= 1.05
self.player_sprite.change_y /= 1.05
elif not self.down_pressed and not self.up_pressed:
if TEMP_ACCELERATION - 0.1 <= 0:
TEMP_ACCELERATION = 0
else:
TEMP_ACCELERATION -= 0.1
self.player_sprite.change_x /= FRICTION + 1
self.player_sprite.change_y /= FRICTION + 1
if self.left_pressed:
self.player_sprite.change_angle += ANGLE_ACCELERATION
elif self.right_pressed:
self.player_sprite.change_angle -= ANGLE_ACCELERATION
if self.player_sprite.change_angle > ANGLE_SPEED_MAX:
self.player_sprite.change_angle = ANGLE_SPEED_MAX
elif self.player_sprite.change_angle < -ANGLE_SPEED_MAX:
self.player_sprite.change_angle = -ANGLE_SPEED_MAX
if not self.left_pressed and not self.right_pressed:
self.player_sprite.change_angle /= 5
if ANGLE_SPEED_MIN > self.player_sprite.change_angle > 0:
self.player_sprite.change_angle = 0
elif -ANGLE_SPEED_MIN < self.player_sprite.change_angle < 0:
self.player_sprite.change_angle = 0
if self.shift_pressed:
FRICTION = DRIFT_FRICTION
ACCELERATIONF = 1.5
else:
FRICTION = NON_DRIFT_FRICTION
ACCELERATIONF = 1.5
if MIN_SPEED > self.player_sprite.change_x > 0:
self.player_sprite.change_x = 0
if MIN_SPEED > self.player_sprite.change_y > 0:
self.player_sprite.change_y = 0
if -MIN_SPEED < self.player_sprite.change_x < 0:
self.player_sprite.change_x = 0
if -MIN_SPEED < self.player_sprite.change_y < 0:
self.player_sprite.change_y = 0
elif self.state == 3:
if self.up_pressed and not self.down_pressed:
self.player_sprite2.forward(0.05)
elif not self.up_pressed and self.down_pressed:
self.player_sprite2.forward(-0.05)
self.player_sprite2.change_x /= FRICTION + 1
self.player_sprite2.change_y /= FRICTION + 1
if MIN_SPEED > self.player_sprite2.change_x > 0:
self.player_sprite2.change_x = 0
if MIN_SPEED > self.player_sprite2.change_y > 0:
self.player_sprite2.change_y = 0
if -MIN_SPEED < self.player_sprite2.change_x < 0:
self.player_sprite2.change_x = 0
if -MIN_SPEED < self.player_sprite2.change_y < 0:
self.player_sprite2.change_y = 0
if self.left_pressed and not self.right_pressed:
self.player_sprite2.change_angle = 4
elif self.right_pressed and not self.left_pressed:
self.player_sprite2.change_angle = -4
if not self.left_pressed and not self.right_pressed:
self.player_sprite2.change_angle = 0
if ANGLE_SPEED_MIN > self.player_sprite2.change_angle > 0:
self.player_sprite2.change_angle = 0
elif -ANGLE_SPEED_MIN < self.player_sprite2.change_angle < 0:
self.player_sprite2.change_angle = 0
def scroll_to_player(self):
global position
"""
Scroll the window to the player.
if CAMERA_SPEED is 1, the camera will immediately move to the desired position.
Anything between 0 and 1 will have the camera move to the location with a smoother
pan.
"""
if not self.state == 3:
position = self.player_sprite.center_x - (self.width / 2), self.player_sprite.center_y - (self.height / 2)
else:
position = self.player_sprite2.center_x - (self.width / 2), self.player_sprite2.center_y - (self.height / 2)
self.camera.move_to(position, CAMERA_SPEED)
def on_key_press(self, key, modifiers):
global TEMP_ACCELERATION_ADD
global TEMP_ACCELERATION
"""Called whenever a key is pressed. """
if key == arcade.key.Z:
self.up_pressed = True
if key == arcade.key.S:
self.down_pressed = True
if key == arcade.key.Q:
self.left_pressed = True
if key == arcade.key.D:
self.right_pressed = True
if key == arcade.key.LSHIFT:
self.shift_pressed = True
if key == arcade.key.UP:
if self.gear + 1 >= 6:
self.gear = 6
else:
self.gear += 1
if TEMP_ACCELERATION - 0.5 < 0:
TEMP_ACCELERATION = 0
else:
TEMP_ACCELERATION -= 0.5
if key == arcade.key.DOWN:
if self.gear - 1 <= -2:
self.gear = 0
else:
self.gear -= 1
if key == arcade.key.T:
# User hits f. Flip between full and not full screen.
self.set_fullscreen(not self.fullscreen)
# Get the window coordinates. Match viewport to window coordinates
# so there is a one-to-one mapping.
width, height = self.get_size()
self.set_viewport(0, width, 0, height)
if key == arcade.key.F and self.speed < 5 and (self.state == 0 or self.state == 3):
if self.ghost.center_x == -1000000:
self.ghost.center_x = self.player_sprite.center_x
self.ghost.center_y = self.player_sprite.center_y
self.ghost.angle = self.player_sprite.angle
self.poscarx = self.player_sprite.center_x
self.poscary = self.player_sprite.center_y
self.poscarang = self.player_sprite.angle
self.player_sprite2.center_x = self.player_sprite.center_x
self.player_sprite2.center_y = self.player_sprite.center_y
self.player_sprite.center_y = -1000000
self.player_sprite.center_x = -1000000
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite2, self.scene["Terrain"])
self.state = 3
else:
self.ghost.center_x = -1000000
self.ghost.center_y = -1000000
self.ghost.angle = 0
self.player_sprite.center_x = self.poscarx
self.player_sprite.center_y = self.poscary
self.player_sprite.angle = self.poscarang
self.player_sprite2.center_x = -1000000
self.player_sprite2.center_y = -1000000
self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.scene["Terrain"])
self.state = 0
self.player_sprite.change_x = 0
self.player_sprite.change_y = 0
self.gear = 0
#quit game
if key == arcade.key.ESCAPE:
arcade.close_window()
def on_key_release(self, key, modifiers):
"""Called when the user releases a key. """
if key == arcade.key.Z:
self.up_pressed = False
if key == arcade.key.S:
self.down_pressed = False
if key == arcade.key.Q:
self.left_pressed = False
if key == arcade.key.D:
self.right_pressed = False
if key == arcade.key.LSHIFT:
self.shift_pressed = False
def main():
""" Main function """
window = Drift(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()
main()