MicroPythonSkateGame

 avatar
unknown
python
14 days ago
11 kB
6
Indexable
#here is the pins set up:
#OLED SCREEN SDA-> pin1, SCL-> pin2, VCC -> +ve (pin 5 out to this and the buttons), GND-> pin23 (Ground)
#Left Button one leg-> pin11 (GP 8), other Leg -> +ve (pin 5 out to this and the screen)
#Right Button one leg-> pin29 (GP 22), other Leg -> +ve (pin 5 out to this and the screen)

# Imports
import time
import framebuf
import _thread
import random
from machine import Pin, I2C
from neopixel import NeoPixel
from ssd1306 import SSD1306_I2C
from picozero import pico_led

pico_led.on()

left_button = Pin(8, Pin.IN, Pin.PULL_DOWN)
right_button = Pin(22, Pin.IN, Pin.PULL_DOWN)

# Set up I2C and the pins we're using for it
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)

# Short delay to stop I2C falling over
time.sleep(2)

# Define the display and size (128x32)
display = SSD1306_I2C(128, 32, i2c)

skateboard = bytearray([
    0x01, 0x02, 0x06, 0x02,
    0x02, 0x02, 0x02, 0x02,
    0x02, 0x06, 0x02, 0x01,
])
skateboardOllieFrame2 = bytearray([
    0x10,0x10,0x30,0x08,0x08,
    0x04,0x04,0x02,0x02,0x05,
])
skateboardOllieFrame3 = bytearray([
    0x40,0x40,0xA0,0x10,
    0x08,0x04,0x03,0x04,
])
skateboardOllieFrame4 = bytearray([
    0x08, 0x08, 0x14,
    0x04, 0x04, 0x02,
    0x02, 0x02, 0x05,
])
rail = bytearray([
    0x01,0x09,0x0F,0x09,0x01,0x01,0x01,0x01,0x01,0x01,
    0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
    0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x09,0x0F,0x09,
    0x01,
])
rail2 = bytearray([
    0x01,0x09,0x0F,0x09,0x01,0x01,0x01,0x01,0x01,0x01,
    0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
    0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
    0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x09,0x0F,0x09,
    0x01,
])
cone = bytearray([
    0x80,0xC0,0xB0,0xAE,0xA1,
    0xAE,0xB0,0xC0,0x80,
])
brokenBoard1 = bytearray([
    0x10,0x20,0x60,0x10,
    0x10,0x00,0x00,0x06,0x01,
])
brokenBoard2 = bytearray([
    0x01,0x00,0x02,0x00,0x02,0x00,0x00,
    0x00,0x30,0x01,0xC0,0x00,0x40,0x00,
])
brokenBoard3 = bytearray([
    0x40,0x30,0x00,0x00,
    0x04,0x04,0x03,0x02,0x04,
])
brokenBoard4 = bytearray([
    0x04,0x00,0x06,0x00,0x19,0x00,0x00,
    0x00,0x80,0x00,0x80,0x00,0x00,0x01,
])

lines1 = bytearray([
    0x01,
    0x05,
    0x04,
])
lines2 = bytearray([
    0x04,
    0x05,
    0x01,
])

fb = framebuf.FrameBuffer(skateboard, 12, 3, framebuf.MONO_VLSB)
fbOllie1 = framebuf.FrameBuffer(skateboardOllieFrame2, 10, 6, framebuf.MONO_VLSB)
fbOllie2 = framebuf.FrameBuffer(skateboardOllieFrame3, 8, 8, framebuf.MONO_VLSB)
fbOllie3 = framebuf.FrameBuffer(skateboardOllieFrame4, 9, 5, framebuf.MONO_VLSB)

fbBrokenBoard1 = framebuf.FrameBuffer(brokenBoard1, 9, 7, framebuf.MONO_VLSB)
fbBrokenBoard2 = framebuf.FrameBuffer(brokenBoard2, 7, 9, framebuf.MONO_VLSB)
fbBrokenBoard3 = framebuf.FrameBuffer(brokenBoard3, 9, 7, framebuf.MONO_VLSB)
fbBrokenBoard4 = framebuf.FrameBuffer(brokenBoard4, 7, 9, framebuf.MONO_VLSB)
fbBrokenBoards = [fbBrokenBoard1, fbBrokenBoard2, fbBrokenBoard3, fbBrokenBoard4]

fbLines1 = framebuf.FrameBuffer(lines1, 3, 3, framebuf.MONO_VLSB)
fbLines2 = framebuf.FrameBuffer(lines2, 3, 3, framebuf.MONO_VLSB)
fbLines = [fbLines1, fbLines1, fbLines1, fbLines1, fbLines1, fbLines1, fbLines2, fbLines2, fbLines2, fbLines2, fbLines2, fbLines2]


fbCone = framebuf.FrameBuffer(cone, 9, 8, framebuf.MONO_VLSB)
fbRail = framebuf.FrameBuffer(rail, 31, 4, framebuf.MONO_VLSB)
fbRail2 = framebuf.FrameBuffer(rail2, 41, 4, framebuf.MONO_VLSB)

ollieing = False
ollieingTailDown = False
onFloor = True
stop = True
readyToRestart = True

skateboardYPos = 28
skateboardXPos = 20
skateboardFrame = 0
railXPos = 140
coneXPos = 240

#show a splash screen to make taking a cool thumbnail easier
display.fill(0)
display.text("Score: 567",2,2)
display.blit(fbRail, 15, 27)
display.blit(fbRail, 115, 27)
display.blit(fbCone, 95, 23)
display.blit(fbLines1, skateboardXPos - 5, skateboardYPos - 4)
display.blit(fbOllie1, skateboardXPos, skateboardYPos - 7)
display.line(0,31,130,31,1)
display.show()
    
def restart():
    global display, ollieing, ollieingTailDown, onFloor, stop, readyToRestart
    global skateboardXPos, skateboardYPos, skateboardFrame
    global railXPos, coneXPos
    
    # Clear the display first
    #display.fill(0)
    ollieing = False
    ollieingTailDown = False
    readyToRestart = False
    onFloor = True
    stop = False
    skateboardYPos = 28
    skateboardXPos = 20
    skateboardFrame = 0
    railXPos = 140
    coneXPos = 240
    #display.blit(fb, skateboardXPos, skateboardYPos)
    #display.line(0,31,130,31,1)
    #display.show()

#this is run on the second thread
def move_objects():
    #global fb, fbOllie1, fbOllie2, fbOllie3
    global railXPos
    global coneXPos
    #global fbCone, fbRail
    global skateboardXPos, skateboardYPos
    global skateboardFrame
    global display
    global stop, readyToRestart
    linesCounter = 0
    score = 0
    
    movingRail = 1
    
    while True:
        time.sleep(0.0125)
        if stop: continue
        
        railXPos = railXPos - 2
        coneXPos = coneXPos - 2
        #reset position to the right side of screen if gone off the left side
        if railXPos < -45:
            if movingRail == 1:
                movingRail = 2
            else:
                movingRail = 1
            #if coneXPos > 140 - 45 and coneXPos < 140 + 45:
                #railXPos = 210
            #else:
            railXPos = 150 + random.randint(-15, 15)
        if coneXPos < -10:
            coneXPos = 150 + random.randint(-15, 15)

        skateboardFbToDraw = fb
        linesYPos = skateboardYPos
        yPosToCheckForCollisions = 27
        if skateboardFrame == 1:
            skateboardFbToDraw = fbOllie1
            linesYPos = skateboardYPos + 3
            yPosToCheckForCollisions = 24
        elif skateboardFrame == 2:
            skateboardFbToDraw = fbOllie2
            linesYPos = skateboardYPos + 5
            yPosToCheckForCollisions = 22
        elif skateboardFrame == 3:
            skateboardFbToDraw = fbOllie3
            linesYPos = skateboardYPos + 2
            yPosToCheckForCollisions = 25
        
        #check for collisions
        if railXPos <= skateboardXPos + 12 and railXPos >= skateboardXPos:
            #expect collision unless yPos is higher than floor
            if skateboardYPos >= yPosToCheckForCollisions:
                stop = True
        if coneXPos <= skateboardXPos + 12 and coneXPos >= skateboardXPos:
            #expect collision unless yPos is higher than floor
            if skateboardYPos >= yPosToCheckForCollisions - 1:
                stop = True
        
        if stop:
            i = 0
            for counter in range(1, 20):
                display.fill(0)
                display.text("Score: " + str(score),2,2)
                if movingRail == 1:
                    display.blit(fbRail, railXPos, 27)
                else:
                    display.blit(fbRail2, railXPos, 27)
                
                display.blit(fbCone, coneXPos, 23)
                display.blit(fbBrokenBoards[i], skateboardXPos, skateboardYPos)
                display.line(0,31,130,31,1)
                display.show()
                skateboardXPos = skateboardXPos - 2
                skateboardYPos = skateboardYPos - 2
                i = i + 1
                if i >= 4:
                    i = 0
                time.sleep(0.0125)
            score = 0
            readyToRestart = True
            continue
        
        display.fill(0)
        display.text("Score: " + str(score),2,2)
        if movingRail == 1:
            display.blit(fbRail, railXPos, 27)
        else:
            display.blit(fbRail2, railXPos, 27)
        display.blit(fbCone, coneXPos, 23)
        display.blit(skateboardFbToDraw, skateboardXPos, skateboardYPos)
        display.blit(fbLines[linesCounter], skateboardXPos - 5, linesYPos)
        linesCounter = linesCounter + 1
        if linesCounter >= 11:
            linesCounter = 0
        display.line(0,31,130,31,1)
        display.show()
        
        score = score + 1
    return


_thread.start_new_thread(move_objects, ())
print("second thread has been called")

while True: # Run forever
    time.sleep(0.025) # Delay next loop
    if stop:
        if left_button() == 1 and readyToRestart:
            restart()
        continue
    
    if left_button() == 1 and not ollieing:
        #print("Ollieing True")
        ollieing = True
        
        #do a bloody ollie
        #frame1
        skateboardYPos = skateboardYPos - 3
        skateboardFrame = 1
        time.sleep(0.05)
        #frame2
        skateboardYPos = skateboardYPos - 2
        skateboardFrame = 2
        time.sleep(0.05)
        ollieingTailDown = True
    elif right_button() == 1 and ollieingTailDown:
        onFloor = False
        #frame3
        skateboardYPos = skateboardYPos + 3
        skateboardFrame = 3
        time.sleep(0.05)
        #frame4backtonormalskateboard
        skateboardYPos = skateboardYPos - 2
        skateboardFrame = 0
        time.sleep(0.05)
        
        for counter in range(1, 3):
            # move the skateboard up pixels
            skateboardYPos = skateboardYPos - 4
            skateboardFrame = 0
            time.sleep(0.05)
        for counter in range(1, 4):
            #hang time
            skateboardFrame = 0
            ollieingTailDown = False
            time.sleep(0.05)
    #if we've let go of the left button then return to normal skateboard image and Ypos
    elif left_button() == 0 and ollieingTailDown and ollieing and onFloor:
        skateboardYPos = 28
        skateboardFrame = 0
        ollieingTailDown = False
        ollieing = False
        
    #check pixels below the board
    pixelsBelowToCheck = 3
    if ollieingTailDown:
        pixelsBelowToCheck = 8
    colourOfPixelDirectlyBelow = display.pixel(skateboardXPos + 2, skateboardYPos + pixelsBelowToCheck)
    colourOfPixelAhead = display.pixel(skateboardXPos + 10, skateboardYPos + pixelsBelowToCheck)
    if colourOfPixelDirectlyBelow == 1 or colourOfPixelAhead == 1:
        #stop moving the skateboard down
        if ollieingTailDown:
            skateboardFrame = 2
            onFloor = True
        else:
            skateboardFrame = 0
            ollieing = False
            onFloor = True
    else:
        #keep moving the skateboard down
        if ollieingTailDown:
            if skateboardYPos < 23:
                skateboardYPos = skateboardYPos + 1
            skateboardFrame = 2
        else:
            if skateboardYPos < 28:
                skateboardYPos = skateboardYPos + 1
            skateboardFrame = 0




Editor is loading...
Leave a Comment