-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source
167 lines (164 loc) · 3.71 KB
/
Source
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
//What's it? https://www.youtube.com/watch?v=XqOr6O2nJsw
#include<GL/freeglut.h>
#include<conio.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<ctime>
#include<GL\GLStd.h>
using namespace std;
const int Wid = 640;//Ширина
const int Hei = 480;//Высота
const int OWid = 150;
const int OHei = 150;
int lives = 12;
int Plives = 7;
int level;
GLfloat x = OWid / 5;
GLfloat y = OHei / 2;
GLfloat Px = OWid / 2; //random(Wid);
GLfloat Py = OHei / 2;// random(Hei);
/*inline void clear()
{
glClear(GL_COLOR_BUFFER_BIT);
}
inline void glTextOut(GLfloat x, GLfloat y, std::string text)
{
glRasterPos2f(x, y);
glutBitmapString(GLUT_BITMAP_9_BY_15, (const unsigned char*)text.c_str());
}
inline int random(int n)
{
srand(time(NULL));
int i = rand() % n;
return i;
}*/
void help()
{
clear();
glColor3f(1, 1, 1);
glTextOut(OWid - OWid / 1.5f, OHei - OHei / 5.0f, "Go Full Screen +\nLeave Full Screen -\nShooting Space\nUp ,Down, Left, Right - W,S,A,D");
glFinish();
Sleep(4500);
}
void Timer(int value)
{
glutPostRedisplay();
glutTimerFunc(45, Timer, 0);
}
inline void shooting()
{
Sleep(10);
glColor3f(0.2f, 1.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(x, y + 1);
glVertex2f(x, y + OHei / 7);
glEnd();
glFlush();
Sleep(88);
if (Py - y <= OHei / 7 && x == Px && Py >= y)
{
y -= (OHei / 30);
if (y <= 0){ y = OHei / 30; };
Plives--;
level += 0.1f;
if (Plives == 0)
{
clear();
glColor3f(0.1f, 1.0f, 0.0f);
glTextOut(OWid - OWid / 1.5f, OHei - OHei / 5.0f, "You WIN!");
glFinish();
Sleep(5000);
exit(0);
}
}
}
void Keyboard(unsigned char key, int po, int lo)
{
switch (key)
{
case '+':glutFullScreen(); ShowCursor(FALSE); ShowCursor(FALSE);
glPointSize(6);
glLineWidth(4); break;
case '-':glutLeaveFullScreen(); ShowCursor(TRUE);
glPointSize(4);
glLineWidth(2);
break;
case 'w':y += 1; if (y >= OHei){ y = OHei - 1; }; break;
case 'd':x += 1; if (x >= OWid){ x = OWid - 1; }; break;
case 's':y -= 1; if (y <= 0){ y = 1; }; break;
case 'a':x -= 1; if (x <= 0){ x = 1; }; break;
case ' ': shooting(); break;
};
}
void Display()
{
if (Py - y <= (OHei / 4) && Py >= y && Px == x)
{
Sleep(10);
glColor3f(0.9f, 0.5f, 0.0f);
//Выстрел
glBegin(GL_LINES);
glVertex2f(Px, Py - 1);
glVertex2f(Px, y);
glEnd();
glFlush();
lives--;
Sleep(95);
if (lives == 0){
clear();
glColor3f(1.0f, 0.0f, 0.0f);
glTextOut(OWid - OWid / 1.5f, OHei - OHei / 5.0f, "You LOOOOOOSE!");
glFinish();
Sleep(3000);
exit(0);
}
}
clear();
if (x == Px && y == Py)
{
Px -= OWid / 10;
Py -= OHei / 10;
x += OWid / 15;
y += OHei / 15;
if (Py <= 0) { Py = 3; }
if (Px <= 0) { Px = 3; }
};
if (random(2) == 1){ Px += random(3) - 1; }//Перемещение по x
else { Py += random(3) - 1; }//перемещение по y
//Не выходит ли за окно?
if (Py <= 0) { Py = 3; }
else if (Py >= OHei - 1) { Py = OHei - 3; }
if (Px <= 0) { Px = 3; }
else if (Px >= OWid - 1) { Px = OWid - 3; }
//cout << x << endl << Px <<endl<< y << endl << Py << endl;
glBegin(GL_POINTS);
glColor3f(1.0f, level, 0.2f);
glVertex2f(Px, Py);
glColor3f(1, 1, 1);
glVertex2f(x, y);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
setlocale(LC_ALL, "RUS");
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(300, 120);
glutInitWindowSize(Wid, Hei);
glutCreateWindow("The Pixel Game.");
glLoadIdentity();
glColor3f(0, 0, 0);
gluOrtho2D(0, OWid, 0, OHei);
glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
glMatrixMode(GL_PROJECTION);
help();
glutDisplayFunc(Display);
glPointSize(4);
glLineWidth(2);
glutTimerFunc(5, Timer, 0);
glutKeyboardFunc(Keyboard);
glutMainLoop();
return 0;
}