Open GL - Ayuda juego pong

   
Vista:

Ayuda juego pong

Publicado por Sergio (1 intervención) el 27/04/2015 19:25:39
Estoy editando un pong muy básico que encontré para hacer mi propio pong practicar y aprender un poco, tengo un par de partes que no consigo implementar, son las siguientes.

Implementar musica y o sonido para la bola. (quizás con mmsystem.h pero no consigo hacer que funcione)
Hacer los números de los marcadores mas grandes como el pong original. (no se me ocurre como hacerlo)
Meter una opción para jugar contra la maquina. (tampoco se me ocurre como montarlo, además creo que dará problemas porque las palas no se pueden mover a la vez)

Aquí el código:

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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Pong by Konstantinos Egarhos (aka konsnos) edited by sergio
 
/*
Requiere

sudo apt-get install build-essential
sudo apt-get install freeglut3 freeglut3-dbg freeglut3-dev

##sudo apt-get install freeglut3-dev

gcc -o opengl opengl.c -lglut -lGLU -lGL

*/
 
#include <GL/freeglut.h>  // OpenGL toolkit
#include <stdio.h>
 
 
 
// Function Initialising
void drawRect(GLfloat, GLfloat, int, int, float, float, float);
void update();
 
// Window dimensions
const int Width = 400;
const int Height = 300;
 
// Dimensions
#define paddleHeight 80
#define paddleWidth 10
#define zAxis 0
 
float p1 = 220;
float p2 = 220;
 
float p3 = 000;
float p4 = 150;
float p5 = 300;
float p6 = 450;
 
// GLfloat player1[4][2] = { 10, p1, 10, p1+paddleHeight, 10+paddleWidth, p1+paddleHeight, 10+paddleWidth, p1 };
// GLfloat player2[4][2] = { 480, p2, 480, p2+paddleHeight, 480+paddleWidth, p2+paddleHeight, 480+paddleWidth, p2 };
const float pspeed = 20;
 
int score[2];// = {0, 0}; // Score for players 1 and 2
 
#define bsize 10
GLdouble bpos[2] = {235,235};
GLfloat bvx = 1; // ball vector in left-right axis
GLfloat bvy = 1; // ball vector in top-bottom axis
// GLfloat ball[4][2] = { pos[0], pos[1], pos[0], pos[1]+bsize, pos[0]+bsize, pos[1]+bsize, pos[0]+bsize, pos[1] };
float bspeed = 2;
 
// Time variables for the speed calculation of the objects
GLdouble time;
GLdouble prtime;
GLdouble timeDiff;
GLdouble runtime;
 
// Text variable
static char str[200];
void * font = GLUT_BITMAP_9_BY_15;
 
void inicio()
{
 
char linea[5];
 
printf("Pong!!!.\n\n");
printf("/*************************/\n\n");
//printf("Utiliza la tecla p para hacer una pausa.\n\n");//
printf("mueve el jugador 1con w s.\n\n");
printf("mueve el jugador 2 con las flechas.\n\n");
 
 
printf("/*************************/\n\n");
 
printf("Teclea la velocitat de la bola i pulsa intro.\n");
printf("--------------------------------------------------\n");
printf("a - Rapido.\n");
printf("b - Normal.\n");
printf("c - Lento.\n\n");
 
fflush(stdin);
printf("Opcion:");
 
 
 fgets(linea,2,stdin);
 
 if(!strcmp(linea,"a")) bspeed=3;
 
 if(!strcmp(linea,"b")) bspeed=2;
 
 if(!strcmp(linea,"c")) bspeed=1;
 
 fflush(stdin);
 system("cls");
} /* fin inicio */
 
 
/*********************************************
    Drawing
*********************************************/
void renderScene(void)
{
     // Elapsed time from the initiation of the game.
     time = glutGet(GLUT_ELAPSED_TIME);
     timeDiff = time - prtime; // Elapsed time from the previous frame.
     prtime = time;
     runtime = timeDiff / 10.0;
 
     update(); // Update the game.
     // Clear the screen.
     glClear(GL_COLOR_BUFFER_BIT);
 
     sprintf(str, "%d       %d", score[0], score[1] );
     glRasterPos2f(205, 450);
     glutBitmapString(font,(unsigned char*)str);
 
     // Draws the 1st paddle and the 2nd.
     drawRect(10.0, p1, paddleHeight, paddleWidth, 0, 0, 1); // Left blue paddle.
     drawRect(480.0, p2, paddleHeight, paddleWidth, 1, 0, 0); // Right red paddle.
 
drawRect(250.0, p3, paddleHeight, paddleWidth, 1, 0, 0); // center line1.
drawRect(250.0, p4, paddleHeight, paddleWidth, 1, 0, 0); // center line2. 
drawRect(250.0, p5, paddleHeight, paddleWidth, 1, 0, 0); // center line3. 
drawRect(250.0, p6, paddleHeight, paddleWidth, 1, 0, 0); // center line4.
 
 
     drawRect(bpos[0], bpos[1], bsize, bsize, 1, 1, 0); // Yellow ball.
 
     glutSwapBuffers(); // Draw the new frame of the game.
}
 
/*********************************************
    Update
*********************************************/
void update()
{
     // Ball collision with the border of the window.
     if(bpos[0] + bspeed > 500-bsize)
     {
          bvx = -1;
          score[0]++;
     }
     else if (bpos[0] - bspeed < 0)
     {
          bvx = 1;
          score[1]++;
     }
     if(bpos[1] + bspeed > 500-bsize)
          bvy = -1;
     else if (bpos[1] - bspeed < 0)
          bvy = 1;
 
     /******** Collision with the paddles ********/
     // Collision with left paddle.
     if(bpos[0] <= 10 + paddleWidth && bpos[1] >= p1 && bpos[1] + bsize <= p1 + paddleHeight)
     {
          bvx *= -1;
          // bspeed *= 1.01;
     }
     // Collision with right paddle.
     if(bpos[0] + bsize >= 480 && bpos[1] >= p2 && bpos[1] + bsize <= p2 + paddleHeight)
     {
          bvx *= -1;
          // bspeed *= 1.01;
     }
 
     // New position of the ball dependent to the speed ***************
     // Because frame rate isn't always the same that might affect objects speed.
     // That's why we multiply the frame speed and multiply it with the object speed.
     // In that way the object speed is always the same.
     bpos[0] += bspeed * bvx * runtime;
     bpos[1] += bspeed * bvy * runtime;
}
 
/*********************************************
    Create and draw a rectangle.
    Generally, we are only interested in the height(y) of the paddle.
*********************************************/
void drawRect(GLfloat x, GLfloat y, int height, int width, float R, float G, float B)
{
     glColor3f(1.0,1.0,1.0);
 
     glBegin(GL_QUADS);
          glVertex2f( x, y);                        // Top Left
          glVertex2f( x, y + height);               // Bottom Left
          glVertex2f( x + width, y + height);       // Bottom Right
          glVertex2f( x + width, y);                // Top Right
     glEnd();
}
 
/*********************************************
    User Input (keyboard)
 
    I don't calculate frame speed, since it doesn't affect much.
*********************************************/
void keyPress(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 'w':
        case 'W':
             if(420>=p1+pspeed)
                      {p1 += pspeed;}
             break;
        case 's':
        case 'S':
             if(0<=p1-pspeed)
                      {p1 -= pspeed;}
             break;
        case 27 :
             exit(0);
    }
}
 
/*********************************************
    Special Keys Input (keyboard arrows)
 
    I don't calculate frame speed, since it doesn't affect much.
*********************************************/
void specialKeyPress(int key, int x, int y)
{
     switch (key)
     {
            case GLUT_KEY_UP:
                 if(420>=p2+pspeed)
                      {p2 += pspeed;}
                 break;
            case GLUT_KEY_DOWN:
                 if(0<=p2-pspeed)
                      {p2 -= pspeed;}
                 break;
     }
}
 
/*********************************************
    Let there be initial variables.
*********************************************/
void init(void)
{
     // The color the windows will redraw. Its done to erase the previous frame.
     glClearColor(0.4f, 0.4f, 0.4f, 0.4f); // Black, no opacity(alpha).
 
     // Orthographic projection matrix (initiating 2D window).
     gluOrtho2D(0,500,0,500);
 
     // Initialize time.
     prtime = time = glutGet(GLUT_ELAPSED_TIME);
 
     // Initialize score.
     score[0] = score[1] = 0;
}
 
/* Main function */
int main(int argc, char *argv[])
{
 
 
inicio();
    // Initialize openGL with Double buffer and RGB color without transparency.
    // Its interesting to try GLUT_SINGLE instead of GLUT_DOUBLE.
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
 
    // Create the window.
    glutInitWindowSize(Width, Height);
    glutInitWindowPosition(150,50);
    glutCreateWindow("Pong");
 
    // Define the draw function.
    glutDisplayFunc(renderScene);
 
    // Define the keyboard input function.
    glutKeyboardFunc(keyPress);
    // Define the keyboards' special keys input function.
    glutSpecialFunc(specialKeyPress);
 
    // Define which function to execute when nothing except the gameplay is updating.
    glutIdleFunc(renderScene);
 
    init();
 
    glutMainLoop();
 
    return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder