-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrobot.py
More file actions
76 lines (65 loc) · 1.71 KB
/
robot.py
File metadata and controls
76 lines (65 loc) · 1.71 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
import pygame
"""Define color"""
red = (200, 0, 0)
blue = (0, 0, 255)
green = (0, 155, 0)
yellow = (155, 155, 0)
white = (255, 255, 255)
black = (0, 0, 0)
class robot:
def __init__(self):
self.x = 0
self.y = 0
self.orientation = 0
def set(self, x, y,orientation):
self.x = x
self.y = y
self.orientation = orientation
def move(self, turn, x,y):
self.orientation = self.orientation + turn
self.x = self.x + x
self.y = self.y - y
def draw(self):
car_img = pygame.image.load("car60_40.png")
img = pygame.transform.rotate(car_img, self.orientation)
screen.blit(img, (self.x, self.y))
class Simulator(object):
def main(self , screen , robot):
clock = pygame.time.Clock()
robot.draw()
x = 0
y = 0
orientation = 0
while 1:
clock.tick(30)
screen.fill(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x = -1
elif event.key == pygame.K_RIGHT:
x = 1
elif event.key == pygame.K_UP:
y = 1
elif event.key == pygame.K_DOWN:
y = -1
elif event.key == pygame.K_a:
orientation = -1
elif event.key == pygame.K_d:
orientation = 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or event.key == pygame.K_UP or event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_DOWN:
x = 0
y = 0
orientation = 0
robot.move(orientation , x , y)
robot.draw()
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
pygame.display.set_caption("Move")
screen = pygame.display.set_mode((640,480))
robot = robot()
Simulator().main(screen , robot)