Move Robot Around

7) Moving along x

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 Simulator(object):
	def main(self , screen):
		clock = pygame.time.Clock()
		robot = pygame.image.load("car60_40.png")
		robot_x = 320
		robot_y = 240
		while 1:
			clock.tick(30)
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
					return
			screen.fill(white)
			robot_x += 1
			screen.blit(robot,(robot_x,robot_y))
			pygame.display.flip()


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

We have separate variable for robot x & y position , so we can manipulate in runtime.
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
(adsbygoogle = window.adsbygoogle || []).push({});

8) Organizing Robot into class

We will build class for mobile robot . Think what property of car is essential for imagining it in plane coordinate.

We need x and y co-ordinate for it's position and angle for it's orientation

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


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

Final code

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 Simulator(object):
	def main(self , screen):
		clock = pygame.time.Clock()
		robot = pygame.image.load("car60_40.png")
		robot_x = 320
		robot_y = 240
		while 1:
			clock.tick(30)
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					return
				if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
					return
			screen.fill(white)
			robot_x +=10
			screen.blit(robot,(robot_x,robot_y))
			pygame.display.flip()


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

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