我对java很陌生,我正在尝试创建一个基本游戏,其中一种敌人可以通过另一个类和方法多次生成。到目前为止,我已经成功地为这个敌人创造了一个具有设定速度x,y和dmg的能力,但是敌人的运动却无法正常运作。出于某种原因,我的敌人似乎停在玩家第一次产生的地方,而不是跟随玩家。我相信这是我的方法的问题,但我不太确定。如果您发现我的代码中有任何不良编码做法或任何我可以改进的内容,请提前告知我并提前致谢。
主程序:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Random;
public class platformer extends Applet implements Runnable, KeyListener, MouseListener
{
Thread main = new Thread (this);
Graphics bufferG;
Image buffer;
boolean playerright=false,playerleft=false,jump=false,playerdown=false,shotleft=false,shotright=false;
int playerx=500,playery=400,yChange=0, height=200, pageCt=1, shotrx=playerx,shotry=playery,shotlx=playerx,shotly=playery,enemyx=1500,enemyy=400,hp=5,enemyhp=2;
Random r = new Random();
Enemy2 enemy= new Enemy2(4,enemyx,enemyy,50,1,playerx,playery,3);
public void init()
{
this.addMouseListener(this);
this.addKeyListener(this);
this.resize (1400,756);
buffer= createImage(this.getWidth(),this.getHeight());
bufferG = buffer.getGraphics();
main.start();
}
public void paint(Graphics g )
{
Font font1 = new Font ( " Bodoni MT",1,25);
if(pageCt==1){
bufferG.setColor(Color.black);
bufferG.fillRect(0,0,1400,800);
bufferG.setColor(Color.gray);
bufferG.fillRect(0,450,1450,350);
enemy.draw(bufferG);
bufferG.setColor(Color.green);
if (playerdown==true){
bufferG.fillRect(playerx,playery+15,50,35);
}
else
bufferG.fillRect(playerx,playery,50,50);
if(shotright==true ){
bufferG.fillOval(shotrx,shotry,25,25);
}
if(shotleft==true ){
bufferG.fillOval(shotlx,shotly,25,25);
}
}
if(pageCt==2){
bufferG.setFont(font1);
bufferG.drawString("You lose",675,150);
}
g.drawImage(buffer,0,0,this);
}
public void run()
{
while(true)
{
repaint();
try
{ main.sleep(10); }
catch (Exception e ) {}
if(pageCt==1){
if (playerright==true ){
playerx+=5;
}
if (playerleft==true ){
playerx-=5;
}
if (jump==true ){
yChange++;
playery=playery+yChange;
if(playery>height){
jump=false;
playery=height;
}
}
if(shotrx>0 && shotrx<1300 && shotright==true)
{
shotrx+=7;
}
else{
shotright=false;
shotrx=playerx;
shotry=playery;
}
if(shotlx>0 && shotlx<1300 && shotleft==true)
{
shotlx-=7;
}
else{
shotleft=false;
shotlx=playerx;
shotly=playery;
}
enemy.move();
if(enemyhp<=0){
enemyhp=2;
int c=r.nextInt(3);
if(c==1)
enemyx=1500;
if(c==2)
enemyx=-200;
}
if(hp<=0)
{
pageCt=2;
}
Rectangle playerRect2 = new Rectangle ( playerx,playery+15,50,35);
Rectangle playerRect1 = new Rectangle ( playerx,playery,50,50);
Rectangle enemyRect = new Rectangle ( enemyx,enemyy,50,50);
Rectangle shotr = new Rectangle(shotrx,shotry,25,25);
Rectangle shotl = new Rectangle(shotlx,shotly,25,25);
if(enemyRect.intersects(playerRect1)){
hp--;
if(enemyx>playerx)
enemyx+=150;
if(enemyx<playerx)
enemyx-=150;
}
if(shotr.intersects(enemyRect)|| shotl.intersects(enemyRect)){
enemyhp--;
shotright=false;
shotleft=false;
if(enemyx>playerx)
enemyx+=100;
if(enemyx<playerx)
enemyx-=100;
}
}
}
}
public void keyReleased(KeyEvent e)
{
int key= e.getKeyCode();
if (key == KeyEvent.VK_A )
{
playerleft=false;
}
if (key == KeyEvent.VK_S )
{
playerdown=false;
}
if (key == KeyEvent.VK_D )
{
playerright=false;
}
}
public void keyPressed(KeyEvent e)
{
int key= e.getKeyCode();
if (key == KeyEvent.VK_A )
{
playerleft=true;
}
if(key == KeyEvent.VK_W && jump==false && playery>=height)
{
jump=true;
yChange=-20;
height=playery;
}
if (key == KeyEvent.VK_S )
{
playerdown=true;
}
if (key == KeyEvent.VK_D )
{
playerright=true;
}
repaint();
}
public void keyTyped(KeyEvent e) {}
public void update(Graphics g)
{
paint(g);
}
public void mouseClicked ( MouseEvent e )
{
int mouseX = e.getX();
int mouseY = e.getY();
if(mouseX>=playerx && shotright==false && shotleft==false)
shotright=true;
if(mouseX<playerx && shotleft==false&& shotright==false)
shotleft=true;
}
public void mousePressed ( MouseEvent e )
{
}
public void mouseReleased ( MouseEvent e ) {}
public void mouseEntered ( MouseEvent e ) {}
public void mouseExited ( MouseEvent e ) {}
}
敌人计划:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Random;
public class Enemy2{
int speed,startX,startY,radius=50,damage=1, playerx,playery,health;
public Enemy2(int s, int sX,int sY,int rad, int dmg, int playx,int playy, int hp){
speed=s;
startX=sX;
startY=sY;
radius=rad;
damage=dmg;
playerx=playx;
playery=playy;
health=hp;
}
public void draw(Graphics g){
g.setColor(Color.red);
g.fillRect(startX,startY,radius,radius);
}
/*public void moveleft(){
if(startX>playerx)startX-=speed;
}
public void moveright(){
if(startX<playerx)startX+=speed;
}*/
public void move(){
if(startX>playerx){
startX-=speed;
}
else if(startX<playerx){
startX-=speed*-1;
}
}
}
答案 0 :(得分:1)
当您创建一个Enemy2对象时,您将为构造函数提供playx和playy整数值,并将它们复制到两个新的整数playerx和playery中。
整数playerx和playery无法更改Enemy2类(因为你没有创建代码来执行此操作)。
您可能认为他们以某种方式连接到您从该位置获取的Player对象的变量,但事实并非如此。
相反,您可以尝试在Enemy2构造函数中传递Player对象,并使用getter方法获取Player对象的当前值,而不是传递两个整数(playerx和playery)。
它应该是这样的:
public Enemy2(int s, int sX,int sY,int rad, int dmg, **Player player**, int hp) {
//constructor code here
}
并且move()方法看起来像这样:
public void move(){
if (startX > player.getPlayerX() //or player.playerx if the fields are not private) {
startX -= speed;
} else if (startX < player.getPlayerX()) {
startX -= speed * (-1);
}
}
顺便说一下,建议几乎总是将Class的字段设置为private,并使用getter和setter来获取或更改它们的值。
只是想补充一点,这是我在堆栈溢出中的第一个答案。 希望我帮忙!