我正在制作一个非常简单的1v1游戏,我的敌人不会画画。这是我游戏的代码:
主要类别:
package Generic;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class Main {
public static Player player;
public static Enemy enemy;
public static void main(String[] args) throws Exception {
Display.setDisplayMode(new DisplayMode (640, 480));
Display.create();
player = new Player();
//Game
while(!Display.isCloseRequested())
{
setCamera();
drawBackground();
player.draw();
enemy.draw();
Display.sync(60);
Display.update();
}
Display.destroy();
}
public static void setCamera(){
glClear(GL_COLOR_BUFFER_BIT);
//Clear Screen
glMatrixMode(GL_PROJECTION);
//Projection Matrix
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
//Modify ModelView Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
public static void drawBackground() {
glBegin(GL_QUADS);
//Draw Sky
glColor3d(0.7, 0.8, 0.9);
glVertex2d(0, 0);
glVertex2d(640, 0);
glColor3d(0.5, 0.6, 0.9);
glVertex2d(640, 480);
glVertex2d(0, 480);
glEnd();
//Draw Ground
glBegin(GL_QUADS);
glColor3d(0.5, 0.25, 0.1);
glVertex2d(0, 0);
glVertex2d(640, 0);
glVertex2d(640, 32);
glVertex2d(0, 32);
glEnd();
//Draw Grass
glBegin(GL_QUADS);
glColor3d(0, 0.7, 0.1);
glVertex2d(0, 25);
glVertex2d(640, 25);
glVertex2d(640, 32);
glVertex2d(0, 32);
glEnd();
}
}
玩家类:
package Generic;
import static org.lwjgl.opengl.GL11.*;
public class Player {
public double x, y, xspeed, yspeed;
public Player() {
x=100;
y=100;
}
public void draw() {
glPushMatrix();
glTranslated(x, y, 0);
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glVertex2d(-8, 0);
glColor3d(0, 1, 0);
glVertex2d(8, 0);
glColor3d(0, 0, 1);
glVertex2d(8, 16);
glColor3d(1, 1, 0);
glVertex2d(-8, 16);
glEnd();
glPopMatrix();
}
}
敌人类:
package Generic;
import static org.lwjgl.opengl.GL11.*;
public class Enemy {
public double x, y;
public Enemy() {
x=200;
y=32;
}
public void logic()
{
x+=1;
if(x<=640+10) x = -10;
}
public void draw() {
logic();
glPushMatrix();
glTranslated(x, y, 0);
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glVertex2d(-8, 0);
glColor3d(0, 1, 0);
glVertex2d(8, 0);
glColor3d(0, 0, 1);
glVertex2d(8, 16);
glColor3d(1, 1, 0);
glVertex2d(-8, 16);
glEnd();
glPopMatrix();
}
}
敌人(应该是一个立方体)没有在屏幕上绘制。请帮忙,因为我是Java / LWJGL的新手,并且不知道如何自己解决它
答案 0 :(得分:0)
看起来你在吸引敌人之后正在召唤drawBackground
,这最有可能覆盖你的敌人?
请记住,在将内容绘制到屏幕上时,计算机将按照您调用它们的顺序绘制它们,而不管它是背景还是某种对象。