From e8701195e66f2d27ffe17fb514eae8173795aaf7 Mon Sep 17 00:00:00 2001 From: Georgiy Bondarenko <69736697+nehilo@users.noreply.github.com> Date: Thu, 4 Mar 2021 22:54:23 +0500 Subject: Initial commit --- Marlin/src/lcd/menu/game/brickout.cpp | 207 ++++++++++++++++ Marlin/src/lcd/menu/game/brickout.h | 38 +++ Marlin/src/lcd/menu/game/game.cpp | 66 +++++ Marlin/src/lcd/menu/game/game.h | 62 +++++ Marlin/src/lcd/menu/game/invaders.cpp | 438 ++++++++++++++++++++++++++++++++++ Marlin/src/lcd/menu/game/invaders.h | 62 +++++ Marlin/src/lcd/menu/game/maze.cpp | 134 +++++++++++ Marlin/src/lcd/menu/game/maze.h | 30 +++ Marlin/src/lcd/menu/game/snake.cpp | 323 +++++++++++++++++++++++++ Marlin/src/lcd/menu/game/snake.h | 38 +++ Marlin/src/lcd/menu/game/types.h | 46 ++++ 11 files changed, 1444 insertions(+) create mode 100644 Marlin/src/lcd/menu/game/brickout.cpp create mode 100644 Marlin/src/lcd/menu/game/brickout.h create mode 100644 Marlin/src/lcd/menu/game/game.cpp create mode 100644 Marlin/src/lcd/menu/game/game.h create mode 100644 Marlin/src/lcd/menu/game/invaders.cpp create mode 100644 Marlin/src/lcd/menu/game/invaders.h create mode 100644 Marlin/src/lcd/menu/game/maze.cpp create mode 100644 Marlin/src/lcd/menu/game/maze.h create mode 100644 Marlin/src/lcd/menu/game/snake.cpp create mode 100644 Marlin/src/lcd/menu/game/snake.h create mode 100644 Marlin/src/lcd/menu/game/types.h (limited to 'Marlin/src/lcd/menu/game') diff --git a/Marlin/src/lcd/menu/game/brickout.cpp b/Marlin/src/lcd/menu/game/brickout.cpp new file mode 100644 index 0000000..4bdc924 --- /dev/null +++ b/Marlin/src/lcd/menu/game/brickout.cpp @@ -0,0 +1,207 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_BRICKOUT) + +#include "game.h" + +#define BRICK_H 5 +#define BRICK_TOP MENU_FONT_ASCENT + +#define PADDLE_H 2 +#define PADDLE_VEL 3 +#define PADDLE_W ((LCD_PIXEL_WIDTH) / 8) +#define PADDLE_Y (LCD_PIXEL_HEIGHT - 1 - PADDLE_H) + +#define BRICK_W ((LCD_PIXEL_WIDTH) / (BRICK_COLS)) +#define BRICK_BOT (BRICK_TOP + BRICK_H * BRICK_ROWS - 1) + +#define BRICK_COL(X) ((X) / (BRICK_W)) +#define BRICK_ROW(Y) ((Y - (BRICK_TOP)) / (BRICK_H)) + +brickout_data_t &bdat = marlin_game_data.brickout; + +inline void reset_bricks(const uint16_t v) { + bdat.brick_count = (BRICK_COLS) * (BRICK_ROWS); + LOOP_L_N(i, BRICK_ROWS) bdat.bricks[i] = v; +} + +void reset_ball() { + constexpr uint8_t ball_dist = 24; + bdat.bally = BTOF(PADDLE_Y - ball_dist); + bdat.ballv = FTOP(1.3f); + bdat.ballh = -FTOP(1.25f); + uint8_t bx = bdat.paddle_x + (PADDLE_W) / 2 + ball_dist; + if (bx >= LCD_PIXEL_WIDTH - 10) { bx -= ball_dist * 2; bdat.ballh = -bdat.ballh; } + bdat.ballx = BTOF(bx); + bdat.hit_dir = -1; +} + +void BrickoutGame::game_screen() { + if (game_frame()) { // Run logic twice for finer resolution + // Update Paddle Position + bdat.paddle_x = constrain(int8_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (PADDLE_W)) / (PADDLE_VEL)); + ui.encoderPosition = bdat.paddle_x; + bdat.paddle_x *= (PADDLE_VEL); + + // Run the ball logic + if (game_state) do { + + // Provisionally update the ball 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? + bdat.ballh = -bdat.ballh; _BUZZ(5, 220); // bounce x + } + if (newy < 0) { // out in y? + bdat.ballv = -bdat.ballv; _BUZZ(5, 280); // bounce v + bdat.hit_dir = 1; + } + // Did the ball go below the bottom? + else if (newy > BTOF(LCD_PIXEL_HEIGHT)) { + _BUZZ(500, 75); + if (--bdat.balls_left) reset_ball(); else game_state = 0; + break; // done + } + + // Is the ball colliding with a brick? + if (WITHIN(newy, BTOF(BRICK_TOP), BTOF(BRICK_BOT))) { + const int8_t bit = BRICK_COL(FTOB(newx)), row = BRICK_ROW(FTOB(newy)); + const uint16_t mask = _BV(bit); + if (bdat.bricks[row] & mask) { + // Yes. Remove it! + bdat.bricks[row] &= ~mask; + // Score! + score += BRICK_ROWS - row; + // If bricks are gone, go to reset state + if (!--bdat.brick_count) game_state = 2; + // Bounce the ball cleverly + if ((bdat.ballv < 0) == (bdat.hit_dir < 0)) { bdat.ballv = -bdat.ballv; bdat.ballh += fixed_t(random(-16, 16)); _BUZZ(5, 880); } + else { bdat.ballh = -bdat.ballh; bdat.ballv += fixed_t(random(-16, 16)); _BUZZ(5, 640); } + } + } + // Is the ball moving down and in paddle range? + else if (bdat.ballv > 0 && WITHIN(newy, BTOF(PADDLE_Y), BTOF(PADDLE_Y + PADDLE_H))) { + // Ball actually hitting paddle + const int8_t diff = FTOB(newx) - bdat.paddle_x; + if (WITHIN(diff, 0, PADDLE_W - 1)) { + + // Reverse Y direction + bdat.ballv = -bdat.ballv; _BUZZ(3, 880); + bdat.hit_dir = -1; + + // Near edges affects X velocity + const bool is_left_edge = (diff <= 1); + if (is_left_edge || diff >= PADDLE_W-1 - 1) { + if ((bdat.ballh > 0) == is_left_edge) bdat.ballh = -bdat.ballh; + } + else if (diff <= 3) { + bdat.ballh += fixed_t(random(-64, 0)); + NOLESS(bdat.ballh, BTOF(-2)); + NOMORE(bdat.ballh, BTOF(2)); + } + else if (diff >= PADDLE_W-1 - 3) { + bdat.ballh += fixed_t(random( 0, 64)); + NOLESS(bdat.ballh, BTOF(-2)); + NOMORE(bdat.ballh, BTOF(2)); + } + + // Paddle hit after clearing the board? Reset the board. + if (game_state == 2) { reset_bricks(0xFFFF); game_state = 1; } + } + } + + bdat.ballx += bdat.ballh; bdat.bally += bdat.ballv; // update with new velocity + + } while (false); + } + + u8g.setColorIndex(1); + + // Draw bricks + if (PAGE_CONTAINS(BRICK_TOP, BRICK_BOT)) { + LOOP_L_N(y, BRICK_ROWS) { + const uint8_t yy = y * BRICK_H + BRICK_TOP; + if (PAGE_CONTAINS(yy, yy + BRICK_H - 1)) { + LOOP_L_N(x, BRICK_COLS) { + if (TEST(bdat.bricks[y], x)) { + const uint8_t xx = x * BRICK_W; + LOOP_L_N(v, BRICK_H - 1) + if (PAGE_CONTAINS(yy + v, yy + v)) + u8g.drawHLine(xx, yy + v, BRICK_W - 1); + } + } + } + } + } + + // Draw paddle + if (PAGE_CONTAINS(PADDLE_Y-1, PADDLE_Y)) { + u8g.drawHLine(bdat.paddle_x, PADDLE_Y, PADDLE_W); + #if PADDLE_H > 1 + u8g.drawHLine(bdat.paddle_x, PADDLE_Y-1, PADDLE_W); + #if PADDLE_H > 2 + u8g.drawHLine(bdat.paddle_x, PADDLE_Y-2, PADDLE_W); + #endif + #endif + } + + // Draw ball while game is running + if (game_state) { + const uint8_t by = FTOB(bdat.bally); + if (PAGE_CONTAINS(by, by+1)) + u8g.drawFrame(FTOB(bdat.ballx), by, 2, 2); + } + // Or draw GAME OVER + else + draw_game_over(); + + if (PAGE_UNDER(MENU_FONT_ASCENT)) { + // Score Digits + //const uint8_t sx = (LCD_PIXEL_WIDTH - (score >= 10 ? score >= 100 ? score >= 1000 ? 4 : 3 : 2 : 1) * MENU_FONT_WIDTH) / 2; + constexpr uint8_t sx = 0; + lcd_put_int(sx, MENU_FONT_ASCENT - 1, score); + + // Balls Left + lcd_moveto(LCD_PIXEL_WIDTH - MENU_FONT_WIDTH * 3, MENU_FONT_ASCENT - 1); + PGM_P const ohs = PSTR("ooo\0\0"); + lcd_put_u8str_P(ohs + 3 - bdat.balls_left); + } + + // A click always exits this game + if (ui.use_click()) exit_game(); +} + +#define SCREEN_M ((LCD_PIXEL_WIDTH) / 2) + +void BrickoutGame::enter_game() { + init_game(2, game_screen); // 2 = reset bricks on paddle hit + constexpr uint8_t paddle_start = SCREEN_M - (PADDLE_W) / 2; + bdat.paddle_x = paddle_start; + bdat.balls_left = 3; + reset_bricks(0x0000); + reset_ball(); + ui.encoderPosition = paddle_start / (PADDLE_VEL); +} + +#endif // MARLIN_BRICKOUT diff --git a/Marlin/src/lcd/menu/game/brickout.h b/Marlin/src/lcd/menu/game/brickout.h new file mode 100644 index 0000000..cf1fbc6 --- /dev/null +++ b/Marlin/src/lcd/menu/game/brickout.h @@ -0,0 +1,38 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ +#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; diff --git a/Marlin/src/lcd/menu/game/game.cpp b/Marlin/src/lcd/menu/game/game.cpp new file mode 100644 index 0000000..c14bd2a --- /dev/null +++ b/Marlin/src/lcd/menu/game/game.cpp @@ -0,0 +1,66 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if HAS_GAMES + +#include "game.h" + +int MarlinGame::score; +uint8_t MarlinGame::game_state; +millis_t MarlinGame::next_frame; + +MarlinGameData marlin_game_data; + +bool MarlinGame::game_frame() { + static int8_t slew; + if (ui.first_page) slew = 2; + ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Refresh as often as possible + return (game_state && slew-- > 0); +} + +void MarlinGame::draw_game_over() { + constexpr int8_t gowide = (MENU_FONT_WIDTH) * 9, + gohigh = MENU_FONT_ASCENT - 3, + lx = (LCD_PIXEL_WIDTH - gowide) / 2, + ly = (LCD_PIXEL_HEIGHT + gohigh) / 2; + if (PAGE_CONTAINS(ly - gohigh - 1, ly + 1)) { + u8g.setColorIndex(0); + u8g.drawBox(lx - 1, ly - gohigh - 1, gowide + 2, gohigh + 2); + u8g.setColorIndex(1); + if (ui.get_blink()) lcd_put_u8str_P(lx, ly, PSTR("GAME OVER")); + } +} + +void MarlinGame::init_game(const uint8_t init_state, const screenFunc_t screen) { + score = 0; + game_state = init_state; + ui.goto_screen(screen); + ui.defer_status_screen(); +} + +void MarlinGame::exit_game() { + ui.goto_previous_screen_no_defer(); +} + +#endif // HAS_GAMES diff --git a/Marlin/src/lcd/menu/game/game.h b/Marlin/src/lcd/menu/game/game.h new file mode 100644 index 0000000..cba79e4 --- /dev/null +++ b/Marlin/src/lcd/menu/game/game.h @@ -0,0 +1,62 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ +#pragma once + +#include "../../../inc/MarlinConfigPre.h" +#include "../../dogm/marlinui_DOGM.h" +#include "../../lcdprint.h" +#include "../../marlinui.h" + +//#define MUTE_GAMES + +#if ENABLED(MUTE_GAMES) || !HAS_BUZZER + #define _BUZZ(D,F) NOOP +#else + #define _BUZZ(D,F) BUZZ(D,F) +#endif + +#if HAS_GAME_MENU + void menu_game(); +#endif + +#if ENABLED(MARLIN_BRICKOUT) + #include "brickout.h" +#endif +#if ENABLED(MARLIN_INVADERS) + #include "invaders.h" +#endif +#if ENABLED(MARLIN_MAZE) + #include "maze.h" +#endif +#if ENABLED(MARLIN_SNAKE) + #include "snake.h" +#endif + +// Pool game data to save SRAM +union MarlinGameData { + TERN_(MARLIN_BRICKOUT, brickout_data_t brickout); + TERN_(MARLIN_INVADERS, invaders_data_t invaders); + TERN_(MARLIN_SNAKE, snake_data_t snake); + TERN_(MARLIN_MAZE, maze_data_t maze); +}; + +extern MarlinGameData marlin_game_data; diff --git a/Marlin/src/lcd/menu/game/invaders.cpp b/Marlin/src/lcd/menu/game/invaders.cpp new file mode 100644 index 0000000..56e4c22 --- /dev/null +++ b/Marlin/src/lcd/menu/game/invaders.cpp @@ -0,0 +1,438 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_INVADERS) + +#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 +const unsigned char invader[3][2][16] PROGMEM = { + { { B00000110,B00000000, + B00001111,B00000000, + B00011111,B10000000, + B00110110,B11000000, + B00111111,B11000000, + B00001001,B00000000, + B00010110,B10000000, + B00101001,B01000000 + }, { + B00000110,B00000000, + B00001111,B00000000, + B00011111,B10000000, + B00110110,B11000000, + B00111111,B11000000, + B00010110,B10000000, + B00100000,B01000000, + B00010000,B10000000 + } + }, { + { B00010000,B01000000, + B00001000,B10000000, + B00011111,B11000000, + B00110111,B01100000, + B01111111,B11110000, + B01011111,B11010000, + B01010000,B01010000, + B00001101,B10000000 + }, { + B00010000,B01000000, + B01001000,B10010000, + B01011111,B11010000, + B01110111,B01110000, + B01111111,B11110000, + B00011111,B11000000, + B00010000,B01000000, + B00100000,B00100000 + } + }, { + { B00001111,B00000000, + B01111111,B11100000, + B11111111,B11110000, + B11100110,B01110000, + B11111111,B11110000, + B00011001,B10000000, + B00110110,B11000000, + B11000000,B00110000 + }, { + B00001111,B00000000, + B01111111,B11100000, + B11111111,B11110000, + B11100110,B01110000, + B11111111,B11110000, + B00011001,B10000000, + B00110110,B11000000, + B00011001,B10000000 + } + } +}; +const unsigned char cannon[] PROGMEM = { + B00000100,B00000000, + B00001110,B00000000, + B00001110,B00000000, + B01111111,B11000000, + B11111111,B11100000, + B11111111,B11100000, + B11111111,B11100000, + B11111111,B11100000 +}; +const unsigned char life[] PROGMEM = { + B00010000, + B01111100, + B11111110, + B11111110, + B11111110 +}; +const unsigned char explosion[] PROGMEM = { + B01000100,B01000000, + B00100100,B10000000, + B00000000,B00000000, + B00110001,B10000000, + B00000000,B00000000, + B00100100,B10000000, + B01000100,B01000000 +}; +const unsigned char ufo[] PROGMEM = { + B00011111,B11000000, + B01111111,B11110000, + B11011101,B11011000, + B11111111,B11111000, + B01111111,B11110000 +}; + +constexpr uint8_t inv_type[] = { + #if INVADER_ROWS == 5 + 0, 1, 1, 2, 2 + #elif INVADER_ROWS == 4 + 0, 1, 1, 2 + #elif INVADER_ROWS == 3 + 0, 1, 2 + #else + #error "INVASION_SIZE must be 3, 4, or 5." + #endif +}; + +invaders_data_t &idat = marlin_game_data.invaders; + +#define INV_X_LEFT(C,T) (idat.pos.x + (C) * (INVADER_COL_W) + inv_off[T]) +#define INV_X_CTR(C,T) (INV_X_LEFT(C,T) + inv_wide[T] / 2) +#define INV_Y_BOT(R) (idat.pos.y + (R + 1) * (INVADER_ROW_H) - 2) + +constexpr uint8_t inv_off[] = { 2, 1, 0 }, inv_wide[] = { 8, 11, 12 }; + +inline void update_invader_data() { + uint8_t inv_mask = 0; + // Get a list of all active invaders + uint8_t sc = 0; + LOOP_L_N(y, INVADER_ROWS) { + uint8_t m = idat.bugs[y]; + if (m) idat.botmost = y + 1; + inv_mask |= m; + LOOP_L_N(x, INVADER_COLS) + if (TEST(m, x)) idat.shooters[sc++] = (y << 4) | x; + } + idat.leftmost = 0; + LOOP_L_N(i, INVADER_COLS) { if (TEST(inv_mask, i)) break; idat.leftmost -= INVADER_COL_W; } + idat.rightmost = LCD_PIXEL_WIDTH - (INVADERS_WIDE); + for (uint8_t i = INVADER_COLS; i--;) { if (TEST(inv_mask, i)) break; idat.rightmost += INVADER_COL_W; } + if (idat.count == 2) idat.dir = idat.dir > 0 ? INVADER_VEL + 1 : -(INVADER_VEL + 1); +} + +inline void reset_bullets() { + LOOP_L_N(i, COUNT(idat.bullet)) idat.bullet[i].v = 0; +} + +inline void reset_invaders() { + idat.pos.x = 0; idat.pos.y = INVADER_TOP; + idat.dir = INVADER_VEL; + idat.count = (INVADER_COLS) * (INVADER_ROWS); + LOOP_L_N(i, INVADER_ROWS) idat.bugs[i] = _BV(INVADER_COLS) - 1; + update_invader_data(); + reset_bullets(); +} + + +inline void spawn_ufo() { + idat.ufov = random(0, 2) ? 1 : -1; + idat.ufox = idat.ufov > 0 ? -(UFO_W) : LCD_PIXEL_WIDTH - 1; +} + +inline void reset_player() { + idat.cannon_x = 0; + ui.encoderPosition = 0; +} + +inline void fire_cannon() { + idat.laser.x = idat.cannon_x + CANNON_W / 2; + idat.laser.y = LCD_PIXEL_HEIGHT - CANNON_H - (LASER_H); + idat.laser.v = -(LASER_H); +} + +inline void explode(const int8_t x, const int8_t y, const int8_t v=4) { + idat.explod.x = x - (EXPL_W) / 2; + idat.explod.y = y; + idat.explod.v = v; +} + +inline void kill_cannon(uint8_t &game_state, const uint8_t st) { + reset_bullets(); + explode(idat.cannon_x + (CANNON_W) / 2, CANNON_Y, 6); + _BUZZ(1000, 10); + if (--idat.cannons_left) { + idat.laser.v = 0; + game_state = st; + reset_player(); + } + else + game_state = 0; +} + +void InvadersGame::game_screen() { + ui.refresh(LCDVIEW_CALL_NO_REDRAW); // Call as often as possible + + // Run game logic once per full screen + if (ui.first_page) { + + // Update Cannon Position + int16_t ep = constrain(int16_t(ui.encoderPosition), 0, (LCD_PIXEL_WIDTH - (CANNON_W)) / (CANNON_VEL)); + ui.encoderPosition = ep; + + ep *= (CANNON_VEL); + if (ep > idat.cannon_x) { idat.cannon_x += CANNON_VEL - 1; if (ep - idat.cannon_x < 2) idat.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 + if (game_state) do { + + // Move the UFO, if any + 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; } + + const bool did_blink = (++idat.blink_count > idat.count >> 1); + if (did_blink) { + idat.game_blink = !idat.game_blink; + idat.blink_count = 0; + } + + if (idat.count && did_blink) { + const int8_t newx = idat.pos.x + idat.dir; + if (!WITHIN(newx, idat.leftmost, idat.rightmost)) { // Invaders reached the edge? + idat.dir *= -1; // Invaders change direction + idat.pos.y += (INVADER_ROW_H) / 2; // Invaders move down + idat.pos.x -= idat.dir; // ...and only move down this time. + 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. + } + + idat.pos.x += idat.dir; // Invaders take one step left/right + + // Randomly shoot if invaders are listed + if (idat.count && !random(0, 20)) { + + // Find a free bullet + laser_t *b = nullptr; + LOOP_L_N(i, COUNT(idat.bullet)) if (!idat.bullet[i].v) { b = &idat.bullet[i]; break; } + if (b) { + // Pick a random shooter and update the bullet + //SERIAL_ECHOLNPGM("free bullet found"); + 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->y = INV_Y_BOT(row); + b->v = 2 + random(0, 2); + } + } + } + + // Update the laser position + if (idat.laser.v) { + idat.laser.y += idat.laser.v; + if (idat.laser.y < 0) idat.laser.v = 0; + } + + // Did the laser collide with an invader? + if (idat.laser.v && WITHIN(idat.laser.y, idat.pos.y, idat.pos.y + INVADERS_HIGH - 1)) { + const int8_t col = idat.laser_col(); + if (WITHIN(col, 0, INVADER_COLS - 1)) { + const int8_t row = idat.laser_row(); + if (WITHIN(row, 0, INVADER_ROWS - 1)) { + const uint8_t mask = _BV(col); + if (idat.bugs[row] & mask) { + const uint8_t type = inv_type[row]; + const int8_t invx = INV_X_LEFT(col, type); + if (WITHIN(idat.laser.x, invx, invx + inv_wide[type] - 1)) { + // Turn off laser + idat.laser.v = 0; + // Remove the invader! + idat.bugs[row] &= ~mask; + // Score! + score += INVADER_ROWS - row; + // Explode sound! + _BUZZ(40, 10); + // Explosion bitmap! + explode(invx + inv_wide[type] / 2, idat.pos.y + row * (INVADER_ROW_H)); + // If invaders are gone, go to reset invaders state + if (--idat.count) update_invader_data(); else { game_state = 20; reset_bullets(); } + } // laser x hit + } // invader exists + } // good row + } // good col + } // laser in invader zone + + // Handle alien bullets + LOOP_L_N(s, COUNT(idat.bullet)) { + laser_t *b = &idat.bullet[s]; + if (b->v) { + // Update alien bullet position + b->y += b->v; + if (b->y >= LCD_PIXEL_HEIGHT) + b->v = 0; // Offscreen + 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 + } + } + + // Randomly spawn a UFO + if (!idat.ufov && !random(0,500)) spawn_ufo(); + + // Did the laser hit a ufo? + 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 + idat.laser.v = idat.ufov = 0; + // Score! + score += 10; + // Explode! + _BUZZ(40, 10); + // Explosion bitmap + explode(idat.ufox + (UFO_W) / 2, 1); + } + + } while (false); + + } + + // Click-and-hold to abort + if (ui.button_pressed()) --idat.quit_count; else idat.quit_count = 10; + + // Click to fire or exit + if (ui.use_click()) { + if (!game_state) + idat.quit_count = 0; + else if (game_state == 1 && !idat.laser.v) + fire_cannon(); + } + + if (!idat.quit_count) exit_game(); + + u8g.setColorIndex(1); + + // Draw invaders + if (PAGE_CONTAINS(idat.pos.y, idat.pos.y + idat.botmost * (INVADER_ROW_H) - 2 - 1)) { + int8_t yy = idat.pos.y; + LOOP_L_N(y, INVADER_ROWS) { + const uint8_t type = inv_type[y]; + if (PAGE_CONTAINS(yy, yy + INVADER_H - 1)) { + int8_t xx = idat.pos.x; + LOOP_L_N(x, INVADER_COLS) { + if (TEST(idat.bugs[y], x)) + u8g.drawBitmapP(xx, yy, 2, INVADER_H, invader[type][idat.game_blink]); + xx += INVADER_COL_W; + } + } + yy += INVADER_ROW_H; + } + } + + // Draw UFO + if (idat.ufov && PAGE_UNDER(UFO_H + 2)) + u8g.drawBitmapP(idat.ufox, 2, 2, UFO_H, ufo); + + // Draw cannon + if (game_state && PAGE_CONTAINS(CANNON_Y, CANNON_Y + CANNON_H - 1) && (game_state < 2 || (game_state & 0x02))) + u8g.drawBitmapP(idat.cannon_x, CANNON_Y, 2, CANNON_H, cannon); + + // Draw laser + if (idat.laser.v && PAGE_CONTAINS(idat.laser.y, idat.laser.y + LASER_H - 1)) + u8g.drawVLine(idat.laser.x, idat.laser.y, LASER_H); + + // Draw invader bullets + LOOP_L_N (i, COUNT(idat.bullet)) { + if (idat.bullet[i].v && PAGE_CONTAINS(idat.bullet[i].y - (SHOT_H - 1), idat.bullet[i].y)) + u8g.drawVLine(idat.bullet[i].x, idat.bullet[i].y - (SHOT_H - 1), SHOT_H); + } + + // Draw explosion + if (idat.explod.v && PAGE_CONTAINS(idat.explod.y, idat.explod.y + 7 - 1)) { + u8g.drawBitmapP(idat.explod.x, idat.explod.y, 2, 7, explosion); + --idat.explod.v; + } + + // Blink GAME OVER when game is over + if (!game_state) draw_game_over(); + + if (PAGE_UNDER(MENU_FONT_ASCENT - 1)) { + // Draw Score + //const uint8_t sx = (LCD_PIXEL_WIDTH - (score >= 10 ? score >= 100 ? score >= 1000 ? 4 : 3 : 2 : 1) * MENU_FONT_WIDTH) / 2; + constexpr uint8_t sx = 0; + lcd_put_int(sx, MENU_FONT_ASCENT - 1, score); + + // Draw lives + if (idat.cannons_left) + 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); + } + +} + +void InvadersGame::enter_game() { + init_game(20, game_screen); // countdown to reset invaders + idat.cannons_left = 3; + idat.quit_count = 10; + idat.laser.v = 0; + reset_invaders(); + reset_player(); +} + +#endif // MARLIN_INVADERS diff --git a/Marlin/src/lcd/menu/game/invaders.h b/Marlin/src/lcd/menu/game/invaders.h new file mode 100644 index 0000000..c99e6c1 --- /dev/null +++ b/Marlin/src/lcd/menu/game/invaders.h @@ -0,0 +1,62 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ +#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; diff --git a/Marlin/src/lcd/menu/game/maze.cpp b/Marlin/src/lcd/menu/game/maze.cpp new file mode 100644 index 0000000..85f752e --- /dev/null +++ b/Marlin/src/lcd/menu/game/maze.cpp @@ -0,0 +1,134 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_MAZE) + +#include "game.h" + +int8_t move_dir, last_move_dir, // NESW0 + prizex, prizey, prize_cnt, old_encoder; +fixed_t playerx, playery; + +// Up to 50 lines, then you win! +typedef struct { int8_t x, y; } pos_t; +uint8_t head_ind; +pos_t maze_walls[50] = { + { 0, 0 } +}; + +// Turn the player cw or ccw +inline void turn_player(const bool cw) { + if (move_dir == 4) move_dir = last_move_dir; + move_dir += cw ? 1 : -1; + move_dir &= 0x03; + last_move_dir = move_dir; +} + +// Reset the player for a new game +void player_reset() { + // Init position + playerx = BTOF(1); + playery = BTOF(GAME_H / 2); + + // Init motion with a ccw turn + move_dir = 0; + turn_player(false); + + // Clear prize flag + prize_cnt = 255; + + // Clear the controls + ui.encoderPosition = 0; + old_encoder = 0; +} + +void MazeGame::game_screen() { + // Run the sprite logic + if (game_frame()) do { // Run logic twice for finer resolution + + // Move the man one unit in the current direction + // Direction index 4 is for the stopped man + const int8_t oldx = FTOB(playerx), oldy = FTOB(playery); + pos_t dir_add[] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 0 } }; + playerx += dir_add[move_dir].x; + playery += dir_add[move_dir].y; + const int8_t x = FTOB(playerx), y = FTOB(playery); + + } while(0); + + u8g.setColorIndex(1); + + // Draw Score + if (PAGE_UNDER(HEADER_H)) lcd_put_int(0, HEADER_H - 1, score); + + // Draw the maze + // LOOP_L_N(n, head_ind) { + // const pos_t &p = maze_walls[n], &q = maze_walls[n + 1]; + // if (p.x == q.x) { + // const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y)); + // if (PAGE_CONTAINS(y1, y2)) + // u8g.drawVLine(GAMEX(p.x), y1, y2 - y1 + 1); + // } + // else if (PAGE_CONTAINS(GAMEY(p.y), GAMEY(p.y))) { + // const int8_t x1 = GAMEX(_MIN(p.x, q.x)), x2 = GAMEX(_MAX(p.x, q.x)); + // u8g.drawHLine(x1, GAMEY(p.y), x2 - x1 + 1); + // } + // } + + // Draw Man + // const int8_t fy = GAMEY(foody); + // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) { + // const int8_t fx = GAMEX(foodx); + // u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH); + // if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2); + // } + + // Draw Ghosts + // const int8_t fy = GAMEY(foody); + // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) { + // const int8_t fx = GAMEX(foodx); + // u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH); + // if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2); + // } + + // Draw Prize + // if (PAGE_CONTAINS(prizey, prizey + PRIZE_WH - 1)) { + // u8g.drawFrame(prizex, prizey, PRIZE_WH, PRIZE_WH); + // if (PRIZE_WH == 5) u8g.drawPixel(prizex + 2, prizey + 2); + // } + + // Draw GAME OVER + if (!game_state) draw_game_over(); + + // A click always exits this game + if (ui.use_click()) exit_game(); +} + +void MazeGame::enter_game() { + init_game(1, game_screen); // Game running + reset_player(); + reset_enemies(); +} + +#endif // MARLIN_MAZE diff --git a/Marlin/src/lcd/menu/game/maze.h b/Marlin/src/lcd/menu/game/maze.h new file mode 100644 index 0000000..ff5dde0 --- /dev/null +++ b/Marlin/src/lcd/menu/game/maze.h @@ -0,0 +1,30 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ +#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; diff --git a/Marlin/src/lcd/menu/game/snake.cpp b/Marlin/src/lcd/menu/game/snake.cpp new file mode 100644 index 0000000..f8892a4 --- /dev/null +++ b/Marlin/src/lcd/menu/game/snake.cpp @@ -0,0 +1,323 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ + +#include "../../../inc/MarlinConfigPre.h" + +#if ENABLED(MARLIN_SNAKE) + +#include "game.h" + +#define SNAKE_BOX 4 + +#define HEADER_H (MENU_FONT_ASCENT - 2) +#define SNAKE_WH (SNAKE_BOX + 1) + +#define IDEAL_L 2 +#define IDEAL_R (LCD_PIXEL_WIDTH - 1 - 2) +#define IDEAL_T (HEADER_H + 2) +#define IDEAL_B (LCD_PIXEL_HEIGHT - 1 - 2) +#define IDEAL_W (IDEAL_R - (IDEAL_L) + 1) +#define IDEAL_H (IDEAL_B - (IDEAL_T) + 1) + +#define GAME_W int((IDEAL_W) / (SNAKE_WH)) +#define GAME_H int((IDEAL_H) / (SNAKE_WH)) + +#define BOARD_W ((SNAKE_WH) * (GAME_W) + 1) +#define BOARD_H ((SNAKE_WH) * (GAME_H) + 1) +#define BOARD_L ((LCD_PIXEL_WIDTH - (BOARD_W) + 1) / 2) +#define BOARD_R (BOARD_L + BOARD_W - 1) +#define BOARD_T (((LCD_PIXEL_HEIGHT + IDEAL_T) - (BOARD_H)) / 2) +#define BOARD_B (BOARD_T + BOARD_H - 1) + +#define GAMEX(X) (BOARD_L + ((X) * (SNAKE_WH))) +#define GAMEY(Y) (BOARD_T + ((Y) * (SNAKE_WH))) + +#if SNAKE_BOX > 2 + #define FOOD_WH SNAKE_BOX +#else + #define FOOD_WH 2 +#endif + +#if SNAKE_BOX < 1 + #define SNAKE_SIZ 1 +#else + #define SNAKE_SIZ SNAKE_BOX +#endif + +constexpr fixed_t snakev = FTOP(0.20); + +snake_data_t &sdat = marlin_game_data.snake; + +// Remove the first pixel from the tail. +// If needed, shift out the first segment. +void shorten_tail() { + pos_t &p = sdat.snake_tail[0], &q = sdat.snake_tail[1]; + bool shift = false; + if (p.x == q.x) { + // Vertical line + p.y += (q.y > p.y) ? 1 : -1; + shift = p.y == q.y; + } + else { + // Horizontal line + p.x += (q.x > p.x) ? 1 : -1; + shift = p.x == q.x; + } + if (shift) { + sdat.head_ind--; + LOOP_LE_N(i, sdat.head_ind) + sdat.snake_tail[i] = sdat.snake_tail[i + 1]; + } +} + +// The food is on a line +inline bool food_on_line() { + LOOP_L_N(n, sdat.head_ind) { + pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1]; + if (p.x == q.x) { + 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; + } + 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 false; +} + +// Add a new food blob +void food_reset() { + do { + sdat.foodx = random(0, GAME_W); + sdat.foody = random(0, GAME_H); + } while (food_on_line()); +} + +// Turn the snake cw or ccw +inline void turn_snake(const bool cw) { + sdat.snake_dir += cw ? 1 : -1; + sdat.snake_dir &= 0x03; + sdat.head_ind++; + sdat.snake_tail[sdat.head_ind].x = FTOB(sdat.snakex); + sdat.snake_tail[sdat.head_ind].y = FTOB(sdat.snakey); +} + +// Reset the snake for a new game +void snake_reset() { + // Init the head and velocity + sdat.snakex = BTOF(1); + sdat.snakey = BTOF(GAME_H / 2); + //snakev = FTOP(0.25); + + // Init the tail with a cw turn + sdat.snake_dir = 0; + sdat.head_ind = 0; + sdat.snake_tail[0].x = 0; + sdat.snake_tail[0].y = GAME_H / 2; + turn_snake(true); + + // Clear food flag + sdat.food_cnt = 5; + + // Clear the controls + ui.encoderPosition = 0; + sdat.old_encoder = 0; +} + +// Check if head segment overlaps another +bool snake_overlap() { + // 4 lines must exist before a collision is possible + if (sdat.head_ind < 4) return false; + // Is the last segment crossing any others? + const pos_t &h1 = sdat.snake_tail[sdat.head_ind - 1], &h2 = sdat.snake_tail[sdat.head_ind]; + // VERTICAL head segment? + if (h1.x == h2.x) { + // Loop from oldest to segment two away from head + LOOP_L_N(n, sdat.head_ind - 2) { + // Segment p to q + const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1]; + if (p.x != q.x) { + // 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; + } // Overlapping vertical segment + else if (h1.x == p.x && _MIN(h1.y, h2.y) <= _MAX(p.y, q.y) && _MAX(h1.y, h2.y) >= _MIN(p.y, q.y)) return true; + } + } + else { + // Loop from oldest to segment two away from head + LOOP_L_N(n, sdat.head_ind - 2) { + // Segment p to q + const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1]; + if (p.y != q.y) { + // 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; + } // Overlapping horizontal segment + else if (h1.y == p.y && _MIN(h1.x, h2.x) <= _MAX(p.x, q.x) && _MAX(h1.x, h2.x) >= _MIN(p.x, q.x)) return true; + } + } + return false; +} + +void SnakeGame::game_screen() { + // Run the snake logic + if (game_frame()) do { // Run logic twice for finer resolution + + // Move the snake's head one unit in the current direction + const int8_t oldx = FTOB(sdat.snakex), oldy = FTOB(sdat.snakey); + switch (sdat.snake_dir) { + case 0: sdat.snakey -= snakev; break; + case 1: sdat.snakex += snakev; break; + case 2: sdat.snakey += snakev; break; + case 3: sdat.snakex -= snakev; break; + } + const int8_t x = FTOB(sdat.snakex), y = FTOB(sdat.snakey); + + // If movement took place... + if (oldx != x || oldy != y) { + + if (!WITHIN(x, 0, GAME_W - 1) || !WITHIN(y, 0, GAME_H - 1)) { + game_state = 0; // Game Over + _BUZZ(400, 40); // Bzzzt! + break; // ...out of do-while + } + + sdat.snake_tail[sdat.head_ind].x = x; + sdat.snake_tail[sdat.head_ind].y = y; + + // Change snake direction if set + const int8_t enc = int8_t(ui.encoderPosition), diff = enc - sdat.old_encoder; + if (diff) { + sdat.old_encoder = enc; + turn_snake(diff > 0); + } + + if (sdat.food_cnt) --sdat.food_cnt; else shorten_tail(); + + // Did the snake collide with itself or go out of bounds? + if (snake_overlap()) { + game_state = 0; // Game Over + _BUZZ(400, 40); // Bzzzt! + } + // Is the snake at the food? + else if (x == sdat.foodx && y == sdat.foody) { + _BUZZ(5, 220); + _BUZZ(5, 280); + score++; + sdat.food_cnt = 2; + food_reset(); + } + } + + } while(0); + + u8g.setColorIndex(1); + + // Draw Score + if (PAGE_UNDER(HEADER_H)) lcd_put_int(0, HEADER_H - 1, score); + + // DRAW THE PLAYFIELD BORDER + u8g.drawFrame(BOARD_L - 2, BOARD_T - 2, BOARD_R - BOARD_L + 4, BOARD_B - BOARD_T + 4); + + // Draw the snake (tail) + #if SNAKE_WH < 2 + + // At this scale just draw a line + LOOP_L_N(n, sdat.head_ind) { + const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1]; + if (p.x == q.x) { + const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y)); + if (PAGE_CONTAINS(y1, y2)) + u8g.drawVLine(GAMEX(p.x), y1, y2 - y1 + 1); + } + else if (PAGE_CONTAINS(GAMEY(p.y), GAMEY(p.y))) { + const int8_t x1 = GAMEX(_MIN(p.x, q.x)), x2 = GAMEX(_MAX(p.x, q.x)); + u8g.drawHLine(x1, GAMEY(p.y), x2 - x1 + 1); + } + } + + #elif SNAKE_WH == 2 + + // At this scale draw two lines + LOOP_L_N(n, sdat.head_ind) { + const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1]; + if (p.x == q.x) { + const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y)); + if (PAGE_CONTAINS(y1, y2 + 1)) + u8g.drawFrame(GAMEX(p.x), y1, 2, y2 - y1 + 1 + 1); + } + else { + const int8_t py = GAMEY(p.y); + if (PAGE_CONTAINS(py, py + 1)) { + const int8_t x1 = GAMEX(_MIN(p.x, q.x)), x2 = GAMEX(_MAX(p.x, q.x)); + u8g.drawFrame(x1, py, x2 - x1 + 1 + 1, 2); + } + } + } + + #else + + // Draw a series of boxes + LOOP_L_N(n, sdat.head_ind) { + const pos_t &p = sdat.snake_tail[n], &q = sdat.snake_tail[n + 1]; + if (p.x == q.x) { + 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)) { + for (int8_t i = y1; i <= y2; ++i) { + const int8_t y = GAMEY(i); + if (PAGE_CONTAINS(y, y + SNAKE_SIZ - 1)) + u8g.drawBox(GAMEX(p.x), y, SNAKE_SIZ, SNAKE_SIZ); + } + } + } + else { + const int8_t py = GAMEY(p.y); + if (PAGE_CONTAINS(py, py + SNAKE_SIZ - 1)) { + const int8_t x1 = _MIN(p.x, q.x), x2 = _MAX(p.x, q.x); + for (int8_t i = x1; i <= x2; ++i) + u8g.drawBox(GAMEX(i), py, SNAKE_SIZ, SNAKE_SIZ); + } + } + } + + #endif + + // Draw food + const int8_t fy = GAMEY(sdat.foody); + if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) { + const int8_t fx = GAMEX(sdat.foodx); + u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH); + if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2); + } + + // Draw GAME OVER + if (!game_state) draw_game_over(); + + // A click always exits this game + if (ui.use_click()) exit_game(); +} + +void SnakeGame::enter_game() { + init_game(1, game_screen); // 1 = Game running + snake_reset(); + food_reset(); +} + +#endif // MARLIN_SNAKE diff --git a/Marlin/src/lcd/menu/game/snake.h b/Marlin/src/lcd/menu/game/snake.h new file mode 100644 index 0000000..09a3a72 --- /dev/null +++ b/Marlin/src/lcd/menu/game/snake.h @@ -0,0 +1,38 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ +#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; diff --git a/Marlin/src/lcd/menu/game/types.h b/Marlin/src/lcd/menu/game/types.h new file mode 100644 index 0000000..f6e6c78 --- /dev/null +++ b/Marlin/src/lcd/menu/game/types.h @@ -0,0 +1,46 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 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 . + * + */ +#pragma once + +#include + +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); +}; -- cgit v1.2.3