Use shared memory space for game data (#14727)

2.0.x
Marcio Teixeira 5 years ago committed by Scott Lahteine
parent 17abb94532
commit fec52e61ea

@ -28,8 +28,6 @@
#define BRICK_H 5 #define BRICK_H 5
#define BRICK_TOP MENU_FONT_ASCENT #define BRICK_TOP MENU_FONT_ASCENT
#define BRICK_ROWS 4
#define BRICK_COLS 16
#define PADDLE_H 2 #define PADDLE_H 2
#define PADDLE_VEL 3 #define PADDLE_VEL 3
@ -42,51 +40,47 @@
#define BRICK_COL(X) ((X) / (BRICK_W)) #define BRICK_COL(X) ((X) / (BRICK_W))
#define BRICK_ROW(Y) ((Y - (BRICK_TOP)) / (BRICK_H)) #define BRICK_ROW(Y) ((Y - (BRICK_TOP)) / (BRICK_H))
uint8_t balls_left, brick_count; brickout_data_t &bdat = marlin_game_data.brickout;
uint16_t bricks[BRICK_ROWS];
inline void reset_bricks(const uint16_t v) { inline void reset_bricks(const uint16_t v) {
brick_count = (BRICK_COLS) * (BRICK_ROWS); bdat.brick_count = (BRICK_COLS) * (BRICK_ROWS);
LOOP_L_N(i, BRICK_ROWS) bricks[i] = v; LOOP_L_N(i, BRICK_ROWS) bdat.bricks[i] = v;
} }
int8_t paddle_x, hit_dir;
fixed_t ballx, bally, ballh, ballv;
void reset_ball() { void reset_ball() {
constexpr uint8_t ball_dist = 24; constexpr uint8_t ball_dist = 24;
bally = BTOF(PADDLE_Y - ball_dist); bdat.bally = BTOF(PADDLE_Y - ball_dist);
ballv = FTOP(1.3f); bdat.ballv = FTOP(1.3f);
ballh = -FTOP(1.25f); bdat.ballh = -FTOP(1.25f);
uint8_t bx = paddle_x + (PADDLE_W) / 2 + ball_dist; uint8_t bx = bdat.paddle_x + (PADDLE_W) / 2 + ball_dist;
if (bx >= LCD_PIXEL_WIDTH - 10) { bx -= ball_dist * 2; ballh = -ballh; } if (bx >= LCD_PIXEL_WIDTH - 10) { bx -= ball_dist * 2; bdat.ballh = -bdat.ballh; }
ballx = BTOF(bx); bdat.ballx = BTOF(bx);
hit_dir = -1; bdat.hit_dir = -1;
} }
void BrickoutGame::game_screen() { void BrickoutGame::game_screen() {
if (game_frame()) { // Run logic twice for finer resolution if (game_frame()) { // Run logic twice for finer resolution
// Update Paddle Position // Update Paddle Position
paddle_x = constrain(int8_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL)); bdat.paddle_x = constrain(int8_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL));
ui.encoderPosition = paddle_x; ui.encoderPosition = bdat.paddle_x;
paddle_x *= (PADDLE_VEL); bdat.paddle_x *= (PADDLE_VEL);
// Run the ball logic // Run the ball logic
if (game_state) do { if (game_state) do {
// Provisionally update the ball position // Provisionally update the ball position
const fixed_t newx = ballx + ballh, newy = bally + ballv; // current next position const fixed_t newx = bdat.ballx + bdat.ballh, newy = bdat.bally + bdat.ballv; // current next position
if (!WITHIN(newx, 0, BTOF(LCD_PIXEL_WIDTH - 1))) { // out in x? if (!WITHIN(newx, 0, BTOF(LCD_PIXEL_WIDTH - 1))) { // out in x?
ballh = -ballh; _BUZZ(5, 220); // bounce x bdat.ballh = -bdat.ballh; _BUZZ(5, 220); // bounce x
} }
if (newy < 0) { // out in y? if (newy < 0) { // out in y?
ballv = -ballv; _BUZZ(5, 280); // bounce v bdat.ballv = -bdat.ballv; _BUZZ(5, 280); // bounce v
hit_dir = 1; bdat.hit_dir = 1;
} }
// Did the ball go below the bottom? // Did the ball go below the bottom?
else if (newy > BTOF(LCD_PIXEL_HEIGHT)) { else if (newy > BTOF(LCD_PIXEL_HEIGHT)) {
BUZZ(500, 75); BUZZ(500, 75);
if (--balls_left) reset_ball(); else game_state = 0; if (--bdat.balls_left) reset_ball(); else game_state = 0;
break; // done break; // done
} }
@ -94,42 +88,42 @@ void BrickoutGame::game_screen() {
if (WITHIN(newy, BTOF(BRICK_TOP), BTOF(BRICK_BOT))) { if (WITHIN(newy, BTOF(BRICK_TOP), BTOF(BRICK_BOT))) {
const int8_t bit = BRICK_COL(FTOB(newx)), row = BRICK_ROW(FTOB(newy)); const int8_t bit = BRICK_COL(FTOB(newx)), row = BRICK_ROW(FTOB(newy));
const uint16_t mask = _BV(bit); const uint16_t mask = _BV(bit);
if (bricks[row] & mask) { if (bdat.bricks[row] & mask) {
// Yes. Remove it! // Yes. Remove it!
bricks[row] &= ~mask; bdat.bricks[row] &= ~mask;
// Score! // Score!
score += BRICK_ROWS - row; score += BRICK_ROWS - row;
// If bricks are gone, go to reset state // If bricks are gone, go to reset state
if (!--brick_count) game_state = 2; if (!--bdat.brick_count) game_state = 2;
// Bounce the ball cleverly // Bounce the ball cleverly
if ((ballv < 0) == (hit_dir < 0)) { ballv = -ballv; ballh += fixed_t(random(-16, 16)); _BUZZ(5, 880); } if ((bdat.ballv < 0) == (bdat.hit_dir < 0)) { bdat.ballv = -bdat.ballv; bdat.ballh += fixed_t(random(-16, 16)); _BUZZ(5, 880); }
else { ballh = -ballh; ballv += fixed_t(random(-16, 16)); _BUZZ(5, 640); } else { bdat.ballh = -bdat.ballh; bdat.ballv += fixed_t(random(-16, 16)); _BUZZ(5, 640); }
} }
} }
// Is the ball moving down and in paddle range? // Is the ball moving down and in paddle range?
else if (ballv > 0 && WITHIN(newy, BTOF(PADDLE_Y), BTOF(PADDLE_Y + PADDLE_H))) { else if (bdat.ballv > 0 && WITHIN(newy, BTOF(PADDLE_Y), BTOF(PADDLE_Y + PADDLE_H))) {
// Ball actually hitting paddle // Ball actually hitting paddle
const int8_t diff = FTOB(newx) - paddle_x; const int8_t diff = FTOB(newx) - bdat.paddle_x;
if (WITHIN(diff, 0, PADDLE_W - 1)) { if (WITHIN(diff, 0, PADDLE_W - 1)) {
// Reverse Y direction // Reverse Y direction
ballv = -ballv; _BUZZ(3, 880); bdat.ballv = -bdat.ballv; _BUZZ(3, 880);
hit_dir = -1; bdat.hit_dir = -1;
// Near edges affects X velocity // Near edges affects X velocity
const bool is_left_edge = (diff <= 1); const bool is_left_edge = (diff <= 1);
if (is_left_edge || diff >= PADDLE_W-1 - 1) { if (is_left_edge || diff >= PADDLE_W-1 - 1) {
if ((ballh > 0) == is_left_edge) ballh = -ballh; if ((bdat.ballh > 0) == is_left_edge) bdat.ballh = -bdat.ballh;
} }
else if (diff <= 3) { else if (diff <= 3) {
ballh += fixed_t(random(-64, 0)); bdat.ballh += fixed_t(random(-64, 0));
NOLESS(ballh, BTOF(-2)); NOLESS(bdat.ballh, BTOF(-2));
NOMORE(ballh, BTOF(2)); NOMORE(bdat.ballh, BTOF(2));
} }
else if (diff >= PADDLE_W-1 - 3) { else if (diff >= PADDLE_W-1 - 3) {
ballh += fixed_t(random( 0, 64)); bdat.ballh += fixed_t(random( 0, 64));
NOLESS(ballh, BTOF(-2)); NOLESS(bdat.ballh, BTOF(-2));
NOMORE(ballh, BTOF(2)); NOMORE(bdat.ballh, BTOF(2));
} }
// Paddle hit after clearing the board? Reset the board. // Paddle hit after clearing the board? Reset the board.
@ -137,7 +131,7 @@ void BrickoutGame::game_screen() {
} }
} }
ballx += ballh; bally += ballv; // update with new velocity bdat.ballx += bdat.ballh; bdat.bally += bdat.ballv; // update with new velocity
} while (false); } while (false);
} }
@ -150,7 +144,7 @@ void BrickoutGame::game_screen() {
const uint8_t yy = y * BRICK_H + BRICK_TOP; const uint8_t yy = y * BRICK_H + BRICK_TOP;
if (PAGE_CONTAINS(yy, yy + BRICK_H - 1)) { if (PAGE_CONTAINS(yy, yy + BRICK_H - 1)) {
for (uint8_t x = 0; x < BRICK_COLS; ++x) { for (uint8_t x = 0; x < BRICK_COLS; ++x) {
if (TEST(bricks[y], x)) { if (TEST(bdat.bricks[y], x)) {
const uint8_t xx = x * BRICK_W; const uint8_t xx = x * BRICK_W;
for (uint8_t v = 0; v < BRICK_H - 1; ++v) for (uint8_t v = 0; v < BRICK_H - 1; ++v)
if (PAGE_CONTAINS(yy + v, yy + v)) if (PAGE_CONTAINS(yy + v, yy + v))
@ -163,20 +157,20 @@ void BrickoutGame::game_screen() {
// Draw paddle // Draw paddle
if (PAGE_CONTAINS(PADDLE_Y-1, PADDLE_Y)) { if (PAGE_CONTAINS(PADDLE_Y-1, PADDLE_Y)) {
u8g.drawHLine(paddle_x, PADDLE_Y, PADDLE_W); u8g.drawHLine(bdat.paddle_x, PADDLE_Y, PADDLE_W);
#if PADDLE_H > 1 #if PADDLE_H > 1
u8g.drawHLine(paddle_x, PADDLE_Y-1, PADDLE_W); u8g.drawHLine(bdat.paddle_x, PADDLE_Y-1, PADDLE_W);
#if PADDLE_H > 2 #if PADDLE_H > 2
u8g.drawHLine(paddle_x, PADDLE_Y-2, PADDLE_W); u8g.drawHLine(bdat.paddle_x, PADDLE_Y-2, PADDLE_W);
#endif #endif
#endif #endif
} }
// Draw ball while game is running // Draw ball while game is running
if (game_state) { if (game_state) {
const uint8_t by = FTOB(bally); const uint8_t by = FTOB(bdat.bally);
if (PAGE_CONTAINS(by, by+1)) if (PAGE_CONTAINS(by, by+1))
u8g.drawFrame(FTOB(ballx), by, 2, 2); u8g.drawFrame(FTOB(bdat.ballx), by, 2, 2);
} }
// Or draw GAME OVER // Or draw GAME OVER
else else
@ -192,18 +186,20 @@ void BrickoutGame::game_screen() {
// Balls Left // Balls Left
lcd_moveto(LCD_PIXEL_WIDTH - MENU_FONT_WIDTH * 3, MENU_FONT_ASCENT - 1); lcd_moveto(LCD_PIXEL_WIDTH - MENU_FONT_WIDTH * 3, MENU_FONT_ASCENT - 1);
PGM_P const ohs = PSTR("ooo\0\0"); PGM_P const ohs = PSTR("ooo\0\0");
lcd_put_u8str_P(ohs + 3 - balls_left); lcd_put_u8str_P(ohs + 3 - bdat.balls_left);
} }
// A click always exits this game // A click always exits this game
if (ui.use_click()) exit_game(); if (ui.use_click()) exit_game();
} }
#define SCREEN_M ((LCD_PIXEL_WIDTH) / 2)
void BrickoutGame::enter_game() { void BrickoutGame::enter_game() {
init_game(2, game_screen); // 2 = reset bricks on paddle hit init_game(2, game_screen); // 2 = reset bricks on paddle hit
constexpr uint8_t paddle_start = SCREEN_M - (PADDLE_W) / 2; constexpr uint8_t paddle_start = SCREEN_M - (PADDLE_W) / 2;
paddle_x = paddle_start; bdat.paddle_x = paddle_start;
balls_left = 3; bdat.balls_left = 3;
reset_bricks(0x0000); reset_bricks(0x0000);
reset_ball(); reset_ball();
ui.encoderPosition = paddle_start / (PADDLE_VEL); ui.encoderPosition = paddle_start / (PADDLE_VEL);

@ -0,0 +1,38 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "types.h"
#define BRICK_ROWS 4
#define BRICK_COLS 16
typedef struct {
uint8_t balls_left, brick_count;
uint16_t bricks[BRICK_ROWS];
int8_t paddle_x, hit_dir;
fixed_t ballx, bally, ballh, ballv;
} brickout_data_t;
class BrickoutGame : MarlinGame { public: static void enter_game(), game_screen(); };
extern BrickoutGame brickout;

@ -30,6 +30,8 @@ int MarlinGame::score;
uint8_t MarlinGame::game_state; uint8_t MarlinGame::game_state;
millis_t MarlinGame::next_frame; millis_t MarlinGame::next_frame;
MarlinGameData marlin_game_data;
bool MarlinGame::game_frame() { bool MarlinGame::game_frame() {
static int8_t slew; static int8_t slew;
if (ui.first_page) slew = 2; if (ui.first_page) slew = 2;

@ -34,45 +34,37 @@
#define _BUZZ(D,F) BUZZ(D,F) #define _BUZZ(D,F) BUZZ(D,F)
#endif #endif
// Simple 8:8 fixed-point
typedef int16_t fixed_t;
#define FTOP(F) fixed_t((F)*256.0f)
#define PTOF(P) (float(P)*(1.0f/256.0f))
#define BTOF(X) (fixed_t(X)<<8)
#define FTOB(X) int8_t(fixed_t(X)>>8)
#define SCREEN_M ((LCD_PIXEL_WIDTH) / 2)
#if HAS_GAME_MENU #if HAS_GAME_MENU
void menu_game(); void menu_game();
#endif #endif
class MarlinGame {
protected:
static int score;
static uint8_t game_state;
static millis_t next_frame;
static bool game_frame();
static void draw_game_over();
static void exit_game();
public:
static void init_game(const uint8_t init_state, const screenFunc_t screen);
};
#if ENABLED(MARLIN_BRICKOUT) #if ENABLED(MARLIN_BRICKOUT)
class BrickoutGame : MarlinGame { public: static void enter_game(); static void game_screen(); }; #include "brickout.h"
extern BrickoutGame brickout;
#endif #endif
#if ENABLED(MARLIN_INVADERS) #if ENABLED(MARLIN_INVADERS)
class InvadersGame : MarlinGame { public: static void enter_game(); static void game_screen(); }; #include "invaders.h"
extern InvadersGame invaders;
#endif
#if ENABLED(MARLIN_SNAKE)
class SnakeGame : MarlinGame { public: static void enter_game(); static void game_screen(); };
extern SnakeGame snake;
#endif #endif
#if ENABLED(MARLIN_MAZE) #if ENABLED(MARLIN_MAZE)
class MazeGame : MarlinGame { public: static void enter_game(); static void game_screen(); }; #include "maze.h"
extern MazeGame maze; #endif
#if ENABLED(MARLIN_SNAKE)
#include "snake.h"
#endif #endif
// Pool game data to save SRAM
union MarlinGameData {
#if ENABLED(MARLIN_BRICKOUT)
brickout_data_t brickout;
#endif
#if ENABLED(MARLIN_INVADERS)
invaders_data_t invaders;
#endif
#if ENABLED(MARLIN_SNAKE)
snake_data_t snake;
#endif
#if ENABLED(MARLIN_MAZE)
maze_data_t maze;
#endif
};
extern MarlinGameData marlin_game_data;

@ -26,6 +26,28 @@
#include "game.h" #include "game.h"
#define CANNON_W 11
#define CANNON_H 8
#define CANNON_VEL 4
#define CANNON_Y (LCD_PIXEL_HEIGHT - 1 - CANNON_H)
#define INVADER_VEL 3
#define INVADER_TOP MENU_FONT_ASCENT
#define INVADERS_WIDE ((INVADER_COL_W) * (INVADER_COLS))
#define INVADERS_HIGH ((INVADER_ROW_H) * (INVADER_ROWS))
#define UFO_H 5
#define UFO_W 13
#define LASER_H 4
#define SHOT_H 3
#define EXPL_W 11
#define LIFE_W 8
#define LIFE_H 5
#define INVADER_RIGHT ((INVADER_COLS) * (INVADER_COL_W))
// 11x8 // 11x8
const unsigned char invader[3][2][16] PROGMEM = { const unsigned char invader[3][2][16] PROGMEM = {
{ { B00000110,B00000000, { { B00000110,B00000000,
@ -120,20 +142,6 @@ const unsigned char ufo[] PROGMEM = {
B01111111,B11110000 B01111111,B11110000
}; };
#define INVASION_SIZE 3
#if INVASION_SIZE == 3
#define INVADER_COLS 5
#elif INVASION_SIZE == 4
#define INVADER_COLS 6
#else
#define INVADER_COLS 8
#undef INVASION_SIZE
#define INVASION_SIZE 5
#endif
#define INVADER_ROWS INVASION_SIZE
constexpr uint8_t inv_type[] = { constexpr uint8_t inv_type[] = {
#if INVADER_ROWS == 5 #if INVADER_ROWS == 5
0, 1, 1, 2, 2 0, 1, 1, 2, 2
@ -146,107 +154,74 @@ constexpr uint8_t inv_type[] = {
#endif #endif
}; };
#define INVADER_RIGHT ((INVADER_COLS) * (COL_W)) invaders_data_t &idat = marlin_game_data.invaders;
#define CANNON_W 11 #define INV_X_LEFT(C,T) (idat.pos.x + (C) * (INVADER_COL_W) + inv_off[T])
#define CANNON_H 8
#define CANNON_VEL 4
#define CANNON_Y (LCD_PIXEL_HEIGHT - 1 - CANNON_H)
#define COL_W 14
#define INVADER_H 8
#define ROW_H (INVADER_H + 2)
#define INVADER_VEL 3
#define INVADER_TOP MENU_FONT_ASCENT
#define INVADERS_WIDE ((COL_W) * (INVADER_COLS))
#define INVADERS_HIGH ((ROW_H) * (INVADER_ROWS))
#define UFO_H 5
#define UFO_W 13
#define LASER_H 4
#define SHOT_H 3
#define EXPL_W 11
#define LIFE_W 8
#define LIFE_H 5
#define INVADER_COL(X) ((X - invaders_x) / (COL_W))
#define INVADER_ROW(Y) ((Y - invaders_y + 2) / (ROW_H))
#define INV_X_LEFT(C,T) (invaders_x + (C) * (COL_W) + inv_off[T])
#define INV_X_CTR(C,T) (INV_X_LEFT(C,T) + inv_wide[T] / 2) #define INV_X_CTR(C,T) (INV_X_LEFT(C,T) + inv_wide[T] / 2)
#define INV_Y_BOT(R) (invaders_y + (R + 1) * (ROW_H) - 2) #define INV_Y_BOT(R) (idat.pos.y + (R + 1) * (INVADER_ROW_H) - 2)
typedef struct { int8_t x, y, v; } laser_t;
uint8_t cannons_left;
int8_t cannon_x;
laser_t explod, laser, bullet[10];
constexpr uint8_t inv_off[] = { 2, 1, 0 }, inv_wide[] = { 8, 11, 12 }; constexpr uint8_t inv_off[] = { 2, 1, 0 }, inv_wide[] = { 8, 11, 12 };
int8_t invaders_x, invaders_y, invaders_dir, leftmost, rightmost, botmost;
uint8_t invader_count, quit_count, bugs[INVADER_ROWS], shooters[(INVADER_ROWS) * (INVADER_COLS)];
inline void update_invader_data() { inline void update_invader_data() {
uint8_t inv_mask = 0; uint8_t inv_mask = 0;
// Get a list of all active invaders // Get a list of all active invaders
uint8_t sc = 0; uint8_t sc = 0;
LOOP_L_N(y, INVADER_ROWS) { LOOP_L_N(y, INVADER_ROWS) {
uint8_t m = bugs[y]; uint8_t m = idat.bugs[y];
if (m) botmost = y + 1; if (m) idat.botmost = y + 1;
inv_mask |= m; inv_mask |= m;
for (uint8_t x = 0; x < INVADER_COLS; ++x) for (uint8_t x = 0; x < INVADER_COLS; ++x)
if (TEST(m, x)) shooters[sc++] = (y << 4) | x; if (TEST(m, x)) idat.shooters[sc++] = (y << 4) | x;
} }
leftmost = 0; idat.leftmost = 0;
LOOP_L_N(i, INVADER_COLS) { if (TEST(inv_mask, i)) break; leftmost -= COL_W; } LOOP_L_N(i, INVADER_COLS) { if (TEST(inv_mask, i)) break; idat.leftmost -= INVADER_COL_W; }
rightmost = LCD_PIXEL_WIDTH - (INVADERS_WIDE); idat.rightmost = LCD_PIXEL_WIDTH - (INVADERS_WIDE);
for (uint8_t i = INVADER_COLS; i--;) { if (TEST(inv_mask, i)) break; rightmost += COL_W; } for (uint8_t i = INVADER_COLS; i--;) { if (TEST(inv_mask, i)) break; idat.rightmost += INVADER_COL_W; }
if (invader_count == 2) invaders_dir = invaders_dir > 0 ? INVADER_VEL + 1 : -(INVADER_VEL + 1); if (idat.count == 2) idat.dir = idat.dir > 0 ? INVADER_VEL + 1 : -(INVADER_VEL + 1);
} }
inline void reset_bullets() { inline void reset_bullets() {
LOOP_L_N(i, COUNT(bullet)) bullet[i].v = 0; LOOP_L_N(i, COUNT(idat.bullet)) idat.bullet[i].v = 0;
} }
inline void reset_invaders() { inline void reset_invaders() {
invaders_x = 0; invaders_y = INVADER_TOP; idat.pos.x = 0; idat.pos.y = INVADER_TOP;
invaders_dir = INVADER_VEL; idat.dir = INVADER_VEL;
invader_count = (INVADER_COLS) * (INVADER_ROWS); idat.count = (INVADER_COLS) * (INVADER_ROWS);
LOOP_L_N(i, INVADER_ROWS) bugs[i] = _BV(INVADER_COLS) - 1; LOOP_L_N(i, INVADER_ROWS) idat.bugs[i] = _BV(INVADER_COLS) - 1;
update_invader_data(); update_invader_data();
reset_bullets(); reset_bullets();
} }
int8_t ufox, ufov;
inline void spawn_ufo() { inline void spawn_ufo() {
ufov = random(0, 2) ? 1 : -1; idat.ufov = random(0, 2) ? 1 : -1;
ufox = ufov > 0 ? -(UFO_W) : LCD_PIXEL_WIDTH - 1; idat.ufox = idat.ufov > 0 ? -(UFO_W) : LCD_PIXEL_WIDTH - 1;
} }
inline void reset_player() { inline void reset_player() {
cannon_x = 0; idat.cannon_x = 0;
ui.encoderPosition = 0; ui.encoderPosition = 0;
} }
inline void fire_cannon() { inline void fire_cannon() {
laser.x = cannon_x + CANNON_W / 2; idat.laser.x = idat.cannon_x + CANNON_W / 2;
laser.y = LCD_PIXEL_HEIGHT - CANNON_H - (LASER_H); idat.laser.y = LCD_PIXEL_HEIGHT - CANNON_H - (LASER_H);
laser.v = -(LASER_H); idat.laser.v = -(LASER_H);
} }
inline void explode(const int8_t x, const int8_t y, const int8_t v=4) { inline void explode(const int8_t x, const int8_t y, const int8_t v=4) {
explod.x = x - (EXPL_W) / 2; idat.explod.x = x - (EXPL_W) / 2;
explod.y = y; idat.explod.y = y;
explod.v = v; idat.explod.v = v;
} }
inline void kill_cannon(uint8_t &game_state, const uint8_t st) { inline void kill_cannon(uint8_t &game_state, const uint8_t st) {
reset_bullets(); reset_bullets();
explode(cannon_x + (CANNON_W) / 2, CANNON_Y, 6); explode(idat.cannon_x + (CANNON_W) / 2, CANNON_Y, 6);
_BUZZ(1000, 10); _BUZZ(1000, 10);
if (--cannons_left) { if (--idat.cannons_left) {
laser.v = 0; idat.laser.v = 0;
game_state = st; game_state = st;
reset_player(); reset_player();
} }
@ -255,8 +230,6 @@ inline void kill_cannon(uint8_t &game_state, const uint8_t st) {
} }
void InvadersGame::game_screen() { void InvadersGame::game_screen() {
static bool game_blink;
ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Call as often as possible ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Call as often as possible
// Run game logic once per full screen // Run game logic once per full screen
@ -267,46 +240,45 @@ void InvadersGame::game_screen() {
ui.encoderPosition = ep; ui.encoderPosition = ep;
ep *= (CANNON_VEL); ep *= (CANNON_VEL);
if (ep > cannon_x) { cannon_x += CANNON_VEL - 1; if (ep - cannon_x < 2) cannon_x = ep; } if (ep > idat.cannon_x) { idat.cannon_x += CANNON_VEL - 1; if (ep - idat.cannon_x < 2) idat.cannon_x = ep; }
if (ep < cannon_x) { cannon_x -= CANNON_VEL - 1; if (cannon_x - ep < 2) cannon_x = ep; } if (ep < idat.cannon_x) { idat.cannon_x -= CANNON_VEL - 1; if (idat.cannon_x - ep < 2) idat.cannon_x = ep; }
// Run the game logic // Run the game logic
if (game_state) do { if (game_state) do {
// Move the UFO, if any // Move the UFO, if any
if (ufov) { ufox += ufov; if (!WITHIN(ufox, -(UFO_W), LCD_PIXEL_WIDTH - 1)) ufov = 0; } if (idat.ufov) { idat.ufox += idat.ufov; if (!WITHIN(idat.ufox, -(UFO_W), LCD_PIXEL_WIDTH - 1)) idat.ufov = 0; }
if (game_state > 1) { if (--game_state == 2) { reset_invaders(); } else if (game_state == 100) { game_state = 1; } break; } if (game_state > 1) { if (--game_state == 2) { reset_invaders(); } else if (game_state == 100) { game_state = 1; } break; }
static uint8_t blink_count; const bool did_blink = (++idat.blink_count > idat.count >> 1);
const bool did_blink = (++blink_count > invader_count >> 1);
if (did_blink) { if (did_blink) {
game_blink = !game_blink; idat.game_blink = !idat.game_blink;
blink_count = 0; idat.blink_count = 0;
} }
if (invader_count && did_blink) { if (idat.count && did_blink) {
const int8_t newx = invaders_x + invaders_dir; const int8_t newx = idat.pos.x + idat.dir;
if (!WITHIN(newx, leftmost, rightmost)) { // Invaders reached the edge? if (!WITHIN(newx, idat.leftmost, idat.rightmost)) { // Invaders reached the edge?
invaders_dir *= -1; // Invaders change direction idat.dir *= -1; // Invaders change direction
invaders_y += (ROW_H) / 2; // Invaders move down idat.pos.y += (INVADER_ROW_H) / 2; // Invaders move down
invaders_x -= invaders_dir; // ...and only move down this time. idat.pos.x -= idat.dir; // ...and only move down this time.
if (invaders_y + botmost * (ROW_H) - 2 >= CANNON_Y) // Invaders reached the bottom? if (idat.pos.y + idat.botmost * (INVADER_ROW_H) - 2 >= CANNON_Y) // Invaders reached the bottom?
kill_cannon(game_state, 20); // Kill the cannon. Reset invaders. kill_cannon(game_state, 20); // Kill the cannon. Reset invaders.
} }
invaders_x += invaders_dir; // Invaders take one step left/right idat.pos.x += idat.dir; // Invaders take one step left/right
// Randomly shoot if invaders are listed // Randomly shoot if invaders are listed
if (invader_count && !random(0, 20)) { if (idat.count && !random(0, 20)) {
// Find a free bullet // Find a free bullet
laser_t *b = nullptr; laser_t *b = nullptr;
LOOP_L_N(i, COUNT(bullet)) if (!bullet[i].v) { b = &bullet[i]; break; } LOOP_L_N(i, COUNT(idat.bullet)) if (!idat.bullet[i].v) { b = &idat.bullet[i]; break; }
if (b) { if (b) {
// Pick a random shooter and update the bullet // Pick a random shooter and update the bullet
//SERIAL_ECHOLNPGM("free bullet found"); //SERIAL_ECHOLNPGM("free bullet found");
const uint8_t inv = shooters[random(0, invader_count + 1)], col = inv & 0x0F, row = inv >> 4, type = inv_type[row]; const uint8_t inv = idat.shooters[random(0, idat.count + 1)], col = inv & 0x0F, row = inv >> 4, type = inv_type[row];
b->x = INV_X_CTR(col, type); b->x = INV_X_CTR(col, type);
b->y = INV_Y_BOT(row); b->y = INV_Y_BOT(row);
b->v = 2 + random(0, 2); b->v = 2 + random(0, 2);
@ -315,34 +287,34 @@ void InvadersGame::game_screen() {
} }
// Update the laser position // Update the laser position
if (laser.v) { if (idat.laser.v) {
laser.y += laser.v; idat.laser.y += idat.laser.v;
if (laser.y < 0) laser.v = 0; if (idat.laser.y < 0) idat.laser.v = 0;
} }
// Did the laser collide with an invader? // Did the laser collide with an invader?
if (laser.v && WITHIN(laser.y, invaders_y, invaders_y + INVADERS_HIGH - 1)) { if (idat.laser.v && WITHIN(idat.laser.y, idat.pos.y, idat.pos.y + INVADERS_HIGH - 1)) {
const int8_t col = INVADER_COL(laser.x); const int8_t col = idat.laser_col();
if (WITHIN(col, 0, INVADER_COLS - 1)) { if (WITHIN(col, 0, INVADER_COLS - 1)) {
const int8_t row = INVADER_ROW(laser.y); const int8_t row = idat.laser_row();
if (WITHIN(row, 0, INVADER_ROWS - 1)) { if (WITHIN(row, 0, INVADER_ROWS - 1)) {
const uint8_t mask = _BV(col); const uint8_t mask = _BV(col);
if (bugs[row] & mask) { if (idat.bugs[row] & mask) {
const uint8_t type = inv_type[row]; const uint8_t type = inv_type[row];
const int8_t invx = INV_X_LEFT(col, type); const int8_t invx = INV_X_LEFT(col, type);
if (WITHIN(laser.x, invx, invx + inv_wide[type] - 1)) { if (WITHIN(idat.laser.x, invx, invx + inv_wide[type] - 1)) {
// Turn off laser // Turn off laser
laser.v = 0; idat.laser.v = 0;
// Remove the invader! // Remove the invader!
bugs[row] &= ~mask; idat.bugs[row] &= ~mask;
// Score! // Score!
score += INVADER_ROWS - row; score += INVADER_ROWS - row;
// Explode sound! // Explode sound!
_BUZZ(40, 10); _BUZZ(40, 10);
// Explosion bitmap! // Explosion bitmap!
explode(invx + inv_wide[type] / 2, invaders_y + row * (ROW_H)); explode(invx + inv_wide[type] / 2, idat.pos.y + row * (INVADER_ROW_H));
// If invaders are gone, go to reset invaders state // If invaders are gone, go to reset invaders state
if (--invader_count) update_invader_data(); else { game_state = 20; reset_bullets(); } if (--idat.count) update_invader_data(); else { game_state = 20; reset_bullets(); }
} // laser x hit } // laser x hit
} // invader exists } // invader exists
} // good row } // good row
@ -350,31 +322,31 @@ void InvadersGame::game_screen() {
} // laser in invader zone } // laser in invader zone
// Handle alien bullets // Handle alien bullets
LOOP_L_N(s, COUNT(bullet)) { LOOP_L_N(s, COUNT(idat.bullet)) {
laser_t *b = &bullet[s]; laser_t *b = &idat.bullet[s];
if (b->v) { if (b->v) {
// Update alien bullet position // Update alien bullet position
b->y += b->v; b->y += b->v;
if (b->y >= LCD_PIXEL_HEIGHT) if (b->y >= LCD_PIXEL_HEIGHT)
b->v = 0; // Offscreen b->v = 0; // Offscreen
else if (b->y >= CANNON_Y && WITHIN(b->x, cannon_x, cannon_x + CANNON_W - 1)) else if (b->y >= CANNON_Y && WITHIN(b->x, idat.cannon_x, idat.cannon_x + CANNON_W - 1))
kill_cannon(game_state, 120); // Hit the cannon kill_cannon(game_state, 120); // Hit the cannon
} }
} }
// Randomly spawn a UFO // Randomly spawn a UFO
if (!ufov && !random(0,500)) spawn_ufo(); if (!idat.ufov && !random(0,500)) spawn_ufo();
// Did the laser hit a ufo? // Did the laser hit a ufo?
if (laser.v && ufov && laser.y < UFO_H + 2 && WITHIN(laser.x, ufox, ufox + UFO_W - 1)) { if (idat.laser.v && idat.ufov && idat.laser.y < UFO_H + 2 && WITHIN(idat.laser.x, idat.ufox, idat.ufox + UFO_W - 1)) {
// Turn off laser and UFO // Turn off laser and UFO
laser.v = ufov = 0; idat.laser.v = idat.ufov = 0;
// Score! // Score!
score += 10; score += 10;
// Explode! // Explode!
_BUZZ(40, 10); _BUZZ(40, 10);
// Explosion bitmap // Explosion bitmap
explode(ufox + (UFO_W) / 2, 1); explode(idat.ufox + (UFO_W) / 2, 1);
} }
} while (false); } while (false);
@ -382,59 +354,59 @@ void InvadersGame::game_screen() {
} }
// Click-and-hold to abort // Click-and-hold to abort
if (ui.button_pressed()) --quit_count; else quit_count = 10; if (ui.button_pressed()) --idat.quit_count; else idat.quit_count = 10;
// Click to fire or exit // Click to fire or exit
if (ui.use_click()) { if (ui.use_click()) {
if (!game_state) if (!game_state)
quit_count = 0; idat.quit_count = 0;
else if (game_state == 1 && !laser.v) else if (game_state == 1 && !idat.laser.v)
fire_cannon(); fire_cannon();
} }
if (!quit_count) exit_game(); if (!idat.quit_count) exit_game();
u8g.setColorIndex(1); u8g.setColorIndex(1);
// Draw invaders // Draw invaders
if (PAGE_CONTAINS(invaders_y, invaders_y + botmost * (ROW_H) - 2 - 1)) { if (PAGE_CONTAINS(idat.pos.y, idat.pos.y + idat.botmost * (INVADER_ROW_H) - 2 - 1)) {
int8_t yy = invaders_y; int8_t yy = idat.pos.y;
for (uint8_t y = 0; y < INVADER_ROWS; ++y) { for (uint8_t y = 0; y < INVADER_ROWS; ++y) {
const uint8_t type = inv_type[y]; const uint8_t type = inv_type[y];
if (PAGE_CONTAINS(yy, yy + INVADER_H - 1)) { if (PAGE_CONTAINS(yy, yy + INVADER_H - 1)) {
int8_t xx = invaders_x; int8_t xx = idat.pos.x;
for (uint8_t x = 0; x < INVADER_COLS; ++x) { for (uint8_t x = 0; x < INVADER_COLS; ++x) {
if (TEST(bugs[y], x)) if (TEST(idat.bugs[y], x))
u8g.drawBitmapP(xx, yy, 2, INVADER_H, invader[type][game_blink]); u8g.drawBitmapP(xx, yy, 2, INVADER_H, invader[type][idat.game_blink]);
xx += COL_W; xx += INVADER_COL_W;
} }
} }
yy += ROW_H; yy += INVADER_ROW_H;
} }
} }
// Draw UFO // Draw UFO
if (ufov && PAGE_UNDER(UFO_H + 2)) if (idat.ufov && PAGE_UNDER(UFO_H + 2))
u8g.drawBitmapP(ufox, 2, 2, UFO_H, ufo); u8g.drawBitmapP(idat.ufox, 2, 2, UFO_H, ufo);
// Draw cannon // Draw cannon
if (game_state && PAGE_CONTAINS(CANNON_Y, CANNON_Y + CANNON_H - 1) && (game_state < 2 || (game_state & 0x02))) if (game_state && PAGE_CONTAINS(CANNON_Y, CANNON_Y + CANNON_H - 1) && (game_state < 2 || (game_state & 0x02)))
u8g.drawBitmapP(cannon_x, CANNON_Y, 2, CANNON_H, cannon); u8g.drawBitmapP(idat.cannon_x, CANNON_Y, 2, CANNON_H, cannon);
// Draw laser // Draw laser
if (laser.v && PAGE_CONTAINS(laser.y, laser.y + LASER_H - 1)) if (idat.laser.v && PAGE_CONTAINS(idat.laser.y, idat.laser.y + LASER_H - 1))
u8g.drawVLine(laser.x, laser.y, LASER_H); u8g.drawVLine(idat.laser.x, idat.laser.y, LASER_H);
// Draw invader bullets // Draw invader bullets
LOOP_L_N (i, COUNT(bullet)) { LOOP_L_N (i, COUNT(idat.bullet)) {
if (bullet[i].v && PAGE_CONTAINS(bullet[i].y - (SHOT_H - 1), bullet[i].y)) if (idat.bullet[i].v && PAGE_CONTAINS(idat.bullet[i].y - (SHOT_H - 1), idat.bullet[i].y))
u8g.drawVLine(bullet[i].x, bullet[i].y - (SHOT_H - 1), SHOT_H); u8g.drawVLine(idat.bullet[i].x, idat.bullet[i].y - (SHOT_H - 1), SHOT_H);
} }
// Draw explosion // Draw explosion
if (explod.v && PAGE_CONTAINS(explod.y, explod.y + 7 - 1)) { if (idat.explod.v && PAGE_CONTAINS(idat.explod.y, idat.explod.y + 7 - 1)) {
u8g.drawBitmapP(explod.x, explod.y, 2, 7, explosion); u8g.drawBitmapP(idat.explod.x, idat.explod.y, 2, 7, explosion);
--explod.v; --idat.explod.v;
} }
// Blink GAME OVER when game is over // Blink GAME OVER when game is over
@ -448,8 +420,8 @@ void InvadersGame::game_screen() {
lcd_put_int(score); lcd_put_int(score);
// Draw lives // Draw lives
if (cannons_left) if (idat.cannons_left)
for (uint8_t i = 1; i <= cannons_left; ++i) for (uint8_t i = 1; i <= idat.cannons_left; ++i)
u8g.drawBitmapP(LCD_PIXEL_WIDTH - i * (LIFE_W), 6 - (LIFE_H), 1, LIFE_H, life); u8g.drawBitmapP(LCD_PIXEL_WIDTH - i * (LIFE_W), 6 - (LIFE_H), 1, LIFE_H, life);
} }
@ -457,9 +429,9 @@ void InvadersGame::game_screen() {
void InvadersGame::enter_game() { void InvadersGame::enter_game() {
init_game(20, game_screen); // countdown to reset invaders init_game(20, game_screen); // countdown to reset invaders
cannons_left = 3; idat.cannons_left = 3;
quit_count = 10; idat.quit_count = 10;
laser.v = 0; idat.laser.v = 0;
reset_invaders(); reset_invaders();
reset_player(); reset_player();
} }

@ -0,0 +1,62 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "types.h"
#define INVASION_SIZE 3
#if INVASION_SIZE == 3
#define INVADER_COLS 5
#elif INVASION_SIZE == 4
#define INVADER_COLS 6
#else
#define INVADER_COLS 8
#undef INVASION_SIZE
#define INVASION_SIZE 5
#endif
#define INVADER_ROWS INVASION_SIZE
#define INVADER_COL_W 14
#define INVADER_H 8
#define INVADER_ROW_H (INVADER_H + 2)
typedef struct { int8_t x, y, v; } laser_t;
typedef struct {
pos_t pos;
uint8_t cannons_left;
int8_t cannon_x;
laser_t bullet[10], laser, explod;
int8_t dir, leftmost, rightmost, botmost;
uint8_t count, quit_count, blink_count;
uint8_t bugs[INVADER_ROWS], shooters[(INVADER_ROWS) * (INVADER_COLS)];
int8_t ufox, ufov;
bool game_blink;
int8_t laser_col() { return ((laser.x - pos.x) / (INVADER_COL_W)); };
int8_t laser_row() { return ((laser.y - pos.y + 2) / (INVADER_ROW_H)); };
} invaders_data_t;
class InvadersGame : MarlinGame { public: static void enter_game(), game_screen(); };
extern InvadersGame invaders;

@ -0,0 +1,30 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "types.h"
typedef struct { pos_t pos; } maze_data_t;
class MazeGame : MarlinGame { public: static void enter_game(), game_screen(); };
extern MazeGame maze;

@ -65,20 +65,12 @@
constexpr fixed_t snakev = FTOP(0.20); constexpr fixed_t snakev = FTOP(0.20);
int8_t snake_dir, // NESW snake_data_t &sdat = marlin_game_data.snake;
foodx, foody, food_cnt,
old_encoder;
fixed_t snakex, snakey;
// Up to 50 lines, then you win!
typedef struct { int8_t x, y; } pos_t;
uint8_t head_ind;
pos_t snake_tail[50];
// Remove the first pixel from the tail. // Remove the first pixel from the tail.
// If needed, shift out the first segment. // If needed, shift out the first segment.
void shorten_tail() { void shorten_tail() {
pos_t &p = snake_tail[0], &q = snake_tail[1]; pos_t &p = sdat.snake_tail[0], &q = sdat.snake_tail[1];
bool shift = false; bool shift = false;
if (p.x == q.x) { if (p.x == q.x) {
// Vertical line // Vertical line
@ -91,21 +83,21 @@ void shorten_tail() {
shift = p.x == q.x; shift = p.x == q.x;
} }
if (shift) { if (shift) {
head_ind--; sdat.head_ind--;
for (uint8_t i = 0; i <= head_ind; ++i) for (uint8_t i = 0; i <= sdat.head_ind; ++i)
snake_tail[i] = snake_tail[i + 1]; sdat.snake_tail[i] = sdat.snake_tail[i + 1];
} }
} }
// The food is on a line // The food is on a line
inline bool food_on_line() { inline bool food_on_line() {
for (uint8_t n = 0; n < head_ind; ++n) { for (uint8_t n = 0; n < sdat.head_ind; ++n) {
pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) { if (p.x == q.x) {
if ((foodx == p.x - 1 || foodx == p.x) && WITHIN(foody, _MIN(p.y, q.y), _MAX(p.y, q.y))) if ((sdat.foodx == p.x - 1 || sdat.foodx == p.x) && WITHIN(sdat.foody, _MIN(p.y, q.y), _MAX(p.y, q.y)))
return true; return true;
} }
else if ((foody == p.y - 1 || foody == p.y) && WITHIN(foodx, _MIN(p.x, q.x), _MAX(p.x, q.x))) else if ((sdat.foody == p.y - 1 || sdat.foody == p.y) && WITHIN(sdat.foodx, _MIN(p.x, q.x), _MAX(p.x, q.x)))
return true; return true;
} }
return false; return false;
@ -114,54 +106,54 @@ inline bool food_on_line() {
// Add a new food blob // Add a new food blob
void food_reset() { void food_reset() {
do { do {
foodx = random(0, GAME_W); sdat.foodx = random(0, GAME_W);
foody = random(0, GAME_H); sdat.foody = random(0, GAME_H);
} while (food_on_line()); } while (food_on_line());
} }
// Turn the snake cw or ccw // Turn the snake cw or ccw
inline void turn_snake(const bool cw) { inline void turn_snake(const bool cw) {
snake_dir += cw ? 1 : -1; sdat.snake_dir += cw ? 1 : -1;
snake_dir &= 0x03; sdat.snake_dir &= 0x03;
head_ind++; sdat.head_ind++;
snake_tail[head_ind].x = FTOB(snakex); sdat.snake_tail[sdat.head_ind].x = FTOB(sdat.snakex);
snake_tail[head_ind].y = FTOB(snakey); sdat.snake_tail[sdat.head_ind].y = FTOB(sdat.snakey);
} }
// Reset the snake for a new game // Reset the snake for a new game
void snake_reset() { void snake_reset() {
// Init the head and velocity // Init the head and velocity
snakex = BTOF(1); sdat.snakex = BTOF(1);
snakey = BTOF(GAME_H / 2); sdat.snakey = BTOF(GAME_H / 2);
//snakev = FTOP(0.25); //snakev = FTOP(0.25);
// Init the tail with a cw turn // Init the tail with a cw turn
snake_dir = 0; sdat.snake_dir = 0;
head_ind = 0; sdat.head_ind = 0;
snake_tail[0].x = 0; sdat.snake_tail[0].x = 0;
snake_tail[0].y = GAME_H / 2; sdat.snake_tail[0].y = GAME_H / 2;
turn_snake(true); turn_snake(true);
// Clear food flag // Clear food flag
food_cnt = 5; sdat.food_cnt = 5;
// Clear the controls // Clear the controls
ui.encoderPosition = 0; ui.encoderPosition = 0;
old_encoder = 0; sdat.old_encoder = 0;
} }
// Check if head segment overlaps another // Check if head segment overlaps another
bool snake_overlap() { bool snake_overlap() {
// 4 lines must exist before a collision is possible // 4 lines must exist before a collision is possible
if (head_ind < 4) return false; if (sdat.head_ind < 4) return false;
// Is the last segment crossing any others? // Is the last segment crossing any others?
const pos_t &h1 = snake_tail[head_ind - 1], &h2 = snake_tail[head_ind]; const pos_t &h1 = sdat.snake_tail[sdat.head_ind - 1], &h2 = sdat.snake_tail[sdat.head_ind];
// VERTICAL head segment? // VERTICAL head segment?
if (h1.x == h2.x) { if (h1.x == h2.x) {
// Loop from oldest to segment two away from head // Loop from oldest to segment two away from head
for (uint8_t n = 0; n < head_ind - 2; ++n) { for (uint8_t n = 0; n < sdat.head_ind - 2; ++n) {
// Segment p to q // Segment p to q
const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x != q.x) { if (p.x != q.x) {
// Crossing horizontal segment // Crossing horizontal segment
if (WITHIN(h1.x, _MIN(p.x, q.x), _MAX(p.x, q.x)) && (h1.y <= p.y) == (h2.y >= p.y)) return true; if (WITHIN(h1.x, _MIN(p.x, q.x), _MAX(p.x, q.x)) && (h1.y <= p.y) == (h2.y >= p.y)) return true;
@ -171,9 +163,9 @@ bool snake_overlap() {
} }
else { else {
// Loop from oldest to segment two away from head // Loop from oldest to segment two away from head
for (uint8_t n = 0; n < head_ind - 2; ++n) { for (uint8_t n = 0; n < sdat.head_ind - 2; ++n) {
// Segment p to q // Segment p to q
const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.y != q.y) { if (p.y != q.y) {
// Crossing vertical segment // Crossing vertical segment
if (WITHIN(h1.y, _MIN(p.y, q.y), _MAX(p.y, q.y)) && (h1.x <= p.x) == (h2.x >= p.x)) return true; if (WITHIN(h1.y, _MIN(p.y, q.y), _MAX(p.y, q.y)) && (h1.x <= p.x) == (h2.x >= p.x)) return true;
@ -189,14 +181,14 @@ void SnakeGame::game_screen() {
if (game_frame()) do { // Run logic twice for finer resolution if (game_frame()) do { // Run logic twice for finer resolution
// Move the snake's head one unit in the current direction // Move the snake's head one unit in the current direction
const int8_t oldx = FTOB(snakex), oldy = FTOB(snakey); const int8_t oldx = FTOB(sdat.snakex), oldy = FTOB(sdat.snakey);
switch (snake_dir) { switch (sdat.snake_dir) {
case 0: snakey -= snakev; break; case 0: sdat.snakey -= snakev; break;
case 1: snakex += snakev; break; case 1: sdat.snakex += snakev; break;
case 2: snakey += snakev; break; case 2: sdat.snakey += snakev; break;
case 3: snakex -= snakev; break; case 3: sdat.snakex -= snakev; break;
} }
const int8_t x = FTOB(snakex), y = FTOB(snakey); const int8_t x = FTOB(sdat.snakex), y = FTOB(sdat.snakey);
// If movement took place... // If movement took place...
if (oldx != x || oldy != y) { if (oldx != x || oldy != y) {
@ -207,17 +199,17 @@ void SnakeGame::game_screen() {
break; // ...out of do-while break; // ...out of do-while
} }
snake_tail[head_ind].x = x; sdat.snake_tail[sdat.head_ind].x = x;
snake_tail[head_ind].y = y; sdat.snake_tail[sdat.head_ind].y = y;
// Change snake direction if set // Change snake direction if set
const int8_t enc = int8_t(ui.encoderPosition), diff = enc - old_encoder; const int8_t enc = int8_t(ui.encoderPosition), diff = enc - sdat.old_encoder;
if (diff) { if (diff) {
old_encoder = enc; sdat.old_encoder = enc;
turn_snake(diff > 0); turn_snake(diff > 0);
} }
if (food_cnt) --food_cnt; else shorten_tail(); if (sdat.food_cnt) --sdat.food_cnt; else shorten_tail();
// Did the snake collide with itself or go out of bounds? // Did the snake collide with itself or go out of bounds?
if (snake_overlap()) { if (snake_overlap()) {
@ -225,11 +217,11 @@ void SnakeGame::game_screen() {
_BUZZ(400, 40); // Bzzzt! _BUZZ(400, 40); // Bzzzt!
} }
// Is the snake at the food? // Is the snake at the food?
else if (x == foodx && y == foody) { else if (x == sdat.foodx && y == sdat.foody) {
_BUZZ(5, 220); _BUZZ(5, 220);
_BUZZ(5, 280); _BUZZ(5, 280);
score++; score++;
food_cnt = 2; sdat.food_cnt = 2;
food_reset(); food_reset();
} }
} }
@ -251,8 +243,8 @@ void SnakeGame::game_screen() {
#if SNAKE_WH < 2 #if SNAKE_WH < 2
// At this scale just draw a line // At this scale just draw a line
for (uint8_t n = 0; n < head_ind; ++n) { for (uint8_t n = 0; n < sdat.head_ind; ++n) {
const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) { if (p.x == q.x) {
const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y)); const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
if (PAGE_CONTAINS(y1, y2)) if (PAGE_CONTAINS(y1, y2))
@ -267,8 +259,8 @@ void SnakeGame::game_screen() {
#elif SNAKE_WH == 2 #elif SNAKE_WH == 2
// At this scale draw two lines // At this scale draw two lines
for (uint8_t n = 0; n < head_ind; ++n) { for (uint8_t n = 0; n < sdat.head_ind; ++n) {
const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) { if (p.x == q.x) {
const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y)); const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
if (PAGE_CONTAINS(y1, y2 + 1)) if (PAGE_CONTAINS(y1, y2 + 1))
@ -286,8 +278,8 @@ void SnakeGame::game_screen() {
#else #else
// Draw a series of boxes // Draw a series of boxes
for (uint8_t n = 0; n < head_ind; ++n) { for (uint8_t n = 0; n < sdat.head_ind; ++n) {
const pos_t &p = snake_tail[n], &q = snake_tail[n + 1]; const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1];
if (p.x == q.x) { if (p.x == q.x) {
const int8_t y1 = _MIN(p.y, q.y), y2 = _MAX(p.y, q.y); const int8_t y1 = _MIN(p.y, q.y), y2 = _MAX(p.y, q.y);
if (PAGE_CONTAINS(GAMEY(y1), GAMEY(y2) + SNAKE_SIZ - 1)) { if (PAGE_CONTAINS(GAMEY(y1), GAMEY(y2) + SNAKE_SIZ - 1)) {
@ -311,9 +303,9 @@ void SnakeGame::game_screen() {
#endif #endif
// Draw food // Draw food
const int8_t fy = GAMEY(foody); const int8_t fy = GAMEY(sdat.foody);
if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) { if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) {
const int8_t fx = GAMEX(foodx); const int8_t fx = GAMEX(sdat.foodx);
u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH); u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH);
if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2); if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2);
} }

@ -0,0 +1,38 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "types.h"
typedef struct {
int8_t snake_dir, // NESW
foodx, foody,
food_cnt,
old_encoder;
pos_t snake_tail[50];
fixed_t snakex, snakey;
uint8_t head_ind;
} snake_data_t;
class SnakeGame : MarlinGame { public: static void enter_game(), game_screen(); };
extern SnakeGame snake;

@ -0,0 +1,46 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include <stdint.h>
typedef struct { int8_t x, y; } pos_t;
// Simple 8:8 fixed-point
typedef int16_t fixed_t;
#define FTOP(F) fixed_t((F)*256.0f)
#define PTOF(P) (float(P)*(1.0f/256.0f))
#define BTOF(X) (fixed_t(X)<<8)
#define FTOB(X) int8_t(fixed_t(X)>>8)
class MarlinGame {
protected:
static int score;
static uint8_t game_state;
static millis_t next_frame;
static bool game_frame();
static void draw_game_over();
static void exit_game();
public:
static void init_game(const uint8_t init_state, const screenFunc_t screen);
};

@ -319,8 +319,11 @@ opt_set Z_DRIVER_TYPE TMC2208
opt_set E0_DRIVER_TYPE TMC2130 opt_set E0_DRIVER_TYPE TMC2130
opt_set X_MIN_ENDSTOP_INVERTING true opt_set X_MIN_ENDSTOP_INVERTING true
opt_set Y_MIN_ENDSTOP_INVERTING true opt_set Y_MIN_ENDSTOP_INVERTING true
opt_enable REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER MONITOR_DRIVER_STATUS STEALTHCHOP_XY STEALTHCHOP_Z STEALTHCHOP_E HYBRID_THRESHOLD USE_ZMIN_PLUG SENSORLESS_HOMING TMC_DEBUG opt_enable REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER \
exec_test $1 $2 "Mixed TMC configuration" MARLIN_BRICKOUT MARLIN_INVADERS MARLIN_SNAKE \
MONITOR_DRIVER_STATUS STEALTHCHOP_XY STEALTHCHOP_Z STEALTHCHOP_E HYBRID_THRESHOLD \
USE_ZMIN_PLUG SENSORLESS_HOMING TMC_DEBUG
exec_test $1 $2 "Mixed TMC configuration, with games!"
# #
# tvrrug Config need to check board type for sanguino atmega644p # tvrrug Config need to check board type for sanguino atmega644p

Loading…
Cancel
Save