This commit is contained in:
RafayAhmad7548 2024-06-16 18:53:25 +05:00
parent 37776af5db
commit ab03d5f10c
4045 changed files with 286212 additions and 3 deletions

View file

@ -0,0 +1,179 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->getBalls()[i]->checkBoundCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,191 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "playing"){
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,201 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "menu"){
DrawRoundRect(220, 650, 200, 100, colors[BLUE], 5);
DrawString(230, 650, "Start Game", colors[WHITE]);
DrawString(230, 550, "Resume Game", colors[WHITE]);
DrawString(230, 450, "Start Game", colors[WHITE]);
DrawString(230, 350, "Start Game", colors[WHITE]);
}
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,194 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,192 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
// brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(level->checkLevelEnd()){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(level->checkLevelEnd()){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,189 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,179 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->getBalls()[i]->checkBoundCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,197 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "menu"){
}
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,194 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,194 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,198 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "menu"){
DrawRoundRect(220, 650, 200, 100, colors[BLUE], 5);
DrawString()
}
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,179 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->getBalls()[i]->checkBoundCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,192 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
// brickBreaker.getLevel()->moveFood();
// brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,178 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,192 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,179 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->getBalls()[i]->checkBoundCollision(level->getLives());
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,200 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "menu"){
DrawRoundRect(220, 650, 200, 100, colors[BLUE], 5);
DrawString(230, 650, "Start Game", colors[WHITE]);
DrawString(230, 550, "Start Game", colors[WHITE]);
DrawString(230, 450, "Exit", colors[WHITE]);
}
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,179 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->getBalls()[i]->checkBoundCollision(level->getLives(), brickBreaker.getCurrentLevel());
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,194 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,181 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
if(level->getBalls()[i]->checkBoundCollision(level->getLives(), brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
}
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,179 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,194 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,181 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
}
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,178 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,185 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,200 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "menu"){
DrawRoundRect(220, 650, 200, 100, colors[BLUE], 5);
DrawString(230, 650, "Start Game", colors[WHITE]);
DrawString(230, 550, "Start Game", colors[WHITE]);
DrawString(230, 450, "Exit", colors[WHITE]);
}
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1 @@
{"version":1,"resource":"file:///home/rafayahmad/Stuff/OOP/Project/game.cpp","entries":[{"id":"fMtY.cpp","timestamp":1715163384081},{"id":"43Up.cpp","timestamp":1715164066151},{"id":"Ezw8.cpp","timestamp":1715164166119},{"id":"IvgM.cpp","timestamp":1715164307974},{"id":"sH3j.cpp","timestamp":1715164537561},{"id":"vS4X.cpp","timestamp":1715164560638},{"id":"znfX.cpp","timestamp":1715164588624},{"id":"W33P.cpp","timestamp":1715164677387},{"id":"rXoR.cpp","timestamp":1715164694877},{"id":"RWY6.cpp","timestamp":1715164708187},{"id":"ZLeO.cpp","timestamp":1715164721060},{"id":"1LWn.cpp","timestamp":1715164735620},{"id":"XNSy.cpp","timestamp":1715172644513},{"id":"mik1.cpp","timestamp":1715173737798},{"id":"pd5X.cpp","timestamp":1715173875490},{"id":"6lpe.cpp","timestamp":1715174169502},{"id":"H1qi.cpp","timestamp":1715174186819},{"id":"amIZ.cpp","timestamp":1715174211597},{"id":"ikiX.cpp","timestamp":1715174293844},{"id":"WNb9.cpp","timestamp":1715174436123},{"id":"DgKO.cpp","timestamp":1715174467897},{"id":"0sRb.cpp","timestamp":1715178978157},{"id":"AjEk.cpp","timestamp":1715179832988},{"id":"JzU5.cpp","timestamp":1715179876390},{"id":"PoCi.cpp","timestamp":1715179981387},{"id":"WGiO.cpp","timestamp":1715180599957},{"id":"YHjz.cpp","timestamp":1715180761283},{"id":"zeJN.cpp","timestamp":1715180900610},{"id":"l8g3.cpp","timestamp":1715180977125},{"id":"ddfk.cpp","timestamp":1715181524916},{"id":"mNNt.cpp","timestamp":1715182494931},{"id":"386o.cpp","timestamp":1715182615623},{"id":"BY4x.cpp","timestamp":1715182708261},{"id":"CSf5.cpp","timestamp":1715185140437},{"id":"rvFh.cpp","timestamp":1715185155157},{"id":"XYfC.cpp","timestamp":1715185165763},{"id":"v7iH.cpp","timestamp":1715185273053},{"id":"TkXH.cpp","timestamp":1715185357033},{"id":"ApSP.cpp","timestamp":1715185642795},{"id":"DM1M.cpp","timestamp":1715185696572},{"id":"jeT2.cpp","timestamp":1715185723978},{"id":"3SVT.cpp","timestamp":1715185743562},{"id":"KLDH.cpp","timestamp":1715185757028},{"id":"dpxK.cpp","timestamp":1715185975187},{"id":"42iW.cpp","timestamp":1715186046161},{"id":"rlfN.cpp","timestamp":1715186080864},{"id":"iwFS.cpp","timestamp":1715186244253},{"id":"p5Iv.cpp","timestamp":1715186257260},{"id":"6jXZ.cpp","timestamp":1715186270110},{"id":"4hZx.cpp","timestamp":1715186345176}]}

View file

@ -0,0 +1,192 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,177 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,192 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(level->checkLevelEnd()){
}
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,198 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "menu"){
DrawRoundRect(220, 650, 200, 100, colors[BLUE], 5);
DrawString(230, 650, "Start Game", colors[WHITE]);
}
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,183 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,185 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), brickBreaker.getGameState());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,208 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
GameLevel* level = brickBreaker.getLevel();
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->getBalls()[i]->checkBoundCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
}
// if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
// for(int i=0;i<3;i++){
// if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
// brickBreaker.getLevel()->getBalls()[i]->move();
// brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
// brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
// if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
// }
// }
// brickBreaker.getLevel()->checkBrickCollision();
// brickBreaker.getLevel()->moveFood();
// brickBreaker.getLevel()->checkFoodCollision();
// }
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,191 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(level->checkLevelEnd()){
}
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getLevel() != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
}
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,188 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives());
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,194 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
level->checkBalls(level->getLives(), 1);
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,193 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
bool a = true;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,195 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
if(brickBreaker.getGameState() == "playing") brickBreaker.getLevel()->draw();
else if(brickBreaker.getGameState() == "paused"){
DrawString(300, 300, "Paused, press p to unpause", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
else if(brickBreaker.getGameState() == "gameover"){
DrawString(300, 300, "Game Over", colors[WHITE]);
brickBreaker.getLevel()->draw();
}
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
level->checkBrickCollision();
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
int a =0;
level->checkBalls(level->getLives(), a);
}
}
}
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,194 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
bool a = true;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
a = true;
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
a = false;
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,182 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
BrickBreaker brickBreaker;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
brickBreaker.getLevel()->draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
GameLevel* level = brickBreaker.getLevel();
if(level != nullptr && brickBreaker.getGameState() == "playing"){
for(int i=0;i<3;i++){
if(level->getBalls()[i] != nullptr){
level->getBalls()[i]->move();
if(level->getBalls()[i]->checkBoundCollision(brickBreaker.getCurrentLevel())){
delete level->getBalls()[i];
level->getBalls()[i] = nullptr;
}
level->getPaddle().checkCollision(*level->getBalls()[i]);
if(level->getPaddle2() != nullptr) level->getPaddle2()->checkCollision(*level->getBalls()[i]);
}
}
level->checkBrickCollision();
level->moveFood();
level->checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(level->getPaddle(), level->getBalls());
glutPostRedisplay();
}
glutTimerFunc(1000.0/60, Timer, 0);
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}

View file

@ -0,0 +1,196 @@
#include <iostream>
#include <string>
#include <cmath>
#include "util.h"
#include "BrickBreaker.h"
using namespace std;
// seed the random numbers generator by current time (see the documentation of srand for further help)...
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height){
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
// Level1 level1;
BrickBreaker brickBreaker;
bool a = true;
void GameDisplay(){
glClearColor(0, 0, 148.0/2550.0, 0); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
// if(brickBreaker.getLevel() != nullptr){
brickBreaker.getLevel()->draw();
// }
// level1.draw();
glutSwapBuffers(); // do not modify this line..
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
* */
void Timer(int m){
if(brickBreaker.getGameState() == "playing"){
a = true;
for(int i=0;i<3;i++){
if(brickBreaker.getLevel()->getBalls()[i] != nullptr){
brickBreaker.getLevel()->getBalls()[i]->move();
brickBreaker.getLevel()->getBalls()[i]->checkBoundCollision();
brickBreaker.getLevel()->getPaddle().checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
if(brickBreaker.getLevel()->getPaddle2() != nullptr) brickBreaker.getLevel()->getPaddle2()->checkCollision(*brickBreaker.getLevel()->getBalls()[i]);
}
}
brickBreaker.getLevel()->checkBrickCollision();
brickBreaker.getLevel()->moveFood();
brickBreaker.getLevel()->checkFoodCollision();
a = false;
}
// level1.getBall().move();
// level1.getBall().checkBoundCollision();
// level1.getPaddle().checkCollision(level1.getBall());
// level1.checkBrickCollision();
// level1.moveFood();
// level1.checkFoodCollision();
if(Food::getPowerActive() && Food::getCounter() < 300) Food::count();
else if(Food::getCounter() == 300) Food::resetPowerup(brickBreaker.getLevel()->getPaddle(), brickBreaker.getLevel()->getBalls());
glutTimerFunc(1000.0/60, Timer, 0);
glutPostRedisplay();
}
/* This function is called (automatically) whenever your mouse moves witin inside the game window
* You will have to add the necessary code here for finding the direction of shooting
* This function has two arguments: x & y that tells the coordinate of current position of move mouse
* */
void MousePressedAndMoved(int x, int y){
glutPostRedisplay();
}
void MouseMoved(int x, int y){
if(x < brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 0;
else if(x > 1050 - brickBreaker.getLevel()->getPaddle().getWidth()/2) x = 1050 - brickBreaker.getLevel()->getPaddle().getWidth();
else x -= brickBreaker.getLevel()->getPaddle().getWidth()/2;
brickBreaker.getLevel()->getPaddle().setX(x);
// level1.getPaddle().setX(x);
glutPostRedisplay();
}
/* This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void NonPrintableKeys(int key, int x, int y){
if(brickBreaker.getLevel()->getPaddle2() != nullptr){
if(key == GLUT_KEY_LEFT){
if(brickBreaker.getLevel()->getPaddle2()->getX() >= 0)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()-10);
}
else if(key == GLUT_KEY_RIGHT){
if(brickBreaker.getLevel()->getPaddle2()->getX()+brickBreaker.getLevel()->getPaddle2()->getWidth() <= 1050)
brickBreaker.getLevel()->getPaddle2()->setX(brickBreaker.getLevel()->getPaddle2()->getX()+10);
}
else if(key == GLUT_KEY_UP){
}
else if(key == GLUT_KEY_DOWN){
}
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y){
if(key == 27){
exit(0);
}
if(key == 13){
if(!a){
brickBreaker.setGameState("paused");
brickBreaker.nextLevel();
}
}
if(key == 112){
if(brickBreaker.getGameState() == "paused") brickBreaker.setGameState("playing");
else if(brickBreaker.getGameState() == "playing") brickBreaker.setGameState("paused");
}
glutPostRedisplay();
}
/*This function is called (automatically) whenever your mouse button is clicked witin inside the game window
* You will have to add the necessary code here for shooting, etc.
* This function has four arguments: button (Left, Middle or Right), state (button is pressed or released),
* x & y that tells the coordinate of current position of move mouse
* */
void MouseClicked(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
}
else if(button == GLUT_RIGHT_BUTTON){
}
glutPostRedisplay();
}
int main(int argc, char*argv[]){
int width = 1050, height = 870; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("OOP Project"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
//glutDisplayFunc(display); // tell library which function to call for drawing Canvas.
glutDisplayFunc(GameDisplay); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0, Timer, 0);
glutMouseFunc(MouseClicked);
glutPassiveMotionFunc(MouseMoved); // Mouse
glutMotionFunc(MousePressedAndMoved); // Mouse
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}