Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@

## 2026-01-09 - Terminal I/O and Blocking
**Learning:** Standard terminal I/O is line-buffered by default. For real-time games, it's essential to use non-canonical mode (raw mode) to capture keypresses immediately. Also, internal journals should be kept clean if they are to be included in the repo.

## 2024-06-03 - Real-time CLI Feedback
**Learning:** For terminal applications, immediate feedback on user input is crucial for a "tactile" feel. Decoupling the UI update from the main game tick and centralizing it in a `render_ui` function ensures the display always reflects the current state instantly. Using `\r` and space padding allows for smooth, in-place updates without the flicker of clearing the whole screen.
**Action:** Always provide immediate visual confirmation of user actions in CLI tools, even if the primary state update happens on a timer.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: C++ Build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Build
run: make

- name: Test
run: |
# Run a simple check to see if the game can start and quit
(echo "q"; sleep 1) | ./game | grep "Thanks for playing!"
22 changes: 0 additions & 22 deletions .github/workflows/rust.yml

This file was deleted.

53 changes: 42 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
#include <poll.h>
#include <unistd.h>
#include <termios.h>
#include <algorithm>

void render_ui(int score, bool hardMode) {
const char* green = "\033[32m";
const char* red = "\033[31m";
const char* bold = "\033[1m";
const char* reset = "\033[0m";

std::cout << "\r" << "Score: " << green << bold << score << reset;
if (hardMode) {
std::cout << " [" << red << bold << "HARD MODE" << reset << "] ";
} else {
std::cout << " [NORMAL] ";
}
std::cout << std::flush;
}

int main() {
struct termios oldt, newt;
Expand All @@ -18,24 +34,39 @@ int main() {

struct pollfd fds[1] = {{STDIN_FILENO, POLLIN, 0}};
auto last_tick = std::chrono::steady_clock::now();

render_ui(score, hardMode);

while (true) {
int timeout = hardMode ? 100 : 1000;
if (poll(fds, 1, 0) > 0) {
int interval = hardMode ? 100 : 1000;
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_tick).count();
int wait_time = std::max(0, (int)(interval - elapsed));

if (poll(fds, 1, wait_time) > 0) {
if (read(STDIN_FILENO, &input, 1) <= 0 || input == 'q') break;
if (input == 'h') {
hardMode = !hardMode;
std::cout << (hardMode ? "\n[HARD MODE] Speed x10!\n" : "\n[NORMAL MODE]\n");
} else score++;
} else {
score++;
}
render_ui(score, hardMode);
}
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_tick).count();
if (elapsed >= timeout) {
score++; last_tick = now;
std::cout << "Score: " << score << (hardMode ? " [FAST] " : " [NORMAL] ") << "\r" << std::flush;

now = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_tick).count();
if (elapsed >= interval) {
score++;
last_tick = now;
render_ui(score, hardMode);
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
std::cout << "\nFinal Score: " << score << "\nThanks for playing!\n";
std::cout << "\n\n==========================\n"
<< " GAME OVER\n"
<< "==========================\n"
<< "Final Score: " << score << "\n"
<< "Thanks for playing!\n";
return 0;
}