0

我尝试将我的 LWJGL 游戏转换为小程序,但它无法加载图像,当我删除图像的代码时,它会引发空指针异常,所以有人可以给我一个关于如何将此代码转换为小程序的演练:

package com.kgt.platform.name;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import kuusisto.tinysound.Sound;
import kuusisto.tinysound.Music;
import kuusisto.tinysound.TinySound;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import static org.lwjgl.opengl.GL11.*;

public class Game {

public static Player player;
public static Enemy enemy;
public static ExitButton exitbutton;
public static MenuButton menubutton;
public static ArrayList<Coin> coinlist;
public static ArrayList<Bullet> bulletlist;
public static ArrayList<Bullet2> bulletlist2;
public static ArrayList<Platform> platformlist;
public static ArrayList<OneWayPlatform> onewayplatformlist;
public static int score;
public static boolean p1on;
public static boolean p2on;
static State state = State.GAME;
public int curlvl = 1;
private static Texture logo;
private static Texture pause;

public enum State {
    PAUSE, GAME, MAIN_MENU, INTRO;
}

public static void init() {
    try {
        // load texture from PNG file
        logo = TextureLoader.getTexture("PNG",       ResourceLoader.getResourceAsStream("res/logo.png"));
        System.out.println("Texture 'logo' Loaded. Format: PNG");

        pause = TextureLoader.getTexture("PNG",     ResourceLoader.getResourceAsStream("res/paused.png"));
        System.out.println("Texture 'pause' Loaded. Format: PNG");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void logo() {
    Color.white.bind();
    logo.bind(); // or GL11.glBind(texture.getTextureID());

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0,0);
        GL11.glVertex2f(192,468);
        GL11.glTexCoord2f(1,0);
        GL11.glVertex2f(192+logo.getTextureWidth(),468);
        GL11.glTexCoord2f(1,1);
        GL11.glVertex2f(192+logo.getTextureWidth(),468-    logo.getTextureHeight());
        GL11.glTexCoord2f(0,1);
        GL11.glVertex2f(192,468-logo.getTextureHeight());
    GL11.glEnd();
}

public static void pause() {
    Color.white.bind();
    pause.bind(); // or GL11.glBind(texture.getTextureID());

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0,0);
        GL11.glVertex2f(256,440);
        GL11.glTexCoord2f(1,0);
        GL11.glVertex2f(256+pause.getTextureWidth(),440);
        GL11.glTexCoord2f(1,1);
        GL11.glVertex2f(256+pause.getTextureWidth(),440-    pause.getTextureHeight());
        GL11.glTexCoord2f(0,1);
        GL11.glVertex2f(256,440-pause.getTextureHeight());
    GL11.glEnd();
}

public static void main(String[] args) throws Exception
{
    System.setProperty("org.lwjgl.librarypath", new  File("natives").getAbsolutePath());
    System.out.println("Native Path: " + System.getProperty("org.lwjgl.librarypath"));
    Display.setDisplayMode(new DisplayMode(640, 480));
    Display.create();
    init();

    score = 0;

    player = new Player();

    exitbutton = new ExitButton(320, 61);
    menubutton = new MenuButton(320, 95);

    enemy = new Enemy();
    coinlist = new ArrayList<Coin>(0);
    bulletlist = new ArrayList<Bullet>(0);
    bulletlist2 = new ArrayList<Bullet2>(0);
    platformlist = new ArrayList<Platform>(0);
    onewayplatformlist = new ArrayList<OneWayPlatform>(0);
    Level1();

    while(!Display.isCloseRequested())
    {
        setCamera();
        drawBackground1();
        for(Coin c: coinlist) c.draw();
        for(Bullet b: bulletlist) b.draw();
        for(Bullet2 b: bulletlist2) b.draw();
        for(Platform b: platformlist) b.draw();
        for(OneWayPlatform b: onewayplatformlist) b.draw();
        enemy.draw();           
        player.draw();

        states();
        stateinput();
        exitbutton.draw();
        menubutton.draw();
        Display.update();
        Display.sync(60);
    }

    Display.destroy();
}

public static void stopsound(){
    TinySound.shutdown();
}


public static void states(){
    switch (state) {
    case INTRO:
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        glColor3f(1.0f, 0.0f, 0.0f);
        glRectf(0, 0, 640, 480);
        break;
    case GAME:
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        break;
    case MAIN_MENU:
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        glColor3f(0.0f, 0.0f, 1.0f);
        glRectf(0, 0, 640, 480);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, logo.getTextureID());
        logo();
        break;
    case PAUSE:
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
        glEnable(GL_COLOR_ARRAY);
        glBegin(GL_QUADS);

        glColor4d(0.5f, 0.5f, 0.5, 0.7);        

        glVertex2d(224, 40);
        glVertex2d(416, 40);

        glColor4d(0.7f, 0.7f, 0.7, 0.7);
        glVertex2d(416, 440);
        glVertex2d(224, 440);


        glEnd();
        glDisable(GL_COLOR_ARRAY);

        glColor3f(0f, 0f, 0f);
        glRectf(224, 40, 226, 440);
        glRectf(416, 40, 414, 440);
        glRectf(224, 40, 416, 42);
        glRectf(224, 438, 416, 440);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, pause.getTextureID());
        pause();

        break;
    }
}

public static void stateinput(){

    while (Keyboard.next()) {
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
            if(state == State.GAME)
                state = State.PAUSE;
            else if(state == State.PAUSE)
                state = State.GAME;
        }
    }

}
public static void jumpsound(){

    TinySound.init();
    Sound jump = TinySound.loadSound("/sound/Jump6.wav");
    jump.play();

}

public static void shootsound(){

    TinySound.init();
    Sound shoot = TinySound.loadSound("/sound/Shoot1.wav");
    shoot.play();
}

public static void selectsound(){
    TinySound.init();
    Sound select = TinySound.loadSound("/sound/select.wav");
    select.play();
}

public static void hurtsound(){
    TinySound.init();
    Sound hurt = TinySound.loadSound("/sound/Hurt1.wav");
    hurt.play();
}

public static void music(int track){

    TinySound.init();
    Music ocean = TinySound.loadMusic("/music/ocean.wav");
    Music menu1 = TinySound.loadMusic("/music/menu1.wav");

    if(track == 1){
    ocean.play(true);
    }
    else if (track == 0){
        menu1.play(true);
    }
}

public static void setCamera(){
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glEnable(GL_COLOR_ARRAY);
    GL11.glShadeModel(GL11.GL_SMOOTH);        
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glDisable(GL11.GL_LIGHTING);                    

    GL11.glEnable( GL11.GL_TEXTURE );
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);                
    GL11.glClearDepth(1);                                       

    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glViewport(0,0,640,480);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 640, 0, 480, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
}

public static void Level1(){
    Game.platformlist.add(new Platform(16, 120));
    Game.platformlist.add(new Platform(48, 120));
    Game.platformlist.add(new Platform(624, 120));
    Game.platformlist.add(new Platform(592, 120));
    Game.onewayplatformlist.add(new OneWayPlatform(128, 200));
    Game.onewayplatformlist.add(new OneWayPlatform(160, 200));
    music(1);
}

public static void drawBackground1(){
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    glBegin(GL_QUADS);

    glColor3d(0.4, 0.5, 0.7);
    glVertex2d(640, 480);
    glVertex2d(0, 480);

    glColor3d(0.7, 0.8, 0.9);       
    glVertex2d(0, 0);
    glVertex2d(640, 0);
    glEnd();

    glBegin(GL_QUADS);

    glColor3d(0.3, 0.2, 0.1);   
    glVertex2d(0, 0);
    glVertex2d(640, 0);

    glColor3d(0.5, 0.2, 0.1);           
    glVertex2d(640, 32);
    glVertex2d(0, 32);
    glEnd();

    glBegin(GL_QUADS);

    glColor3d(0.5, 0.2, 0.1);   
    glVertex2d(0, 32);
    glVertex2d(640, 32);

    glColor3d(0.2, 0.5, 0.1);   
    glVertex2d(640, 47);
    glVertex2d(0, 47);

    glColor3d(1, 1, 1);

    glEnd();
}
}
4

0 回答 0