start game

This commit is contained in:
Wolfgang Hottgenroth 2024-03-13 17:35:46 +01:00
parent e5381a8c9d
commit 2b34a3e51d
7 changed files with 121 additions and 33 deletions

View File

@ -11,7 +11,7 @@ CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
$(ARTIFACT).elf: main.o led.o time.o PontCoopScheduler.o displayDriver.o canvas.o shapes.o shape_i.o shape_j.o shape_l.o shape_o.o shape_s.o shape_t.o shape_z.o
$(ARTIFACT).elf: main.o led.o time.o PontCoopScheduler.o displayDriver.o canvas.o shapes.o shape_i.o shape_j.o shape_l.o shape_o.o shape_s.o shape_t.o shape_z.o game.o
$(CC) -o $@ $(LDFLAGS) $^
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt

67
game.c Normal file
View File

@ -0,0 +1,67 @@
#include "stddef.h"
#include "stdint.h"
#include "game.h"
#include "PontCoopScheduler.h"
#include "shapes.h"
#include "canvas.h"
typedef enum { e_idle, e_start, e_newStone, e_down, e_gameOver, e_delay } state_t;
void gameExec(void *handle) {
static state_t state = e_start;
static uint8_t delay;
switch (state) {
case e_idle:
break;
case e_start:
canvasClear();
state = e_newStone;
break;
case e_newStone:
stoneCreate();
if (stoneDraw()) {
state = e_down;
} else {
state = e_gameOver;
}
break;
case e_down:
if (! stoneMoveDown()) {
state = e_newStone;
}
break;
case e_gameOver:
for (uint8_t c = 0; c < canvas.width; c++) {
canvasSetPixel(c, 0, 0x0d);
canvasSetPixel(c, canvas.height-1, 0x0d);
}
for (uint8_t r = 0; r < canvas.height; r++) {
canvasSetPixel(0, r, 0x0d);
canvasSetPixel(canvas.width-1, r, 0x0d);
}
delay = 10;
state = e_delay;
break;
case e_delay:
delay--;
if (delay == 0) {
state = e_start;
}
break;
}
canvasShow();
}
void gameInit() {
schAdd(gameExec, NULL, 0, 1000);
}

7
game.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _GAME_H_
#define _GAME_H_
void gameInit();
#endif // _GAME_H_

4
main.c
View File

@ -8,7 +8,7 @@
#include "led.h"
#include "displayDriver.h"
#include "canvas.h"
#include "shapes.h"
#include "game.h"
int main() {
@ -29,7 +29,7 @@ int main() {
displayDriverInit();
canvasInit();
shapesInit();
gameInit();
__enable_interrupt();

View File

@ -1,26 +1,31 @@
#include "shapes.h"
#include "shape_i.h"
#include "canvas.h"
uint8_t draw_i() {
return 0;
canvasSetPixel(stone.x, stone.y, 0x01);
canvasSetPixel(stone.x, stone.y+1, 0x01);
canvasSetPixel(stone.x, stone.y+2, 0x01);
canvasSetPixel(stone.x, stone.y+3, 0x01);
return 1;
}
uint8_t moveDown_i() {
return 0;
return 1;
}
uint8_t moveLeft_i() {
return 0;
return 1;
}
uint8_t moveRight_i() {
return 0;
return 1;
}
uint8_t rotateLeft_i() {
return 0;
return 1;
}
uint8_t rotateRight_i() {
return 0;
return 1;
}

View File

@ -11,15 +11,6 @@
#include "shape_j.h"
typedef enum { e_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeEnd } shape_t;
typedef enum { e_0, e_90, e_180, e_270 } orientation_t;
typedef struct {
shape_t shape;
orientation_t orientation;
uint8_t x; // column
uint8_t y; // row
} stone_t;
typedef struct {
uint8_t (* draw)();
@ -42,37 +33,38 @@ const stoneOperations_t stoneOperations[] = {
stone_t stone;
uint8_t draw() {
void stoneCreate() {
stone.shape = e_I;
stone.orientation = e_0;
stone.x = 5;
stone.y = 0;
}
// all of them return 1 in case of success and 0 in case of error
uint8_t stoneDraw() {
return stoneOperations[stone.shape].draw();
}
uint8_t moveDown() {
uint8_t stoneMoveDown() {
return stoneOperations[stone.shape].moveDown();
}
uint8_t moveLeft() {
uint8_t stoneMoveLeft() {
return stoneOperations[stone.shape].moveLeft();
}
uint8_t moveRight() {
uint8_t stoneMoveRight() {
return stoneOperations[stone.shape].moveRight();
}
uint8_t rotateLeft() {
uint8_t stoneRotateLeft() {
return stoneOperations[stone.shape].rotateLeft();
}
uint8_t rotateRight() {
uint8_t stoneRotateRight() {
return stoneOperations[stone.shape].rotateRight();
}
void shapesExec(void *handle) {
draw();
}
void shapesInit() {
shapesExec(NULL);
}

View File

@ -1,11 +1,28 @@
#ifndef _SHAPES_H_
#define _SHAPES_H_
#include <stdint.h>
void stoneCreate();
uint8_t stoneDraw();
uint8_t stoneMoveDown();
uint8_t stoneMoveLeft();
uint8_t stoneMoveRight();
uint8_t stoneRotateLeft();
uint8_t stoneRotateRight();
void shapesInit();
void shapesExec(void *handle);
typedef enum { e_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeEnd } shape_t;
typedef enum { e_0, e_90, e_180, e_270 } orientation_t;
typedef struct {
shape_t shape;
orientation_t orientation;
uint8_t x; // column
uint8_t y; // row
} stone_t;
extern stone_t stone;
#endif // _SHAPES_H_