-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated to 1.1 release, added pong and moved libs
- Loading branch information
Anne Jan Brouwer
committed
Nov 14, 2014
1 parent
c3c5c40
commit 67f2dd6
Showing
31 changed files
with
750 additions
and
18 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <Arduino.h> | ||
|
||
#include "Ball.h" | ||
|
||
int Ball::randomAngle(int row) | ||
{ | ||
int angle = random(1, 4) * 45; | ||
|
||
if ((row == 0 && angle < 90) || | ||
(row == 7 && angle > 90)) | ||
angle = 90; | ||
|
||
return angle; | ||
} | ||
|
||
unsigned long Ball::accelerate(unsigned long delay) | ||
{ | ||
if (MIN_DELAY + DELAY_STEP <= delay) | ||
delay -= DELAY_STEP; | ||
|
||
return delay; | ||
} | ||
|
||
bool Ball::move(Paddle& paddle0, Paddle& paddle1, unsigned long& delay, int &startPlayer) | ||
{ | ||
bool carryOn = true; | ||
|
||
if (angle_ > 0) { | ||
col_++; | ||
} else { | ||
col_--; | ||
} | ||
|
||
if (col_ == MAX_COLUMN) { | ||
if (paddle1.hit(row_, col_)) { | ||
col_ = MAX_COLUMN - 2; | ||
angle_ = -randomAngle(row_); | ||
delay = accelerate(delay); | ||
} else { | ||
carryOn = false; | ||
startPlayer = 0; | ||
} | ||
} else if (col_ == MIN_COLUMN) { | ||
if (paddle0.hit(row_, col_)) { | ||
col_ = MIN_COLUMN + 2; | ||
angle_ = randomAngle(row_); | ||
delay = accelerate(delay); | ||
} else { | ||
carryOn = false; | ||
startPlayer = 1; | ||
} | ||
} else if (row_ == MIN_ROW) { | ||
if (abs(angle_) < 90) { | ||
angle_ *= 3; | ||
} | ||
} else if (row_ == MAX_ROW) { | ||
if (abs(angle_) > 90) { | ||
angle_ /= 3; | ||
} | ||
} | ||
|
||
if (abs(angle_) > 90) { | ||
row_++; | ||
} else if (abs(angle_) < 90) { | ||
row_--; | ||
} | ||
|
||
return carryOn; | ||
} | ||
|
||
// Local variables: | ||
// mode: c++ | ||
// End: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef BALL_H | ||
#define BALL_H | ||
|
||
#include "Paddle.h" | ||
|
||
class Ball { | ||
public: | ||
Ball(int rows, int cols) : | ||
MIN_ROW(0), | ||
MIN_COLUMN(0), | ||
MAX_ROW(rows - 1), | ||
MAX_COLUMN(cols - 1) {} | ||
|
||
static const unsigned long MIN_DELAY = 50; | ||
static const unsigned long MAX_DELAY = 300; | ||
static const unsigned long DELAY_STEP = 10; | ||
|
||
int row() const { return row_; } | ||
int col() const { return col_; } | ||
|
||
void setPosition(int row, int col, int angle) { | ||
row_ = row; | ||
col_ = col; | ||
angle_ = angle; | ||
} | ||
|
||
bool move(Paddle& paddle0, Paddle& paddle1, unsigned long& delay, int &startPlayer); | ||
|
||
private: | ||
const int MIN_ROW, MIN_COLUMN; | ||
const int MAX_ROW, MAX_COLUMN; | ||
|
||
int randomAngle(int row); | ||
unsigned long accelerate(unsigned long delay); | ||
|
||
int row_; | ||
int col_; | ||
int angle_; | ||
}; | ||
|
||
#endif // BALL_H | ||
|
||
// Local variables: | ||
// mode: c++ | ||
// End: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef PADDLE_H | ||
#define PADDLE_H | ||
|
||
class Paddle { | ||
public: | ||
Paddle(int pin, int row, int col) : | ||
pin_(pin), row_(row), col_(col) {} | ||
|
||
void row(int row) { row_ = row; } | ||
|
||
bool hit(int row, int col) const { | ||
return col == col_ && (row == row_ || row == row_ + 1); | ||
} | ||
|
||
int pin() const { return pin_; } | ||
int row() const { return row_; } | ||
int col() const { return col_; } | ||
|
||
private: | ||
int pin_; | ||
int row_; | ||
const int col_; | ||
}; | ||
|
||
#endif // PADDLE_H | ||
|
||
// Local variables: | ||
// mode: c++ | ||
// End: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <Arduino.h> | ||
|
||
#include "Pong.h" | ||
|
||
void Pong::start() | ||
{ | ||
if (startPlayer_ == 0) { | ||
ball_.setPosition(4, 1, 90); | ||
} else { | ||
ball_.setPosition(4, MAX_COLUMN - 1, -90); | ||
} | ||
|
||
showBall(ball_); | ||
t_ = t0_ = millis(); | ||
delay_ = Ball::MAX_DELAY; | ||
} | ||
|
||
void Pong::update() | ||
{ | ||
movePaddle(paddle0_); | ||
movePaddle(paddle1_); | ||
|
||
if ((t_ = millis()) - t0_ > delay_) { | ||
if (!moveBall()) { | ||
if (gameOver()) { | ||
startGame(); | ||
} else { | ||
showScore(); | ||
start(); | ||
} | ||
} else { | ||
t0_ = t_; | ||
} | ||
} | ||
} | ||
|
||
void Pong::movePaddle(Paddle& p) | ||
{ | ||
int row1 = map(analogRead(p.pin()), 0, 1000, 0, MAX_ROW - 1); | ||
int row2 = map(analogRead(p.pin()), 0, 1000, 0, MAX_ROW - 1); | ||
|
||
int row = (row1 + row2) / 2; | ||
|
||
if (row != p.row()) { | ||
hidePaddle(p); | ||
p.row(row); | ||
} | ||
showPaddle(p); | ||
} | ||
|
||
bool Pong::moveBall() | ||
{ | ||
hideBall(ball_); | ||
|
||
bool carryOn = ball_.move(paddle0_, paddle1_, delay_, startPlayer_); | ||
|
||
if (carryOn) { | ||
showBall(ball_); | ||
} else { | ||
score_[startPlayer_]++; | ||
} | ||
|
||
return carryOn; | ||
} | ||
|
||
bool Pong::gameOver() | ||
{ | ||
if (score_[0] > 9 || score_[1] > 9) { | ||
// show winner | ||
score_[0] = 0; | ||
score_[1] = 0; | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef PONG_H | ||
#define PONG_H | ||
|
||
#include "Ball.h" | ||
#include "Paddle.h" | ||
|
||
class Pong | ||
{ | ||
public: | ||
Pong(int rows, int cols, int paddle0_pin, int paddle1_pin) : | ||
MAX_ROW(rows - 1), | ||
MAX_COLUMN(cols - 1), | ||
paddle0_(paddle0_pin, (MAX_ROW / 2) - 1, 0), | ||
paddle1_(paddle1_pin, (MAX_ROW / 2) - 1, MAX_COLUMN), | ||
ball_(rows, cols), | ||
startPlayer_(0) { | ||
score_[0] = 0; | ||
score_[1] = 0; | ||
} | ||
|
||
void start(); | ||
void update(); | ||
|
||
protected: | ||
const int MAX_ROW; | ||
const int MAX_COLUMN; | ||
|
||
void movePaddle(Paddle& p); | ||
bool moveBall(); | ||
bool gameOver(); | ||
|
||
virtual void showPaddle(Paddle& p) = 0; | ||
virtual void hidePaddle(Paddle& p) = 0; | ||
virtual void showBall(Ball& b) = 0; | ||
virtual void hideBall(Ball& b) = 0; | ||
virtual void showScore() = 0; | ||
virtual void startGame() = 0; | ||
|
||
Paddle paddle0_; | ||
Paddle paddle1_; | ||
Ball ball_; | ||
int startPlayer_; | ||
int score_[2]; | ||
unsigned long t_, t0_, delay_; | ||
}; | ||
|
||
#endif // PONG_H | ||
|
||
// Local variables: | ||
// mode: c++ | ||
// End: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
PONG | ||
==== | ||
|
||
Arduino Pong for an 8x8 LED Matrix and the LoLShield. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build-* | ||
libraries |
Oops, something went wrong.