Created
February 11, 2016 16:21
-
-
Save mavhc/d3e9907070dbdadcf31a to your computer and use it in GitHub Desktop.
This file contains 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 microbit | |
# background oxo grid at brightness 1 | |
gridImg = microbit.Image("01010\n" | |
"11111\n" | |
"01010\n" | |
"11111\n" | |
"01010\n") | |
playerBrightness = [5,8] | |
def check_victory(): | |
#code slightly altered from http://stackoverflow.com/a/15839640/1499289 | |
combinations = [ | |
# horizontal | |
((0,0), (1,0), (2,0)), | |
((0,1), (1,1), (2,1)), | |
((0,2), (1,2), (2,2)), | |
# vertical | |
((0,0), (0,1), (0,2)), | |
((1,0), (1,1), (1,2)), | |
((2,0), (2,1), (2,2)), | |
# crossed | |
((0,0), (1,1), (2,2)), | |
((2,0), (1,1), (0,2)) | |
] | |
for coordinates in combinations: | |
# our squares in are 0,2,4, so *2 | |
squares = [microbit.display.get_pixel(x*2,y*2) for x,y in coordinates] | |
# cheating, as there's no possibility of this going wrong with our brightness values of 5 and 8 | |
for i, brightness in enumerate(playerBrightness): | |
if sum(squares) == brightness*3: | |
return i+1 # returns number of player that won | |
return 0 | |
def check_squares_filled(): | |
coordinates = ((0,0), (1,0), (2,0), | |
(0,1), (1,1), (2,1), | |
(0,2), (1,2), (2,2)) | |
squares = [microbit.display.get_pixel(x*2,y*2) for x,y in coordinates] | |
for square in squares: | |
if square == 0: return False | |
return True | |
def grid(): | |
microbit.display.show(gridImg) | |
# returns 0 to 4 from -1024 to +1024, empirically calculated | |
def tilt_scale(a): | |
a = a + 1024 # range 0 to 2048 | |
a = min(a, 2048) | |
a = max(a, 0) | |
# < 512 = 0, 512-853(512+341) = 1, 853-1194 = 2, 1194-1536 = 3, 1536-2048 = 4 | |
if a<512: return 0 | |
if a<853: return 1 | |
if a<1194: return 2 | |
if a<1536: return 3 | |
return 4 | |
def get_tilt(): | |
x,y,z = microbit.accelerometer.get_values() | |
#print(x+1024,y+1024,z+1024) | |
x = tilt_scale(x) | |
y = tilt_scale(y) | |
z = tilt_scale(z) | |
#print(x,y,z) | |
return x,y,z | |
# play game forever | |
while True: | |
#display grid | |
grid() | |
won = False | |
player = 2 # first loop switches to player 1 | |
xpos = 2 # start in centre | |
ypos = 2 | |
oldBrightness = 0 # start on dark pixel | |
while not won: | |
player = 3 - player # switch player | |
#print("player =",player) | |
goodSquare = False # goodSquare = clicked player button on a dark pixel | |
while not goodSquare: # loop until blank square selected | |
buttonPressed = False | |
while not buttonPressed: # loop until button pressed | |
#redraw previous (usually current pixel) | |
microbit.display.set_pixel(xpos, ypos, oldBrightness) | |
xpos,ypos,zpos = get_tilt() | |
#print(xpos,ypos) | |
# remember old brightness, to restore at top of loop | |
oldBrightness = microbit.display.get_pixel(xpos,ypos) | |
# make current position max brightness | |
microbit.display.set_pixel(xpos, ypos, 9) | |
# check if correct player's button is clicked | |
buttonPressed = (microbit.button_a.is_pressed() and player == 1) or (microbit.button_b.is_pressed() and player == 2) | |
microbit.sleep(100) | |
# if dark pixel clicked, good square has been chosen, set brightness for current player 5 or 8 | |
if oldBrightness == 0: | |
goodSquare = True | |
#print(xpos,ypos) | |
oldBrightness = playerBrightness[player-1] | |
microbit.display.set_pixel(xpos,ypos, oldBrightness) | |
squaresFilled = check_squares_filled() | |
if check_victory()>0: | |
won = True | |
elif squaresFilled: | |
won = True | |
player = 3 | |
if player < 3: | |
microbit.display.scroll("Player "+str(player)*8+" won!!!") | |
else: | |
microbit.display.scroll("Draw ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This file is public domain