Skip to content

Instantly share code, notes, and snippets.

@penguineer
Created December 29, 2014 01:18
Show Gist options
  • Select an option

  • Save penguineer/741d13b14f59dc324a1c to your computer and use it in GitHub Desktop.

Select an option

Save penguineer/741d13b14f59dc324a1c to your computer and use it in GitHub Desktop.
Minion for the @stratum0 mensa board
#!/usr/bin/env python2
# Walking Minion on the @stratum0 mensa board
#
# Author Tux <[email protected]> @plappertux
#
# Disclaimer: First python program. Do not expect good stlye or sufficient
# commentry. The code is Minion stlye. ;)
# (Better code can be shown on request.)
#
# Use the code to your own leisure. No warranty regarding the suitability,
# functionality, or any damage caused by running this program!
import client
import time
import random
line=7
# 5x7 font icons
# every byte contains pixel data for one column, LSB is on top, MSB is ignored.
FONT = {
0: [0x00, 0x00, 0x00, 0x00, 0x00],
1: [0x3E, 0x7F, 0x71, 0x34, 0x46],
2: [0x7C, 0x7E, 0x3B, 0x31, 0x7E],
3: [0x7E, 0x3B, 0x31, 0x3B, 0x7E],
4: [0x7E, 0x31, 0x3B, 0x7E, 0x7C],
5: [0x46, 0x34, 0x71, 0x7F, 0x3E]
}
# Probability for walking straight
PROB_WALK_ON = 80
# transitions from an action to another action
# will be normalized to a sum of 100
TRANSITIONS = {
# walk to right
1: [PROB_WALK_ON, 100-PROB_WALK_ON, 0, 0, 0],
# look right
2: [30, 10, 60, 0, 0],
# look straight
3: [0, 25, 50, 25, 0],
# look left
4: [0, 0, 60, 10, 30],
# walk to left
5: [0, 0, 0, 100-PROB_WALK_ON, PROB_WALK_ON]
}
DELAYS = [0.2, 0.3, 0.4, 0.3, 0.2]
def char_to_pixel_segment(c):
pixels = [0] * client.PWIDTH * client.PHEIGHT
if(c not in FONT.keys()):
c = " ";
for x in xrange(0, client.PWIDTH):
for y in xrange(0, client.PHEIGHT):
pix = (FONT[c][x] & (1<<y)) >> y
pixels[y * client.PWIDTH + x] = pix * 255
return pixels
def put_sprite(pos, idx):
y = line;
x = pos;
pixels = char_to_pixel_segment(idx)
client.screenbuf_blit(x*client.PWIDTH, y*client.PHEIGHT,
client.PWIDTH, client.PHEIGHT,
pixels)
def clear_last_x(last_x):
client.write(last_x, line, " ")
# advance the postion with wrap-around
def advance(pos, amount):
x = pos+amount
if x >= client.NUM_SEG_X:
x = x - client.NUM_SEG_X
if x < 0:
x = x + client.NUM_SEG_X
return x
def normalize_transitions(trans):
# get the sum
sum=0
for i in range(5):
sum += trans[i]
t = [0] * 5
for i in range(5):
t[i] = int(trans[i]*100/sum)
return t
MAX_SEED = 1000
# transition between actions
def transition(action, pos):
# get a random value
seed = int(random.random() * MAX_SEED)
# get the transition list
t = list(TRANSITIONS[action])
#print pos
#print t
# left border
if pos == 0:
t[5-1] = 0
# right border
if pos == client.NUM_SEG_X-1:
t[1-1] = 0
t = normalize_transitions(t)
#print t
a = 5
# threshold value for transition
thres=0
for n in range(5):
thres += t[n]*10
if thres > seed:
a = n+1
break
#print 'a=', a
return a
if __name__=="__main__":
for x in range(0, client.NUM_SEG_X):
clear_last_x(x)
x = client.NUM_SEG_X/2
y = line
last_x = 0
action = 3
while(True):
# clear the last sprite
clear_last_x(last_x)
# find the action
action = transition(action, x)
if action == 1: # walk to the right
x = advance(x, 1)
# elif action == 2: # stand and look right
# elif action == 3: # stand still and look straight
# elif action == 4: # stand and look left
elif action == 5: # walk to the left
x = advance(x, -1)
sprite = action
# put the new sprite
put_sprite(x, sprite)
last_x = x
# wait
time.sleep(DELAYS[action-1])
# End of Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment