Files
typinggame/main.c
T
2026-09-27 10:29:40 -07:00

95 lines
2.0 KiB
C

#include <raylib.h>
#include <stdio.h>
#include <string.h>
char *word_bank[] = {
"hypothesis",
"science",
"usurp",
"conditional",
"passionate",
"orion",
NULL // sentinel value marking end
};
#define WINDOW_HEIGHT 500
#define WINDOW_WIDTH 700
#define CHAR_SPACING 40
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
int main(int argc, char *argv[]) {
char **curr_word = word_bank;
unsigned short curr_letter_index = 0;
unsigned short wins = 0;
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Typing game");
while (!WindowShouldClose()) {
char buf[2] = {'c', '\0'};
size_t word_len = strlen(*curr_word);
int k = 0;
const char *key_name;
// process queued key events
while ((k = GetKeyPressed()) != 0) {
key_name = GetKeyName(k);
char c = (*curr_word)[curr_letter_index];
sprintf(buf, "%c\0", c);
if (key_name != NULL &&
strcmp(key_name, buf) == 0) {
if (curr_letter_index < word_len - 1) { // next letter
curr_letter_index++;
} else { // next word
if (*(curr_word + 1) == NULL) { // wrap around
curr_word = word_bank;
} else {
curr_word++;
wins++;
}
curr_letter_index = 0;
}
}
}
// render
BeginDrawing();
ClearBackground(WHITE);
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 = GRAY;
} else {
color = DARKGRAY;
}
DrawText(buf, x, y, 50, color);
x += CHAR_SPACING;
}
DrawFPS(WINDOW_WIDTH - 100, 10); // top right
char wins_text[20];
snprintf(wins_text, 20, "%d wins", wins);
DrawText(wins_text, WINDOW_WIDTH - 100, WINDOW_HEIGHT - 20, 20, GRAY);
EndDrawing();
}
CloseWindow();
return 0;
}