#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include "paddle.h"
#include "scoreBoard.h"
#include "ball.h"
#include "ansi.h"
#include "user.h"
using namespace std;
static PlayerPaddle player(1);
static ComputerPaddle computer(2);
static Ball ball;
static ScoreBoard score;
//helper functions
static void init();
static void cleanup();
static void draw();
static void update();
static void erase();
int main(void) {
init();
//game loop
while(true) {
erase();
update();
draw();
usleep(100000);
}
return 0;
}
static void
init() {
cout << clearScreen;
cout << cursorOff;
setBlocking(false);
setEcho(false);
ball.reset();
srand(time(0));
}
static void
cleanup() {
cout << cursorOn;
setBlocking(true);
setEcho(true);
cout << clearScreen;
}
static void
draw() {
player.draw();
computer.draw();
score.draw();
ball.draw();
}
static void
update() {
int y;
ball.update();
computer.update(ball.getY());
player.update();
score.update();
//check for ball collision
if(ball.getX() <= player.getX()+1) {
y = ball.getY() - player.getY();
if(y >= -2 && y<=3)
ball.bouncePaddle(y);
else {
//ball left player 1 , player 2 scores!
if(ball.getX() <= player.getX()) {
ball.reset();
score.addPlayer2();
}
}
}
if(ball.getX() >= computer.getX()-2) {
y = ball.getY() - computer.getY();
if(y >= -2 && y<=3)
ball.bouncePaddle(y);
else {
if(ball.getX() >= computer.getX() -1) {
ball.reset();
score.addPlayer1();
}
}
}
//check for wall bounce
if(ball.getY() <= 1 || ball.getY() >=23)
ball.bounceWall();
}
static void
erase() {
ball.erase();
computer.erase();
player.erase();
score.erase();
}