juegos

Juegos de terminal de Pancho: Catan (TUI, GUI, web, servidor, PicoCalc), ajedrez, calculadora y minijuegos
git clone https://git.lu3dhn.xyz/juegos.git
Log | Files | Refs

app.c (3274B)


      1 /* app.c - hola mundo para la PicoCalc: un cuadrado que se mueve con las flechas.
      2  *
      3  * Todo el programa usa solo hal.h (pantalla, teclado, SD) y gfx.h (dibujo), así
      4  * que el mismo código corre en la PicoCalc y en el simulador de la PC.
      5  *
      6  * Idea clave: el estado del juego vive en una struct. paint() solo DIBUJA ese
      7  * estado, nunca lo modifica, porque hal_draw() la llama 10 veces por cuadro
      8  * (una por franja de 32 filas de la pantalla). */
      9 #include "hal.h"
     10 
     11 typedef struct {
     12     int x, y;              /* posición del cuadrado */
     13     int last_key;          /* última tecla, para mostrarla */
     14     int presses;
     15     int battery;
     16     char saved[32];        /* lo que se leyó de la SD al arrancar */
     17 } Game;
     18 
     19 static void paint(Surface *s, void *ctx)
     20 {
     21     const Game *g = ctx;
     22     char buf[64];
     23     int p;
     24 
     25     gfx_rect(s, 0, 0, 320, 320, RGB(10, 20, 50));                  /* fondo */
     26     gfx_text_center(s, &font_bold, 0, 12, 320, "Hola PicoCalc!", C_ACCENT);
     27     gfx_text_center(s, &font_normal, 0, 34, 320, "flechas: mover   Enter: guardar", C_DIM);
     28 
     29     gfx_frame(s, 20, 60, 280, 200, C_DIM);                          /* cancha */
     30     gfx_rect(s, g->x, g->y, 20, 20, C_OK);                          /* jugador */
     31     gfx_disc(s, 160, 160, 6, C_RED);
     32 
     33     p = str_put(buf, sizeof buf, 0, "tecla: ");
     34     if (g->last_key >= 32 && g->last_key < 127) { buf[p++] = (char)g->last_key; buf[p] = 0; }
     35     else p = str_int(buf, sizeof buf, p, g->last_key);
     36     p = str_put(buf, sizeof buf, p, "  veces: ");
     37     str_int(buf, sizeof buf, p, g->presses);
     38     gfx_text(s, &font_normal, 20, 270, buf, C_TEXT);
     39 
     40     p = str_put(buf, sizeof buf, 0, "bateria: ");
     41     str_int(buf, sizeof buf, p, g->battery);
     42     gfx_text(s, &font_normal, 20, 288, buf, C_TEXT);
     43     gfx_text(s, &font_small, 180, 290, g->saved, C_DIM);
     44 }
     45 
     46 void app_main(void)
     47 {
     48     static Game g;           /* static: la pila de la Pico es chica */
     49     hal_init();              /* pantalla, teclado, SD; muestra un log de arranque */
     50 
     51     g.x = 150; g.y = 150; g.last_key = -1;
     52     g.battery = hal_battery();
     53     char buf[32];
     54     int n = hal_store_load(buf, sizeof buf - 1);
     55     buf[n > 0 ? n : 0] = 0;
     56     str_put(g.saved, sizeof g.saved, 0, n > 0 ? buf : "(nada guardado)");
     57 
     58     bool dirty = true;
     59     uint32_t last_bat = hal_ms();
     60     for (;;) {
     61         int k = hal_key();                /* -1 = nada; 0 = "redibujá" */
     62         if (k > 0) {
     63             g.last_key = k;
     64             g.presses++;
     65             if (k == K_LEFT  && g.x > 22)  g.x -= 10;
     66             if (k == K_RIGHT && g.x < 278) g.x += 10;
     67             if (k == K_UP    && g.y > 62)  g.y -= 10;
     68             if (k == K_DOWN  && g.y < 238) g.y += 10;
     69             if (k == K_OK) {              /* guarda "x,y" en la SD */
     70                 int p = str_int(buf, sizeof buf, 0, g.x);
     71                 p = str_put(buf, sizeof buf, p, ",");
     72                 p = str_int(buf, sizeof buf, p, g.y);
     73                 hal_store_save(buf, p);
     74                 str_put(g.saved, sizeof g.saved, 0, "guardado!");
     75             }
     76         }
     77         if (k >= 0) dirty = true;
     78         if (hal_ms() - last_bat > 5000) { g.battery = hal_battery(); last_bat = hal_ms(); dirty = true; }
     79         if (dirty) { hal_draw(paint, &g); dirty = false; }
     80         hal_sleep(10);
     81     }
     82 }