progressbar/game.c

162 lines
5 KiB
C
Raw Normal View History

2024-07-13 21:31:16 +02:00
#include "raylib.h"
#include <math.h>
#include <stdio.h>
// #include <stdlib.h>
// #include <time.h>
struct level {
int currentlevel;
int neededpoint;
};
// const char *thefunny[] = {"keep it up!", "edging is very fun", "good job",
// "spam that spacebar!", ":3"};
// int array_size = sizeof(thefunny) / sizeof(thefunny[0]);
int getlevel(int score);
int calculatepoint(int point, struct level lvl) {
int newpoint = point;
if (IsKeyDown(KEY_SPACE) || IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
newpoint += 1 * lvl.currentlevel;
} else if (point > 0) {
newpoint -= 1 * lvl.currentlevel;
}
return newpoint;
}
int getscore(int point, bool lost, struct level lvl) {
if (lost) {
return 0;
}
if (point > 386) {
return 25 * (lvl.currentlevel * 2);
} else if (point > 354) {
return 12 * (lvl.currentlevel * 2);
} else if (point > 295) {
return 5 * (lvl.currentlevel * 2);
} else if (point > 236) {
return 2 * (lvl.currentlevel * 2);
} else if (point > 197) {
return 1 * (lvl.currentlevel * 2);
}
return 0;
}
void congrats(int level, int *framecounter) { *framecounter = 100; }
int getrequiredPoints(int level) {
int result = 0;
while (level >= 3) {
result += pow(5, 3);
level -= 3;
}
if (level > 0) {
result += pow(5, level);
}
return result;
}
void updateLevel(struct level *lvl, int points, int *framecounter) {
int requiredPoints = 1000 * getrequiredPoints(lvl->currentlevel);
if (points >= requiredPoints) {
lvl->currentlevel++;
congrats(lvl->currentlevel, framecounter);
lvl->neededpoint = 1000 * getrequiredPoints(lvl->currentlevel);
}
}
int main(void) {
const int windowWidth = 800;
const int windowHeight = 450;
InitWindow(windowWidth, windowHeight, "me edging my progressbar");
const char *title = "me edging my progressbar";
const char *subtitle = "a game about edging progressbars";
const char *losttext = "your progress was \"filled\"";
const char *sublosttext = "press e to try again (or just click, mf mobile users smh)";
SetTargetFPS(120);
int point = 0;
struct level playerLevel = {1, 5000};
int score = 0;
bool lost = false;
int framecounter = 0;
while (!WindowShouldClose()) {
framecounter--;
BeginDrawing();
ClearBackground(DARKGRAY);
if (framecounter > 0) {
DrawTextPro(GetFontDefault(),
TextFormat("advanced to lvl%d", playerLevel.currentlevel),
(Vector2){600, 180},
(Vector2){MeasureText(TextFormat("advanced to lvl%d",
playerLevel.currentlevel),
20) /
2.0f,
10.0f},
10, 20, 1, GRAY);
}
if (!lost) {
DrawText(title, (windowWidth - MeasureText(title, 40)) / 2, 20, 40,
LIGHTGRAY);
DrawText(subtitle, (windowWidth - MeasureText(subtitle, 20)) / 2, 90, 20,
LIGHTGRAY);
} else {
DrawText(losttext, (windowWidth - MeasureText(losttext, 40)) / 2, 20, 40,
LIGHTGRAY);
DrawText(sublosttext, (windowWidth - MeasureText(sublosttext, 20)) / 2,
90, 20, LIGHTGRAY);
}
if (lost && (IsKeyDown(KEY_E)|| IsMouseButtonDown(MOUSE_LEFT_BUTTON))) {
point = 0;
lost = false;
score = 0;
playerLevel.currentlevel = 1;
}
// srand(time(NULL));
// if (rand() % 100000) {
// srand(time(NULL));
// int random = rand() % array_size;
// DrawText(thefunny[random], 400 - MeasureText(thefunny[random], 26), 180, 26, LIGHTGRAY);
// }
lost = (point >= 394);
if (!lost) {
point = calculatepoint(point, playerLevel);
}
DrawText(TextFormat("score: %d", score), 100, 300, 20, LIGHTGRAY);
DrawText(TextFormat("level: %d", playerLevel.currentlevel), 100, 330, 20,
LIGHTGRAY);
DrawText(TextFormat("next level: %d points", playerLevel.neededpoint),
windowWidth -
MeasureText(TextFormat("next level: %d points",
playerLevel.neededpoint),
20) -
20,
300, 20, LIGHTGRAY);
updateLevel(&playerLevel, score, &framecounter);
printf("needed: %d p: %d - s: %d - l: %d\n", playerLevel.neededpoint, point,
score, playerLevel.currentlevel);
score += getscore(point, lost, playerLevel);
DrawRectangle(203, 203, point, 44, PINK);
DrawRectangle(397, 203, 3, 44, (Color){255, 216, 167, 255});
DrawRectangle(436, 203, 3, 44, (Color){246, 182, 182, 255});
DrawRectangle(495, 203, 3, 44, (Color){227, 150, 204, 255});
DrawRectangle(554, 203, 3, 44, (Color){209, 115, 227, 255});
DrawRectangle(586, 203, 3, 44, (Color){171, 84, 231, 255});
Rectangle rec1 = {200, 200, 400, 50};
DrawRectangle(600, 200, 400, 70, DARKGRAY);
DrawRectangleLinesEx(rec1, 3, RED);
EndDrawing();
}
CloseWindow();
return 0;
}