🤖 Add & move robot on the grid
This commit is contained in:
parent
ec40c35fda
commit
792fec50d6
43
jacovirt/jacovirt/bot/bot.py
Normal file
43
jacovirt/jacovirt/bot/bot.py
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import arcade
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
class Bot(arcade.Sprite):
|
||||||
|
"""
|
||||||
|
Bot sprite
|
||||||
|
"""
|
||||||
|
def __init__(self, start, grid, scale=0.75):
|
||||||
|
self.sprite = ":resources:images/animated_characters/robot/robot_idle.png"
|
||||||
|
|
||||||
|
self.grid = grid
|
||||||
|
self.pos = list(start)
|
||||||
|
|
||||||
|
super().__init__(self.sprite, scale, hit_box_algorithm="None")
|
||||||
|
self.set_pos()
|
||||||
|
|
||||||
|
def set_pos(self):
|
||||||
|
for tile in self.grid.tiles:
|
||||||
|
if tile.pos == tuple(self.pos):
|
||||||
|
self.position = tile.position
|
||||||
|
logging.debug(f"Move bot to {self.pos} - {self.position}")
|
||||||
|
break
|
||||||
|
|
||||||
|
def left(self):
|
||||||
|
if self.pos[0] > 0:
|
||||||
|
self.pos[0] -= 1
|
||||||
|
self.set_pos()
|
||||||
|
|
||||||
|
def right(self):
|
||||||
|
if self.pos[0] < self.grid.row - 1:
|
||||||
|
self.pos[0] += 1
|
||||||
|
self.set_pos()
|
||||||
|
|
||||||
|
def up(self):
|
||||||
|
if self.pos[1] < self.grid.column - 1:
|
||||||
|
self.pos[1] += 1
|
||||||
|
self.set_pos()
|
||||||
|
|
||||||
|
def down(self):
|
||||||
|
if self.pos[1] > 0:
|
||||||
|
self.pos[1] -= 1
|
||||||
|
self.set_pos()
|
|
@ -3,6 +3,7 @@ import jacovirt.logger
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from jacovirt.bot.grid import Grid
|
from jacovirt.bot.grid import Grid
|
||||||
|
from jacovirt.bot.bot import Bot
|
||||||
|
|
||||||
# Screen title and size
|
# Screen title and size
|
||||||
SCREEN_TITLE = "JacoVirt Bot"
|
SCREEN_TITLE = "JacoVirt Bot"
|
||||||
|
@ -30,12 +31,14 @@ class Window(arcade.Window):
|
||||||
arcade.set_background_color(BACKGROUND_COLOR)
|
arcade.set_background_color(BACKGROUND_COLOR)
|
||||||
|
|
||||||
self.grid = None
|
self.grid = None
|
||||||
|
self.bot = None
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
"""Set up the pad"""
|
"""Set up the pad"""
|
||||||
logging.info("Set up the bot.")
|
logging.info("Set up the bot.")
|
||||||
|
|
||||||
self.grid = Grid(X_START, Y_START, ROW, COLUMN, SIZE_TILE, SPACING)
|
self.grid = Grid(X_START, Y_START, ROW, COLUMN, SIZE_TILE, SPACING)
|
||||||
|
self.bot = Bot((0,5), self.grid)
|
||||||
|
|
||||||
def on_draw(self):
|
def on_draw(self):
|
||||||
"""Render the screen"""
|
"""Render the screen"""
|
||||||
|
@ -51,6 +54,8 @@ class Window(arcade.Window):
|
||||||
for tile in self.grid.tiles:
|
for tile in self.grid.tiles:
|
||||||
tile.debug()
|
tile.debug()
|
||||||
|
|
||||||
|
self.bot.draw()
|
||||||
|
|
||||||
def on_key_press(self, symbol, modifiers):
|
def on_key_press(self, symbol, modifiers):
|
||||||
"""Called when the user presses key"""
|
"""Called when the user presses key"""
|
||||||
if symbol == arcade.key.R:
|
if symbol == arcade.key.R:
|
||||||
|
@ -60,6 +65,15 @@ class Window(arcade.Window):
|
||||||
if symbol == arcade.key.Q:
|
if symbol == arcade.key.Q:
|
||||||
arcade.exit()
|
arcade.exit()
|
||||||
|
|
||||||
|
if symbol == arcade.key.LEFT:
|
||||||
|
self.bot.left()
|
||||||
|
if symbol == arcade.key.RIGHT:
|
||||||
|
self.bot.right()
|
||||||
|
if symbol == arcade.key.UP:
|
||||||
|
self.bot.up()
|
||||||
|
if symbol == arcade.key.DOWN:
|
||||||
|
self.bot.down()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main method"""
|
"""Main method"""
|
||||||
window = Window()
|
window = Window()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user