# 9) Building Real Model

In real car does not move in such manner . We must apply some of the math we read in school to have work it like real life.

We only apply orientation and acceleration as forward to the car . But how can we move our car in real manner see the code below, if you didn't get it contact me.

def move(self, turn, forward):
self.orientation = self.orientation + turn
#cos value 0degree->1 , 90degree->0, 180degree->-1
#we must pass degree in radian
self.x = self.x + forward*cos(self.orientation*pi/180)
#sin value 0degree->0 , 90degree->1, 180degree->0
#we must pass degree in radian
self.y = self.y - forward*sin(self.orientation*pi/180)
1
2
3
4
5
6
7
8

# 10) Move within the boundary

self.orientation %= 360
self.x %= world_size
self.y %= world_size
1
2
3

The above code simply bound robot inside window i.e robot will come out of opposite side if it crossover one of the side .

# Final Code

import pygame
from math import *

"""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)

display_width = 800
display_height = 800
world_size = display_width

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, forward):
self.orientation = self.orientation + turn
#cos value 0degree->1 , 90degree->0, 180degree->-1
#we must pass degree in radian
self.x = self.x + forward*cos(self.orientation*pi/180)
#sin value 0degree->0 , 90degree->1, 180degree->0
#we must pass degree in radian
self.y = self.y - forward*sin(self.orientation*pi/180)

self.orientation %= 360
self.x %= world_size
self.y %= world_size

def draw(self):
car_img = pygame.image.load("car60_40.png")
img = pygame.transform.rotate(car_img, self.orientation)
print self.orientation
screen.blit(img, (self.x, self.y))

class Simulator(object):
def main(self , screen , robot):
clock = pygame.time.Clock()
robot.draw()
delta_orient = 0.0
delta_forward = 0.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:
delta_orient = 1
elif event.key == pygame.K_RIGHT:
delta_orient = -1
elif event.key == pygame.K_UP:
delta_forward = 1
elif event.key == pygame.K_DOWN:
delta_forward = -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_DOWN:
delta_orient = 0.0
delta_forward = 0.0

robot.move(delta_orient, delta_forward)
robot.draw()
pygame.display.flip()

if __name__ == '__main__':
pygame.init()
pygame.display.set_caption("Move")
screen = pygame.display.set_mode((display_width,display_height))
robot = robot()
Simulator().main(screen , robot)

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