High school programming project, (COMPLETE BEGINNER)

Hello my name is Max, and I am a complete beginner in processing,but am starting to enjoy coding and reading interesting / exciting new codes ! In my code i have an enum,(which i dont know what it means) a Lambda methode (again still don’t know what it means) basically i have an advanced code in comparision for my level,but i am ready to learn everything that my code is doing. My question is: Can somebody either explain to me in detail what my code does or tell me a way where i can programm it through simpler methodes. Thank you for your patience and i am looking forward to anyones response !

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 obstacles = new ArrayList();
ArrayList highScores = new ArrayList();

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;
}

In its most basic form, an enum is just labels that we can store in a variable:

The code above declares a variable called state of datatype GameState and initializes it w/ the “label” MENU.

The idea is that depending on the “label” currently stored in the variable state should determine which “action” the code has to take; for example, the switch () {} block within draw().

A Java lambda is an alternative syntax to declare a function in the form of an expression.

On the code above, (a, b) -> b.score - a.score is a lambda function which is passed as the argument to the method sort().

Method sort() will call back that lambda many times as a “rule” on how we want the List highScores to be sorted.

In that case, the “rule” is to sort the List in descending order based on the field score from class ScoreEntry.

More precisely within the lambda, its parameter a represents 1 ScoreEntry, which is then compared to the 2nd parameter b, which is also a ScoreEntry instance.

By subtracting their corresponding score fields, we’ll get either a positive value, or a negative value or a zero value.

A positive result means that b should be positioned before a within the List.

A negative result means that b should be positioned after a within the List.

While a zero 0 result means both b & a have the same score; and thus no changes are needed.

BtW, a Java lambda function automatically returns the result of the expression within its body.

Hello @dinomaster123,

What is the source of the code that was posted?

Please read:
Post Only Your Own Stuff

:)

1 Like