Instantly share code, notes, and snippets.

# RemyDCF/game.py Last active Dec 26, 2017

Hello Spaace !
 import math import random import time import unicornhathd import pygame from pygame.locals import * class Game(): catchedBlocks = [] spaceshipPosition = 0 def __init__(self): self.main() def setPixels(self, array): for y in range(0, 16): for x in range(0, 16): color = array[x][y] if color == 127: unicornhathd.set_pixel(x, y, 0, 0, 255) else: unicornhathd.set_pixel(x, y, color, color, color) for x in self.catchedBlocks: unicornhathd.set_pixel(x[0], x[1] + self.spaceshipPosition, 0, 0, 255) unicornhathd.show() def left(self): if self.spaceshipPosition >= -2: self.spaceshipPosition -= 1 def right(self): if self.spaceshipPosition <= 2: self.spaceshipPosition += 1 def drawSpaceship(self, array): # Initiate spaceship spaceship = [[-1, 7], [-1, 8], [-2, 7], [-2, 8], [-1, 4], [-1, 5], [-1, 6], [-1, 9], [-1, 10], [-1, 11], [-2, 4], [-2, 5], [-2, 6], [-2, 9], [-2, 10], [-2, 11]] for x in spaceship: array[x[0]][x[1] + self.spaceshipPosition] = 255 return array def main(self): unicornhathd.rotation(270) unicornhathd.brightness(1.0) lines = [] for x in range(0,16): a = [] for y in range(0,16): a.append(0) lines.append(a) lines = self.drawSpaceship(lines) self.setPixels(lines) pygame.init() for x in range(0, 166): for event in pygame.event.get(): if event.type == MOUSEBUTTONDOWN and event.button == 1: self.left() elif event.type == MOUSEBUTTONDOWN and event.button == 3: self.right() for (i, line) in enumerate(lines): for (ii, y) in enumerate(line): if i != 15 and i != 14 and ii >= (4 + self.spaceshipPosition) and ii <= (12 + self.spaceshipPosition): if [i, ii] not in self.catchedBlocks and y == 127 and ([i + 1, ii] in self.catchedBlocks or lines[i + 1][ii] == 255) and ([i + 2, ii] in self.catchedBlocks or lines[i + 2][ii] == 255): if ii > 15: self.catchedBlocks.append([i, 15]) else: self.catchedBlocks.append([i, ii]) lines[i][ii] = 0 b = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []] for (index, line) in enumerate(lines): b[index + 1] = line a = [] for y in range(0,16): if random.randint(0, 15) == 3 and lines[0][y] == 0 and lines[1][y] == 0 and lines[2][y] == 0 and x <= 150: a.append(127) else: a.append(0) b[0] = a a = [] for y in range(0,16): a.append(0) del b[-2] b[-1] = a b[-2] = a lines = self.drawSpaceship(b) self.setPixels(lines) time.sleep(0.05) time.sleep(2) for x in range(0, 16): for y in range(0, 16): unicornhathd.set_pixel(x, y, 255, 255, 255) unicornhathd.show() time.sleep(0.2) time.sleep(1) for x in range(0, 16): for y in range(0, 16): unicornhathd.set_pixel(x, y, 0, 0, 0) unicornhathd.show() a = len(self.catchedBlocks) if a > 64: unicornhathd.set_pixel(4, 7, 255, 255, 255) a -= 64 else: unicornhathd.set_pixel(4, 7, 255, 0, 0) if a > 32: unicornhathd.set_pixel(5, 7, 255, 255, 255) a -= 32 else: unicornhathd.set_pixel(5, 7, 255, 0, 0) if a > 16: unicornhathd.set_pixel(6, 7, 255, 255, 255) a -= 16 else: unicornhathd.set_pixel(6, 7, 255, 0, 0) if a > 8: unicornhathd.set_pixel(7, 7, 255, 255, 255) a -= 8 else: unicornhathd.set_pixel(7, 7, 255, 0, 0) if a > 4: unicornhathd.set_pixel(8, 7, 255, 255, 255) a -= 4 else: unicornhathd.set_pixel(8, 7, 255, 0, 0) if a > 2: unicornhathd.set_pixel(9, 7, 255, 255, 255) a -= 2 else: unicornhathd.set_pixel(9, 7, 255, 0, 0) if a > 1: unicornhathd.set_pixel(10, 7, 255, 255, 255) a -= 1 else: unicornhathd.set_pixel(10, 7, 255, 0, 0) unicornhathd.show() print("You saved the space from " + str(len(self.catchedBlocks)) + " spacejunk") Game()
Owner

### RemyDCF commented Dec 16, 2017 • edited

 For this code, you will need: Python 3 PyGame A Raspberry Pi A Pimoroni's Unicorn HAT HD A mouse And a video of the project : https://twitter.com/asmartcode/status/942338383023607808