#testing the led matrix from astro_pi import AstroPi ap = AstroPi() ap.clear() #ap.show_message("Hello Space") ap.set_pixel(0, 0, 255, 0, 0) ap.set_pixel(7, 7, 255, 255, 255) #ap.set_pixel(7, 7, 0, 0, 0) ap.clear() #ap.clear([0,0,255])
move_ball() #Adds a move move += 1 #Return these variables because I didn't get my head around globalling at the #Start, but it no difference to performance return move, ballMoveTime #Clears the board incase there was something on it before ap.clear() #Sort and show bat straight away batMiddle = 3 ap.set_pixel(0, batMiddle, [100, 100, 100]) #Choose starting location of ball randomRow = random.randint(2, 4) ballLocation = [3, randomRow] #Sets the time interval the ball moves ballMoveTime = 0.4 #Initialises the teleports teleportLocations = [[], []] #Starts the angle going towards the side without bat as it's the nice thing #to do angle = 180
class AstroPiSnake(): UP = 0 DOWN = 1 RIGHT = 2 LEFT = 3 BACKCOL = [0, 0, 0] SNAKECOL = [50, 50, 100] APPLECOL = [100, 0, 0] def __init__(self): pygame.init() pygame.display.set_mode((640, 480)) self.ap = AstroPi() def startGame(self): self.ap.clear(self.BACKCOL) self.direction = self.UP self.length = 3 self.tail = [] self.tail.insert(0, [4, 4]) self.createApple() self.score = 0 playing = True while (playing): sleep(0.2) for event in pygame.event.get(): if event.type == KEYDOWN: self._handle_event(event) playing = self.move() self.ap.clear() def _handle_event(self, event): if event.key == pygame.K_DOWN: self.down() elif event.key == pygame.K_UP: self.up() elif event.key == pygame.K_LEFT: self.left() elif event.key == pygame.K_RIGHT: self.right() def createApple(self): badApple = True #try and fnd a location for the apple while (badApple): x = randint(0, 7) y = randint(0, 7) badApple = self.checkCollision(x, y) self.apple = [x, y] self.ap.set_pixel(x, y, self.APPLECOL) def checkCollision(self, x, y): #is this outside the screen if x > 7 or x < 0 or y > 7 or y < 0: return True else: #or in the snakes tail for segment in self.tail: if segment[0] == x and segment[1] == y: return True else: return False def addSegment(self, x, y): #create the new segment of the snake self.ap.set_pixel(x, y, self.SNAKECOL) self.tail.insert(0, [x, y]) #do I need to clear a segment if len(self.tail) > self.length: lastSegment = self.tail[-1] self.ap.set_pixel(lastSegment[0], lastSegment[1], self.BACKCOL) self.tail.pop() def move(self): #work out where the new segment of the snake will be newSegment = [self.tail[0][0], self.tail[0][1]] if self.direction == self.UP: if newSegment[1] == 0: newSegment[1] = 7 else: newSegment[1] -= 1 elif self.direction == self.DOWN: if newSegment[1] == 7: newSegment[1] = 0 else: newSegment[1] += 1 elif self.direction == self.LEFT: if newSegment[0] == 0: newSegment[0] = 7 else: newSegment[0] -= 1 elif self.direction == self.RIGHT: if newSegment[0] == 7: newSegment[0] = 0 else: newSegment[0] += 1 if self.checkCollision(newSegment[0], newSegment[1]): #game over snakehead = self.tail[0] for flashHead in range(0, 5): self.ap.set_pixel(snakehead[0], snakehead[1], self.SNAKECOL) sleep(0.2) self.ap.set_pixel(snakehead[0], snakehead[1], self.BACKCOL) sleep(0.2) self.ap.show_message("Score = {}".format(self.score), text_colour=self.APPLECOL) else: self.addSegment(newSegment[0], newSegment[1]) #has the snake eaten the apple? if newSegment[0] == self.apple[0] and newSegment[1] == self.apple[ 1]: self.length += 1 self.score += 10 self.createApple() return True def up(self): if self.direction != self.DOWN: self.direction = self.UP def down(self): if self.direction != self.UP: self.direction = self.DOWN def left(self): if self.direction != self.RIGHT: self.direction = self.LEFT def right(self): if self.direction != self.LEFT: self.direction = self.RIGHT
snakeColour = [0, 100, 0] snakeBodyLength = 1 snakeBodyCoords = [[0, 0]] applesEaten = 0 #State of death flashing initialised state = True #Create a colour for reference, unecessary black = [0, 0, 0] #Starts the snake going right previous = "RIGHT" #Set the first snakes colour and locaiton ap.set_pixel(0, 0, snakeColour) #Create an apple to start create_apple() #Tell the loop the snake is alive alive = True startTime = datetime.datetime.now() while alive: #Move body and store return of it in check variable check = move_body() #Tells program if you are dead or alive if check == "DEAD" or check == "WON": alive = False
from astro_pi import AstroPi ap = AstroPi() ap.set_pixel(2,2,[0,0,255]) ap.set_pixel(4,2,[0,0,255]) ap.set_pixel(3,4,[100,0,0]) ap.set_pixel(1,5,[255,0,0]) ap.set_pixel(2,6,[255,0,0]) ap.set_pixel(3,6,[255,0,0]) ap.set_pixel(4,6,[255,0,0]) ap.set_pixel(5,5,[255,0,0])
#When it has broken from the loop it moves the ball move_ball() #Adds a move move += 1 #Return these variables because I didn't get my head around globalling at the #Start, but it no difference to performance return move, ballMoveTime #Clears the board incase there was something on it before ap.clear() #Sort and show bat straight away batMiddle = 3 ap.set_pixel(0,batMiddle, [100,100,100]) #Choose starting location of ball randomRow = random.randint(2,4) ballLocation = [3,randomRow] #Sets the time interval the ball moves ballMoveTime = 0.4 #Initialises the teleports teleportLocations = [[],[]] #Starts the angle going towards the side without bat as it's the nice thing #to do angle = 180
class AstroPiSnake(): UP = 0 DOWN = 1 RIGHT = 2 LEFT = 3 BACKCOL = [0, 0, 0] SNAKECOL = [0, 255, 0] APPLECOL = [255, 0, 0] def __init__(self): pygame.init() pygame.display.set_mode((640, 480)) self.ap = AstroPi() def startGame(self): self.ap.clear(self.BACKCOL) self.direction = self.UP self.length = 3 self.tail = [] self.tail.insert(0, [4, 4]) self.createApple() self.score = 0 playing = True while(playing): sleep(0.5) for event in pygame.event.get(): if event.type == KEYDOWN: self._handle_event(event) playing = self.move() self.ap.clear() def _handle_event(self, event): if event.key == pygame.K_DOWN: self.down() elif event.key == pygame.K_UP: self.up() elif event.key == pygame.K_LEFT: self.left() elif event.key == pygame.K_RIGHT: self.right() def createApple(self): badApple = True #try and fnd a location for the apple while(badApple): x = randint(0, 7) y = randint(0, 7) badApple = self.checkCollision(x, y) self.apple = [x, y] self.ap.set_pixel(x, y, self.APPLECOL) def checkCollision(self, x, y): #is this outside the screen if x > 7 or x < 0 or y > 7 or y < 0: return True else: #or in the snakes tail for segment in self.tail: if segment[0] == x and segment[1] == y: return True else: return False def addSegment(self, x, y): #create the new segment of the snake self.ap.set_pixel(x, y, self.SNAKECOL) self.tail.insert(0, [x, y]) #do I need to clear a segment if len(self.tail) > self.length: lastSegment = self.tail[-1] self.ap.set_pixel(lastSegment[0], lastSegment[1], self.BACKCOL) self.tail.pop() def move(self): #work out where the new segment of the snake will be newSegment = [self.tail[0][0], self.tail[0][1]] if self.direction == self.UP: newSegment[1] -= 1 elif self.direction == self.DOWN: newSegment[1] += 1 elif self.direction == self.LEFT: newSegment[0] -= 1 elif self.direction == self.RIGHT: newSegment[0] += 1 if self.checkCollision(newSegment[0], newSegment[1]): #game over snakehead = self.tail[0] for flashHead in range(0,5): self.ap.set_pixel(snakehead[0], snakehead[1], self.SNAKECOL) sleep(0.2) self.ap.set_pixel(snakehead[0], snakehead[1], self.BACKCOL) sleep(0.2) self.ap.show_message("Score = {}".format(self.score), text_colour = self.APPLECOL) else: self.addSegment(newSegment[0], newSegment[1]) #has the snake eaten the apple? if newSegment[0] == self.apple[0] and newSegment[1] == self.apple[1]: self.length += 1 self.score += 10 self.createApple() return True def up(self): if self.direction != self.DOWN: self.direction = self.UP def down(self): if self.direction != self.UP: self.direction = self.DOWN def left(self): if self.direction != self.RIGHT: self.direction = self.LEFT def right(self): if self.direction != self.LEFT: self.direction = self.RIGHT
if(x > 7) : x = 7 if(y < 0) : y = 0 if(y > 7) : y = 7 p = ap.get_pixel(x,y) #debugging info as setting 0, 255, 255 did not work #print p ' #check if the point has been collected if(p[0] == pr and p[1] == pg and p[2] == pb): #its a point! points = points + 1 speed = speed + 1 # increase speed make_point() # make another point ap.set_pixel(x,y,r,g,b) time.sleep( float(1)/speed) #check for events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False #change the co-efficient depending on the key pressed , or exit the game if event.type==KEYDOWN: if event.key==K_ESCAPE: running = False if event.key==K_LEFT: leftco = -1 upco = 0 elif event.key==K_RIGHT: leftco = 1 upco = 0
# The calibration program will produce the file RTIMULib.ini # Copy it into the same folder as your Python code led_loop = [4, 5, 6, 7, 15, 23, 31, 39, 47, 55, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8, 0, 1, 2, 3] ap = AstroPi() ap.set_rotation(0) ap.clear() prev_x = 0 prev_y = 0 led_degree_ratio = len(led_loop) / 360.0 while True: dir = ap.get_compass() dir_inverted = 360 - dir # So LED appears to follow North led_index = int(led_degree_ratio * dir_inverted) offset = led_loop[led_index] y = offset // 8 # row x = offset % 8 # column if x != prev_x or y != prev_y: ap.set_pixel(prev_x, prev_y, 0, 0, 0) ap.set_pixel(x, y, 0, 0, 255) prev_x = x prev_y = y
snakeColour = [0,100,0] snakeBodyLength = 1 snakeBodyCoords = [[0,0]] applesEaten = 0 #State of death flashing initialised state = True #Create a colour for reference, unecessary black = [0,0,0] #Starts the snake going right previous = "RIGHT" #Set the first snakes colour and locaiton ap.set_pixel(0,0,snakeColour) #Create an apple to start create_apple() #Tell the loop the snake is alive alive = True startTime = datetime.datetime.now() while alive: #Move body and store return of it in check variable check = move_body() #Tells program if you are dead or alive