Created
June 18, 2017 14:03
-
-
Save SpaceVoyager/0c5ff008f39eccffa791db5c154531d0 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 < rows and c >= 0 and c < cols: | |
return manhattan_grid[r,c] | |
else: | |
return None | |
def find_entities(old_position, new_position): | |
(old_r, old_c) = old_position | |
(new_r, new_c) = new_position | |
nw_block_entity = get_block_entity(new_r - 1, new_c - 1) | |
ne_block_entity = get_block_entity(new_r - 1, new_c) | |
sw_block_entity = get_block_entity(new_r, new_c - 1) | |
if old_r == new_r: | |
return (nw_block_entity, sw_block_entity) | |
else: | |
return (ne_block_entity, nw_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) | |
old_r = None | |
old_c = None | |
def rabbit_tapped(sender): | |
global old_c, old_r, money_balance | |
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: | |
blocks = find_entities((old_r, old_c), (my_row_num, my_col_num)) | |
for b in blocks: | |
if b == 'B': | |
money_balance += bank_money_robbed_once | |
if b == 'W': | |
money_balance += money_taken_once | |
score_label2.text = '$' + str(money_balance) | |
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: | |
old_c = my_col_num | |
old_r = my_row_num | |
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 + c*float(board.width-10)/cols | |
test_rabbit_button.y = -5 + r*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