implement basic game
This commit is contained in:
+10
@@ -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
|
||||
@@ -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.
|
||||
|
||||

|
||||
@@ -0,0 +1,94 @@
|
||||
#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;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 5.6 KiB |
Reference in New Issue
Block a user