Skip to content
Snippets Groups Projects
Commit daea4972 authored by Thomas VADEBOUT's avatar Thomas VADEBOUT
Browse files

Merge branch 'test/pyglet' into 'master'

Test/pyglet

See merge request cdf2020/tools/ddviz-python!1
parents 3c4e0bf3 e3be0a12
No related branches found
No related tags found
1 merge request!1Test/pyglet
__pycache__/
# ddviz-python
## Installation
2d visualization tool for the 2021 edition of "Coupe de France de robotique"
\ No newline at end of file
```bash
pip3 install --upgrade pyglet
```
## Launch
```
python3 Node.py
```
\ No newline at end of file
ressources/galere.png

6.17 KiB

This diff is collapsed.
ressources/terrain_high.png

878 KiB

ressources/terrain_low.png

440 KiB

"""
Something that can be viewed on the visualization window
and which can evolve over time (position, rotation, status)
"""
from pyglet import sprite
from pyglet import image
class DynamicObject():
def __init__(self, sprite_file_dir):
self.sprite_file_dir = sprite_file_dir
self.x = 0 # x coordinate in mm
self.y = 0 # y coordinate in mm
self.r = 0 # angle in radians
sprite_img = image.load(sprite_file_dir)
# set anchor at the middle of the image (useful for sprite pos and rot)
sprite_img.anchor_x = sprite_img.width // 2
sprite_img.anchor_y = sprite_img.height // 2
self.sprite = sprite.Sprite(sprite_img, x=self.x, y=self.y)
def set_x(self, x):
self.x = x
self.sprite.x = self.x
def set_y(self, y):
self.y = y
self.sprite.y = self.y
def set_r(self, r):
self.r = r
self.sprite.rotation = self.r * 57.2957795 # rad -> deg
def __repr__(self):
print("___DynamicObject___")
print("sprite_file_dir : "+self.sprite_file_dir)
from Robot import Robot
sprite_file_dir = "../ressources/galere.png"
class LaGalere(Robot):
def __init__(self):
Robot.__init__(self, sprite_file_dir)
from LaGalere import LaGalere
import VisualizationCore
galere = LaGalere()
galere.set_x(1500)
galere.set_y(1000)
galere.set_r(3*1.57)
VisualizationCore.addDynamicObject(galere)
VisualizationCore.initVisualization()
from DynamicObject import DynamicObject
class Robot(DynamicObject):
def __init__(self, sprite_file_dir):
DynamicObject.__init__(self, sprite_file_dir)
import pyglet
from pyglet import image
from pyglet import sprite
from pyglet import gl
from DynamicObject import DynamicObject
winWidth = 1500
winHeight = 1000
dynamicObjecsList = []
def loadBackground():
return image.load('../ressources/terrain_high.png')
def initVisualization():
#allow use of an extra sample buffer (antialiasing)
config = pyglet.gl.Config(sample_buffers=1, samples=4)
window = pyglet.window.Window(config=config, width=winWidth,height=winHeight, resizable=False)
bg = loadBackground()
# scale to fit well terrain
x_scaling = winWidth/bg.width
y_scaling = winHeight/bg.height
gl.glScalef(x_scaling, y_scaling, 1)
print("window launched with scaling "+ str((x_scaling, y_scaling)))
@window.event
def on_draw():
window.clear()
bg.blit(0, 0, 0)
for dynObj in dynamicObjecsList:
dynObj.sprite.draw()
# enter in event loop until window is closed -> blocking
pyglet.app.run()
def addDynamicObject(dynObj):
dynamicObjecsList.append(dynObj)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment