import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
class Game
{
protected int WIDTH = 800;
protected int HEIGHT = 600;
private float angle = 0.0f;
public Player player = new Player();
public Entity entity = new Entity();
Game()
{
entity.setX(3.0f);
entity.setZ(-40.0f);
}
public void init()
{
try
{
Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
Display.create();
}
catch(LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
}
public void initGL()
{
float aspectRatio;
if(HEIGHT > WIDTH)
aspectRatio = (float)HEIGHT/(float)WIDTH;
else
aspectRatio = (float)WIDTH/(float)HEIGHT;
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glFrontFace(GL11.GL_CCW);
GL11.glClearColor(0.0f,0.1f,0.0f,1.0f);
GL11.glViewport(0,0,WIDTH,HEIGHT);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45,aspectRatio,1,-100);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}
public void logic()
{
while(!Display.isCloseRequested())
{
if(Keyboard.isKeyDown(Keyboard.KEY_A))
player.setX(player.getX() - 0.01f);
else if(Keyboard.isKeyDown(Keyboard.KEY_D))
player.setX(player.getX() + 0.01f);
else if(Keyboard.isKeyDown(Keyboard.KEY_W))
player.setZ(player.getZ() - 0.01f);
else if(Keyboard.isKeyDown(Keyboard.KEY_S))
player.setZ(player.getZ() + 0.01f);
else if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
player.setY(player.getY() + 0.01f);
else if(Keyboard.isKeyDown(Keyboard.KEY_E))
{
angle += 0.1f;
if(angle > 360.0f)
angle = 0.0f;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_Q))
{
angle -= 0.1f;
if(angle < 0.0f)
angle = 360.0f;
}
render();
}
}
public void render()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glPushMatrix();
GL11.glTranslatef(-5.0f,-12.0f,-40.0f);
GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1.0f,0.0f,0.0f);
GL11.glVertex3f(0.0f,0.0f,0.0f);
GL11.glVertex3f(10.0f,0.0f,0.0f);
GL11.glVertex3f(10.0f,10.0f,0.0f);
GL11.glVertex3f(0.0f,10.0f,0.0f);
GL11.glColor3f(0.0f,1.0f,0.0f);
GL11.glVertex3f(10.0f,0.0f,0.0f);
GL11.glVertex3f(10.0f,0.0f,10.0f);
GL11.glVertex3f(10.0f,10.0f,10.0f);
GL11.glVertex3f(10.0f,10.0f,0.0f);
GL11.glColor3f(1.0f,0.0f,0.0f);
GL11.glVertex3f(10.0f,0.0f,10.0f);
GL11.glVertex3f(0.0f,0.0f,10.0f);
GL11.glVertex3f(0.0f,10.0f,10.0f);
GL11.glVertex3f(10.0f,10.0f,10.0f);
GL11.glColor3f(0.0f,1.0f,0.0f);
GL11.glVertex3f(0.0f,0.0f,10.0f);
GL11.glVertex3f(0.0f,0.0f,0.0f);
GL11.glVertex3f(0.0f,10.0f,0.0f);
GL11.glVertex3f(0.0f,10.0f,10.0f);
GL11.glColor3f(0.0f,1.0f,1.0f);
GL11.glVertex3f(0.0f,10.0f,0.0f);
GL11.glVertex3f(10.0f,10.0f,0.0f);
GL11.glVertex3f(10.0f,10.0f,10.0f);
GL11.glVertex3f(0.0f,10.0f,10.0f);
GL11.glEnd();
GL11.glPopMatrix();
GL11.glTranslatef(-player.getX(),-player.getY(),-player.getZ());
GL11.glRotatef(angle,0.0f,1.0f,0.0f);
GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(0.0f,0.3f,0.0f);
GL11.glVertex3f(-50.0f,0.0f,50.0f);
GL11.glColor3f(0.0f,0.8f,0.0f);
GL11.glVertex3f(50.0f,0.0f,50.0f);
GL11.glColor3f(0.0f,0.3f,0.0f);
GL11.glVertex3f(50.0f,0.0f,-50.0f);
GL11.glColor3f(0.0f,0.8f,0.0f);
GL11.glVertex3f(-50.0f,0.0f,-50.0f);
GL11.glEnd();
GL11.glPopMatrix();
GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1.0f,0.0f,1.0f);
GL11.glVertex3f(entity.getX() + 0.0f,0.0f,entity.getZ() + 0.0f);
GL11.glVertex3f(entity.getX() + 10.0f,0.0f,entity.getZ() + 0.0f);
GL11.glVertex3f(entity.getX() + 10.0f,10.0f,entity.getZ() + 0.0f);
GL11.glVertex3f(entity.getX() + 0.0f,10.0f,entity.getZ() + 0.0f);
GL11.glEnd();
GL11.glPopMatrix();
GL11.glPopMatrix();
Display.update();
}
public void cleanup()
{
Display.destroy();
}
public static void main(String[] args)
{
Game game = new Game();
game.init();
game.initGL();
game.logic();
game.cleanup();
}
}