gpt4 book ai didi

Java while循环条件问题

转载 作者:行者123 更新时间:2023-12-01 23:03:33 24 4
gpt4 key购买 nike

这个简单的代码应该是尝试制作一个游戏。目前有可移动的绿色矩形(玩家)和两个带有半径的黑色矩形(敌人)。如果玩家进入raidus,敌人开始改变它的位置。我想实现敌方直辖区移动到玩家的 x 和 y 坐标,但是每次敌人击中玩家的 x 或 y 轴坐标(只有其中之一,而不是两者)时,它就会停止。我希望它也完成第二轴移动。为什么会发生这种情况?我想考虑到这是移动条件的位置,问题可能出在 run{} 方法中。

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class Drawing extends JPanel implements KeyListener,Runnable
{
public static JFrame frame;
public static JLabel label;
public static int[] x=new int[10];
public static int[] y=new int[10];
public static int a;
public static Random random;
public static int p_x=0;
public static int p_y=0;
public static int enemy_stop=0;
public static boolean isin=false;
public static Thread move;
public static Rectangle active;
public static Rectangle[] generated=new Rectangle[10];;

public static void drawframe(int width,int height)
{
frame=new JFrame("The game");
frame.setLayout(new BorderLayout());
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);

drawenemies(g,2);
setradius(g);
setplayer(g);

if(entered()&&isin!=true)
{
isin=true;
move=new Thread(new Drawing());
move.start();
}

if(entered()==false&&isin!=false)
{
isin=false;
}
}

public void drawenemies(Graphics g,int amount)
{
random=new Random();
a=amount;
enemy_stop=enemy_stop+1;

if(enemy_stop<=1)
{
for(int i=1;i<=amount;i++)
{
x[i]=random.nextInt(frame.getWidth());
y[i]=random.nextInt(frame.getHeight());
}
}

for(int i=1;i<=amount;i++)
{
g.fillRect(x[i], y[i], 20, 20);
Rectangle store=new Rectangle(x[i], y[i], 20, 20);
generated[i]=store;
}
}

public void setradius(Graphics g)
{
g.setColor(Color.RED);

for(int i=1;i<=a;i++)
{
g.drawRect(x[i]-40, y[i]-40, 100, 100);
}
}

public void setplayer(Graphics g)
{
g.setColor(Color.GREEN);
g.fillRect(p_x, p_y, 20, 20);
g.setColor(Color.BLACK);
g.drawRect(p_x, p_y, 20, 20);
}

public static void main(String args[])
{
drawframe(500,500);
Drawing instance=new Drawing();
frame.add(instance);
frame.addKeyListener(instance);

}

public boolean entered()
{
boolean entered=false;

for(int i=1;i<=a;i++)
{
Rectangle q=new Rectangle(x[i]-40, y[i]-40, 100, 100);
Rectangle s=new Rectangle(p_x, p_y, 20, 20);

if(q.contains(s))
{
entered=true;
active=new Rectangle(x[i], y[i], 20, 20);
}
}

return entered;
}

@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==39)
{
p_x=p_x+10;
repaint();
}
if(e.getKeyCode()==37)
{
p_x=p_x-10;
repaint();
}
if(e.getKeyCode()==38)
{
p_y=p_y-10;
repaint();
}
if(e.getKeyCode()==40)
{
p_y=p_y+10;
repaint();
}
}

public void keyReleased(KeyEvent arg0){}
public void keyTyped(KeyEvent arg0){}

@Override
public void run()
{
try
{

for(int i=1;i<=a;i++)
{
if(active.getX()==generated[i].getX())
{
while(x[i]!=p_x&&y[i]!=p_y)
{
if(x[i]>p_x)
x[i]=x[i]-1;
else
x[i]=x[i]+1;

if(y[i]>p_y)
y[i]=y[i]-1;
else
y[i]=y[i]+1;

Thread.sleep(1500);
frame.repaint();
}
}
}
}
catch(InterruptedException e)
{
}
}
}

最佳答案

我猜在 while(x[i]!=p_x&&y[i]!=p_y)应该是||而不是&&

也在 run , entered , setradiusdrawenemies for 循环的方法应该是: for(int i=0; i < a; i++)因为Java中的数组是从0开始计数的至n-1哪里n是数组的大小。您的代码将生成 ArrayIndexOutOfBoundsException

keyPressed方法你可以替换这四个 if使用一个 switch 语句。

您还可以考虑将方法名称更改为 camelCase

关于Java while循环条件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23170052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com