#include <FEHLCD.h>
#include <FEHIO.h>
#include <FEHUtility.h>
#include <stdlib.h>
/*class that will be used for the paddle, blockx and blocky are
* coordinates for paddle */
class Block
{
public:
Block();
int BlockPlacement(float, float, int);
void RedrawR();
void RedrawL();
float blockx, blocky;
};
/*class that will be used for the ball, Xball and Yball are coordinates of ball,
Y/X min/max are the values of the edges of the screen, and broken is an array
containing counters that are 0 when a brick has not been hit and 1 after they’ve been hit*/
class Ball
{
public:
Ball();
int RedrawBall(float, float, int, int, int *);
void InitialTouch();
float Xball, Yball, Radius, Ymax, Ymin, Xmax, Xmin;
int wait_time, Xvel, Yvel, score, broken[22];
};
/*class for the bricks, Xstart and Ystart are the coordinates of the top
left corner of each brick*/
class Brick
{
public:
Brick();
void DrawBricks(int []);
float Xstart,Ystart, Width, Height;
};
int main()
{
/*game will always be 1 to allow game to run indefinitely */
int game = 1, highscore, i = 0, p;
/* j and k are used to save the coordinates that are gotten from
the touch command that is used several times to make the game wait for the
user to stop touching the screen*/
float x, y, j, k;
// indefinite do while loop to run the game in
do
{
LCD.Clear();
int score_quit = 1, play = 1, touches = 0;
/* create start menu: draw rectangles with the title of each option that user
* can tap in to choose. */
// titles of each, .WriteRC gives the row and column where we want the text
LCD.WriteRC(“Play”, 2, 11);
LCD.WriteRC(“High Scores”, 5,8 );
LCD.WriteRC(“Instruction Manual”, 8,6);
LCD.WriteRC(“Credits”, 11, 10);
/* draw rectangles around each title to define the area where user can tap,
* each rectangle spans the width of the screen in the x direction with 2 pixels
* on each side, and leaves 2 pixels between each rectangle in the y direction
* along with 2 pixels between the rectangles and the top/bottom of the screen */
LCD.DrawRectangle(2,2,315,58);
LCD.DrawRectangle(2,60,315,58);
LCD.DrawRectangle(2,118,315,58);
LCD.DrawRectangle(2,176, 315, 58);
// wait for user to touch screen, save coordinates of screen touch, wait for user to stop touching screen
while (!LCD.Touch(&x,&y));
LCD.Touch(&x,&y);
while (LCD.Touch(&j,&k));
// perform an action if the user touched somewhere within one of the rectangles
if (x > 2 && x < 315 && y > 2 && y < 60)
{
LCD.Clear();
// create needed objects
Block paddle;
Ball sphere;
Brick Bricky;
/* draw left and right movement buttons, a quit button, and the top line of the play area */
LCD.DrawRectangle(245,2,30,30);
LCD.DrawRectangle(287,2,30,30);
LCD.DrawRectangle(2, 2, 30, 30);
LCD.DrawLine(0,34,319,34); // top line
LCD.WriteAt(“>”,408,23);
LCD.WriteRC(“<“,1,23);
LCD.WriteRC(“Quit”,1,2);
// call function to draw the bricks
Bricky.DrawBricks(sphere.broken);
// will run until loss condition changes play to 0
while (play ==1)
{
if (touches == 0) // only runs if screen has not been touched since loop started
{
sphere.InitialTouch(); // calls function that starts ball movement
touches = 1; // reads that screen has been touched
}
while (play==1)
{
if (LCD.Touch(&x,&y)) // if screen is being touched
{
play = paddle.BlockPlacement(x,y, play); // call function that moves paddle
}
if (!LCD.Touch(&x,&y)) // if screen is not being touched
{
Bricky.DrawBricks(sphere.broken); // calls function that moves sphere
play = sphere.RedrawBall(paddle.blockx, paddle.blocky, play, i, &highscore); // will set play to 0 if loss condition happens
}
}
}
}
else if (x > 2 && x < 315 && y > 60 && y < 118) // if user taps high score
{
LCD.Clear();
LCD.WriteRC(“The highscore is: “, 3, 2);
LCD.WriteRC(highscore, 3, 10); // displays high score
while (!LCD.Touch(&x,&y));
LCD.Touch(&x,&y);
while (LCD.Touch(&j,&k));
// waits for user to touch and release to return to menu
}
else if (x > 2 && x < 315 && y > 118 && y < 176) // if user taps manual
{
x = 0;
y = 0;
j = 0;
k =0;
score_quit = 1;
LCD.Clear();
// prints page one of manual
LCD.WriteRC(“How to Play”, 1, 1);
LCD.WriteRC(“Stop the ball from “, 3, 1);
LCD.WriteRC(“bouncing past your paddle!”, 5,1);
LCD.WriteRC(“Move the paddle side to side”, 7, 1);
LCD.WriteRC(“by touching the left and right buttons”, 9, 1);
LCD.WriteRC(“labeled as ‘<’ and ‘>’ respectively”, 11, 1);
while(!LCD.Touch(&x,&y));
LCD.Touch(&x,&y);
while(!LCD.Touch(&j,&k));
// waits for user to tap to second page
LCD.Clear();
// prints second page of manual
LCD.WriteRC(“Get points by bouncing the ball” , 1, 1);
LCD.WriteRC(“against the bricks!”, 3, 1);
LCD.WriteRC(“Be careful though; as the score “,5, 1);
LCD.WriteRC(“increases, so does the speed of the ball!”, 7, 1);
LCD.DrawRectangle(2,2,45,30); //Back button//
LCD.WriteRC(“Back”,0,0);
while (score_quit == 1) // waits for user to tap to go back to menu
{
x = 0;
y = 0;
while(!LCD.Touch(&x,&y));
LCD.Touch(&x,&y);
while(!LCD.Touch(&x,&y));
if (x>2 && x<47 && y>2 && y<32)
{
score_quit = 0;
}
}
}
else if ( x > 2 && x < 315 && y > 176 && y < 234) // if credits is tapped
{
x = 0;
y = 0;
j = 0;
k = 0;
score_quit = 1l;
LCD.Clear();
// prints page one of credits, waits for user to tap, prints page two of credits
LCD.WriteRC(“Created by Kevin Uth”, 1, 1);
LCD.WriteRC(“and Colin Jordan”, 1, 2);
LCD.WriteRC(“Game idea and inspiration “, 3, 1);
LCD.WriteRC(“from the original game ‘Breakout’”,4, 1);
LCD.WriteRC(“which was designed by “, 5, 1);
LCD.WriteRC(“Nolan Bushnell and Steve Wozniak”, 6, 1);
while(!LCD.Touch(&x,&y));
LCD.Touch(&x,&y);
while(!LCD.Touch(&j,&k));
LCD.WriteRC(“Huge thanks to Gabriella, “, 7, 1);
LCD.WriteRC(“Becca and Dr. Harper”,8,1);
LCD.WriteRC(“This game would not be “, 10, 1);
LCD.WriteRC(“possible without their help!”, 11, 1);
LCD.DrawRectangle(2,2,45,30);
LCD.WriteRC(“Back”,0,0);
while (score_quit ==1)// waits for user to tap to go back to menu
{
x = 0;
y = 0;
while(!LCD.Touch(&x,&y));
LCD.Touch(&x,&y);
while(!LCD.Touch(&x,&y));
if (x>2 && x < 47 && y>2 && y<32)
{
score_quit = 0;
}
}
}
}
while (!LCD.Touch(&x,&y));
while (LCD.Touch(&x,&y));
while( game == 1); // end of game do while
}
// constructor for paddle, draws it in opening position
Block::Block()
{
blockx = 140;
blocky = 200;
LCD.DrawRectangle(blockx,blocky,45,15);
}
// constructor for ball
Ball::Ball()
{
int p;
Xball=162;
Yball=201;
Radius= 10;
LCD.DrawCircle(Xball, Yball, 10);
Xvel = 0;
Yvel = 0;
Xmin = 0;
Xmax = 319;
Ymax = 34;
Ymin = 239;
wait_time = 10;
score = 0;
// sets broken counters as 0 (not hit yet)
for (p = 0; p < 22; p++)
{
broken[p] = 0;
}
}
// constructor for bricks
Brick::Brick()
{
Width = 0;
Height = 0;
Xstart = 0;
Ystart = 0;
}
void Brick::DrawBricks(int a[])
{
int p;
Width = 29;
Height = 10;
for (p = 0; p < 22; p++)
{
// sets x and y coordinates for first row of bricks
if (p < 11 and a[p] == 0)
{
Ystart = 34;
Xstart = p * Width + 1;
}
// sets x and y coordinates for bottom left brick
else if (p == 11 and a[p] == 0)
{
Ystart = 44;
Xstart = (p – 11) * Width + 1;
}
// sets x and y coordinates for second row of bricks
else if (p > 11 and a[p] == 0)
{
Ystart = 44;
Xstart = (p – 11) * Width + 1;
}
LCD.DrawRectangle(Xstart,Ystart,Width,Height);
}
}
int Block::BlockPlacement(float c, float d, int e)
{
if (c>245 && c<285 && d>2 && d<42) //left move button//
{
RedrawL();//Call function which redraws the buttons, the top line of the game, and moves paddle left//
}
else if (c>287 && c<317 && d>2 && d<42) //right move button//
{
RedrawR();//Call function which redraws the buttons, the top line of the game, and moves paddle right///
}
else if (c>2 && c<32 && d>2 && d<32) //quit button//
{
e = 0;
//sets play to 0 to exit game
}
return e;
}
// moves paddle to left and redraws screen
void Block::RedrawL()
{
LCD.Clear();//Clear screen//
blockx -= 20;
LCD.DrawRectangle(blockx,blocky,45,15);
LCD.DrawRectangle(245,2,30,30); //< button//
LCD.DrawRectangle(287,2,30,30); //> button//
LCD.DrawRectangle(2, 2, 30, 30); //Quit button?//
LCD.WriteRC(“>”,1,24);
LCD.WriteRC(“<“,1,22);
LCD.WriteRC(“Quit”, 1, 2);
}
// moves paddle to right and redraws screen
void Block::RedrawR()
{
LCD.Clear();//Clear screen//
blockx += 20;
LCD.DrawRectangle(blockx,blocky,45,15);
LCD.DrawRectangle(245,2,30,30); //< button//
LCD.DrawRectangle(287,2,30,30); //> button//
LCD.DrawRectangle(2, 2, 30, 30); //Quit button?//
LCD.WriteRC(“>”,1,24);
LCD.WriteRC(“<“,1,22);
LCD.WriteRC(“Quit”, 1, 2);
}
// sets initial movement of ball based on first screen touch
void Ball::InitialTouch()
{
float x, y, j, k;
// wait for user touch
while(!LCD.Touch(&x,&y));
LCD.Touch(&x,&y);
while(!LCD.Touch(&j,&k));
// touch left half, move to left
if (x>0 && x<156 && y>0 && y<239)
{
Xvel = –4;
Yvel = –4;
}
// touch right half, move to right
else if (x>156 && x<317 && y>0 && y<239)
{
Xvel = 4;
Yvel = –4;
}
}
// moves ball, checks if ball has hit anything, and redraws game
int Ball::RedrawBall(float a, float b, int c, int d, int *e)
{
int p, hits = 0;
LCD.Clear();
LCD.DrawRectangle(a ,b ,45,15);
LCD.DrawRectangle(245,2,30,30); //< button//
LCD.DrawRectangle(287,2,30,30); //> button//
LCD.DrawRectangle(2, 2, 30, 30); //Quit button?//
LCD.DrawLine(0,34,319,34); // top line
LCD.WriteRC(“>”,1,24);
LCD.WriteRC(“<“,1,22);
LCD.WriteRC(“Quit”, 1, 2);
// moves ball
Xball += Xvel;
Yball += Yvel;
// right boundary
if (Xball + 10 >= Xmax)
{
Xvel = –Xvel;
}
// left boundary
else if (Xball – 10 <= Xmin)
{
Xvel = –Xvel;
}
// top boundary
else if (Yball – 10 <= Ymax)
{
Yvel = –Yvel;
}
/*bottom boundary, checks if user has broken all of the bricks, prints that user beat
game if this is true, otherwise just prints score and endgame messages,
saves score if it is a high score, then returns to main menu*/
else if (Yball + 10 == Ymin)
{
c = 0;
for (p = 0; p < 22; p++)
{
if (broken[p] == 0)
{
hits += 1;
}
}
switch (hits)
{
case (22):
LCD.Clear();
LCD.WriteRC(“GAME OVER”, 4, 10);
Sleep (5.0);
LCD.Clear();
LCD.WriteRC(“You beat the game!”, 5, 4);
Sleep (5.0);
LCD.Clear();
LCD.WriteRC(“Thanks for playing!”, 5, 4);
LCD.WriteRC(“Your score was : “, 6, 5);
LCD.WriteAt(score, 225, 110);
Sleep(5.0);
if (score > *e)
{
*e = score;
}
break;
default:
LCD.Clear();
LCD.WriteRC(“GAME OVER”, 4, 10);
Sleep (5.0);
LCD.Clear();
LCD.Clear();
LCD.WriteRC(“Thanks for playing!”, 5, 4);
LCD.WriteRC(“Your score was : “, 6, 5);
LCD.WriteAt(score, 255, 100);
Sleep(5.0);
if (score > *e)
{
*e = score;
c = 0;
}
}
}
// if ball hits left half of paddle, bounce to the left
else if (Yball + 10 > 202 && Xball > a +1 && Xball < a + 22)
{
Xvel = -abs(Xvel);
Yvel = -abs(Yvel);
}
// if ball hits right half of paddle, bounce to the right
else if (Yball+10 > 202 && Xball < a + 46 && Xball > a + 23)
{
Xvel = abs(Xvel);
Yvel =- abs(Yvel);
}
/* checks if the ball hits any of the bottoms of the bricks by checking
if the ball is within the coordinates of the bottoms of them, if the brick
counter is still zero, and if the ball is moving up*/
for (p = 0; p < 22; p++)
{
// top row
if (p < 11 and broken[p] == 0)
{
if ((Yball – 10) > 32 && (Yball – 10) < 46 && Xball > (p * 29) && Xball < ((p + 1) * 29) && broken[p] == 0 && Yvel < 0)
{
Yvel = –Yvel;
score += 1;
broken[p] = 1;
}
}
// bottom left
else if (p == 11 and broken[p] == 0)
{
if ((Yball – 10) > 52 && (Yball – 10) < 56 && Xball > ((p – 11) * 29) && Xball < ((p – 10) * 29) && broken[p] == 0 && Yvel < 0)
{
Yvel = –Yvel;
score += 1;
broken[p] = 1;
}
// bottom row
}
else if (p > 11 and broken[p] == 0)
{
if ((Yball – 10) > 52 && (Yball – 10) < 56 && Xball > ((p – 11) * 29) && Xball < ((p – 10) * 29) && broken[p] == 0 && Yvel < 0)
{
Yvel = –Yvel;
score += 1;
broken[p] = 1;
}
}
}
LCD.DrawCircle(Xball, Yball, 10);
Sleep(wait_time);
return c;
}