J'ai écrit un jeu de casse de briques simple dans OpenGL. La balle est un cercle 2D dessiné en utilisant:Aide Brick Breaker. cercles, pagaies et rebondissements maladroits
for (float angle = 0; angle < (10); angle+=0.01)
{
glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS));
}
Cela provoque une distorsion lorsque le jeu passe en mode plein écran. RADIUS est défini comme 0,025. J'ai également besoin d'aide pour rendre les mouvements de la palette plus lisses. En outre, si vous jouez plusieurs fois au jeu, vous remarquerez que vers l'extrême gauche, lorsque la balle atteint la pagaie, elle monte jusqu'à une certaine hauteur et rebondit ensuite vers le bas. code complet:
#include <GL/openglut.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#define RADIUS 0.025
#define SPEED 0.001
int WIDTH = 900;
int HEIGHT = 650;
int RATIO = WIDTH/HEIGHT;
bool show[5][10];
float x_brick[4][9];
float y_brick[4][9];
float P_XPOS = 0;
float P_YPOS = -0.8;
bool phit_center = false , phit_corner = false;
bool game_over = false;
bool RIGHT = 1,LEFT = 0,UP = 1,DOWN = 0;
bool started = false;
float x_pos = 0,y_pos = -0.75;
bool hit = false;
int lives = 3;
using namespace std;
void b_draw()
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
for(int a = 0; a < 9; a++)
{
for(int b = 0; b < 4; b++)
{
if(show[b][a] == 1)
{
glVertex2f(x_brick[b][a],y_brick[b][a]);
glVertex2f(x_brick[b][a],y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a]+0.2,y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a]+0.2,y_brick[b][a]);
}
}
}
glEnd();
}
void c_draw()
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x_pos,y_pos);
for (float angle = 0; angle < (10); angle+=0.01)
{
glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS));
}
glEnd();
}
bool b_hit()
{
hit = false;
int flag = 1;
for(int a = 0; a < 10; a++)
{
for(int b =0; b < 4; b++)
{
if(x_pos >= x_brick[b][a] && x_pos <= x_brick[b][a] + 0.2)
{
if(y_pos <= y_brick[b][a] && y_pos >= y_brick[b][a] - 0.1)
{
if(show[b][a] == 1)
{
show[b][a] = 0;
flag = 0;
hit = true;
break;
}
}
}
}
if(flag == 0)
break;
}
return hit;
}
bool crashed()
{
if(y_pos < P_YPOS - 0.05)
return true;
else return false;;
}
void p_hit()
{
phit_corner = false;
phit_center = false;
if(x_pos <= P_XPOS + 0.13 && x_pos >= P_XPOS - 0.13)
{
if(y_pos <= P_YPOS)
{
phit_center = true;
}
}
else if((x_pos >= P_XPOS + 0.13 && x_pos <= P_XPOS + 0.2) || (x_pos <= P_XPOS - 0.13 && x_pos >= P_XPOS - 0.2))
{
if(y_pos <= P_YPOS)
{
phit_corner = true;
}
}
}
void c_move()
{
if(UP && RIGHT)
{
x_pos += (SPEED);
y_pos += (SPEED);
}
if(UP && LEFT)
{
x_pos -= (SPEED);
y_pos += (SPEED);
}
if(DOWN && RIGHT)
{
x_pos += (SPEED);
y_pos -= (SPEED);
}
if(DOWN && LEFT)
{
x_pos -= (SPEED);
y_pos -= (SPEED);
}
b_hit();
if(x_pos >= (RATIO-RADIUS))
{
RIGHT = 0;
LEFT = 1;
}
else if(x_pos <= (-RATIO+RADIUS))
{
RIGHT = 1;
LEFT = 0;
}
if(y_pos >= (RATIO-RADIUS) || hit)
{
UP = 0;
DOWN = 1;
}
else if(y_pos <= (-RATIO+RADIUS) || hit)
{
UP = 1;
DOWN = 0;
}
p_hit();
if(phit_center)
{
DOWN = 0;
UP = 1;
}
if(phit_corner)
{
if(LEFT)
{
LEFT = 0;
RIGHT = 1;
}
else
{
RIGHT = 0;
LEFT = 1;
}
UP = 1;
DOWN = 0;
}
}
void p_draw()
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(P_XPOS-0.2,P_YPOS);
glVertex2f(P_XPOS+0.2,P_YPOS);
glVertex2f(P_XPOS+0.2,P_YPOS-0.05);
glVertex2f(P_XPOS-0.2,P_YPOS-0.05);
glEnd();
}
void BallLoop()
{
glClearColor(1.0,1.0,1.0,0);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
c_draw();
b_draw();
p_draw();
glFlush();
if(started)
c_move();
if(crashed())
{
x_pos = 0;
y_pos = -0.7;
started = 0;
UP = 1;
RIGHT = 1;
DOWN = 0;
LEFT = 0;
}
glutSwapBuffers();
glutPostRedisplay();
}
void user_input(unsigned char key, int x, int y)
{
if(key == 13)
started = true;
}
void ArrowKeys(int key, int x, int y)
{
if(key==GLUT_KEY_LEFT && P_XPOS >= -0.8)
for(float a = 0; a < 0.05; a+= 0.001)
{
P_XPOS -=0.003;
BallLoop();
}
if(key==GLUT_KEY_RIGHT && P_XPOS <= 0.8)
{
for(float a = 0; a < 0.05; a+= 0.001)
{
P_XPOS +=0.003;
BallLoop();
}
}
}
void set_xy()
{
for(int a = 0; a < 5; a++)
{
for(int b = 0; b < 10; b++)
{
show[a][b] = 1;
}
}
int c = 0;
for(float a = -0.94; c <= 8; a+=0.21)
{
for(int b = 0; b <= 5; b++)
{
x_brick[b][c] = a;
}
c++;
}
int d = 0;
for(float s = 0.99; d <= 3; s-=0.11)
{
for(int r = 0; r < 9; r++)
{
y_brick[d][r] = s;
}
d++;
}
}
void changeSize(int w, int h)
{
if(h == 0)
h = 1;
RATIO = w/h;
float ratio = 1.0* w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
BallLoop();
}
int main(int argc, char **argv)
{
set_xy();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(WIDTH,HEIGHT);
glutCreateWindow("Brick Breaker - By Viraj");
glutReshapeFunc(changeSize);
glutDisplayFunc(BallLoop);
glutKeyboardFunc(user_input);
glutSpecialFunc(ArrowKeys);
glutMainLoop();
return 0;
}
Pour un mouvement régulier, vérifier cela: http://unity3d.com/support/documentation/ScriptReference/Input.GetAxis.html L'axe a une augmentation exponentielle alors quand vous appuyez sur le bouton vers le bas puis de nouveau tout à coup, votre pagaie ne bouge plus autant. – holgac