Files
typinggame/main.c
T

85 lines
1.7 KiB
C

#include <raylib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "game_state.h"
#define WINDOW_HEIGHT 500
#define WINDOW_WIDTH 700
#define CHAR_SPACING 40
void process_key_events() {
char buf[2] = {'c', '\0'};
int k;
const char *key_name;
while ((k = GetKeyPressed()) != 0) {
key_name = GetKeyName(k);
char c = get_curr_letter();
sprintf(buf, "%c\0", c);
if (key_name != NULL &&
strcmp(key_name, buf) == 0) {
next();
}
}
}
void render_game_state() {
char buf[2] = {'c', '\0'};
char *curr_word = get_curr_word();
int curr_letter_index = get_curr_letter_index();
size_t word_len = strlen(curr_word);
DrawText(curr_word, 10, 10, 20, BLACK);
int x = (WINDOW_WIDTH / 2) - (CHAR_SPACING * (word_len / 2));
int y = WINDOW_HEIGHT / 2;
for (int i = 0; i < word_len; i++) {
char c = curr_word[i];
sprintf(buf, "%c\0", c);
Color color;
if (i == curr_letter_index) {
color = GOLD;
} else if (i < curr_letter_index) {
color = DARKGRAY;
} else {
color = GRAY;
}
DrawText(buf, x, y, 50, color);
x += CHAR_SPACING;
}
char wins_text[20];
snprintf(wins_text, 20, "%d wins", get_wins());
DrawText(wins_text, WINDOW_WIDTH - 100, WINDOW_HEIGHT - 20, 20, GRAY);
}
int main(int argc, char *argv[]) {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Typing game");
init_game_state();
while (!WindowShouldClose()) {
process_key_events();
BeginDrawing();
ClearBackground(WHITE);
render_game_state();
DrawFPS(WINDOW_WIDTH - 100, 10); // top right
EndDrawing();
}
CloseWindow();
return 0;
}