-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
110 lines (86 loc) · 3.43 KB
/
environment.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
# http://richard.cgpublisher.com/product/pub.84/prod.11
# INTIALISATION
import pygame, math, sys
from pygame.locals import *
import utils
import pygame_utils as putils
class Environment(object):
def __init__(self, board):
self.BG= (0,0,0)
self.MAX_Y = 768
self.MAX_X = 1024
self.board = board
def start(self):
screen = pygame.display.set_mode((self.MAX_X, self.MAX_Y))
car = putils.gradient_surface((10, 33), (255, 0, 255, 255), (0, 255, 255, 255))
clock = pygame.time.Clock() # load clock
play = True
x, d = 0, 0
while play:
clock.tick(30)
for event in pygame.event.get():
if not hasattr(event, 'key'):
continue
if event.key == K_ESCAPE:
play = False
down = event.type == KEYDOWN
up = event.type == KEYUP
if down:
if event.key == K_UP:
x = 1
if event.key == K_DOWN:
x = -1
if event.key == K_RIGHT:
d = 1
if event.key == K_LEFT:
d = -1
if up:
if event.key == K_UP:
x = 0
if event.key == K_DOWN:
x = 0
if event.key == K_LEFT:
d = 0
if event.key == K_RIGHT:
d = 0
screen.fill(self.BG)
print(f"x: {x}")
print(f"d: {d}")
# if accelerate:
if x == 0:
self.board.move_target_ang_vel_to_zero()
if x == 1:
self.board.increase_target_ang_vel()
if x == -1:
self.board.decrease_target_ang_vel()
if d == 0:
self.board.move_target_diff_ang_vel_to_zero()
if d == 1:
self.board.increase_target_diff_ang_vel()
if d == -1:
self.board.decrease_target_diff_ang_vel()
self.board.move_wheels_towards_targets()
dx, dy, dtheta = self.board.change_in_position()
self.board.position[0] += dx
self.board.position[1] += dy
self.board.theta += dtheta
# print(f"Board position x diff: {dx * math.cos(self.board.theta)}")
# print(f"Board position y diff: {dy * math.sin(self.board.theta)}")
# print(f"Board position theta diff: {dtheta}")
# make sure the car doesn't exit the screen
if self.board.position[1] < 0:
self.board.position[1] = 0 # TODO is there another way to treat this?
elif self.board.position[1] > self.MAX_Y:
self.board.position[1] = self.MAX_Y
if self.board.position[0] < 0:
self.board.position[0] = 0
elif self.board.position[0] > self.MAX_X:
self.board.position[0] = self.MAX_X
rotated = pygame.transform.rotate(car, utils.rad_to_deg(self.board.theta))
rect = rotated.get_rect()
rect.center = self.board.position
print(self.board.position)
print(self.board.theta)
screen.blit(rotated, rect)
pygame.display.flip()
sys.exit(0) # quit the game