Skip to content

Commit

Permalink
tetris: sorted out flickering and some portability issues
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasbaumann committed Nov 30, 2024
1 parent 412502e commit 3e45209
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions third_party/tetris/tetris.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#include <string.h>
#include "lib/screen.h"

#define WIDTH 12
#define HEIGHT 20
#define WIDTH (uint8_t)12
#define HEIGHT (uint8_t)20

#define W (WIDTH + 2)
#define H (HEIGHT + 1)
#define WXH (W * H)
#define WXH (uint16_t)(W * H)

#define E '.'
#define F '#'
Expand Down Expand Up @@ -56,7 +56,7 @@ uint16_t figure[][4] = {
uint8_t field[WXH];

struct player {
uint8_t figure_index, rot, x, y;
int8_t figure_index, rot, x, y;
} gp;

static void cr()
Expand All @@ -81,8 +81,12 @@ int init()
screen_showcursor(0); // invisible cursor

// Init random generator
srand(42);

// TODO: can we access the zero page from a user program? Or do we
// need a system call to get sort of a time since boot?
// uint16_t seed = (*((uint8_t *)0x004F) << 8) | *((uint8_t *)0x004E) ;
uint16_t seed = 21342;
srand( seed );

// Init field
memset(field, E, WXH-W);

Expand Down Expand Up @@ -112,7 +116,7 @@ int figure_draw(char ch, struct player p)
for (y = 0; y < 4; y++) {
for (x = 0; x < 4; x++) {
if (figure[p.figure_index][p.rot] & mask) {
int offset = (y + p.y) * W + x + p.x;
int16_t offset = (y + p.y) * W + x + p.x;

if (ch == F && field[offset] != E)
return 0;
Expand Down Expand Up @@ -179,7 +183,7 @@ void end(uint16_t score)
cpm_warmboot();
}

void field_print(int score)
void field_print(uint16_t score)
{
uint8_t y, x, sy, sx;

Expand Down Expand Up @@ -212,7 +216,7 @@ int main()
if (draw_next == 1) {
draw_next = 0;

gp.figure_index = rand() % 7;
gp.figure_index = (uint8_t)rand() % 7;
gp.x = W / 2 - 2;
gp.y = 0;
gp.rot = 0;
Expand All @@ -228,7 +232,7 @@ int main()
}

p = gp;
key = screen_getchar(10);
// key = screen_getchar(10);

switch (key) {
case 'w':
Expand Down

0 comments on commit 3e45209

Please sign in to comment.