Exemplo n.º 1
0
#05/06/15
import pygame, random, datetime, math, os, time
from pygame.locals import *
from astro_pi import AstroPi
ap = AstroPi()


ap.clear()

pygame.init()
pygame.display.set_mode((640, 480))


#Handle the joystick input
def handle_event(event):
    if event.key == pygame.K_DOWN:
        return "DOWN"
        
    elif event.key == pygame.K_UP:
        return "UP"

    elif event.key == pygame.K_LEFT:
        return "LEFT"

    elif event.key == pygame.K_RIGHT:
        return "RIGHT"

    elif event.key == pygame.K_RETURN:
        return "RETURN"

Exemplo n.º 2
0
#!/usr/bin/python
import os
import time
import pygame  # See http://www.pygame.org/docs

print("Press Escape to quit")
time.sleep(1)

from pygame.locals import *
from astro_pi import AstroPi

pygame.init()
pygame.display.set_mode((640, 480))

ap = AstroPi()
ap.clear()  # Blank the LED matrix

# 0, 0 = Top left
# 7, 7 = Bottom right
UP_PIXELS = [[3, 0], [4, 0]]
DOWN_PIXELS = [[3, 7], [4, 7]]
LEFT_PIXELS = [[0, 3], [0, 4]]
RIGHT_PIXELS = [[7, 3], [7, 4]]
CENTRE_PIXELS = [[3, 3], [4, 3], [3, 4], [4, 4]]


def set_pixels(pixels, col):
    for p in pixels:
        ap.set_pixel(p[0], p[1], col[0], col[1], col[2])

Exemplo n.º 3
0
#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])
Exemplo n.º 4
0
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
Exemplo n.º 5
0
msleep = lambda x: time.sleep(x / 1000.0)


def next_colour():
    global r
    global g
    global b

    if (r == 255 and g < 255 and b == 0):
        g += 1

    if (g == 255 and r > 0 and b == 0):
        r -= 1

    if (g == 255 and b < 255 and r == 0):
        b += 1

    if (b == 255 and g > 0 and r == 0):
        g -= 1

    if (b == 255 and r < 255 and g == 0):
        r += 1

    if (r == 255 and b > 0 and g == 0):
        b -= 1

while True:
    ap.clear([r, g, b])
    msleep(2)
    next_colour()
Exemplo n.º 6
0
#!/usr/bin/python
from astro_pi import AstroPi
import time

ap = AstroPi()
temp = ap.get_temperature()
humidity = ap.get_humidity()
pressure = ap.get_pressure()


print("Temperature: %s C" % temp)               # Show temp on console
print("Humidity: %s %%rH" % humidity)        # Show humidity on console
print("Pressure: %s Millibars" % pressure)    # Show pressure on console

ap.set_rotation(180)        # Set LED matrix to scroll from right to left
              
ap.show_message("Temperature: %.2f C" % temp, scroll_speed=0.05, text_colour=[0, 255, 0])

time.sleep(1)           # Wait 1 second

ap.show_message("Humidity: %.2f %%rH" % humidity, scroll_speed=0.05, text_colour=[255, 0, 0]) 

time.sleep(1)      # Wait 1 second

ap.show_message("Pressure: %.2f Millibars" % humidity, scroll_speed=0.05, text_colour=[0, 0, 255])

ap.clear()      # Clear LED matrix
Exemplo n.º 7
0
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