Marian Mehling
Published © CC BY

Funk-Pong

Das altbekannte Spiel Pong für den Calliope mini als 2 Spieler Version. Es werden 2 Calliope mini benötigt welche per Funk kommunizieren.

AdvancedFull instructions provided30 minutes9,643
Funk-Pong

Things used in this project

Hardware components

Calliope mini
Calliope mini
×2

Story

Read more

Schematics

Code in Block-Ansicht

Unübersichtlich?
Dafür gibts ja zum Glück die Ansicht als Text in JavaScript.

Code

Version 1.1

JavaScript
Eine kleine aktualisierte Version von Funk Pong.
Jetzt bewegt sich der eigene Balken am unteren Bildschirmrand sofort sobald man die Knöpfe drückt.
Dadurch ist das spielen finde ich etwas angenehmer und leichter.

Am grundliegenden Konzept wurde nichts geändert, lediglich bei "Wenn Knopf A bzw. B gedrückt" wurden jeweils zwei Zeilen Code hinzugefügt und im "Dauerhaft"-Bereich eine Schleife durch einen gleichwertigen aber einfacheren Block("Bildschirminhalt löschen") ersetzt.

Zum selber machen einfach auf https://pxt.calliope.cc/ gehen,
oben auf JavaScript klicken und den Code einfügen.
let richtung_x = 0
let richtung_y = 0
let x = 0
let y = 0
let balken_x = 0
let status = 0
radio.onDataPacketReceived(({receivedString: name, receivedNumber: value}) => {
    if (value == 137) {
        status = 2
    } else if (value == 11) {
        y = -2
        richtung_y = -1
        status = 1
    } else {
        x = parseInt(name)
        richtung_x = value
        y = -1
        richtung_y = 1
        status = 1
    }
})
basic.forever(() => {
    if (status == 1) {
        basic.pause(500)
        y += richtung_y
        x += richtung_x
        if (x < 0 || x > 4) {
            richtung_x = richtung_x * -1
            x += richtung_x
            x += richtung_x
        }
        if (y > 3) {
            if (x + richtung_x * -1 == balken_x || x + richtung_x * -1 == balken_x + 1) {
                richtung_y = richtung_y * -1
                y += richtung_y
                y += richtung_y
            } else {
                status = 3
                radio.sendValue("lost", 137)
            }
        } else if (y == -1) {
            richtung_x = richtung_x * -1
            x += richtung_x
            if (x < 0 || x > 4) {
                richtung_x = richtung_x * -1
                x += richtung_x
                x += richtung_x
            }
            x += -4
            x = x * -1
            radio.sendValue(x.toString(), richtung_x)
        }
        basic.clearScreen()
        led.plot(x, y)
        led.plot(balken_x, 4)
        led.plot(balken_x + 1, 4)
    } else if (status == 2) {
        basic.showLeds(`
            . # . # .
            . . . . .
            . . . . .
            # . . . #
            . # # # .
            `)
    } else if (status == 3) {
        basic.showLeds(`
            . # . # .
            . . . . .
            . . . . .
            . # # # .
            # . . . #
            `)
    }
})
input.onButtonPressed(Button.B, () => {
    if (status == 0) {
        radio.sendValue("go", 11)
        richtung_y = -1
        richtung_x = 1
        x = 3
        y = 3
        led.plot(3, 3)
        status = 1
    }
    if (balken_x < 3) {
        balken_x += 1
        led.unplot(balken_x - 1, 4)
        led.plot(balken_x + 1, 4)
    }
})
input.onButtonPressed(Button.A, () => {
    if (balken_x > 0) {
        balken_x += -1
        led.unplot(balken_x + 2, 4)
        led.plot(balken_x, 4)
    }
})
status = 0
basic.pause(1000)

Version 1.0

JavaScript
Zum selber machen einfach auf https://pxt.calliope.cc/ gehen,
oben auf JavaScript klicken und den Code einfügen.
let richtung_x = 0
let richtung_y = 0
let x = 0
let y = 0
let balken_x = 0
let status = 0
basic.forever(() => {
    if (status == 1) {
        basic.pause(500)
        y += richtung_y
        x += richtung_x
        if (x < 0 || x > 4) {
            richtung_x = richtung_x * -1
            x += richtung_x
            x += richtung_x
        }
        if (y > 3) {
            if (x + richtung_x * -1 == balken_x || x + richtung_x * -1 == balken_x + 1) {
                richtung_y = richtung_y * -1
                y += richtung_y
                y += richtung_y
            } else {
                status = 3
                radio.sendValue("lost", 137)
            }
        } else if (y == -1) {
            richtung_x = richtung_x * -1
            x += richtung_x
            if (x < 0 || x > 4) {
                richtung_x = richtung_x * -1
                x += richtung_x
                x += richtung_x
            }
            x += -4
            x = x * -1
            radio.sendValue(x.toString(), richtung_x)
        }
        for (let index = 0; index <= 4; index++) {
            for (let indey = 0; indey <= 4; indey++) {
                led.unplot(index, indey)
            }
        }
        led.plot(x, y)
        led.plot(balken_x, 4)
        led.plot(balken_x + 1, 4)
    } else if (status == 2) {
        basic.showLeds(`
            . # . # .
            . . . . .
            . . . . .
            # . . . #
            . # # # .
            `)
    } else if (status == 3) {
        basic.showLeds(`
            . # . # .
            . . . . .
            . . . . .
            . # # # .
            # . . . #
            `)
    }
})
input.onButtonPressed(Button.A, () => {
    if (balken_x > 0) {
        balken_x += -1
    }
})
radio.onDataPacketReceived(({receivedString: name, receivedNumber: value}) => {
    if (value == 137) {
        status = 2
    } else if (value == 11) {
        y = -2
        richtung_y = -1
        status = 1
    } else {
        x = parseInt(name)
        richtung_x = value
        y = -1
        richtung_y = 1
        status = 1
    }
})
input.onButtonPressed(Button.B, () => {
    if (status == 0) {
        radio.sendValue("go", 11)
        richtung_y = -1
        richtung_x = 1
        x = 3
        y = 3
        led.plot(3, 3)
        status = 1
    }
    if (balken_x < 3) {
        balken_x += 1
    }
})
status = 0
basic.pause(1000)

Credits

Marian Mehling

Marian Mehling

3 projects • 3 followers

Comments