-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.cpp
121 lines (93 loc) · 1.77 KB
/
ball.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
#include "ball.h"
#include <SOIL.h>
using namespace Nball;
Ball::Ball()
{
BallImg = SOIL_load_OGL_texture
(
"img/ball_vgs.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
x = Width/2;
y = Height/2;
vx = 50;
vy = 50;
}
void Ball::Draw()
{
glPushMatrix();
glTranslatef(x,y,0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D , BallImg);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-20,-20);
glTexCoord2f(1,0);
glVertex2f(20,-20);
glTexCoord2f(1,1);
glVertex2f(20,20);
glTexCoord2f(0,1);
glVertex2f(-20,20);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glPopMatrix();
}
void Ball::Update(float DeltaTime)
{
if (glfwGetKey(GLFW_KEY_KP_8)) //go up!
{
vy -= 1000 * DeltaTime;
}
if (glfwGetKey(GLFW_KEY_KP_5)) // go down!
{
vy += 1000 * DeltaTime;
}
if (glfwGetKey(GLFW_KEY_KP_4)) // go left!
{
vx -= 1000 * DeltaTime;
}
if (glfwGetKey(GLFW_KEY_KP_6)) //go right!
{
vx += 1000 * DeltaTime;
}
if (x+20>Width)
{
x = Width-20;
vx *= -1;
}
else if (x-20<0)
{
x = 20;
vx *= -1;
}
if (y - 20 <= 0) // check if it hit the top, if it did add a point for the AI and revert it back to the middle.
{
Score->p1Add();
Score->Display(); // display the new score.
x = Width/2;
y = Height/2;
}
if (y + 20 >= Height)
{
Score->aiAdd();
Score->Display();
x = Width/2;
y = Height/2;
}
x += vx*DeltaTime;
y += vy*DeltaTime;
}
void Nball::SetScreen(unsigned int w, unsigned int h)
{
Width = w;
Height = h;
}
void Ball::SetScore(CScore* foo)
{
Score = foo;
}