示例#1
0
def test():
    """Test code."""
    display = Display(spi, SPI_CS, SPI_DC)

    display.draw_image('images/RaspberryPiWB128x128.raw', 0, 0, 128, 128)
    sleep(5)

    display.draw_image('images/MicroPython128x128.raw', 0, 0, 128, 128)
    sleep(5)

    display.draw_image('images/Tabby128x128.raw', 0, 0, 128, 128)
    sleep(5)

    display.draw_image('images/Tortie128x128.raw', 0, 0, 128, 128)
    sleep(10)

    display.cleanup()
示例#2
0
def main():
    """Initialize display."""

    if sys.platform == 'esp8266':
        print('1.4 inch TFT screen test on ESP8266')
        SPI_CS = 16
        SPI_DC = 15
        spi = SPI(1)

    elif sys.platform == 'esp32':
        print('1.4 inch TFT screen test on ESP32')
        sck = Pin(18)
        miso = Pin(19)
        mosi = Pin(23)
        SPI_CS = 26
        SPI_DC = 5

    # Baud rate of 14500000 seems about the max
    spi = SPI(2, baudrate=32000000, sck=sck, mosi=mosi, miso=miso)
    display = Display(spi, SPI_CS, SPI_DC)
    #    spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23))

    # Draw background image
    display.draw_image('images/Arkanoid_Border128x118.raw', 0, 10, 128, 118)

    # Initialize ADC on VP pin 36
    adc = ADC(Pin(36))
    # Set attenuation 0-3.3V
    adc.atten(ADC.ATTN_11DB)

    # Seed random numbers
    seed(ticks_us())

    # Generate bricks
    MAX_LEVEL = const(9)
    level = 1
    bricks = load_level(level, display)

    # Initialize paddle
    paddle = Paddle(display)

    # Initialize score
    score = Score(display)

    # Initialize balls
    balls = []
    # Add first ball
    balls.append(Ball(59, 111, -2, -1, display, frozen=True))

    # Initialize lives
    lives = []
    for i in range(1, 3):
        lives.append(Life(i, display))

    # Initialize power-ups
    powerups = []

    try:
        while True:
            timer = ticks_us()
            # Set paddle position to ADC spinner (scale 6 - 98)
            paddle.h_position((4096 - adc.read()) // 44 + 5)
            # Handle balls
            score_points = 0
            for ball in balls:
                # Position
                ball.set_position(paddle.x, paddle.y, paddle.x2, paddle.center)

                # Check for collision with bricks if not frozen
                if not ball.frozen:
                    prior_collision = False
                    ball_x = ball.x
                    ball_y = ball.y
                    ball_x2 = ball.x2
                    ball_y2 = ball.y2
                    ball_center_x = ball.x + ((ball.x2 + 1 - ball.x) // 2)
                    ball_center_y = ball.y + ((ball.y2 + 1 - ball.y) // 2)
                    # Check for hits
                    for brick in bricks:
                        if (ball_x2 >= brick.x and ball_x <= brick.x2
                                and ball_y2 >= brick.y and ball_y <= brick.y2):
                            # Hit
                            if not prior_collision:
                                ball.x_speed, ball.y_speed = brick.bounce(
                                    ball.x, ball.y, ball.x2, ball.y2,
                                    ball.x_speed, ball.y_speed, ball_center_x,
                                    ball_center_y)
                                prior_collision = True
                            score_points += 1
                            brick.clear()
                            bricks.remove(brick)

                    # Generate random power-ups
                    if score_points > 0 and randint(1, 20) == 7:
                        powerups.append(Powerup(ball.x, 64, display))

                # Check for missed
                if ball.y2 > display.height - 3:
                    ball.clear_previous()
                    balls.remove(ball)
                    if not balls:
                        # Clear powerups
                        powerups.clear()
                        # Lose life if last ball on screen
                        if len(lives) == 0:
                            score.game_over()
                        else:
                            # Subtract Life
                            lives.pop().clear()
                            # Add ball
                            balls.append(
                                Ball(59, 112, 2, -3, display, frozen=True))
                else:
                    # Draw ball
                    ball.draw()
            # Update score if changed
            if score_points:
                score.increment(score_points)
            # Handle power-ups
            for powerup in powerups:
                powerup.set_position(paddle.x, paddle.y, paddle.x2,
                                     paddle.center)
                powerup.draw()
                if powerup.collected:
                    # Power-up collected
                    powerup.clear()
                    # Add ball
                    balls.append(
                        Ball(powerup.x, 112, 2, -1, display, frozen=False))
                    powerups.remove(powerup)
                elif powerup.y2 > display.height - 3:
                    # Power-up missed
                    powerup.clear()
                    powerups.remove(powerup)

            # Check for level completion
            if not bricks:
                for ball in balls:
                    ball.clear()
                balls.clear()
                for powerup in powerups:
                    powerup.clear()
                powerups.clear()
                level += 1
                if level > MAX_LEVEL:
                    level = 1
                bricks = load_level(level, display)
                balls.append(Ball(59, 111, -2, -1, display, frozen=True))
            # Attempt to set framerate to 30 FPS
            timer_dif = 33333 - ticks_diff(ticks_us(), timer)
            if timer_dif > 0:
                sleep_us(timer_dif)
    except KeyboardInterrupt:
        display.cleanup()