incase my code isnt visable:
// Geo-Dash mit Dreiecken, AABB-Kollisionen, Bestenliste mit Namenseingabe
import java.util.Collections;
enum GameState { MENU, GAME, PAUSE, LOSE, WIN, LEADERBOARD, ENTER_NAME }
GameState state = GameState.MENU;
float playerX = 100, playerY = 300;
float playerW = 40, playerH = 40;
float ySpeed = 0, gravity = 0.8;
boolean isJumping = false;
ArrayList<TriangleObstacle> obstacles = new ArrayList<TriangleObstacle>();
ArrayList<ScoreEntry> highScores = new ArrayList<ScoreEntry>();
int score = 0;
String currentName = "";
void setup() {
size(800, 400);
frameRate(60);
textAlign(CENTER, CENTER);
}
void draw() {
background(30);
switch(state) {
case MENU:
drawMenu();
break;
case GAME:
runGame();
break;
case LOSE:
drawLose();
break;
case WIN:
drawWin();
break;
case PAUSE:
drawPause();
break;
case LEADERBOARD:
drawLeaderboard();
break;
case ENTER_NAME:
drawNameEntry();
break;
}
}
void drawMenu() {
fill(255);
textSize(40);
text("GEO-DASH", width/2, height/3);
textSize(20);
text("[S] Start | [M] Bestenliste", width/2, height/2);
}
void drawLose() {
fill(255, 0, 0);
textSize(32);
text("YOU LOSE!", width/2, height/2 - 20);
textSize(16);
text("Bitte gib deinen Namen ein:", width/2, height/2 + 20);
state = GameState.ENTER_NAME;
}
void drawNameEntry() {
fill(255);
textSize(20);
text("Name: " + currentName, width/2, height/2);
text("Drücke [ENTER], um zu speichern", width/2, height/2 + 40);
}
void drawWin() {
fill(0, 255, 0);
textSize(32);
text("CONGRATS, YOU WIN!", width/2, height/2);
}
void drawPause() {
fill(255);
textSize(32);
text("PAUSE", width/2, height/2);
}
void drawLeaderboard() {
background(0);
fill(255);
textSize(32);
text("Bestenliste", width/2, 50);
textSize(20);
if (highScores.size() == 0) {
text("Keine Einträge.", width/2, height/2);
} else {
for (int i = 0; i < min(5, highScores.size()); i++) {
ScoreEntry entry = highScores.get(i);
text((i+1) + ". " + entry.name + " – " + entry.score + " Punkte", width/2, 100 + i * 30);
}
}
textSize(16);
text("[M] zurück zum Menü", width/2, height - 30);
}
void runGame() {
fill(0, 255, 255);
rect(playerX, playerY, playerW, playerH);
ySpeed += gravity;
playerY += ySpeed;
if (playerY + playerH > height - 20) {
playerY = height - 20 - playerH;
ySpeed = 0;
isJumping = false;
}
if (frameCount % 180 == 0) {
float s = random(30, 60);
obstacles.add(new TriangleObstacle(width, height - 20, s));
}
if (frameCount % 30 == 0) {
score++;
}
fill(255);
textSize(16);
text("Score: " + score, 70, 30);
for (TriangleObstacle to : obstacles) {
to.move();
to.display();
if (checkAABB(playerX, playerY, playerW, playerH, to.x, to.y - to.size, to.size, to.size)) {
state = GameState.LOSE;
}
}
}
void keyPressed() {
if (state == GameState.MENU) {
if (key == 's' || key == 'S') {
restartGame();
state = GameState.GAME;
} else if (key == 'm' || key == 'M') {
state = GameState.LEADERBOARD;
}
}
else if (state == GameState.GAME) {
if ((key == ' ' || keyCode == UP) && !isJumping) {
ySpeed = -12;
isJumping = true;
} else if (key == 'p' || key == 'P') {
state = GameState.PAUSE;
}
}
else if (state == GameState.ENTER_NAME) {
if (key == ENTER || key == RETURN) {
highScores.add(new ScoreEntry(currentName, score));
highScores.sort((a, b) -> b.score - a.score);
currentName = "";
state = GameState.LEADERBOARD;
} else if (key == BACKSPACE && currentName.length() > 0) {
currentName = currentName.substring(0, currentName.length()-1);
} else if (key != CODED && key != ENTER && key != RETURN) {
currentName += key;
}
}
else if (state == GameState.LEADERBOARD && (key == 'm' || key == 'M')) {
state = GameState.MENU;
}
else if (state == GameState.PAUSE && (key == 'p' || key == 'P')) {
state = GameState.GAME;
}
}
void restartGame() {
playerY = 300;
ySpeed = 0;
obstacles.clear();
score = 0;
isJumping = false;
currentName = "";
}
class TriangleObstacle {
float x, y, size;
TriangleObstacle(float x_, float y_, float size_) {
x = x_;
y = y_;
size = size_;
}
void move() {
x -= 5;
}
void display() {
fill(255, 0, 0);
triangle(x, y, x + size/2, y - size, x + size, y);
}
}
class ScoreEntry {
String name;
int score;
ScoreEntry(String n, int s) {
name = n;
score = s;
}
}
boolean checkAABB(float x1, float y1, float w1, float h1,
float x2, float y2, float w2, float h2) {
return x1 < x2 + w2 && x1 + w1 > x2 &&
y1 < y2 + h2 && y1 + h1 > y2;
}