Advertisement
Guest User

Untitled

a guest
May 28th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import unicornhat as unicorn
  2. import random
  3. import time
  4.  
  5. unicorn.brightness(0.20)
  6.  
  7. # create a grid of variable length
  8. def grid_gen(xrange = 8, yrange = 8):
  9.  
  10. grid = []
  11. x_max = xrange - 1
  12. y_max = yrange - 1
  13.  
  14. for row in range(xrange):
  15.  
  16. for column in range(yrange):
  17. grid.append((row, column))
  18.  
  19.  
  20.  
  21. return (grid, x_max, y_max)
  22.  
  23.  
  24.  
  25.  
  26.  
  27. # class create player with position(start position (0,0))
  28.  
  29. class player_attr(object):
  30.  
  31. def __init__(self):
  32. self.location = (0, 0)
  33. self.colour = [0, 255, 0]
  34.  
  35. def __str__(self):
  36. return '{} location and colour is {}'.format(self.location,
  37. self.colour)
  38.  
  39.  
  40.  
  41. # create function that randomly generates dot on (8,8) grid
  42.  
  43. def get_location(grid):
  44.  
  45. player = player_attr()
  46. light = random.choice(grid)
  47.  
  48. if player.location == light:
  49. print("Player and light are the same")
  50. get_location(final_grid)
  51.  
  52. lx, ly = light
  53. unicorn.set_pixel(lx, ly, 255, 0, 0)
  54.  
  55. return (player, light)
  56.  
  57. # move player
  58.  
  59. def move_player(player,x_max = 7, yrange = 7):
  60.  
  61. move = input("which direction to me?\nL - Left, R - Right, U - Up & D - Down")
  62. x, y = player.location
  63. if move.upper() == "L":
  64. y -= 1
  65. elif move.upper() == "R":
  66. y += 1
  67. elif move.upper() == "U":
  68. x -= 1
  69. elif move.upper() == "D":
  70. x += 1
  71. else:
  72. print("you have not moved")
  73. player.location = x, y
  74. return
  75.  
  76. # check if player is out of bound or on light
  77.  
  78. def location_check(player, light, x_max, y_max):
  79.  
  80. x, y = player.location
  81.  
  82. if x < 0:
  83. print("out of bounds")
  84. return False
  85. if x > x_max:
  86. print("out of bounds")
  87. return False
  88. if y < 0:
  89. print("out of bounds")
  90. return False
  91. if y > y_max:
  92. print("out of bounds")
  93. return False
  94. if player.location == light:
  95. print("You've won")
  96.  
  97. # set up LED for player and light
  98.  
  99. def LED_setup(player, light):
  100. px, py = player.location
  101. lx, ly = light
  102.  
  103. unicorn.set_pixel(px, py, 255, 0, 0)
  104. unicorn.set_pixel(lx, ly, 0, 255, 0)
  105.  
  106. unicorn.show()
  107. time.sleep(0.1)
  108.  
  109. return
  110.  
  111. final_grid, x_max, y_max = grid_gen()
  112. player, light = get_location(final_grid)
  113.  
  114. while True:
  115. move_player(player)
  116. check = location_check(player, light, x_max, y_max)
  117. if check != True:
  118. break
  119. LED_setup(player,light)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement