好吧,所以当我导出我的游戏时,它会运行并打开一个jframe,但由于某种原因不会转移到我的下一个游戏场景,我认为它应该发出声音的错误。提前致谢。德韦恩。我也试过jar拼接和jar matie。似乎没什么用。
主要课程
package com.DwainEdwards.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import com.DwainEdwards.main.Screen;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1550691097823471818L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
private Menu menu;
private Handler handler;
private HUD hud;
private Hudp2 hudp2;
private HUDP1 hudp1;
public ID id;
private Spawn spawner;
private Spawn2 spawner2;
private Screen screen;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer())
.getData();
static Texture tex;
public void init() {
tex = new Texture();
screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
}
public static Texture getInstance() {
return tex;
}
public enum STATE {
Intro, Menu, Play, Singleplayer, GameOver,
easy, Game, Game2, Game3, Level1C, Level2C,
Level3C, Hardcore, survive, local, Help,
Credits, Character, Skin, Player1wins, Player2wins
};
public static float clamp(float var, float min, int max) {
if (var >= max)
return var = max;
else if (var <= min)
return var = min;
else
return var;
}
public static void main(String args[]) {
new Game();
}
public STATE gameState = STATE.Intro;
public Game() {
new Window(WIDTH, HEIGHT, "the Epic Game", this);
handler = new Handler();
menu = new Menu(this, handler);
this.addMouseListener(menu);
hud = new HUD();
hudp2 = new Hudp2();
this.addKeyListener(new KeyInput(handler));
spawner = new Spawn(handler, hud);
spawner2 = new Spawn2(handler, hudp1);
AudioPlayer.Load();
new Random();
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
init();
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (shouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
System.out.println(ticks + " ticks, " + frames + " frames");
frames = 0;
ticks = 0;
}
}
}
@SuppressWarnings("static-access")
private void tick() {
handler.tick();
if (gameState == STATE.Game) {
hud.tick();
spawner.tick();
if (hud.HEALTH <= 0) {
hud.HEALTH = 100;
handler.clearEnemys();
AudioPlayer.getSound("gosound").play();
gameState = STATE.GameOver;
} else if (hud.level == 10) {
HUD.HEALTH = 100;
handler.clearEnemys();
AudioPlayer.getSound("victory").play();
gameState = STATE.Level1C;
}
}else if (gameState == STATE.Intro) {
menu.tick();
} else if (gameState == STATE.Game2) {
hud.tick();
spawner.tick();
if (HUD.HEALTH <= 0) {
HUD.HEALTH = 100;
handler.clearEnemys();
gameState = STATE.GameOver;
} else if (HUD.level == 20) {
HUD.HEALTH = 100;
handler.clearEnemys();
AudioPlayer.getSound("victory").play();
gameState = STATE.Level2C;
}
} else if (gameState == STATE.Game3) {
hud.tick();
spawner.tick();
if (HUD.HEALTH <= 0) {
HUD.HEALTH = 100;
handler.clearEnemys();
gameState = STATE.GameOver;
} else if (HUD.level == 30) {
HUD.HEALTH = 100;
handler.clearEnemys();
gameState = STATE.Level3C;
if (HUD.HEALTH <= 0) {
HUD.HEALTH = 100;
handler.clearEnemys2();
gameState = STATE.GameOver;
}
} else if (gameState == STATE.Menu) {
menu.tick();
} else if (gameState == STATE.Help) {
menu.tick();
} else if (gameState == STATE.GameOver) {
menu.tick();
} else if (gameState == STATE.Level1C) {
menu.tick();
} else if (gameState == STATE.Level2C) {
menu.tick();
} else if (gameState == STATE.Credits) {
menu.tick();
} else if (gameState == STATE.Character) {
menu.tick();
} else if (gameState == STATE.Play) {
menu.tick();
} else if (gameState == STATE.Singleplayer) {
menu.tick();
} else if (gameState == STATE.local) {
hudp1.tick();
hudp2.tick();
spawner2.tick();
if (HUD.HEALTH <= 0) {
HUD.HEALTH = 100;
Hudp2.HEALTH = 100;
handler.clearEnemys2();
gameState = STATE.Player1wins;
} else if (Hudp2.HEALTH <= 0) {
HUD.HEALTH = 100;
Hudp2.HEALTH = 100;
handler.clearEnemys2();
gameState = STATE.Player2wins;
}
}
} else if (gameState == STATE.Skin) {
menu.tick();
} else if (gameState == STATE.Player1wins) {
menu.tick();
} else if (gameState == STATE.Player2wins) {
menu.tick();
}
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
screen.render(pixels, 0, WIDTH);
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
handler.render(g);
if (gameState == STATE.Game) {
hud.render(g);
} else if (gameState == STATE.Game2) {
hud.render(g);
} else if (gameState == STATE.Game3) {
hud.render(g);
} else if (gameState == STATE.local) {
hud.render(g);
hudp2.render(g);
} else if (gameState == STATE.Menu) {
menu.render(g);
} else if (gameState == STATE.Help) {
menu.render(g);
} else if (gameState == STATE.Credits) {
menu.render(g);
} else if (gameState == STATE.GameOver) {
g.setColor(Color.red);
g.fillRect(0, 0, WIDTH, HEIGHT);
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 15);
g.setColor(Color.gray);
g.fillRect(250, 350, 150, 64);
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("Game Over", 200, 100);
g.setFont(fnt3);
g.setColor(Color.black);
g.drawString("You have lost with a score of: " + hud.getScore(),
160, 200);
g.drawString("You lost on level: " + hud.getLevel(), 160, 250);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Try Again", 260, 390);
g.setColor(Color.black);
g.drawRect(250, 350, 150, 64);
} else if (gameState == STATE.Level1C) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 15);
g.setFont(fnt);
g.setColor(Color.green);
g.drawString("Stage Complete!", 110, 100);
g.setFont(fnt3);
g.setColor(Color.green);
g.drawString("You have won with a score of: " + hud.getScore(),
160, 200);
g.drawString("You won on level: " + hud.getLevel(), 160, 250);
g.setFont(fnt2);
g.setColor(Color.green);
g.drawString("Next Level", 260, 390);
g.setColor(Color.green);
g.drawRect(250, 350, 170, 64);
} else if (gameState == STATE.Level3C) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 15);
g.setColor(Color.gray);
g.fillRect(250, 350, 170, 64);
g.setFont(fnt);
g.setColor(Color.green);
g.drawString("Stage 3 Complete!", 110, 100);
g.setFont(fnt3);
g.setColor(Color.green);
g.drawString("You have won with a score of: " + hud.getScore(),
160, 200);
g.drawString("You won on level: " + hud.getLevel(), 160, 250);
g.setFont(fnt2);
g.setColor(Color.green);
g.drawString("Next Level", 260, 390);
g.setColor(Color.green);
g.drawRect(250, 350, 170, 64);
} else if (gameState == STATE.Level2C) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 15);
g.setFont(fnt);
g.setColor(Color.green);
g.drawString("Stage 2 Complete!", 110, 100);
g.setFont(fnt3);
g.setColor(Color.green);
g.drawString("You have won with a score of: " + hud.getScore(),
160, 200);
g.drawString("You won on level: " + hud.getLevel(), 160, 250);
g.setFont(fnt2);
g.setColor(Color.green);
g.drawString("Next Level", 260, 390);
g.setColor(Color.green);
g.drawRect(250, 350, 170, 64);
} else if (gameState == STATE.Character) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
g.setColor(Color.gray);
g.fillRect(25, 370, 150, 64);
g.setColor(Color.gray);
g.fillRect(25, 130, 150, 64);
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("Player", 40, 80);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Back", 60, 410);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Skin", 65, 170);
g.setColor(Color.black);
g.drawRect(25, 370, 150, 64);
g.setColor(Color.black);
g.drawRect(25, 130, 150, 64);
} else if (gameState == STATE.Play) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
g.setColor(Color.gray);
g.fillRect(236, 370, 150, 64);
g.setColor(Color.gray);
g.fillRect(236, 270, 150, 64);
g.setColor(Color.gray);
g.fillRect(236, 170, 150, 64);
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("Play", 255, 70);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Back", 275, 410);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Solo", 275, 210);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Local", 265, 310);
g.setColor(Color.black);
g.drawRect(236, 370, 150, 64);
g.setColor(Color.black);
g.drawRect(236, 270, 150, 64);
g.setColor(Color.black);
g.drawRect(236, 170, 150, 64);
} else if (gameState == STATE.Singleplayer) {
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 25);
g.setColor(Color.gray);
g.fillRect(248, 100, 120, 50);
g.setColor(Color.gray);
g.fillRect(248, 169, 120, 50);
g.setColor(Color.gray);
g.fillRect(248, 240, 120, 50);
g.setColor(Color.gray);
g.fillRect(248, 310, 120, 50);
g.setColor(Color.gray);
g.fillRect(236, 370, 150, 64);
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("Solo", 255, 70);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Back", 275, 410);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Baby", 275, 134);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Classic", 265, 200);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Hardcore", 253, 272);
g.setFont(fnt2);
g.setColor(Color.black);
g.drawString("Survival", 257, 344);
g.setColor(Color.black);
g.drawRect(248, 100, 120, 50);
g.setColor(Color.black);
g.drawRect(248, 169, 120, 50);
g.setColor(Color.black);
g.drawRect(248, 240, 120, 50);
g.setColor(Color.black);
g.drawRect(248, 310, 120, 50);
g.setColor(Color.black);
g.drawRect(236, 370, 150, 64);
} else if (gameState == STATE.Intro) {
Font fnt = new Font("Arial", 1, 50);
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("The Epic Game", 0, 50);
g.setColor(Color.white);
g.fillRect(232, 232, 32, 32);
g.setColor(Color.red);
g.fillRect(332, 332, 16, 16);
} else if (gameState == STATE.Skin) {
Font fnt = new Font("arial", 1, 20);
g.setColor(Color.gray);
g.fillRect(540, 400, 90, 45);
g.setColor(Color.black);
g.drawRect(540, 400, 90, 45);
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("Back", 563, 432);
g.setColor(Color.white);
g.fillRect(32, 32, 32, 32);
g.setColor(Color.orange);
g.fillRect(64, 32, 32, 32);
g.setColor(Color.red);
g.fillRect(96, 32, 32, 32);
g.setColor(Color.black);
g.fillRect(128, 32, 32, 32);
g.setColor(Color.yellow);
g.fillRect(160, 32, 32, 32);
g.setColor(Color.magenta);
g.fillRect(192, 32, 32, 32);
g.setColor(Color.blue);
g.fillRect(224, 32, 32, 32);
g.drawImage(tex.player[0], 256, 32, null);
g.drawImage(tex.player[1], 288, 32, null);
g.drawImage(tex.player[2], 320, 32, null);
g.setColor(Color.pink);
g.fillRect(352, 32, 32, 32);
g.drawImage(tex.player[3], 384, 32, null);
g.drawImage(tex.player[4], 416, 32, null);
}
g.dispose();
bs.show();
}
}
AudioPlayer类
package com.DwainEdwards.main;
import java.util.HashMap;
import java.util.Map;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
public class AudioPlayer {
public static Map<String, Sound> soundMap = new HashMap<String, Sound>();
public static Map<String, Music> musicMap = new HashMap<String, Music>();
public static void Load() {
try {
musicMap.put("music", new Music("com/DwainEdwards/sounds/Menu.ogg"));
soundMap.put("gosound", new Sound("com/DwainEdwards/sounds/GameOver.ogg"));
soundMap.put("punch", new Sound("com/DwainEdwards/sounds/Punch.ogg"));
soundMap.put("victory", new Sound("com/DwainEdwards/sounds/Victory.ogg"));
soundMap.put("cssound", new Sound("com/DwainEdwards/sounds/CSVO.ogg"));
soundMap.put("white", new Sound("com/DwainEdwards/sounds/WHITE.ogg"));
soundMap.put("orange", new Sound("com/DwainEdwards/sounds/ORANGE.ogg"));
soundMap.put("red", new Sound ("com/DwainEdwards/sounds/RED.ogg"));
soundMap.put("black", new Sound ("com/DwainEdwards/sounds/BLACK.ogg"));
soundMap.put("yellow", new Sound ("com/DwainEdwards/sounds/YELLOW.ogg"));
soundMap.put("purple", new Sound ("com/DwainEdwards/sounds/PURPLE.ogg"));
soundMap.put("hitler", new Sound ("com/DwainEdwards/sounds/HITLER.ogg"));
soundMap.put("disturbed", new Sound ("com/DwainEdwards/sounds/DISTURBED.ogg"));
soundMap.put("mario", new Sound ("com/DwainEdwards/sounds/MARIO.ogg"));
soundMap.put("blue", new Sound ("com/DwainEdwards/sounds/BLUE.ogg"));
} catch (SlickException e) {
e.printStackTrace();
}
}
public static Music getMusic(String key) {
return musicMap.get(key);
}
public static Sound getSound(String key) {
return soundMap.get(key);
}
}