Untitled
unknown
plain_text
a year ago
5.5 kB
8
Indexable
import kaboom from "kaboom"
import "kaboom/global"
// Höhe und Breite vom Bildschirm bestimmen
kaboom({
width: 600,
height: 400,
});
// Sprites laden
loadSprite("hintergrund","sprites/hintergrund.png")
loadSpriteAtlas("sprites/spritesheet frosch.png", {
"frosch": {
"x": 0,
"y": 0,
"width": 480,
"height": 100,
"sliceX": 4,
"sliceY": 1,
"anims": {
"links": { from: 0, to: 1, speed: 4, loop: true },
"rechts": { from: 2, to: 3, speed: 4, loop: true }
}
}
});
loadSpriteAtlas("sprites/spritesheet rubine.png", {
"rubine": {
"x": 0,
"y": 0,
"width": 240,
"height": 80,
"sliceX": 3,
"sliceY": 1,
"anims": {
"drehen": { from: 0, to: 1, speed: 4, loop: true },
"explodieren": { from: 2, to: 2, loop: false }
}
}
});
loadSprite("feind", "sprites/feind.png")
loadSprite("herz", "sprites/herz.png")
scene("game",() =>{
// Frosch hinzufügen
add([
sprite("hintergrund",
{width:width(),
height:height()
})
]);
// Frosch hinzufügen
const frosch = add([
sprite("frosch"),
pos(100,300),
area(),
{
punkte: 0,
lebenspunkte: 2,
geschosseneRubine: 0,
},
"frosch",
]);
// Bewegung des Frosches
const froschGeschwindigkeit= 300;
onKeyDown("left", () => {
if (frosch.pos.x > 50) {
frosch.move(-froschGeschwindigkeit, 0)
frosch.play('links')
}
});
onKeyDown("right", () => {
if (frosch.pos.x < 450) {
frosch.move(froschGeschwindigkeit, 0)
frosch.play('rechts')
}
});
// Rubine hinzufügen
for (let col = 0; col < 6; col++) {
const rubine = add([
pos(col * 60 + 50, 60),
sprite("rubine"),
area(),
"rubine",
]);
rubine.play("drehen");
}
const rubine = get('rubine')
// Variablen für Rubinen Bewegung definieren
let rubineRichtung = 1;
let rubineSchritteZaehler = 0;
let rubineZeilenZaehler = 0;
// Rubine bewegen
onUpdate(() => {
rubine.forEach((r) => {
r.move(rubineRichtung * 20, 0);
});
rubineSchritteZaehler++;
if (rubineSchritteZaehler > 400) {
rubineRichtung = rubineRichtung * -1;
rubineSchritteZaehler = 0;
rubineZeilenZaehler++;
rubine.forEach((r) => {
r.moveBy(0, 40);
})
}
if (rubineZeilenZaehler > 4) {
go("gameover");
}
})
// Funktion Kugel spawnen
function kugelSpawnen(p) {
add([
rect(6,10),
pos(p),
anchor("center"),
area(),
"kugel",
])
}
//Kugel spawnen beim druecken der Leertaste
onKeyPress("space", () => {
kugelSpawnen(frosch.pos.add(0,-25))
})
// Kugel Bewegung
onUpdate("kugel", (k) => {
k.move(0,-400)
if (k.pos.y == 0) {
destroy(k)
}
})
//Hinzufügen Punktestand
let punktestand = add([
text(0),
pos(20, 20),
])
//Kollision Kugel mit Rubine
onCollide("kugel", "rubine", (k,r) => {
frosch.geschosseneRubine++; // Update geschosseneRubine
destroy(k)
r.play("explodieren")
wait(0.2, () => {
destroy(r)
})
})
onCollide("kugel", "rubine", (k,r) => {
shake(4)
destroy(k)
r.play("explodieren")
wait(0.2, () => {
destroy(r)
})
frosch.punkte++
punktestand.text = frosch.punkte.toString()
if (frosch.punkte >= 6){
go("win")
}
})
//Feind spawnen
function spawnFeind() {
add([
sprite('feind'),
pos(randi(50, 450), randi(30, 250)),
scale(0.5),
area(),
'feind'
])
}
// Feind jede x Sekunden spawnen
loop(5, () => {
spawnFeind()
})
// Kollision Feind mit Kugel
onCollide('feind', 'kugel', (w, k) => {
destroy(k)
destroy(w)
})
// Lebensstand Objekt hinzufügen
const lebensstand = add([
text(2),
pos(500, 20),
scale(1)
])
add([
sprite('herz'),
pos(520, 0),
scale(3)
])
// Kollision Feind mit Kugel
onCollide('feind', 'kugel', (w, k) => {
shake(15)
destroy(k)
destroy(w)
frosch.lebenspunkte -= 1
lebensstand.text = frosch.lebenspunkte.toString()
if (frosch.lebenspunkte == 0) {
go('gameover')
}
})
// Timer Objekt hinzufügen
const timer = add([
text('0'),
pos(300, 20),
scale(0.5),
{
time: 15,
},
])
// Timer aktualisieren
timer.onUpdate(() => {
timer.time -= dt()
timer.text = timer.time.toFixed(1)
if (timer.time <= 0) {
go('gameover')
}
})
// Gewinn Szene erstellen
scene('win', () => {
add([
text('YOU WIN'),
anchor('center'),
scale(1),
pos(width() / 2, height() / 2)
])
})
// Gameover Szene erstellen
scene("gameover", () => {
add([
rect(width(), height()),
color(100,100,100),
])
add([
text(frosch.punkte.toString()),
anchor('center'),
scale(3),
pos(width() / 2, height() / 2)
])
// Neustartknopf
const restartButton = add([
rect(200, 40),
pos(width() / 2, height() / 2 + 110),
anchor("center"),
color(255, 0, 0),
area(),
])
// Neustartknopf Text
add([
pos(width() / 2, height() / 2 + 110),
text("Neustart"),
anchor("center"),
area(),
])
restartButton.onUpdate(() => {
if (restartButton.isClicked()) {
frosch.punkte = 0;
go("game");
}
})
});
})
go("game")Editor is loading...
Leave a Comment