Michael Klein
Published © CC BY

GameZip64 am Calliope Mini

Snake und farbige Laufschrift am GameZIP64 und Calliope Mini

IntermediateFull instructions provided30 minutes59
GameZip64 am Calliope Mini

Things used in this project

Hardware components

Calliope mini
Calliope mini
×1
callio:bit
×1

Software apps and online services

MakeCode
Microsoft MakeCode

Story

Read more

Schematics

Snake.hex

Hexfile für Snake mit Callio:bit M

Laufschrift.hex

Laufschriftprogramm

Code

Snake

Typescript
Snake Programmfile
let food: neopixel.Strip = null
let position_1 = 0
let speed = 0
let new_head = 0
let score = 0
let food_position = 0
let direction = ""
let count = 0
let snake_array: number[] = []
let value = 0
let snake_head = 0
let right = 0
let display: neopixel.Strip = null
let alive = false
let left = 0
let down = 0
let up = 0
let food_x = 0
let food_y = 0
basic.showString("SNAKE")
led.enable(false)
value = 0
food_x = Math.randomRange(0, 7)
food_y = Math.randomRange(0, 7)
count = 0
direction = "up"
score = 0
speed = 250
alive = true
display = neopixel.create(DigitalPin.C16, 64, NeoPixelMode.RGB)
display.setBrightness(20)
snake_array = [33]
for (let value2 of snake_array) {
    position_1 = custom.integer_position(value2)
    display.setPixelColor(position_1, neopixel.colors(NeoPixelColors.Green))
}
food_position = custom.pixel_position(food_x, food_y)
food = display.range(food_position, 1)
food.showColor(neopixel.colors(NeoPixelColors.Red))
display.show()
basic.pause(500)
basic.forever(() => {
    up = pins.digitalReadPin(DigitalPin.C4)
    down = pins.digitalReadPin(DigitalPin.C6)
    left = pins.digitalReadPin(DigitalPin.C12)
    right = pins.digitalReadPin(DigitalPin.C5)
    if (up == 0) {
        if (direction != "down") {
            direction = "up"
        }
    } else if (down == 0) {
        if (direction != "up") {
            direction = "down"
        }
    } else if (left == 0) {
        if (direction != "right") {
            direction = "left"
        }
    } else if (right == 0) {
        if (direction != "left") {
            direction = "right"
        }
    }
})
basic.forever(() => {
    while (alive == true) {
        display.clear()
        display.show()
        snake_head = snake_array[0]
        if (custom.integer_position(snake_head) == food_position) {
            // Buzzer
            pins.analogWritePin(AnalogPin.P1, 511)
            pins.analogSetPeriod(AnalogPin.P1, 500)
            basic.pause(50)
            pins.analogWritePin(AnalogPin.P1, 0)
            pins.analogSetPeriod(AnalogPin.P1, 500)
            if (direction == "up") {
                snake_array.push(snake_array[snake_array.length - 1] + 1)
            } else if (direction == "down") {
                snake_array.push(snake_array[snake_array.length - 1] - 1)
            } else if (direction == "left") {
                snake_array.push(snake_array[snake_array.length - 1] + 10)
            } else if (direction == "right") {
                snake_array.push(snake_array[snake_array.length - 1] - 10)
            }
            food_x = Math.randomRange(0, 7)
food_y = Math.randomRange(0, 7)
food_position = custom.pixel_position(food_x, food_y)
            count = snake_array.length
            while (count > 0) {
                if (food_position == custom.integer_position(snake_array[count - 1])) {
                    food_x = Math.randomRange(0, 7)
food_y = Math.randomRange(0, 7)
food_position = custom.pixel_position(food_x, food_y)
                    count = snake_array.length
                } else {
                    count += -1
                }
            }
            if (snake_array.length == 63) {
                display.clear()
                display.show()
                led.enable(true)
                basic.showString("You Win!")
            } else if (snake_array.length < 10) {
                score += 1
            } else if (snake_array.length < 20) {
                score += 2
            } else if (snake_array.length < 30) {
                score += 3
            } else if (snake_array.length < 40) {
                score += 4
            } else if (snake_array.length < 50) {
                score += 5
            }
            if (score == 10) {
                speed += -25
            } else if (score == 25) {
                speed += -25
            } else if (score == 40) {
                speed += -25
            }
        }
        count = snake_array.length
        while (count > 1) {
            if (snake_head == snake_array[count - 1]) {
                alive = false
            }
            count += -1
        }
        if (direction == "up") {
            if (custom.y_value(snake_head) == 0) {
                new_head = snake_head + 7
            } else {
                new_head = snake_head - 1
            }
        } else if (direction == "down") {
            if (custom.y_value(snake_head) == 7) {
                new_head = snake_head - 7
            } else {
                new_head = snake_head + 1
            }
        } else if (direction == "left") {
            if (custom.x_value(snake_head) == 0) {
                new_head = snake_head + 70
            } else {
                new_head = snake_head - 10
            }
        } else if (direction == "right") {
            if (custom.x_value(snake_head) == 7) {
                new_head = snake_head - 70
            } else {
                new_head = snake_head + 10
            }
        }
        count = snake_array.length
        while (count > 0) {
            snake_array[count - 1] = snake_array[count - 2]
            count += -1
        }
        snake_array[0] = new_head
        for (let value3 of snake_array) {
            position_1 = custom.integer_position(value3)
            display.setPixelColor(position_1, neopixel.colors(NeoPixelColors.Green))
        }
        food = display.range(food_position, 1)
        food.showColor(neopixel.colors(NeoPixelColors.Red))
        display.show()
        basic.pause(speed)
    }
    while (alive == false) {
        pins.digitalWritePin(DigitalPin.C17, 1)
        basic.pause(200)
        pins.digitalWritePin(DigitalPin.C17, 0)
        basic.pause(100)
        pins.digitalWritePin(DigitalPin.C17, 1)
        basic.pause(200)
        pins.digitalWritePin(DigitalPin.C17, 0)
        basic.pause(100)
        pins.digitalWritePin(DigitalPin.C17, 1)
        basic.pause(200)
        pins.digitalWritePin(DigitalPin.C17, 0)
        display.clear()
        display.show()
        led.enable(true)
        basic.showString("Game Over!")
        basic.showNumber(score)
        if (input.buttonIsPressed(Button.AB)) {
            basic.clearScreen()
            led.enable(false)
            alive = true
            snake_array = [33]
            direction = "up"
            score = 0
            speed = 250
            food_x = Math.randomRange(0, 7)
food_y = Math.randomRange(0, 7)
        }
    }
})

Custom

Typescript
Die custom-Datei mit den fehlenden math-funktionen im Calliope V0 Core
// notwendige Funktionen für das aktuelle Neopixel-Paket usw.
namespace Math {
    export function idiv(x: number, y: number) { return x / y }
    export function round(x: number) { return x }
    export function randomRange(x: number, y: number) { return random(7) }
}

/**
 * Custom blocks
 */
//% weight=100 color=#0fbc11 icon=""
namespace custom {

    /**
     * Convert 8x8 xy Coordinates to LED Position
     * @param x describe value here, eg: 0
     * @param y describe value here, eg: 0
     */
    //% block
    export function pixel_position(x: number, y: number): number {
        return x + (y * 8);
    }

    /**
     * Convert integer number to LED Position
     * @param xy describe value here, eg: 0
     */
    //% block
    export function integer_position(xy: number): number {
        /*x1 = xy / 10;
        y1 = xy % 10;
        return x1 + (y1 * 8);*/
        return (xy / 10) + ((xy % 10) * 8)
    }

    /**
     * Convert integer number to x coordinate
     * @param xy describe value here, eg: 0
     */
    //% block
    export function x_value(xy: number): number {
        /*x1 = xy / 10;
        y1 = xy % 10;
        return x1 + (y1 * 8);*/
        return (xy / 10)
    }

    /**
     * Convert integer number to y coordinate
     * @param xy describe value here, eg: 0
     */
    //% block
    export function y_value(xy: number): number {
        /*x1 = xy / 10;
        y1 = xy % 10;
        return x1 + (y1 * 8);*/
        return (xy % 10)
    }
}

Credits

Michael Klein

Michael Klein

27 projects • 14 followers
Na klar Bio! Oder direkt aus dem Garten ;-)

Comments