import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Move {
private static Texture texture;
private static Texture bull;
public static int mx = 200;
public static int my = 400;
public static int bvelx = SetShip.x;
public static int bvely = SetShip.y;
public static void main(String[] args){
setUpDisplay(640,480);
SetShip.init();
ShootBullet.init();
while (true){
glClear(GL_COLOR_BUFFER_BIT);
SetShip.draw();
logic();
Display.update();
Display.sync(60);
if(Display.isCloseRequested()){
Display.destroy();
System.exit(0);
}
}
}
private static void logic() {
// TODO Auto-generated method stub
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
mx = -5;
SetShip.update(mx);
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
mx = 5;
SetShip.update(mx);
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
bvely = -15;
ShootBullet.update(bvely);
ShootBullet.draw();
}
}
private static void setUpDisplay(int width, int height) {
// TODO Auto-generated method stub
try{
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle("Space Invaders");
Display.create();
} catch (LWJGLException e){
e.printStackTrace();
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
}
private static class ShootBullet {
private static int bulletx = SetShip.x, bullety = SetShip.y;
static void init(){
try{
bull = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/bullet.png")));
} catch (IOException e){
e.printStackTrace();
}
}
static void update(int by){
bullety += by;
}
static void draw(){
bull.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(bulletx, bullety);
glTexCoord2f(1,0);
glVertex2f(bulletx +50, bullety);
glTexCoord2f(1,1);
glVertex2f(bulletx +50, bullety+50);
glTexCoord2f(0,1);
glVertex2f(bulletx, bullety + 50);
glEnd();
}
}
private static class SetShip{
public static int x = 200, y = 400;
static void init() {
// TODO Auto-generated method stub
try{
texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/spaceship.png")));
} catch (IOException e){
e.printStackTrace();
}
}
static void update(int mx){
x += mx;
}
static void draw() {
// TODO Auto-generated method stub
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(x, y);
glTexCoord2f(1,0);
glVertex2f(x +200, y);
glTexCoord2f(1,1);
glVertex2f(x +200, y+100);
glTexCoord2f(0,1);
glVertex2f(x, y + 100);
glEnd();
}
}
}
And the images used
(ship)
http://i.imgur.com/5ZsbS.png
(bullet)
http://i.imgur.com/w5qIM.png
Thanks in advance!