Java代写:CS335 Space Racing Simulator

Requirement

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的部分代码

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
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();
}
...
}