初始化提交
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
MD_MAX72XX::fontType_t _Fixed_5x3[] PROGMEM = {
|
||||
'F', 1, 32, 127, 5,
|
||||
2, 0, 0, // 32 - 'Space'
|
||||
1, 23, // 33 - '!'
|
||||
3, 3, 0, 3, // 34 - '"'
|
||||
3, 31, 10, 31, // 35 - '#'
|
||||
3, 22, 31, 13, // 36 - '$'
|
||||
3, 9, 4, 18, // 37 - '%'
|
||||
3, 10, 21, 26, // 38 - '&'
|
||||
1, 3, // 39
|
||||
2, 14, 17, // 40 - '('
|
||||
2, 17, 14, // 41 - ')'
|
||||
3, 10, 4, 10, // 42 - '*'
|
||||
3, 4, 14, 4, // 43 - '+'
|
||||
2, 16, 8, // 44 - ','
|
||||
3, 4, 4, 4, // 45 - '-'
|
||||
1, 16, // 46 - '.'
|
||||
3, 8, 4, 2, // 47 - '/'
|
||||
3, 31, 17, 31, // 48 - '0'
|
||||
2, 0, 31, // 49 - '1'
|
||||
3, 29, 21, 23, // 50 - '2'
|
||||
3, 17, 21, 31, // 51 - '3'
|
||||
3, 7, 4, 31, // 52 - '4'
|
||||
3, 23, 21, 29, // 53 - '5'
|
||||
3, 31, 21, 29, // 54 - '6'
|
||||
3, 1, 1, 31, // 55 - '7'
|
||||
3, 31, 21, 31, // 56 - '8'
|
||||
3, 23, 21, 31, // 57 - '9'
|
||||
1, 10, // 58 - ':'
|
||||
2, 16, 10, // 59 - ';'
|
||||
3, 4, 10, 17, // 60 - '<'
|
||||
3, 10, 10, 10, // 61 - '='
|
||||
3, 17, 10, 4, // 62 - '>'
|
||||
3, 1, 21, 3, // 63 - '?'
|
||||
3, 14, 21, 22, // 64 - '@'
|
||||
3, 30, 5, 30, // 65 - 'A'
|
||||
3, 31, 21, 10, // 66 - 'B'
|
||||
3, 14, 17, 17, // 67 - 'C'
|
||||
3, 31, 17, 14, // 68 - 'D'
|
||||
3, 31, 21, 17, // 69 - 'E'
|
||||
3, 31, 5, 1, // 70 - 'F'
|
||||
3, 14, 17, 29, // 71 - 'G'
|
||||
3, 31, 4, 31, // 72 - 'H'
|
||||
3, 17, 31, 17, // 73 - 'I'
|
||||
3, 8, 16, 15, // 74 - 'J'
|
||||
3, 31, 4, 27, // 75 - 'K'
|
||||
3, 31, 16, 16, // 76 - 'L'
|
||||
3, 31, 2, 31, // 77 - 'M'
|
||||
3, 31, 14, 31, // 78 - 'N'
|
||||
3, 14, 17, 14, // 79 - 'O'
|
||||
3, 31, 5, 2, // 80 - 'P'
|
||||
3, 14, 25, 30, // 81 - 'Q'
|
||||
3, 31, 5, 26, // 82 - 'R'
|
||||
3, 18, 21, 9, // 83 - 'S'
|
||||
3, 1, 31, 1, // 84 - 'T'
|
||||
3, 15, 16, 15, // 85 - 'U'
|
||||
3, 7, 24, 7, // 86 - 'V'
|
||||
3, 15, 28, 15, // 87 - 'W'
|
||||
3, 27, 4, 27, // 88 - 'X'
|
||||
3, 3, 28, 3, // 89 - 'Y'
|
||||
3, 25, 21, 19, // 90 - 'Z'
|
||||
2, 31, 17, // 91 - '['
|
||||
3, 2, 4, 8, // 92 - '\'
|
||||
2, 17, 31, // 93 - ']'
|
||||
3, 2, 1, 2, // 94 - '^'
|
||||
3, 16, 16, 16, // 95 - '_'
|
||||
2, 1, 2, // 96 - '`'
|
||||
3, 12, 18, 28, // 97 - 'a'
|
||||
3, 31, 18, 12, // 98 - 'b'
|
||||
3, 12, 18, 18, // 99 - 'c'
|
||||
3, 12, 18, 31, // 100 - 'd'
|
||||
3, 12, 26, 20, // 101 - 'e'
|
||||
3, 4, 31, 5, // 102 - 'f'
|
||||
3, 20, 26, 12, // 103 - 'g'
|
||||
3, 31, 2, 28, // 104 - 'h'
|
||||
1, 29, // 105 - 'i'
|
||||
2, 16, 13, // 106 - 'j'
|
||||
3, 31, 8, 20, // 107 - 'k'
|
||||
1, 31, // 108 - 'l'
|
||||
3, 30, 6, 30, // 109 - 'm'
|
||||
3, 30, 2, 28, // 110 - 'n'
|
||||
3, 12, 18, 12, // 111 - 'o'
|
||||
3, 30, 10, 4, // 112 - 'p'
|
||||
3, 4, 10, 30, // 113 - 'q'
|
||||
2, 30, 4, // 114 - 'r'
|
||||
3, 20, 30, 10, // 115 - 's'
|
||||
3, 4, 30, 4, // 116 - 't'
|
||||
3, 14, 16, 30, // 117 - 'u'
|
||||
3, 14, 16, 14, // 118 - 'v'
|
||||
3, 14, 24, 14, // 119 - 'w'
|
||||
3, 18, 12, 18, // 120 - 'x'
|
||||
3, 22, 24, 14, // 121 - 'y'
|
||||
3, 26, 30, 22, // 122 - 'z'
|
||||
3, 4, 27, 17, // 123 - '{'
|
||||
1, 27, // 124 - '|'
|
||||
3, 17, 27, 4, // 125 - '}'
|
||||
3, 6, 2, 3, // 126 - '~'
|
||||
3, 31, 31, 31, // 127 - 'Full Block'
|
||||
};
|
||||
@@ -0,0 +1,466 @@
|
||||
// Implements the game of Pong using MD_MAXPanel library
|
||||
//
|
||||
// Hardware Used
|
||||
// =============
|
||||
// UP_PIN - left bat up switch, INPUT_PULLUP
|
||||
// RIGHT_PIN - right bat up switch, INPUT_PULLUP
|
||||
// RIGHT_PIN - left bat up switch, INPUT_PULLUP
|
||||
// DOWN_PIN - right bat up switch, INPUT_PULLUP
|
||||
// SELECT_PIN - unused
|
||||
// ENTER_PIN - unused
|
||||
// BEEPER_PIN - piezo speaker
|
||||
// CLK_PIN, DATA_PIN, CS_PIN - LED matrix display connections
|
||||
//
|
||||
// Libraries used
|
||||
// ==============
|
||||
// MD_MAX72XX available from https://github.com/MajicDesigns/MD_MAX72XX
|
||||
//
|
||||
// Rules of the Game
|
||||
// =================
|
||||
// Simple game like table tennis where each player has to bat the ball to keep
|
||||
// it in play. Points awarded when the ball goes out at the opponents side. First
|
||||
// to reach MAX_SCORE is the winner.
|
||||
|
||||
|
||||
#include <MD_MAXPanel.h>
|
||||
#include "Font5x3.h"
|
||||
#include "score.h"
|
||||
#include "sound.h"
|
||||
|
||||
// Turn on debug statements to the serial output
|
||||
#define DEBUG 0
|
||||
|
||||
#if DEBUG
|
||||
#define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
|
||||
#define PRINTS(x) { Serial.print(F(x)); }
|
||||
#define PRINTD(x) { Serial.print(x, DEC); }
|
||||
#define PRINTXY(s, x, y) { Serial.print(s); Serial.print(F("(")); Serial.print(x); Serial.print(F(",")); Serial.print(y); Serial.print(")"); }
|
||||
|
||||
#else
|
||||
#define PRINT(s, x)
|
||||
#define PRINTS(x)
|
||||
#define PRINTD(x)
|
||||
#define PRINTXY(s, x, y)
|
||||
|
||||
#endif
|
||||
|
||||
// Hardware pin definitions.
|
||||
// All momentary on switches are initialised INPUT_PULLUP
|
||||
const uint8_t UP_PIN = 2;
|
||||
const uint8_t RIGHT_PIN = 3;
|
||||
const uint8_t DOWN_PIN = 4;
|
||||
const uint8_t LEFT_PIN = 5;
|
||||
const uint8_t SELECT_PIN = 6;
|
||||
const uint8_t ENTER_PIN = 7;
|
||||
const uint8_t BEEPER_PIN = 9;
|
||||
|
||||
// Define the number of devices in the chain and the SPI hardware interface
|
||||
// NOTE: These pin numbers will probably not work with your hardware and may
|
||||
// need to be adapted
|
||||
const MD_MAX72XX::moduleType_t HARDWARE_TYPE = MD_MAX72XX::FC16_HW;
|
||||
const uint8_t X_DEVICES = 4;
|
||||
const uint8_t Y_DEVICES = 5;
|
||||
|
||||
const uint8_t CLK_PIN = 13; // or SCK
|
||||
const uint8_t DATA_PIN = 11; // or MOSI
|
||||
const uint8_t CS_PIN = 10; // or SS
|
||||
|
||||
// SPI hardware interface
|
||||
MD_MAXPanel mp = MD_MAXPanel(HARDWARE_TYPE, CS_PIN, X_DEVICES, Y_DEVICES);
|
||||
// Arbitrary pins
|
||||
// MD_MAXPanel mx = MD_MAXPanel(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, X_DEVICES, Y_DEVICES);
|
||||
|
||||
uint16_t FIELD_TOP; // needs to be initialised in setup()
|
||||
const uint16_t FIELD_BOTTOM = 0;
|
||||
|
||||
const uint8_t BAT_SIZE_DEFAULT = 3;
|
||||
const uint8_t BAT_EDGE_OFFSET = 1;
|
||||
|
||||
const char TITLE_TEXT[] = "PONG";
|
||||
const uint16_t SPLASH_DELAY = 5000; // in milliseconds
|
||||
|
||||
const char GAME_TEXT[] = "GAME";
|
||||
const char OVER_TEXT[] = "OVER";
|
||||
const uint16_t GAME_OVER_DELAY = 3000; // in milliseconds
|
||||
|
||||
const uint8_t FONT_NUM_WIDTH = 3;
|
||||
const uint8_t MAX_SCORE = 11;
|
||||
|
||||
// A class to encapsulate the pong bat
|
||||
// Bats are used either side of the display, moving up and down
|
||||
class cPongBat
|
||||
{
|
||||
private:
|
||||
uint16_t _x, _y; // the position of the center of the bat
|
||||
uint16_t _ymin, _ymax; // the max and min bat boundaries
|
||||
int8_t _vel; // the velocity of the bat (+1 for moving up, -1 moving down)
|
||||
uint8_t _size; // the size in pixels for the bat (odd number)
|
||||
uint8_t _pinUp; // the pin for the up switch
|
||||
uint8_t _pinDown; // the pin for th down switch
|
||||
uint16_t _batDelay; // the delay between possible moves of the bat in milliseconds
|
||||
uint32_t _timeLastMove; // the millis() value for the last time we moved the bat
|
||||
|
||||
public:
|
||||
enum hitType_t { NO_HIT, CORNER_HIT, FLAT_HIT };
|
||||
|
||||
void begin(uint16_t x, uint16_t y, uint16_t ymin, uint16_t ymax, uint8_t size, uint8_t pinU, uint8_t pinD)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_ymin = ymin;
|
||||
_ymax = ymax;
|
||||
_vel = 0;
|
||||
_size = size;
|
||||
_pinUp = pinU;
|
||||
_pinDown = pinD;
|
||||
_batDelay = 40;
|
||||
pinMode(_pinUp, INPUT_PULLUP);
|
||||
pinMode(_pinDown, INPUT_PULLUP);
|
||||
}
|
||||
|
||||
uint16_t getX(void) { return (_x); }
|
||||
uint16_t getY(void) { return (_y); }
|
||||
|
||||
int8_t getVelocity(void) { return(_vel); }
|
||||
void draw(void) { mp.drawVLine(_x, _y - (_size / 2), _y + (_size / 2), true); }
|
||||
void erase(void) { mp.drawVLine(_x, _y - (_size / 2), _y + (_size / 2), false); }
|
||||
bool anyKey(void) { return((digitalRead(_pinUp) == LOW) || (digitalRead(_pinDown) == LOW)); }
|
||||
|
||||
hitType_t hit(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
|
||||
{
|
||||
int16_t dy = y1 - y0;
|
||||
|
||||
// if we are not in the same x plane as the bat there can be no hit
|
||||
if (x1 != _x) return(NO_HIT);
|
||||
|
||||
// if the ball is at
|
||||
// - the top of the bat and traveling down, or
|
||||
// - the bottom of the bat and traveling up
|
||||
// then it is a corner hit
|
||||
if ((y1 == _y + (_size / 2) && dy < 0) || (y1 == _y - (_size / 2) && dy>0))
|
||||
return(CORNER_HIT);
|
||||
|
||||
// the ball is between the top and bottom of the bat boundaries inclusive
|
||||
// then it is a flat hit
|
||||
if (y1 <= _y + (_size / 2) && y1 >= _y - (_size / 2))
|
||||
return(FLAT_HIT);
|
||||
|
||||
// and in case we missed something, a default no hit
|
||||
return(NO_HIT);
|
||||
}
|
||||
|
||||
void move(void)
|
||||
{
|
||||
bool moveUp = (digitalRead(_pinUp) == LOW);
|
||||
bool moveDown = (digitalRead(_pinDown) == LOW);
|
||||
|
||||
// if this is the time for a move?
|
||||
if (millis() - _timeLastMove <= _batDelay)
|
||||
return;
|
||||
_timeLastMove = millis();
|
||||
|
||||
mp.update(false);
|
||||
|
||||
if (moveUp)
|
||||
{
|
||||
PRINTS("\n-- BAT move up");
|
||||
erase();
|
||||
_vel = 1;
|
||||
_y++;
|
||||
if (_y + (_size/2) > _ymax) _y--; // keep within top boundary
|
||||
draw();
|
||||
}
|
||||
else if (moveDown)
|
||||
{
|
||||
PRINTS("\n-- BAT move down");
|
||||
erase();
|
||||
_vel = -1;
|
||||
_y--;
|
||||
if (_y - (_size/2) < _ymin) _y++; // keep within bottom boundary
|
||||
draw();
|
||||
}
|
||||
else
|
||||
_vel = 0;
|
||||
|
||||
mp.update(true);
|
||||
}
|
||||
};
|
||||
|
||||
// A class to encapsulate the pong ball
|
||||
// Ball will bounce around. Bounces off the edges and bats.
|
||||
class cPongBall
|
||||
{
|
||||
private:
|
||||
uint16_t _x, _y; // the position of the center of the ball
|
||||
int8_t _dx, _dy; // the offsets for the x and y direction
|
||||
uint32_t _timeLastMove; // last time the ball was moved
|
||||
uint16_t _ballDelay; // the delay between ball moves in milliseconds
|
||||
bool _run; // ball is running when true
|
||||
|
||||
public:
|
||||
enum bounce_t { BOUNCE_NONE, BOUNCE_BACK, BOUNCE_TOP, BOUNCE_BOTTOM, BOUNCE_LEFT, BOUNCE_RIGHT };
|
||||
|
||||
void begin(uint16_t x, uint16_t y)
|
||||
{
|
||||
reset(x, y);
|
||||
_dx = _dy = 1;
|
||||
_timeLastMove = 0;
|
||||
_ballDelay = 100;
|
||||
_run = false;
|
||||
}
|
||||
|
||||
uint16_t getX(void) { return (_x); }
|
||||
uint16_t getY(void) { return (_y); }
|
||||
uint16_t getNextX(void) { return (_x + _dx); }
|
||||
uint16_t getNextY(void) { return (_y + _dy); }
|
||||
|
||||
void start(void) { _run = true; }
|
||||
void stop(void) { _run = false; }
|
||||
void draw(void) { mp.setPoint(_x, _y, true); } //PRINTS("\nball@"); PRINTXY(_x, _y); }
|
||||
void erase(void) { mp.setPoint(_x, _y, false); }
|
||||
void reset(uint16_t x, uint16_t y) { _x = x; _y = y; }
|
||||
|
||||
bool move(void)
|
||||
{
|
||||
// if this is the time for a move?
|
||||
if (_run && millis() - _timeLastMove <= _ballDelay)
|
||||
return(false);
|
||||
_timeLastMove = millis();
|
||||
|
||||
// do the animation
|
||||
mp.update(false);
|
||||
erase();
|
||||
_x = _x + _dx;
|
||||
_y = _y + _dy;
|
||||
draw();
|
||||
mp.update(true);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void bounce(bounce_t b)
|
||||
{
|
||||
switch (b)
|
||||
{
|
||||
case BOUNCE_TOP:
|
||||
case BOUNCE_BOTTOM: _dy = -_dy; break;
|
||||
case BOUNCE_LEFT:
|
||||
case BOUNCE_RIGHT: _dx = -_dx; break;
|
||||
case BOUNCE_BACK: _dx = -_dx; _dy = -_dy; break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// main objects coordinated by the code logic
|
||||
cPongBat batL, batR;
|
||||
cScore scoreL, scoreR;
|
||||
cPongBall ball;
|
||||
cSound sound;
|
||||
|
||||
void centerLine(void)
|
||||
// Dotted line down the middle
|
||||
{
|
||||
bool bOn = true;
|
||||
|
||||
mp.update(false);
|
||||
for (uint16_t y = FIELD_BOTTOM + 1; y < FIELD_TOP; y++)
|
||||
{
|
||||
mp.setPoint(mp.getXMax() / 2, y, bOn);
|
||||
mp.setPoint((mp.getXMax() + 1) / 2, y, bOn);
|
||||
bOn = !bOn;
|
||||
}
|
||||
mp.update(true);
|
||||
}
|
||||
|
||||
void setupField(void)
|
||||
// Draw the playing field at the start of the game.
|
||||
{
|
||||
mp.clear();
|
||||
mp.drawHLine(FIELD_BOTTOM, 0, mp.getXMax());
|
||||
mp.drawHLine(FIELD_TOP, 0, mp.getXMax());
|
||||
centerLine();
|
||||
batL.draw();
|
||||
batR.draw();
|
||||
scoreL.draw();
|
||||
scoreR.draw();
|
||||
ball.draw();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
#if DEBUG
|
||||
Serial.begin(57600);
|
||||
#endif
|
||||
PRINTS("\n[MD_MAXPanel_Pong]");
|
||||
|
||||
mp.begin();
|
||||
mp.setFont(_Fixed_5x3);
|
||||
mp.setIntensity(4);
|
||||
mp.setRotation(MD_MAXPanel::ROT_90);
|
||||
|
||||
sound.begin(BEEPER_PIN);
|
||||
|
||||
FIELD_TOP = mp.getYMax() - mp.getFontHeight() - 2;
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
static enum { S_SPLASH, S_INIT, S_GAME_START, S_POINT_PLAY, S_POINT_END, S_WAIT_LSTART, S_WAIT_RSTART, S_GAME_OVER } runState = S_SPLASH;
|
||||
|
||||
switch (runState)
|
||||
{
|
||||
case S_SPLASH: // show splash screen at start
|
||||
{
|
||||
const uint16_t border = 2;
|
||||
|
||||
mp.clear();
|
||||
mp.drawRectangle(border, border, mp.getXMax() - border, mp.getYMax() - border);
|
||||
mp.drawLine(0, 0, border, border);
|
||||
mp.drawLine(0, mp.getYMax(), border, mp.getYMax()-border);
|
||||
mp.drawLine(mp.getXMax(), 0, mp.getXMax()-border, border);
|
||||
mp.drawLine(mp.getXMax(), mp.getYMax(), mp.getXMax()-border, mp.getYMax()-border);
|
||||
mp.drawText((mp.getXMax() - mp.getTextWidth(TITLE_TEXT)) / 2, (mp.getYMax() + mp.getFontHeight())/2 , TITLE_TEXT);
|
||||
sound.splash();
|
||||
delay(SPLASH_DELAY);
|
||||
runState = S_INIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_INIT: // initialise for a new game
|
||||
batL.begin(BAT_EDGE_OFFSET, (FIELD_TOP - FIELD_BOTTOM) / 2, FIELD_BOTTOM + 1, FIELD_TOP - 1, BAT_SIZE_DEFAULT, UP_PIN, LEFT_PIN);
|
||||
batR.begin(mp.getXMax() - BAT_EDGE_OFFSET, (FIELD_TOP - FIELD_BOTTOM) / 2, FIELD_BOTTOM + 1, FIELD_TOP - 1, BAT_SIZE_DEFAULT, RIGHT_PIN, DOWN_PIN);
|
||||
|
||||
scoreL.begin(&mp, BAT_EDGE_OFFSET, FIELD_TOP + 1 + mp.getFontHeight(), MAX_SCORE);
|
||||
scoreR.limit(MAX_SCORE); // set width() used below
|
||||
scoreR.begin(&mp, mp.getXMax() - (scoreR.width() * (FONT_NUM_WIDTH + mp.getCharSpacing())) + mp.getCharSpacing(), FIELD_TOP + 1 + mp.getFontHeight(), MAX_SCORE);
|
||||
|
||||
ball.begin((mp.getXMax() / 2) - BAT_EDGE_OFFSET - 1, (FIELD_TOP - FIELD_BOTTOM) / 3);
|
||||
|
||||
setupField();
|
||||
|
||||
runState = S_GAME_START;
|
||||
break;
|
||||
|
||||
case S_GAME_START: // waiting for the start of a new game
|
||||
if (batL.anyKey() || batR.anyKey())
|
||||
{
|
||||
PRINTS("\n-- Starting Game");
|
||||
scoreL.reset();
|
||||
scoreR.reset();
|
||||
sound.start();
|
||||
ball.start();
|
||||
runState = S_POINT_PLAY;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_POINT_PLAY: // playing a point
|
||||
// handle the bat animation first
|
||||
batL.move();
|
||||
batR.move();
|
||||
|
||||
// now move the ball and check what this means
|
||||
if (ball.move())
|
||||
{
|
||||
cPongBat::hitType_t lastHit;
|
||||
|
||||
// redraw the centerline if the ball is near it
|
||||
if (ball.getX() >= (mp.getXMax() / 2) - 1 || ball.getX() >= (mp.getXMax() / 2) + 2)
|
||||
{
|
||||
centerLine();
|
||||
ball.draw();
|
||||
}
|
||||
|
||||
// check for top/bottom edge collisions
|
||||
if (ball.getY() == FIELD_TOP - 1)
|
||||
{
|
||||
PRINTS("\n-- COLLISION top edge");
|
||||
ball.bounce(cPongBall::BOUNCE_TOP);
|
||||
sound.bounce();
|
||||
}
|
||||
else if (ball.getY() == FIELD_BOTTOM + 1)
|
||||
{
|
||||
PRINTS("\n-- COLLISION bottom edge");
|
||||
ball.bounce(cPongBall::BOUNCE_BOTTOM);
|
||||
sound.bounce();
|
||||
}
|
||||
|
||||
// check for bat collisions
|
||||
if ((lastHit = batL.hit(ball.getX(), ball.getY(), ball.getNextX(), ball.getNextY())) != cPongBat::NO_HIT)
|
||||
{
|
||||
PRINTS("\n-- COLLISION left bat");
|
||||
ball.bounce(lastHit == cPongBat::CORNER_HIT ? cPongBall::BOUNCE_BACK: cPongBall::BOUNCE_LEFT);
|
||||
sound.hit();
|
||||
}
|
||||
else if ((lastHit = batR.hit(ball.getX(), ball.getY(), ball.getNextX(), ball.getNextY())) != cPongBat::NO_HIT)
|
||||
{
|
||||
PRINTS("\n-- COLLISION right bat");
|
||||
ball.bounce(lastHit == cPongBat::CORNER_HIT ? cPongBall::BOUNCE_BACK : cPongBall::BOUNCE_RIGHT);
|
||||
sound.hit();
|
||||
}
|
||||
|
||||
// check for out of bounds
|
||||
if ((ball.getX() < BAT_EDGE_OFFSET) || (ball.getX() > mp.getXMax() - BAT_EDGE_OFFSET))
|
||||
{
|
||||
PRINTS("\n-- OUT!");
|
||||
runState = S_POINT_END;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case S_POINT_END: // handle the ball going out
|
||||
ball.stop();
|
||||
batL.draw();
|
||||
batR.draw();
|
||||
sound.point();
|
||||
delay(500);
|
||||
ball.erase();
|
||||
if (ball.getX() < BAT_EDGE_OFFSET) // out on the left side
|
||||
{
|
||||
PRINTS("\n--- LEFT side");
|
||||
ball.reset(BAT_EDGE_OFFSET + 1, batL.getY());
|
||||
ball.bounce(cPongBall::BOUNCE_LEFT);
|
||||
scoreR.increment();
|
||||
if (scoreR.score() == MAX_SCORE)
|
||||
runState = S_GAME_OVER;
|
||||
else
|
||||
runState = S_WAIT_LSTART;
|
||||
}
|
||||
else // out on the right side
|
||||
{
|
||||
PRINTS("\n--- RIGHT side");
|
||||
ball.reset(mp.getXMax() - BAT_EDGE_OFFSET - 1, batR.getY());
|
||||
ball.bounce(cPongBall::BOUNCE_RIGHT);
|
||||
scoreL.increment();
|
||||
if (scoreL.score() == MAX_SCORE)
|
||||
runState = S_GAME_OVER;
|
||||
else
|
||||
runState = S_WAIT_RSTART;
|
||||
}
|
||||
ball.draw();
|
||||
break;
|
||||
|
||||
case S_WAIT_LSTART: // waiting for left playter to restart the game
|
||||
if (batL.anyKey())
|
||||
{
|
||||
ball.start();
|
||||
runState = S_POINT_PLAY;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_WAIT_RSTART: // waiting fo the right player to restrt the game
|
||||
if (batR.anyKey())
|
||||
{
|
||||
ball.start();
|
||||
runState = S_POINT_PLAY;
|
||||
}
|
||||
break;
|
||||
|
||||
case S_GAME_OVER:
|
||||
mp.drawText((mp.getXMax() - mp.getTextWidth(GAME_TEXT))/2, (FIELD_TOP - FIELD_BOTTOM)/2 + mp.getFontHeight() + 1, GAME_TEXT);
|
||||
mp.drawText((mp.getXMax() - mp.getTextWidth(OVER_TEXT)) / 2, (FIELD_TOP - FIELD_BOTTOM)/2 - 1, OVER_TEXT);
|
||||
sound.over();
|
||||
delay(GAME_OVER_DELAY);
|
||||
runState = S_INIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <MD_MAXPanel.h>
|
||||
|
||||
// A class to encapsulate the score display
|
||||
class cScore
|
||||
{
|
||||
private:
|
||||
MD_MAXPanel *_mp; // the MAXPanel object for drawing the score
|
||||
uint16_t _score; // the score
|
||||
uint16_t _x, _y; // coordinate of top left for display
|
||||
uint8_t _width; // number of digits wide
|
||||
uint16_t _limit; // maximum value allowed
|
||||
|
||||
public:
|
||||
void begin(MD_MAXPanel *mp, uint16_t x, uint16_t y, uint16_t maxScore) { _mp = mp; _x = x, _y = y; limit(maxScore); reset(); }
|
||||
void reset(void) { erase(); _score = 0; draw(); }
|
||||
void set(uint16_t s) { if (s <= _limit) { erase(); _score = s; draw(); } }
|
||||
void increment(uint16_t inc = 1) { if (_score + inc <= _limit) { erase(); _score += inc; draw(); } }
|
||||
void decrement(uint16_t dec = 1) { if (_score >= dec) { erase(); _score -= dec; draw(); } }
|
||||
uint16_t score(void) { return(_score); }
|
||||
void erase(void) { draw(false); }
|
||||
uint16_t width(void) { return(_width); }
|
||||
|
||||
void limit(uint16_t m)
|
||||
{
|
||||
erase(); // width may change, so delete with the curret parameters
|
||||
|
||||
_limit = m;
|
||||
// work out how many digits this is
|
||||
_width = 0;
|
||||
do
|
||||
{
|
||||
_width++;
|
||||
m /= 10;
|
||||
} while (m != 0);
|
||||
}
|
||||
|
||||
void draw(bool state = true)
|
||||
{
|
||||
char sz[_width + 1];
|
||||
uint16_t s = _score;
|
||||
|
||||
// PRINT("\n-- SCORE: ", _score);
|
||||
sz[_width] = '\0';
|
||||
for (int i = _width - 1; i >= 0; --i)
|
||||
{
|
||||
sz[i] = (s % 10) + '0';
|
||||
s /= 10;
|
||||
}
|
||||
|
||||
_mp->drawText(_x, _y, sz, MD_MAXPanel::ROT_0, state);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
// A class to encapsulate primitive sound effects
|
||||
class cSound
|
||||
{
|
||||
private:
|
||||
const uint16_t EOD = 0; // End Of Data marker
|
||||
uint8_t _pinBeep; // the pin to use for beeping
|
||||
|
||||
// Sound data - frequency followed by duration in pairs.
|
||||
// Data ends in End Of Data marker EOD.
|
||||
const uint16_t soundSplash[1] PROGMEM = { EOD };
|
||||
const uint16_t soundHit[3] PROGMEM = { 1000, 50, EOD };
|
||||
const uint16_t soundBounce[3] PROGMEM = { 500, 50, EOD };
|
||||
const uint16_t soundPoint[3] PROGMEM = { 150, 150, EOD };
|
||||
const uint16_t soundStart[7] PROGMEM = { 250, 100, 500, 100, 1000, 100, EOD };
|
||||
const uint16_t soundOver[7] PROGMEM = { 1000, 100, 500, 100, 250, 100, EOD };
|
||||
|
||||
void playSound(const uint16_t *table)
|
||||
// Play sound table data. Data table must end in EOD marker.
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
//PRINTS("\nTone Data ");
|
||||
while (table[idx] != EOD)
|
||||
{
|
||||
uint16_t t = table[idx++];
|
||||
uint16_t d = table[idx++];
|
||||
|
||||
//PRINTXY("-", t, d);
|
||||
tone(_pinBeep, t);
|
||||
delay(d);
|
||||
}
|
||||
//PRINTS("-EOD");
|
||||
noTone(_pinBeep); // be quiet now!
|
||||
}
|
||||
|
||||
public:
|
||||
void begin(uint8_t pinBeep) { _pinBeep = pinBeep; }
|
||||
void splash(void) { playSound(soundSplash); }
|
||||
void start(void) { playSound(soundStart); }
|
||||
void hit(void) { playSound(soundHit); }
|
||||
void bounce(void) { playSound(soundBounce); }
|
||||
void point(void) { playSound(soundPoint); }
|
||||
void over(void) { playSound(soundOver); }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user