Java代码编写:CS335 Space Racing Simulator
  TEZNKK3IfmPf 2024年08月09日 60 0

This final project is a group project, each group should have two people. The goal of this project is to develop a java based racing game. The space racing game should provide the following functions:

  1. One computer graphics environment
  2. User-controlled car
  3. User controlled viewpoint
  4. Game AI

The racing vehicle car’s movement can be determined by its velocity and acceleration vectors.
When the user wants to turn the car, the direction of the velocity is changed based on the amount of turning the user provides.

Analysis

本题需要基于​​OpenGL​​开发一个竞速小游戏,需要了解OpenGL的编程框架,以及掌握OpenGL函数库的基本用法,并以此来实现竞速小游戏。整个游戏地图包括边界、坡度、障碍物等。
本题的学习曲线较为陡峭,但是如果掌握了OpenGL的基本编程方法,难度也不是很大。此外,一个值得注意的地方是,本题的AI需要额外设计,对障碍物、边界等地区,进行判断。

Tips

本题代码量很大,下面仅给出display的部分代码

public class SpaceRacingSimulatorEventListerer implements
GLEventListener, KeyListener, MouseListener, MouseMotionListener {
...
private GLU glu = new GLU();
@Override
public void display(GLAutoDrawable gLDrawable) {
final GL2 gl = gLDrawable.getGL().getGL2();
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

gl.glPushMatrix();
gl.glRotatef(rot, 0, 1, 0);
gl.glRotatef(rotX, 1, 0, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION,lightPos, 0);
gl.glPushMatrix();
gl.glTranslatef(-0.5f, -0.5f, -0.5f);
// The color of the sphere
float materialColor[] = {1.0f, 1.0f, 0.0f, 1.0f};
// The specular (shiny) component of the material
float materialSpecular[] = {0.0f, 0.0f, 1.0f, 1.0f};
// The color emitted by the material
float materialEmission[] = {1.0f, 1.0f, 0.0f, 1.0f};
...
gl.glPushMatrix();
gl.glTranslatef(0, 2, 0);
gl.glColor3f(0.8f, 0.8f, 0.8f);
gl.glPopMatrix();
gl.glTranslatef(2, 0, 0);
gl.glScalef(0.5f, 0.5f, 0.5f);
gl.glTranslatef(-0.5f, -0.5f, -0.5f);
drawCube(gl);

int width = 100, height = 100;
byte[] src = new byte[width * height];
for (int a = 0; a < height; a++) {
int color = (int)(a * 1.0f / height * 255);
for(int b = 0; b < width; b++) {
src[a * width + b] = (byte)color;
}
}

gl.glPixelStorei(GL2.GL_UNPACK_ALIGNMENT, 1);
gl.glPixelStorei(GL2.GL_UNPACK_SKIP_PIXELS, 0);
gl.glPixelStorei(GL2.GL_UNPACK_SKIP_ROWS, 0);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();

glu.gluOrtho2D(0, windowWidth, 0, windowHeight);
gl.glRasterPos2f(windowWidth / 2, windowHeight / 2);
gl.glPopMatrix();

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPopMatrix();
}
...
}
```
```
复制代码
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年08月09日 0

暂无评论

推荐阅读
  I7JaHrFMuDsU   2024年08月09日   72   0   0 java设计模式
  I7JaHrFMuDsU   2024年08月09日   72   0   0 javalinux
TEZNKK3IfmPf