当运行状况栏耗尽时,代码似乎退出了整个程序,而不是返回菜单屏幕。想知道如何重写if语句,以便它返回到主菜单。目前似乎只能完全退出该程序。提供的运行状况栏代码以及DroneGame中的运行状况栏使用的代码。 healthbar的所有其他方面似乎都在起作用。如有必要,可以提供更多代码。任何帮助,将不胜感激。
package solar;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import SolarClasses.Class1;
import SolarClasses.Class2;
public class Player extends Object implements Class1 {
private double velX = 0;
private double velY = 0;
private ImageTextures txt;
DroneGame game;
Controller controller;
public Player(double x, double y, ImageTextures txt, DroneGame game, Controller controller) {
super(x, y);
this.txt = txt;
this.game = game;
this.controller = controller;
}
public void tick() {
x += velX;
y += velY;
if (x <= 0)
x = 0;
if (x >= 640 - 16)
x = 640 - 16;
if (y >= 480 - 32)
y = 480 - 32;
for (int i = 0; i < game.ct.size(); i++) {
Class2 tempEnt = game.ct.get(i);
if (Collision.Collisions(this, game.ct)) {
controller.removeEnemy(tempEnt);
DroneGame.HEALTH -= 10;
game.setEnemy_killed(game.getEnemy_killed() + 1);
if (DroneGame.HEALTH <= 0) {
System.exit(1);
}
}
}
}
public Rectangle getBounds() {
return new Rectangle((int) x, (int) y, 32, 32);
}
public void render(Graphics g) {
g.drawImage(txt.Player, (int) x, (int) y, null);
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setVelX(double velX) {
this.velX = velX;
}
public void setVelY(double velY) {
this.velY = velY;
}
}
package solar;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import SolarClasses.Class1;
import SolarClasses.Class2;
import java.awt.event.KeyEvent;
public class DroneGame extends Canvas implements Runnable {
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
public static final String TITLE = "Drone Game";
private boolean running = false;
private Thread thread;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private BufferedImage spriteSheet = null;
private boolean is_shooting = false;
private int count_enemy = 5;
private int killed_enemy = 0;
private Player p;
private Controller c;
private ImageTextures txt;
private Menu menu;
public LinkedList < Class1 > cf;
public LinkedList < Class2 > ct;
public static int HEALTH = 100;
public static enum DESC {
MENU,
GAME
};
public static DESC Desc = DESC.MENU;
public void initial() {
BufferedImageLoad load = new BufferedImageLoad();
try {
spriteSheet = ImageIO.read(new File("C:/JavaFXprogramming/solar/sp/DroneImageSim.png"));
} catch (IOException e) {
e.printStackTrace();
}
addKeyListener(new KeyBoardInput(this));
txt = new ImageTextures(this);
c = new Controller(this, txt);
p = new Player(200, 200, txt, this, c);
menu = new Menu();
cf = c.getClass1();
ct = c.getClass2();
this.addKeyListener(new KeyBoardInput(this));
this.addMouseListener(new MouseInput());
c.createEnemy(count_enemy);
}
private synchronized void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop() {
if (!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(1);
}
public void run() {
initial();
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " Ticks, Fps " + frames);
updates = 0;
frames = 0;
}
}
stop();
}
private void tick() {
if (Desc == DESC.GAME) {
p.tick();
c.tick();
}
if (killed_enemy >= count_enemy) {
count_enemy += 2;
killed_enemy = 0;
c.createEnemy(count_enemy);
}
}
private void render() {
BufferStrategy sb = this.getBufferStrategy();
if (sb == null) {
createBufferStrategy(3);
return;
}
Graphics g = sb.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
if (Desc == DESC.GAME) {
p.render(g);
c.render(g);
g.setColor(Color.gray);
g.fillRect(5, 5, 100, 50);
g.setColor(Color.green);
g.fillRect(5, 5, HEALTH, 50);
g.setColor(Color.white);
g.drawRect(5, 5, 100, 50);
} else if (Desc == DESC.MENU) {
menu.render(g);
}
g.dispose();
sb.show();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (Desc == DESC.GAME) {
if (key == KeyEvent.VK_RIGHT) {
p.setVelX(5);
} else if (key == KeyEvent.VK_LEFT) {
p.setVelX(-5);
} else if (key == KeyEvent.VK_DOWN) {
p.setVelY(5);
} else if (key == KeyEvent.VK_UP) {
p.setVelY(-5);
} else if (key == KeyEvent.VK_SPACE && !is_shooting) {
is_shooting = true;
c.addBullet(new Bullet(p.getX(), p.getY(), txt, this));
}
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT) {
p.setVelX(0);
} else if (key == KeyEvent.VK_LEFT) {
p.setVelX(0);
} else if (key == KeyEvent.VK_DOWN) {
p.setVelY(0);
} else if (key == KeyEvent.VK_UP) {
p.setVelY(0);
} else if (key == KeyEvent.VK_SPACE) {
is_shooting = false;
}
}
public static void main(String args[]) {
DroneGame dronegame = new DroneGame();
dronegame.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
dronegame.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
dronegame.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame(dronegame.TITLE);
frame.add(dronegame);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
dronegame.start();
}
public BufferedImage getSpriteSheet() {
return spriteSheet;
}
public int getEnemy_count() {
return count_enemy;
}
public void setEnemy_count(int count_enemy) {
this.count_enemy = count_enemy;
}
public int getEnemy_killed() {
return killed_enemy;
}
public void setEnemy_killed(int killed_enemy) {
this.killed_enemy = killed_enemy;
}
}
您需要调整类的设计,并进一步了解OOP基础知识。
您应该将Player.tick()
方法中的碰撞检查移至DroneGame
,并且仅使该方法专注于移动播放器。您还可以在Player
类中创建一个函数,以通过传入要检查的对象来检查是否存在碰撞,以及是否减少了玩家的生命值,并为游戏返回一个布尔值以对其进行其他处理。
这听起来像是一个有趣的项目,但是我强烈建议您研究面向对象的原理,以帮助您更好地设计这款游戏,以免最终被“黑客入侵”,并且将来更易于维护和增强。