Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unicornhat as unicorn
- import random
- import time
- unicorn.brightness(0.20)
- # create a grid of variable length
- def grid_gen(xrange = 8, yrange = 8):
- grid = []
- x_max = xrange - 1
- y_max = yrange - 1
- for row in range(xrange):
- for column in range(yrange):
- grid.append((row, column))
- return (grid, x_max, y_max)
- # class create player with position(start position (0,0))
- class player_attr(object):
- def __init__(self):
- self.location = (0, 0)
- self.colour = [0, 255, 0]
- def __str__(self):
- return '{} location and colour is {}'.format(self.location,
- self.colour)
- # create function that randomly generates dot on (8,8) grid
- def get_location(grid):
- player = player_attr()
- light = random.choice(grid)
- if player.location == light:
- print("Player and light are the same")
- get_location(final_grid)
- lx, ly = light
- unicorn.set_pixel(lx, ly, 255, 0, 0)
- return (player, light)
- # move player
- def move_player(player,x_max = 7, yrange = 7):
- move = input("which direction to me?\nL - Left, R - Right, U - Up & D - Down")
- x, y = player.location
- if move.upper() == "L":
- y -= 1
- elif move.upper() == "R":
- y += 1
- elif move.upper() == "U":
- x -= 1
- elif move.upper() == "D":
- x += 1
- else:
- print("you have not moved")
- player.location = x, y
- return
- # check if player is out of bound or on light
- def location_check(player, light, x_max, y_max):
- x, y = player.location
- if x < 0:
- print("out of bounds")
- return False
- if x > x_max:
- print("out of bounds")
- return False
- if y < 0:
- print("out of bounds")
- return False
- if y > y_max:
- print("out of bounds")
- return False
- if player.location == light:
- print("You've won")
- # set up LED for player and light
- def LED_setup(player, light):
- px, py = player.location
- lx, ly = light
- unicorn.set_pixel(px, py, 255, 0, 0)
- unicorn.set_pixel(lx, ly, 0, 255, 0)
- unicorn.show()
- time.sleep(0.1)
- return
- final_grid, x_max, y_max = grid_gen()
- player, light = get_location(final_grid)
- while True:
- move_player(player)
- check = location_check(player, light, x_max, y_max)
- if check != True:
- break
- LED_setup(player,light)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement