我有一个游戏的问题,一个nullPointerException,它有时只会累积。 我会把代码放进去,如果有人可以提供帮助,那对我很有帮助。我只会提供我认为重要的代码,以便在需要更多编辑时使用。 谢谢。
package wave.myFirstGameEver;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;
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 Random r;
private Handler handler;
private HUD hud;
private Spawn spawner;
private Menu menu;
public enum STATE { // enum inside of our class, but we can do a different
// enum class like the ID class
Menu,
Game,
Help,
End
};
public static STATE gameState = STATE.Menu; //changes the game's state to Menu or Game
public Game() {
handler = new Handler();
hud = new HUD();
menu = new Menu(this, handler, hud);
this.addKeyListener(new KeyInput(handler));
this.addMouseListener(menu);
new Window(WIDTH, HEIGHT, "Wave!", this);
spawner = new Spawn(handler, hud);
r = new Random();
if (gameState == STATE.Game)
{
handler.addObject(new Player(WIDTH / 2 - 32, HEIGHT / 2 - 32, ID.Player, handler));
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
}else{
for(int i = 0; i < 15; i++){
handler.addObject(new MenuParticle(r.nextInt(WIDTH),r.nextInt(HEIGHT), ID.MenuParticle, handler));
}
}
}
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();
}
}
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
delta--;
}
if (running)
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
private void tick() {
handler.tick();
if(gameState == STATE.Game) {
hud.tick();
spawner.tick();
if(HUD.HEALTH <= 0){ //for the particle effect in the menu and at the end when u die
HUD.HEALTH = 100;
gameState = STATE.End;
handler.clearEnemys();
for(int i = 0; i < 15; i++){
handler.addObject(new MenuParticle(r.nextInt(WIDTH),r.nextInt(HEIGHT), ID.MenuParticle, handler));
}
}
}else if(gameState == STATE.Menu || gameState == STATE.End){
menu.tick();
}
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
handler.render(g);
if (gameState == STATE.Game) {
hud.render(g);
}else if(gameState == STATE.Menu || gameState == STATE.Help || gameState == STATE.End){
menu.render(g);
}
g.dispose();
bs.show();
}
public static float clamp(float var, float min, float 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();
}
}
package wave.myFirstGameEver;
import java.awt.Graphics;
import java.awt.Rectangle;
public abstract class GameObject {
protected float x,y;
protected ID id;
protected float velX, velY;
public GameObject(float x, float y, ID id){
this.x = x;
this.y = y;
this.id = id;
}
public abstract void tick();
public abstract void render(Graphics g);
public abstract Rectangle getBounds();
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public float getX(){
return x;
}
public float getY(){
return y;
}
public void setID(ID id){
this.id = id;
}
public ID getId(){
return id;
}
public void setVelX(int velX){
this.velX = velX;
}
public void setVelY(int velY){
this.velY = velY;
}
public float getVelX(){
return velX;
}
public float getVelY(){
return velY;
}
}
package wave.myFirstGameEver;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import wave.myFirstGameEver.Game.STATE;
public class Menu extends MouseAdapter {
private Game game;
private Handler handler;
private HUD hud;
private Random r = new Random();
public Menu(Game game, Handler handler, HUD hud){
this.game = game;
this.hud = hud;
this.handler = handler;
}
public void mousePressed(MouseEvent e){ //when i press it stores what the x and y position is
int mx = e.getX();
int my = e.getY();
if(game.gameState == STATE.Menu){
//play button
if(mouseOver(mx, my, 210, 150, 200, 64)){
game.gameState = STATE.Game;
handler.addObject(new Player(Game.WIDTH / 2 - 32, Game.HEIGHT / 2 - 32, ID.Player, handler));
handler.clearEnemys();
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
}
//help button
if(mouseOver(mx, my, 210, 250, 200, 64)){
game.gameState = STATE.Help;
}
//guit button
if(mouseOver(mx, my, 210, 350, 200, 64)){
System.exit(1);
}
}
//back button for help
if(game.gameState == STATE.Help){
if(mouseOver(mx, my, 210, 350, 200, 64)){
game.gameState = STATE.Menu;
return;
}
}
//back button for end
if(game.gameState == STATE.End){
if(mouseOver(mx, my, 210, 350, 200, 64)){
game.gameState = STATE.Game;
hud.setLevel(1);
hud.setScore(0);
handler.addObject(new Player(Game.WIDTH / 2 - 32, Game.HEIGHT / 2 - 32, ID.Player, handler));
handler.clearEnemys();
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, handler));
}
}
}
public void mouseReleased(MouseEvent e){
}
// this method is checking is the mouse is over a selectide target
private boolean mouseOver(int mx, int my, int x, int y, int width, int height){
if(mx > x && mx < x + width){
if(my > y && my < y + height){
return true;
}else return false;
}else return false;
}
public void tick(){
}
public void render(Graphics g){
if(game.gameState == STATE.Menu){
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
g.setFont(fnt);
g.setColor(Color.white);
g.drawString("Wave", 240, 70);
g.setFont(fnt2);
g.drawRect(210, 150, 200, 64);
g.drawString("Play", 280, 190);
g.drawRect(210, 250, 200, 64);
g.drawString("Help", 280, 290);
g.drawRect(210, 350, 200, 64);
g.drawString("Quit", 280, 390);
}else if(game.gameState == STATE.Help){
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 20);
g.setFont(fnt);
g.setColor(Color.white);
g.drawString("Help", 240, 70);
g.setFont(fnt3);
g.drawString("Use WASD keys to move player and dodge enemies", 70, 200);
g.setFont(fnt2);
g.drawRect(210, 350, 200, 64);
g.drawString("Back", 280, 390);
}else if(game.gameState == STATE.End){
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 30);
Font fnt3 = new Font("arial", 1, 20);
g.setFont(fnt);
g.setColor(Color.white);
g.drawString("Game Over", 180, 70);
g.setFont(fnt3);
g.drawString("You lost with a score of: " + hud.getScore(), 170, 200);
g.setFont(fnt2);
g.drawRect(210, 350, 200, 64);
g.drawString("Try Again", 245, 390);
}
}
}
package wave.myFirstGameEver;
import java.awt.Graphics;
import java.util.ArrayList;
//import java.util.LinkedList; this gives a nullPointer....90% of the cases, see later
public class Handler {
ArrayList<GameObject> object = new ArrayList<GameObject>();
public void tick(){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
tempObject.render(g);
}
}
public void clearEnemys(){
for(int i = 0; i < object.size(); i++){
GameObject tempObject = object.get(i);
if(tempObject.getId() == ID.Player)
{
object.clear();
if(Game.gameState != Game.STATE.End)
addObject(new Player((int)tempObject.getX(), (int)tempObject.getVelY(), ID.Player, this));
}
}
}
public void addObject(GameObject object){
this.object.add(object);
}
public void removeObject(GameObject object){
this.object.remove(object);
}
}
package wave.myFirstGameEver;
import java.awt.Color;
import java.awt.Graphics;
public class HUD {
public static float HEALTH = 100;
private float greenValue = 255;
private int score = 0;
private int level = 1;
public void tick(){
HEALTH = (int) Game.clamp(HEALTH, 0, 100);
greenValue = (int) Game.clamp(greenValue, 0, 255);
greenValue = HEALTH * 2;
score++;
}
public void render(Graphics g){
g.setColor(Color.gray);
g.fillRect(15, 15, 200, 32);
g.setColor(new Color(75,(int) greenValue, 0));
g.fillRect(15, 15,(int) HEALTH * 2, 32);
g.setColor(Color.white);
g.drawRect(15, 15, 200, 32);
g.drawString("Score: " + score, 15, 64);
g.drawString("Level: " + level, 15, 80);
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return score;
}
public int getLevel(){
return level;
}
public void setLevel(int level){
this.level = level;
}
}