forked from LoisLab/qbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.py
67 lines (58 loc) · 2.18 KB
/
renderer.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
import turtle
import numpy as np
import math
#########################################################################
# #
# Utility class to render robot onscreen using Python turtle graphics. #
# #
#########################################################################
class Renderer:
def __init__(self, n=1):
turtle.radians()
self.n = n
self.count = -1
def render_clear():
turtle.clear()
self.count = -1
def render_reset(self, env):
self.count += 1
self.render_backdrop(env.obstacles, env.bot)
def render_backdrop(self, obstacles, bot):
if self.count % self.n == 0:
turtle.hideturtle()
turtle.colormode(255)
turtle.pencolor((np.random.randint(255),np.random.randint(255),np.random.randint(255)))
turtle.pensize(3+np.random.randint(5))
for o in obstacles:
self.render_obstacle(o)
turtle.penup()
turtle.goto(bot.x, bot.y)
turtle.setheading(bot.heading)
turtle.turtlesize(3)
turtle.showturtle()
def render_obstacle(self, obstacle):
turtle.hideturtle()
turtle.penup()
turtle.goto(obstacle.x+obstacle.radius,obstacle.y)
turtle.setheading(math.pi/2)
turtle.pendown()
turtle.circle(obstacle.radius)
def render_raytrace(self, env):
turtle.hideturtle()
turtle.goto(env.bot.x, env.bot.y)
turtle.showturtle()
for o in env.obstacles:
a,b,m,n = env.bot.bearings_to_ob(o)
d = env.bot.distance_to_ob(o)
env.render_reset_turtle()
turtle.pendown()
turtle.goto(m[0], m[1])
env.render_reset_turtle()
turtle.pendown()
turtle.goto(n[0], n[1])
env.render_reset_turtle()
def render_step(self, environment):
if self.count % self.n == 0:
turtle.setheading(environment.bot.heading)
turtle.pendown()
turtle.goto(environment.bot.x, environment.bot.y)