-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
208 lines (169 loc) · 7.03 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//============================================================================
// Name : .cpp
// Author : Sibt ul Hussain
// Version :
// Copyright : (c) Reserved
// Description : Basic 2D game of Centipede...
//============================================================================
#ifndef CENTIPEDE_CPP_
#define CENTIPEDE_CPP_
//#include "Board.h"
#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();
}
/*
* Main Canvas drawing function.
* */
//Board *b;
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
//Fire Gun
DrawSquare( 400 , 20 ,40,colors[RED]);
//Mushroom
DrawSquare( 250 , 250 ,20,colors[GREEN]);
//Display Score
DrawString( 50, 800, "Score=0", colors[MISTY_ROSE]);
//Spider
DrawTriangle( 300, 450 , 340, 450 , 320 , 490, colors[MISTY_ROSE] );
// Trianlge Vertices v1(300,50) , v2(500,50) , v3(400,250)
//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]);
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...
} else if (key
== GLUT_KEY_RIGHT /*GLUT_KEY_RIGHT is constant and contains ASCII for right arrow key*/) {
} else if (key
== GLUT_KEY_UP/*GLUT_KEY_UP is constant and contains ASCII for up arrow key*/) {
}
else if (key
== GLUT_KEY_DOWN/*GLUT_KEY_DOWN is constant and contains ASCII for down arrow key*/) {
}
/* 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
// once again we tell the library to call our Timer function after next 1000/FPS
glutTimerFunc(1000.0, 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) {
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
{
}
glutPostRedisplay();
}
/*
* our gateway main function
* */
int main(int argc, char*argv[]) {
//b = new Board(60, 60); // create a new board object to use in the Display Function ...
int width = 1020, height = 840; // i have set my window size to be 800 x 600
//b->InitalizeBoard(width, height);
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("PF 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;
}
#endif /* AsteroidS_CPP_ */