Files
wordclock/include/pong.h
2023-08-21 20:26:38 +02:00

101 lines
2.0 KiB
C++

/**
* @file pong.h
* @author techniccontroller (mail[at]techniccontroller.com)
* @brief Class declaration for pong game
* @version 0.1
* @date 2022-03-05
*
* @copyright Copyright (c) 2022
*
* main code from https://elektro.turanis.de/html/prj041/index.html
*
*/
#ifndef PONG_H
#define PONG_H
#include <Arduino.h>
#include "ledmatrix.h"
#include "udp_logger.h"
#ifdef DEBOUNCE_TIME
#undef DEBOUNCE_TIME
#endif
#define DEBOUNCE_TIME 10 // in ms
#define X_MAX 11
#define Y_MAX 11
#ifdef GAME_DELAY
#undef GAME_DELAY
#endif
#define GAME_DELAY 80 // in ms
#define BALL_DELAY_MAX 350 // in ms
#define BALL_DELAY_MIN 50 // in ms
#define BALL_DELAY_STEP 5 // in ms
#define PLAYER_AMOUNT 2
#define PLAYER_1 0
#define PLAYER_2 1
#define PADDLE_WIDTH 3
#define PADDLE_MOVE_NONE 0
#define PADDLE_MOVE_UP 1
#define PADDLE_MOVE_DOWN 2
#ifdef LED_TYPE_OFF
#undef LED_TYPE_OFF
#endif
#define LED_TYPE_OFF 1
#define LED_TYPE_PADDLE 2
#define LED_TYPE_BALL 3
#define LED_TYPE_BALL_RED 4
#define GAME_STATE_RUNNING 1
#define GAME_STATE_END 2
#define GAME_STATE_INIT 3
class Pong
{
struct Coords
{
uint8_t x;
uint8_t y;
};
public:
Pong();
Pong(LEDMatrix * matrix, UDPLogger * logger);
void loopCycle();
void initGame(uint8_t numBots);
void ctrlUp(uint8_t playerid);
void ctrlDown(uint8_t playerid);
void ctrlNone(uint8_t playerid);
private:
LEDMatrix *_ledmatrix;
UDPLogger *_logger;
uint8_t _gameState = 0;
uint8_t _numBots = 0;
uint8_t _playerMovement[PLAYER_AMOUNT] = {0};
Coords _paddles[PLAYER_AMOUNT][PADDLE_WIDTH] = {0};
Coords _ball = {0, 0};
Coords _ball_old = {0, 0};
int _ballMovement[2] = {0, 0};
unsigned int _ballDelay = 0;
unsigned long _lastDrawUpdate = 0;
unsigned long _lastBallUpdate = 0;
unsigned long _lastButtonClick = 0;
void updateBall();
void endGame();
void updateGame();
uint8_t getPlayerMovement(uint8_t playerId);
void resetLEDs();
void toggleLed(uint8_t x, uint8_t y, uint8_t type);
};
#endif /* PONG_H */