gpt4 book ai didi

java - 整数会调用一个类,但不会调用另一个类?

转载 作者:行者123 更新时间:2023-11-29 03:35:28 26 4
gpt4 key购买 nike

我正在尝试制作“贪吃蛇”游戏,但我被卡在了尾部上。正确的现在,我只是想让第一个尾部 block 与头。这就是我所拥有的。对于主类:

public class Game extends JPanel implements ActionListener {
Snake p;
RandomDot r;
Trail trail;
Point w;
Image img;
Timer time;
int t=75;
int score = 0;
int count = 0;
int x, y;


boolean lost = false;

public Game(){
p = new Snake();
r = new RandomDot();
trail = new Trail();
w = new Point();
addKeyListener(new AL());
setFocusable(true);
ImageIcon i = new ImageIcon("C:/Matt's stuff/background.png");
img = i.getImage();
time = new Timer(t, this);
time.start();
}

@Override
public void actionPerformed(ActionEvent e)
{
p.move();
p.moveup();
w.points();
trail.getFirstDot();
trail.findCoords();
if (score>=1)
{
trail.getFirstDot();
}
if (lost==true)
{
score++;
r.getRandom();
lost = false;
}
repaint();
checkCollisions();
}


public int score()
{
return score;
}

public boolean getLost()
{
return lost;
}

public void checkCollisions()
{
Rectangle r1 = r.getBounds();
Rectangle r2 = p.getBounds();
if (r1.intersects(r2))
{
lost = true;
}
}

@Override
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, null);
g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
g2d.drawImage(r.getImage2(), r.getX2(), r.getY2(), null);
if (score>=1)
{
g2d.drawImage(trail.getImage(), trail.getX(), trail.getY(), null);
}
System.out.println(score + "00");
}

private class AL extends KeyAdapter{
@Override
public void keyReleased(KeyEvent e)
{
p.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e)
{
p.keyPressed(e);
}
}

}

对于蛇头类:

public class Snake {
Integer x = 480, y = 280, dx = 0, dy = 0, h, m;
boolean dead = false;
boolean didmove = false;
Image still;
RandomDot r;
Game g;
Trail t;

public Snake(){
ImageIcon i = new ImageIcon("C:/Matt's Stuff/snake.png");
still = i.getImage();
}

public Rectangle getBounds(){
return new Rectangle(x,y, 10, 10);
}


public void move(){
x = x + dx;
h = x;
didmove = true;
if (x>=990||x<=5){
dead = true;
x = 480;
y = 280;
dx = 0;
dy = 0;
didmove = false;
}
}

public void moveup(){
y = y + dy;
m = y;
didmove = true;
if (y>=590||y<=0){
dead = true;
x = 480;
y = 280;
dx = 0;
dy = 0;
didmove = false;
}
}

public Integer getX(){
return x;
}

public Integer getY(){
return y;
}

public Image getImage(){
return still;
}
public boolean getBoolean(){
return dead;
}

public boolean didmove(){
return didmove;
}

public void keyPressed(KeyEvent e){
int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT){
if (dead == false){
dx = -10;
dy = 0;
} else {
x = 480;
y = 280;
dx = 0;
dy = 0;
}
}

if (key == KeyEvent.VK_RIGHT){
if (dead == false){
dx = 10;
dy = 0;
} else {
x = 480;
y = 280;
dx = 0;
dy = 0;
}
}

if (key == KeyEvent.VK_UP){
if (dead == false){
dy = -10;
dx = 0;
}else {
x = 480;
y = 280;
dx = 0;
dy = 0;
}
}

if (key == KeyEvent.VK_DOWN){
if (dead == false){
dy = 10;
dx = 0;
}else {
x = 480;
y = 280;
dx = 0;
dy = 0;
}
}
}

public void keyReleased(KeyEvent e){
int key = e.getKeyCode();



}

}

最后,尾部:

public class Trail {
Image still;
Integer x = 500, y = 500, dx, dy, dx2, dy2;
Game g;
Snake p;
Point d;
int count=0;


boolean visible = false;
public Trail(){
ImageIcon i = new ImageIcon("C:/Matt's Stuff/Dot.png");
still = i.getImage();
p = new Snake();
d = new Point();
}

public void getFirstDot(){
visible = true;
}

public void findCoords(){
if (visible == true)
{
x = p.getX();
y = p.getY();
}
}

public Integer getX(){
return x;
}

public Integer getY(){
return y;
}

public Image getImage(){
return still;
}
}

现在我的问题是:尾类中的p.getX()p.getY() 不起作用!尾 block 就在蛇头最初开始的地方!我不知道发生了什么,我的研究似乎都没有帮助!请帮帮我好吗?为什么 p.getX()p.getY() 可以在主 Game 类中工作,但不能在 Tail 类中工作?提前致谢!

最佳答案

您正在 Trail 构造函数中创建一个 new Snake(),然后引用 that Snake 而不是 Game 知道的那个。与其创建一个新的 Snake,不如传入对构造函数的引用,以便调用实际正在更新的对象的方法:

// In the Trail class
public Trail(Snake s) {
p = s;
// other stuff
}

// In the Game class:
trail = new Trail(p);
// ...

现在,当您调用 p.getX() 等时,您实际上会指向正确的 Snake,因此您应该会正确地看到您的位置更新。

关于java - 整数会调用一个类,但不会调用另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15774276/

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