from xenographic_wall.pgfw.Configuration import TypeDeclarations
from xenographic_wall.pgfw.Game import Game
from xenographic_wall.Introduction import Introduction
from xenographic_wall.world.World import World

class XenographicWall(Game):

    def __init__(self):
        Game.__init__(self, type_declarations=Types())
        self.introduction.activate()

    def set_children(self):
        Game.set_children(self)
        self.introduction = Introduction(self)
        self.world = World(self)

    def update(self):
        self.introduction.update()
        self.world.update()


class Types(TypeDeclarations):

    additional_defaults = \
        {"intro": \
         {"path": ["mask", "cord", "starfield", "sunburst"],
          "int": ["frame-duration", "starfield-max-alpha",
                  "prompt-visible-frames", "reset-delay"],
          "int-list": ["cord-y-range", "zoom-upper-left",
                       "zoom-region-dimensions"],
          "float": ["cord-speed", "zoom-step-ratio", "sunburst-delay",
                    "zoom-delay", "starfield-fade-step"]},
         "scope": \
         {"int": ["diameter", "ring-count", "flash-length"],
          "list": ["colors", "flash-colors"],
          "float": ["glow-step"]},
         "world": \
         {"path": "audio",
          "int-list": ["dimensions", "frame-border", "scroll-speed"]},
         "mushrooms": \
         {"int": "count", "list": "images", "path": "path"},
         "creature": \
         {"path": "root"}}
from pygame import mouse, Color, Surface, Rect

from xenographic_wall.pgfw.GameChild import GameChild
from xenographic_wall.world.scope.Scope import Scope
from xenographic_wall.world.mushrooms.Mushrooms import Mushrooms
from xenographic_wall.creatures.Creatures import Creatures

class World(GameChild, Surface):

    def __init__(self, parent):
        GameChild.__init__(self, parent)
        self.init_surface()
        self.deactivate()
        self.initialize_clip()
        self.scope = Scope(self)
        self.mushrooms = Mushrooms(self)
        self.set_frame()
        self.subscribe_to(self.get_custom_event_id(), self.respond_to_event)
        self.creatures = Creatures(self)
        self.reset()

    def init_surface(self):
        size = self.get_configuration().get("world", "dimensions")
        Surface.__init__(self, size)

    def deactivate(self):
        self.active = False

    def respond_to_event(self, evt):
        if self.is_command(evt, "cancel-introduction"):
            self.activate()
        elif self.is_command(evt, "reset-game"):
            self.reset()

    def activate(self):
        self.active = True
        self.scope.activate()
        self.get_audio().play_bgm(self.get_resource("world", "audio"))
        mouse.set_pos(self.get_screen().get_rect().center)

    def get_mouse_pos(self):
        pos = mouse.get_pos()
        clip = self.get_clip()
        return pos[0] + clip.left, pos[1] + clip.top

    def initialize_clip(self):
        clip = Rect((0, 0), self.get_screen().get_size())
        clip.center = self.get_rect().center
        self.set_clip(clip)

    def set_frame(self):
        border = self.get_configuration("world", "frame-border")
        screen = self.get_screen()
        frame = Rect((0, 0), screen.get_size())
        frame.w -= border[0]
        frame.h -= border[1]
        frame.center = screen.get_rect().center
        self.frame = frame

    def reset(self):
        self.deactivate()
        self.scope.reset()
        self.initialize_clip()
        self.creatures.reset()

    def update(self):
        if self.active:
            self.clear()
            self.update_clip()
            self.scope.update()
            self.mushrooms.update()
            self.creatures.update()
            self.draw()

    def clear(self):
        self.fill(Color("black"))

    def update_clip(self):
        x, y = mouse.get_pos()
        frame = self.frame
        move = self.move_clip
        dx, dy = 0, 0
        if not frame.collidepoint((x, y)):
            if y < frame.top:
                dy = y - frame.top
            elif y > frame.bottom:
                dy = y - frame.bottom
            if x < frame.left:
                dx = x - frame.left
            elif x > frame.right:
                dx = x - frame.right
            mx, my = self.get_configuration("world", "scroll-speed")
            move(dx / mx, dy / my)

    def move_clip(self, x=0, y=0):
        clip = self.get_clip()
        bound = self.get_rect()
        original_center = clip.center
        clip.top += y
        if clip.top < bound.top:
            clip.top = bound.top
        elif clip.bottom > bound.bottom:
            clip.bottom = bound.bottom
        clip.left += x
        if clip.left < bound.left:
            clip.left = bound.left
        elif clip.right > bound.right:
            clip.right = bound.right
        self.set_clip(clip)
        new_center = clip.center
        return new_center[0] - original_center[0], \
               new_center[1] - original_center[1]

    def draw(self):
        screen = self.get_screen()
        screen.blit(self, (0, 0), self.get_clip())
from random import randrange

from pygame import Surface, Color, Rect

from xenographic_wall.pgfw.GameChild import GameChild

class Mushroom(GameChild, Surface):

    transparent_color = Color("magenta")

    def __init__(self, parent, group, images):
        GameChild.__init__(self, parent)
        self.group = group
        self.images = images
        self.init_surface()
        self.place()
        self.eaten = False
        self.bitten = False

    def init_surface(self):
        image = self.images[0]
        Surface.__init__(self, image.get_size())
        self.clear()
        self.set_colorkey(self.transparent_color)
        self.blit(image, (0, 0))

    def clear(self):
        self.fill(self.transparent_color)

    def place(self):
        world = self.parent.parent
        size = self.get_size()
        x_bound = world.get_width() - size[0]
        y_bound = world.get_height() - size[1]
        self.rect = Rect((randrange(0, x_bound), randrange(0, y_bound)), size)

    def eat(self):
        self.eaten = True

    def bite(self):
        self.clear()
        self.blit(self.images[1], (0, 0))
        self.bitten = True
        sound = self.get_configuration("mushrooms", "bite-sound")
        self.get_audio().play_fx(sound, self.get_x_ratio())

    def get_x_ratio(self):
        return float(self.rect.centerx) / self.parent.parent.get_width()
        
    def update(self):
        self.draw()

    def draw(self):
        self.parent.parent.blit(self, self.rect)
18.191.111.40
18.191.111.40
18.191.111.40
 
January 23, 2021

I wanted to document this chat-controlled robot I made for Babycastles' LOLCAM📸 that accepts a predefined set of commands like a character in an RPG party 〰 commands like walk, spin, bash, drill. It can also understand donut, worm, ring, wheels, and more. The signal for each command is transmitted as a 24-bit value over infrared using two Arduinos, one with an infrared LED, and the other with an infrared receiver. I built the transmitter circuit, and the receiver was built into the board that came with the mBot robot kit. The infrared library IRLib2 was used to transmit and receive the data as a 24-bit value.


fig. 1.1: the LEDs don't have much to do with this post!

I wanted to control the robot the way the infrared remote that came with the mBot controlled it, but the difference would be that since we would be getting input from the computer, it would be like having a remote with an unlimited amount of buttons. The way the remote works is each button press sends a 24-bit value to the robot over infrared. Inspired by Game Boy Advance registers and tracker commands, I started thinking that if we packed multiple parameters into the 24 bits, it would allow a custom move to be sent each time, so I wrote transmitter and receiver code to process commands that looked like this:

bit
name
description
00
time
multiply by 64 to get duration of command in ms
01
02
03
04
left
multiply by 16 to get left motor power
05
06
07
08
right
multiply by 16 to get right motor power
09
10
11
12
left sign
0 = left wheel backward, 1 = left wheel forward
13
right sign
0 = right wheel forward, 1 = right wheel backward
14
robot id
0 = send to player one, 1 = send to player two
15
flip
negate motor signs when repeating command
16
repeats
number of times to repeat command
17
18
19
delay
multiply by 128 to get time between repeats in ms
20
21
22
23
swap
swap the motor power values on repeat
fig 1.2: tightly stuffed bits

The first command I was able to send with this method that seemed interesting was one that made the mBot do a wheelie.

$ ./send_command.py 15 12 15 1 0 0 0 7 0 1
sending 0xff871fcf...


fig 1.3: sick wheels

A side effect of sending the signal this way is any button on any infrared remote will cause the robot to do something. The star command was actually reverse engineered from looking at the code a random remote button sent. For the robot's debut, it ended up with 15 preset commands (that number is in stonks 📈). I posted a highlights video on social media of how the chat controls turned out.

This idea was inspired by a remote frog tank LED project I made for Ribbit's Frog World which had a similar concept: press a button, and in a remote location where 🐸 and 🐠 live, an LED would turn on.


fig 2.1: saying hi to froggo remotely using an LED

😇 The transmitter and receiver Arduino programs are available to be copied and modified 😇