我正在尝试为我的游戏添加拍摄,但是我无法将我的Player类更改为抽象,或者它无法正常工作,因此我仅限于我可以做的事情。
我已经尝试了一百万种不同的方式来解决这个问题。我搜索和搜索并观看视频教程无济于事。这是我的最后一招,我相信有人可以帮助我。
这是我的第一个applet,但我不是java的新手。这不是2D游戏。这只是我一直在做的基本实验。
MainClass.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);
}
}
Player.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;
}
}
Enemy.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但我不能将keylistener与“Graphics g”一起使用而不将我的Player类设置为abstract,这是我无法做到的,否则我的游戏将无效。我一直在寻找如何做到这一点,但无济于事。对不起,如果这看起来有些奇怪,我可能会更好地重新启动,但我已经花了好几个小时。