This is my code:
Code: Select all
from sense_hat import SenseHat
from time import sleep
sense = SenseHat()
b = (0, 0, 0)
w = (255, 255, 255)
board = [[b,b,b,b,b,b,b,b],
[b,b,b,b,b,b,b,b],
[b,b,b,b,b,b,b,b],
[b,b,b,b,b,b,b,b],
[b,b,b,b,b,b,b,b],
[b,b,b,b,b,b,b,b],
[b,b,b,b,b,b,b,b],
[b,b,b,b,b,b,b,b] ]
y=2
x=2
board[y][x]=w
board_1D=sum(board,[])
print(board_1D)
sense.set_pixels(board_1D)
def move_marble(pitch,roll,x,y):
new_x = x #assume no change to start with
new_y = y #assume no change to start with
if 1 < pitch < 179 and x != 0:
new_x -= 1 # move left
elif 359 > pitch > 179 and x != 7:
new_x += 1 # move right
if 1 < roll < 179 and y != 7:
new_y += 1 # move up
elif 359 > roll > 179 and y != 0:
new_y -= 1 # move down
return new_x, new_y
while True:
pitch = sense.get_orientation()['pitch']
roll = sense.get_orientation()['roll']
x, y = move_marble(pitch, roll, x, y)
board[y][x] = w
print("pitch {0} roll {1}".format(round(pitch,0), round(roll,0))
sleep (0.05)
2. You will now add boundary walls on the board that the marble cannot cross. A simple boundary wall can be as follows, which is defined by using red colour pixels.
This is my code added on to the above code(or should i do it separately?):
r = (255, 0, 0)
board = [[r,r,r,r,r,r,r,r],
[r,b,b,b,b,b,b,r],
[r,b,b,b,b,b,b,r],
[r,b,b,b,b,b,b,r],
[r,b,b,b,b,b,b,r],
[r,b,b,b,b,b,b,r],
[r,b,b,b,b,b,b,r],
[r,r,r,r,r,r,r,r] ]
def check_wall(x,y,new_x,new_y):
if board[new_y][new_x] != r:
return new_x, new_y
elif board[new_y][x] != r:
return x, new_y
elif board[y][new_x] != r:
return new_x, y
else:
return x,y
def move_marble(pitch,roll,x,y):
new_x = x
new_y = y
if 1 < pitch < 179 and x != 0:
new_x -= 1
elif 359 > pitch > 179 and x != 7:
new_x += 1
if 1 < roll < 179 and y != 7:
new_y += 1
elif 359 > roll > 179 and y != 0:
new_y -= 1
new_x,new_y = check_wall(x,y,new_x,new_y)
return new_x, new_y
3. Add the new function check_wall() to your program. Execute the program and observe that the marble will only move within the boundary and cannot cross the wall as you tilt the board. To complete the game, you will now add a target in your maze design, such as by using a green color to indicate its location as shown below. Modify your program and add the necessary code that will terminate the game once the target is captured by the Marble.
This is my code(do i add on or do it separately?):
board = [[r,r,r,b,b,b,b,r],
[r,b,b,b,b,b,b,r],
[b,b,b,b,b,r,b,r],
[b,r,r,b,r,r,b,r],
[b,b,b,b,b,b,b,b],
[b,r,b,r,r,b,b,b],
[b,b,b,r,b,b,b,r],
[r,r,b,b,b,r,r,r] ]
def check_wall(x,y,new_x,new_y):
if board[new_y][new_x] != r:
return new_x, new_y
elif board[new_y][x] != r:
return x, new_y
elif board[y][new_x] != r:
return new_x, y
else:
return x,y
g = (0, 255, 0)
board = [[r,r,r,b,b,b,b,r],
[r,b,b,b,b,b,b,r],
[b,b,b,b,g,r,b,r],
[b,r,r,b,r,r,b,r],
[b,b,b,b,b,b,b,b],
[b,r,b,r,r,b,b,b],
[b,b,b,r,b,b,b,r],
[r,r,b,b,b,r,r,r] ]
while not game_over:
pitch = sense.get_orientation()['pitch']
roll = sense.get_orientation()['roll']
x,y = move_marble(pitch,roll,x,y)
if board[y][x] == g:
sense.show_message("Win")
game_over = True
board [y][x] = w
sense.set_pixels(sum(board,[]))
sleep(0.05)
board[y][x] = b
sense.show_message('yay!!')[moderator added code tags, please do so yourself next time!]