前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java代码编写:CS335 Space Racing Simulator

Java代码编写:CS335 Space Racing Simulator

原创
作者头像
拓端
发布2022-10-25 18:15:01
1890
发布2022-10-25 18:15:01
举报
文章被收录于专栏:拓端tecdat拓端tecdat

全文链接:tecdat.cn/?p=29635

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

代码语言:javascript
复制
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();
  }
  ...
}
```
```
复制代码

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 全文链接:tecdat.cn/?p=29635
    • Requirement
      • Analysis
        • Tips
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档