我正在尝试制作“Snake”游戏,但我被困在了尾巴上。现在,我只是想让第一个尾部块与头部具有相同的坐标。这就是我所拥有的。对于主类:
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()
andp.getY()
不起作用!尾块就在蛇头最初开始的地方!我不知道发生了什么,我的研究似乎没有任何帮助!请帮帮我?为什么会这样p.getX()
并且p.getY()
会在主要的游戏类中工作,而不是在尾巴的?先感谢您!