Add initial version of feeding raspimon (joystick control)
Change-Id: I274fa80c08c37c147cd27ccf49ea32824338f884
diff --git a/raspimon_eats.py b/raspimon_eats.py
new file mode 100644
index 0000000..e1c7764
--- /dev/null
+++ b/raspimon_eats.py
@@ -0,0 +1,83 @@
+from sense_hat import SenseHat
+from time import sleep
+from random import randint
+
+sense = SenseHat()
+
+# Variables ---------------------------
+raspimon = [2,4]
+berries = []
+white = (255, 255, 255)
+blank = (0, 0, 0)
+red = (255, 0, 0)
+direction = "right"
+pause = 1.0
+
+# Functions ---------------------------
+def move():
+ global raspimon, pause
+
+ # Copy the current position
+ next = list(raspimon)
+
+ # Find the next pixel in the direction the slug is currently moving
+ if direction == "right":
+ # Move along the column
+ if raspimon[0] + 1 == 8:
+ next[0] = 0
+ else:
+ next[0] = raspimon[0] + 1
+
+ elif direction == "left":
+ if raspimon[0] - 1 == -1:
+ next[0] = 7
+ else:
+ next[0] = raspimon[0] - 1
+
+ elif direction == "down":
+ if raspimon[1] + 1 == 8:
+ next[1] = 0
+ else:
+ next[1] = raspimon[1] + 1
+
+ elif direction == "up":
+ if raspimon[1] - 1 == -1:
+ next[1] = 7
+ else:
+ next[1] = raspimon[1] - 1
+
+ # Update the position
+ sense.set_pixel(raspimon[0], raspimon[1], blank)
+ sense.set_pixel(next[0], next[1], white)
+
+ # If next position is a berry, eat it
+ if next in berries:
+ berries.remove(next)
+
+ raspimon = next
+
+def joystick_moved(event):
+ global direction
+ direction = event.direction
+
+def add_berry():
+ x = randint(0, 7)
+ y = randint(0, 7)
+ new = [x, y]
+ sense.set_pixel(x, y, red)
+ berries.append(new)
+
+# Main program ------------------------
+sense.clear()
+sense.stick.direction_any = joystick_moved
+
+for _ in range(5):
+ add_berry()
+
+while True:
+ move()
+ sleep(pause)
+ if len(berries) == 0:
+ break
+
+sense.clear()