Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
✨ Add graphical interface
Browse files Browse the repository at this point in the history
  • Loading branch information
killian-mahe committed Sep 27, 2021
1 parent 90c7bc4 commit a17d6f3
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .idea/agent-aspirateur.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions hoover-agent/__main__.py

This file was deleted.

172 changes: 0 additions & 172 deletions hoover-agent/environment.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyQt5~=5.15.4
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
author='Killian Mahé',
author_email='[email protected]',
packages=[
'pretty-tables',
'colorama',
'numpy',
'PyQt5',
]
)
File renamed without changes.
116 changes: 116 additions & 0 deletions vacuum-agent/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from threading import Thread
import sys

from agent import Environment, Dirt, Jewel, Position

from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView
from PyQt5.QtCore import QThread, QRectF, Qt, QPointF
from PyQt5.QtGui import QBrush, QPolygonF


class EnvironmentThread(QThread):

def __init__(self, environment: Environment):
QThread.__init__(self)
self.environment = environment

def run(self):
self.environment.run()


class AgentThread(QThread):

def run(self):
pass


def convert_position(position: Position):
return Position(100*position.x + 50, 100*position.y + 50)


class Window(QMainWindow):

def __init__(self, parent=None):
super().__init__(parent)
self.central_widget = self.centralWidget()
self.scene = QGraphicsScene()
self.view = QGraphicsView(self.scene)

self.environment = Environment()
self.environment.thing_spawn.connect(self.spawn_handler)
self.environment.thing_deleted.connect(self.deleted_thing_handler)

self.env_thread = EnvironmentThread(self.environment)
self.env_thread.finished.connect(app.exit)
self.env_thread.start()

self.dirt_rects = []
self.jewel_rects = []

self.setup_ui()
self.view.show()

def spawn_handler(self, thing):
if isinstance(thing, Dirt):
self.draw_dirt(thing.position)
elif isinstance(thing, Jewel):
self.draw_jewel(thing.position)

def deleted_thing_handler(self, thing):
position = convert_position(thing.position)
if isinstance(thing, Dirt):
for i in range(0, len(self.dirt_rects)):
if self.dirt_rects[i].contains(QPointF(position.x, position.y)):
self.scene.removeItem(self.dirt_rects.pop(i))
return
elif isinstance(thing, Jewel):
for i in range(0, len(self.jewel_rects)):
if self.jewel_rects[i].contains(QPointF(position.x, position.y)):
self.scene.removeItem(self.jewel_rects.pop(i))
return

def draw_jewel(self, position: Position):
position = convert_position(position)
brush = QBrush()
brush.setStyle(Qt.SolidPattern)
brush.setColor(Qt.cyan)

self.jewel_rects.append(self.scene.addPolygon(QPolygonF([
QPointF(position.x - 10, position.y),
QPointF(position.x, position.y - 10),
QPointF(position.x + 10, position.y),
QPointF(position.x, position.y + 20)
]), brush=brush))

def draw_dirt(self, position: Position):
position = convert_position(position)
brush = QBrush()
brush.setStyle(Qt.Dense5Pattern)
brush.setColor(Qt.gray)
self.dirt_rects.append(self.scene.addRect(QRectF(position.x - 50, position.y - 50, 100, 100),
brush=brush))

def setup_ui(self):
self.setWindowTitle("Vacuum Agent")
self.resize(1000, 800)
self.setCentralWidget(self.view)

# Build scene
self.scene.addRect(QRectF(0, 0, 500, 500))

for y in range(0, self.environment.y_max):
for x in range(0, self.environment.x_max):
self.scene.addRect(QRectF(100 * x, 100 * y, 100, 100))


app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())

# environment = Environment()
# agent = Agent()
# environment.agent = agent
#
# env_thread = Thread(environment.run())
# env_thread.run()
Loading

0 comments on commit a17d6f3

Please sign in to comment.