Created
June 3, 2017 15:32
-
-
Save SpaceVoyager/9572c28069110a93d52a867d14664a1a to your computer and use it in GitHub Desktop.
rabbitrobber.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ui | |
import random | |
import numpy as np | |
rabbit_size = 50 | |
v = ui.View(background_color=('#bcdeff')) | |
board = ui.View() | |
v.add_subview(board) | |
v.present('full_screen', hide_title_bar=False , orientations=['landscape']) | |
board.frame = (v.width-v.height + rabbit_size/2, rabbit_size/2 , v.height - rabbit_size, v.height - rabbit_size) | |
board.border_width = 10 | |
board.border_color = ('black') | |
board.background_color = ('#ffffff') | |
rows = 8 | |
cols = 8 | |
blocks = [] | |
money_balance = 0 | |
bank_money_robbed_once = 10000 | |
money_taken_once = -20000 | |
score_label = ui.Label() | |
score_label.text = 'Money Balance:' | |
score_label.width = 300 | |
score_label.x = 20 | |
score_label.y = 20 | |
score_label.font = ('Chalkduster', 26) | |
score_label.alignment = ui.ALIGN_LEFT | |
v.add_subview(score_label) | |
score_label2 = ui.Label() | |
score_label2.text = '$0' | |
score_label2.width = 300 | |
score_label2.text_color = .0, 0.8, .35 | |
score_label2.x = 20 | |
score_label2.y = 60 | |
score_label2.font = ('Chalkduster', 30) | |
score_label2.alignment = ui.ALIGN_LEFT | |
v.add_subview(score_label2) | |
manhattan_grid = np.chararray((rows, cols)) | |
manhattan_grid[:] = 'O' # fill up manhattan_grid with 'O', which means empty block | |
for r in range(rows): | |
for c in range(cols): | |
random_num = random.randint(1, 100) | |
image = ui.ImageView() | |
if random_num < 12: | |
image = ui.ImageView() | |
image.image = ui.Image('emj:Boar') | |
manhattan_grid[r,c] = 'W' | |
elif random_num < 24 and random_num > 11: | |
image = ui.ImageView() | |
image.image = ui.Image('emj:Bank') | |
manhattan_grid[r,c] = 'B' | |
else: | |
image.background_color = ('#838383') | |
image.width = board.height/cols | |
image.height = board.height/rows | |
image.border_color = ('black') | |
image.border_width = 5 | |
image.x = image.width*c | |
image.y = image.height*r | |
image.font = ('American Typewriter',60) | |
board.add_subview(image) | |
blocks.append({'image' : image, 'row_number' : r, 'column_number' : c}) | |
print manhattan_grid | |
def get_block_entity(r, c): | |
if r >= 0 and r < n_row and c >= 0 and c < n_col: | |
return manhattan_grid[r,c] | |
else: | |
return None | |
# When the rabbit tries to figure out the best way to go to | |
# the intersection at r row and c column, it needs to know | |
# what kinds of entities lie long the streets. | |
# This function finds the entities in the NW, NE and SW blocks | |
# and returns them in a tuple (NW entity, NE entity, SW entity) | |
def find_entities(r, c): | |
nw_block_entity = get_block_entity(r - 1, c - 1) | |
ne_block_entity = get_block_entity(r - 1, c) | |
sw_block_entity = get_block_entity(r, c - 1) | |
return (nw_block_entity, ne_block_entity, sw_block_entity) | |
rabbit_buttons = [] | |
normal_rabbit_color = (1.0, .54, .78, 1.0) | |
ghost_rabbit_color = (1.0, .96, 1.0, 0.5) | |
trail_rabbit_color = (0.9, .96, 1.0, 1.0) | |
def rabbit_tapped(sender): | |
if sender.tint_color in [normal_rabbit_color, ghost_rabbit_color]: | |
for b in rabbit_buttons: | |
if b['rabbit'] == sender: | |
my_col_num = b['column_number'] | |
my_row_num = b['row_number'] | |
break | |
if sender.tint_color == ghost_rabbit_color: | |
for b in rabbit_buttons: | |
if b['rabbit'].tint_color == normal_rabbit_color: | |
b['rabbit'].tint_color = trail_rabbit_color | |
sender.tint_color = normal_rabbit_color | |
for b in rabbit_buttons: | |
if b['rabbit'].tint_color == ghost_rabbit_color: | |
b['rabbit'].image = None | |
b['rabbit'].tint_color = (1,1,1,1) | |
if sender.tint_color == normal_rabbit_color: | |
for b in rabbit_buttons: | |
if b['row_number'] == my_row_num + 1 and b['column_number'] == my_col_num: | |
b['rabbit'].tint_color = ghost_rabbit_color | |
b['rabbit'].image = ui.Image('emj:Rabbit_Face') | |
if b['row_number'] == my_row_num and b['column_number'] == my_col_num + 1: | |
b['rabbit'].tint_color = ghost_rabbit_color | |
b['rabbit'].image = ui.Image('emj:Rabbit_Face') | |
for r in range(rows + 1): | |
for c in range(cols + 1): | |
test_rabbit_button = ui.Button() | |
if r == 0 and c == 0: | |
test_rabbit_button.image = ui.Image('emj:Rabbit_Face') | |
test_rabbit_button.tint_color = normal_rabbit_color | |
test_rabbit_button.x = -18 + v.width-v.height + rabbit_size/2 + r*float(board.width-10)/cols | |
test_rabbit_button.y = -5 + c*float(board.height - 3)/rows | |
test_rabbit_button.width = rabbit_size | |
test_rabbit_button.height = rabbit_size | |
test_rabbit_button.action = rabbit_tapped | |
v.add_subview(test_rabbit_button) | |
rabbit_buttons.append({'rabbit' : test_rabbit_button, 'row_number' : r, 'column_number' : c}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment