commit bfdf3ad573dd3e19c7a6ee6542841a78b915e018 Author: talksik Date: Sun Sep 27 10:29:40 2026 -0700 implement basic game diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2428d9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Emacs backup files (e.g., filename.txt~) +*~ + +# Emacs auto-save files (e.g., #filename.txt#) +\#*\# + +# Emacs lock files / crash recovery links (e.g., .#filename.txt) +.\#* + +*.out \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f27faab --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Typing game +Simple game to type words from a default word bank. + +This is a demonstration of using `raylib` to create a simple rendering loop, handle input events, and manage game state. + +![Screenshot](screenshot.png) \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..4a7665f --- /dev/null +++ b/main.c @@ -0,0 +1,94 @@ +#include +#include +#include + +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; +} diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..0f3e167 Binary files /dev/null and b/screenshot.png differ