我正在尝试将射击添加到我的游戏中,但我无法将我的 Player 类更改为抽象类,否则它将无法正常工作,因此我只能做我能做的事情。
我已经尝试了一百万种不同的方法来解决这个问题,但都没有奏效。我搜索并搜索并观看了视频教程无济于事。这是我最后的手段,我相信有人可以帮助我。
这是我的第一个小程序,但我对 java 并不陌生。这不是 2D 游戏。这只是我一直在做的一个基本实验。
主类.java
/**
*
* @author Alexander Benoit
*/
import java.applet.*;
import java.awt.*;
public class MainClass extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
Thread thread = new Thread(this);
boolean running = true;
Player p;
Enemy e;
Image dbImage;
Graphics dbg;
int xpos;
int ypos;
int mousex;
int mousey;
@Override
public void init() {
setSize(400, 400);
p = new Player(this);
e = new Enemy(this);
}
@Override
public void start() {
thread.start();
}
@Override
public void destroy() {
running = false;
}
@Override
public void stop() {
running = false;
}
@Override
public void run() {
while (running) {
repaint();
p.update(this);
e.update(this, p);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
System.out.print("Error occured");
}
}
}
@Override
public void update(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
@Override
public void paint(Graphics g) {
g.drawString("Game", 180, 20);
p.paint(g, this);
e.paint(g, this);
}
}
播放器.java
/**
*
* @author Alexander Benoit
*/
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
public class Player implements KeyListener {
private int x = 200, y = 200, velX = 0, velY = 0;
private URL url;
private Image player;
private Image playerimg;
private Image Image;
public boolean isShooting;
public Player(MainClass mc) {
url = mc.getDocumentBase();
player = mc.getImage(url, "http://i563.photobucket.com/albums/ss75/belhelmiel/player-1.png");
}
public void update(MainClass mc) {
mc.addKeyListener(this);
x += velX;
y += velY;
}
public void paint(Graphics g, MainClass mc) {
g.drawImage(player, x, y, mc);
g.drawString("X:" + x + " Y:" + y, 10, 10);
//g.drawRect(x, y, 50, 50);
}
@Override
public void keyTyped(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_SPACE: {
//This should make the player start shooting
//I've tried a million different things
//each one gave me some kind of problem primarily
//because I can't set this class to abstract
break;
}
case KeyEvent.VK_RIGHT: {
if (x >= 430) {
x = -40;
}
velX = 2;
break;
}
case KeyEvent.VK_LEFT: {
if (x <= -40) {
x = 430;
}
velX = -2;
break;
}
case KeyEvent.VK_UP: {
if (y <= -35) {
y = 430;
}
velY = -2;
break;
}
case KeyEvent.VK_DOWN: {
if (y >= 400) {
y = -30;
}
velY = 2;
break;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_SPACE: {
//This should make the player stop shooting
break;
}
case KeyEvent.VK_RIGHT: {
velX = 0;
break;
}
case KeyEvent.VK_LEFT: {
velX = 0;
break;
}
case KeyEvent.VK_UP: {
velY = 0;
break;
}
case KeyEvent.VK_DOWN: {
velY = 0;
break;
}
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
void restart() {
x = 200 - 38 / 2;
y = 200 - 38 / 2;
}
}
敌人.java
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
/**
*
* @author Alexander Benoit
*/
public class Enemy {
private int x = 50, y = 50, Speed=3;
private URL url;
private Image enemy;
public Enemy(MainClass mc) {
url = mc.getDocumentBase();
enemy = mc.getImage(url, "http://i563.photobucket.com/albums/ss75/belhelmiel/enemy.png");
}
public void update(MainClass mc, Player p) {
x+=Speed;
if(x<=0){
Speed=3;
}else if(x>=mc.getWidth()-33){
Speed=-3;
}
Collision(p);
}
public void paint(Graphics g, MainClass mc) {
g.drawImage(enemy, x, y, mc);
}
private void Collision(Player p) {
int PX = p.getX();
int PY = p.getY();
if(PX-38 <= x && PX+38 >= x && PY-38 <= y && PY+38 >= y) {
p.restart();
System.out.print("Collision Detected");
}
}
}
对不起,如果我不能为您提供足够的信息,我自己也缺乏它,因为我在尝试了很长时间之后在网上找不到任何有用的信息。
摘要:当玩家按住空格键时,我需要让某种子弹发射。我知道如何使用 keylistener,但如果不将我的 Player 类设置为抽象,我就不能将 keylistener 与“Graphics g”一起使用,这是我做不到的,否则我的游戏将无法运行。我一直在寻找,试图弄清楚如何做到这一点,但无济于事。抱歉,如果这看起来有点奇怪,我重新启动可能会更好,但我已经为此工作了几个小时。