//============================================================================ // Name : .cpp // Author : FAST CS Department // Version : // Copyright : (c) Reserved // Description : Basic 2D game of Rush Hour... //============================================================================ #ifndef RushHour_CPP_ #define RushHour_CPP_ #include "util.h" #include <iostream> #include<string> #include<cmath> // for basic math functions such as cos, sin, sqrt 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(); } int xI = 400, yI = 400; void drawCar() { DrawSquare(xI, yI, 20, colors[WHITE]); glutPostRedisplay(); } bool flag = true; void moveCar() { if (xI > 10 && flag) { xI -= 10; cout << "going left"; if(xI < 100) flag = false; } else if (xI < 1010 && !flag) { cout << "go right"; xI += 10; if (xI > 900) flag = true; } } /* * Main Canvas drawing function. * */ void GameDisplay()/**/{ // set the background color using function glClearColor. // to change the background play with the red, green and blue values below. // Note that r, g and b values must be in the range [0,1] where 0 means dim rid and 1 means pure red and so on. glClearColor(0/*Red Component*/, 0, //148.0/255/*Green Component*/, 0.0/*Blue Component*/, 0 /*Alpha component*/); // Red==Green==Blue==1 --> White Colour glClear (GL_COLOR_BUFFER_BIT); //Update the colors //Red Square DrawSquare(400, 20, 40, colors[RED]); //Green Square DrawSquare( 250 , 250 ,20,colors[GREEN]); //Display Score DrawString( 50, 800, "Score=0", colors[MISTY_ROSE]); // Trianlge Vertices v1(300,50) , v2(500,50) , v3(400,250) DrawTriangle( 300, 450 , 340, 450 , 320 , 490, colors[MISTY_ROSE] ); //DrawLine(int x1, int y1, int x2, int y2, int lwidth, float *color) DrawLine( 550 , 50 , 550 , 480 , 10 , colors[MISTY_ROSE] ); DrawCircle(50,670,10,colors[RED]); DrawCircle(70,670,10,colors[RED]); DrawCircle(90,670,10,colors[RED]); DrawRoundRect(500,200,50,100,colors[DARK_SEA_GREEN],70); DrawRoundRect(100,200,100,50,colors[DARK_OLIVE_GREEN],20); DrawRoundRect(100,100,50,100,colors[DARK_OLIVE_GREEN],30); DrawRoundRect(200,100,100,50,colors[LIME_GREEN],40); DrawRoundRect(350,100,100,50,colors[LIME_GREEN],20); drawCar(); glutSwapBuffers(); // do not modify this line.. } /*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 (key == GLUT_KEY_LEFT /*GLUT_KEY_LEFT is constant and contains ASCII for left arrow key*/) { // what to do when left key is pressed... xI -= 10; } else if (key == GLUT_KEY_RIGHT /*GLUT_KEY_RIGHT is constant and contains ASCII for right arrow key*/) { xI += 10; } else if (key == GLUT_KEY_UP/*GLUT_KEY_UP is constant and contains ASCII for up arrow key*/) { yI += 10; } else if (key == GLUT_KEY_DOWN/*GLUT_KEY_DOWN is constant and contains ASCII for down arrow key*/) { yI -= 10; } /* This function calls the Display function to redo the drawing. Whenever you need to redraw just call * this function*/ 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/* Escape key ASCII*/) { exit(1); // exit the program when escape key is pressed. } if (key == 'b' || key == 'B') //Key for placing the bomb { //do something if b is pressed cout << "b pressed" << endl; } glutPostRedisplay(); } /* * 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) { // implement your functionality here moveCar(); // once again we tell the library to call our Timer function after next 1000/FPS glutTimerFunc(100, 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) { cout << x << " " << y << endl; glutPostRedisplay(); } void MouseMoved(int x, int y) { //cout << x << " " << y << endl; 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) // dealing only with left button { cout << GLUT_DOWN << " " << GLUT_UP << endl; } else if (button == GLUT_RIGHT_BUTTON) // dealing with right button { cout<<"Right Button Pressed"<<endl; } glutPostRedisplay(); } /* * our gateway main function * */ int main(int argc, char*argv[]) { int width = 1020, height = 840; // 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("Rush Hour by Naveed Ahmad"); // 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; } #endif /* RushHour_CPP_ */
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}