glut生成opengl反弹小方块效果

本文详细介绍了如何使用OpenGL编程语言实现一个简单的反弹小方块游戏,包括初始化窗口、设置颜色清除、绘制小方块、实现碰撞检测和游戏逻辑更新等关键步骤。通过调整速度和边界条件,玩家可以体验到一个动态且交互性强的游戏场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      利用opengl实现了反弹小方块的效果,蓝宝书代码并不全,所以自己实现了没有缺少的部分。

#include "../../shared/gltools.h"
GLfloat x1 = 0.0f;
GLfloat y1 = 0.0f;
GLfloat rSize = 50.0f;
GLfloat xStep = 5.0f;
GLfloat yStep = 5.0f;
GLfloat windowWidth = 400;
GLfloat windowHeight = 300;
void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rSize,y1-rSize);
glutSwapBuffers();
}
void Change(GLsizei w,GLsizei h){

GLfloat aspectRatio;
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w/(GLfloat)h;
if(w<=h){
glOrtho(-h/2,h/2,-w/2*aspectRatio,w/2*aspectRatio,1.0,-1.0);
windowWidth = w/2*aspectRatio;
windowHeight = h/2;
}
else{
glOrtho(-h/2*aspectRatio,h/2*aspectRatio,-w/2,w/2,1.0,-1.0);
windowWidth = h/2*aspectRatio;
windowHeight = w/2;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void SetClearColor(void){
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
void TimerFunction(int value){
if(x1>windowWidth - rSize || x1< -windowWidth)
xStep = -xStep;
if(y1>windowHeight || y1<-windowHeight+rSize)
yStep = -yStep;
x1+=xStep;
y1+=yStep;
if(x1>(windowWidth-rSize+xStep))
x1=windowWidth - rSize -1;
else if(x1<-(windowWidth+xStep))
x1=-windowWidth -1;
if(y1>(windowHeight+yStep))
y1=windowHeight - 1;
else if(y1<-(windowHeight-rSize+yStep))
y1=-windowHeight+rSize-1;
glutPostRedisplay();
glutTimerFunc(33,TimerFunction,1);
}
int main(int argc,char* argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800,600);
glutCreateWindow("lqwang");
glutDisplayFunc(RenderScene);
glutReshapeFunc(Change);
glutTimerFunc(33,TimerFunction,1);


SetClearColor();
glutMainLoop();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值