import time,random
from LightStrip import *
from Buzzer import *
from Button import *
from Displays import LCDDisplay
from Counters import *
from Log import *
class Player:
def __init__(self):
self._score = 0
class AlienBase:
def __init__(self, color=None):
if color:
self._color = color
else:
c = random.randint(1,4)
self._color = RED if c==1 else YELLOW if c==2 else BLUE if c==3 else WHITE
def getColor(self):
return self._color
class Board:
def __init__(self, size=16, initsize=3):
self._size = size
self._bases = [None]*initsize
self._lights = LightStrip(pin=2, numleds=size, brightness=1)
self._buz = PassiveBuzzer(17)
self._display = LCDDisplay(sda=0, scl=1, i2cid=0)
self._timer = SoftwareTimer(handler=self)
self._wb = Button(21, 'white', buttonhandler = self)
self._rb = Button(20, 'red', buttonhandler = self)
self._yb = Button(19, 'yellow', buttonhandler = self)
self._bb = Button(18, 'blue', buttonhandler = self)
for x in range(0,initsize):
self._bases[x] = AlienBase()
self._playing = False
self._score = 0
def shoot(self, color):
if len(self._bases) > 0:
topbase = self._bases[len(self._bases)-1]
if topbase.getColor() == color:
for x in range(0,self._size - len(self._bases)+1):
self._lights.setPixel(x, color)
time.sleep(0.05)
self._lights.setPixel(x, BLACK)
self._buz.play(200)
self._bases.pop()
time.sleep(0.2)
self._buz.stop()
self._score = self._score + 1
self._display.showText('Score: {:>2}'.format(self._score), 0, 3)
else:
for x in range(0,self._size - len(self._bases)):
self._lights.setPixel(x, color)
time.sleep(0.05)
self._lights.setPixel(x, BLACK)
self._buz.play(600)
time.sleep(0.2)
self._buz.stop()
def buttonPressed(self, name):
if not self._playing:
self._playing = True
self._timer.start(0.5)
elif name == 'white':
self.shoot(WHITE)
elif name == 'red':
self.shoot(RED)
elif name == 'yellow':
self.shoot(YELLOW)
else:
self.shoot(BLUE)
def buttonReleased(self, name):
pass
def timeout(self):
self._bases.append(AlienBase())
self.refresh()
if len(self._bases) == self._size:
self._display.showText('GAME OVER', 1, 3)
self._timer.cancel()
return
self._timer.start(1)
def check(self):
self._timer.check()
def refresh(self):
for x in range(0, len(self._bases)):
b = self._bases[x]
if b != None:
self._lights.setPixel(self._size - x-1, b.getColor(), show=False)
self._lights.show()
if __name__ == '__main__':
Log.level = NONE
b = Board()
b.refresh()
while True:
time.sleep(0.1)
b.check()