帖子底部的实际问题!
首先,我想解释一下我的问题。
我正在写一个基本的Snake游戏,我让蛇自动移动了。执行代码时,它会自动像预期的那样移动到窗口的右侧。但是,我无法按照我想要的方式操纵蛇,它根本不会改变方向。
为避免混淆,player
是类Snake
的实例。
解释蛇的运动:
Snake
对象具有一个coordinates
属性,该属性是一个包含SnakePart
对象的ArrayList。每个SnakePart
对象都具有属性x
和y
。使用此ArrayList,通过在画布的y和x轴上使用x
和y
属性在画布上绘制小矩形来使蛇移动。
Snake
对象还具有一个dx
和一个dy
属性,该属性被添加(或减去-取决于蛇的方向)到x
和y
对象的SnakePart
属性可将蛇沿一个方向移动。
要更新Snake.java
中的ArrayList:
public void move() {
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);
this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);
}
要在Board.java
中的画布上绘制蛇(部分地,此方法暂时不需要):
@Override
public void paintComponent(Graphics g) {
this.player.coordinates.forEach(snakePart -> {
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
});
}
要操纵蛇,我想使用箭头键。根据所按下的箭头键,修改蛇的x
和y
坐标/属性(Board.java
):
@Override
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == 37) {
this.player.dx = -10;
this.player.dy = 0;
} else if (keyCode == 38) {
this.player.dx = 0;
this.player.dy = -10;
} else if (keyCode == 39) {
this.player.dx = 10;
this.player.dy = 0;
} else if (keyCode == 40) {
this.player.dx = 0;
this.player.dy = 10;
}
}
整个代码:
Snake.java
:
package com.codef0x.snake;
import java.util.ArrayList;
public class Snake {
ArrayList < SnakePart > coordinates;
int dx = 10;
int dy = 0;
public Snake(ArrayList < SnakePart > coords) {
this.coordinates = coords;
}
public void move() {
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);
this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);
}
public void grow() {
SnakePart newPart = new SnakePart(0, 0);
newPart.x = this.coordinates.get(this.coordinates.size() - 1).x - 10;
newPart.y = this.coordinates.get(this.coordinates.size() - 1).y;
this.coordinates.add(this.coordinates.size() - 1, newPart);
}
}
Board.java
(仅显示相关部分,否则代码太多)
package com.codef0x.snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class Board extends JPanel implements KeyListener {
Snake player;
ArrayList<SnakePart> snakeCoordinates;
public Board() {
this.snakeCoordinates = new ArrayList<>();
snakeCoordinates.add(new SnakePart(150, 150));
snakeCoordinates.add(new SnakePart(140, 150));
snakeCoordinates.add(new SnakePart(130, 150));
snakeCoordinates.add(new SnakePart(120, 150));
this.player = new Snake(snakeCoordinates);
this.food = new Food();
}
@Override
public void paintComponent(Graphics g) {
clear(g);
this.player.coordinates.forEach(snakePart - > {
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
});
}
public void clear(Graphics g) {
g.clearRect(0, 0, getHeight(), getWidth());
}
@Override
public void update(Graphics g) {
paintComponent(g);
}
@Override
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == 37) {
this.player.dx = -10;
this.player.dy = 0;
} else if (keyCode == 38) {
this.player.dx = 0;
this.player.dy = -10;
} else if (keyCode == 39) {
this.player.dx = 10;
this.player.dy = 0;
} else if (keyCode == 40) {
this.player.dx = 0;
this.player.dy = 10;
}
}
@Override
public void keyTyped(KeyEvent event) {
return;
}
@Override
public void keyReleased(KeyEvent event) {
return;
}
public void run(Board board) {
Timer game = new Timer();
game.schedule(new TimerTask() {
boolean initiallySpawned = false;
@Override
public void run() {
Graphics g = board.getGraphics();
if (hitSomething()) { // removed method hitSomething, not relevant
game.cancel();
return;
}
player.move();
update(g);
}
}, 0, 500);
}
}
SnakePart.java
:
package com.codef0x.snake;
public class SnakePart {
int x;
int y;
public SnakePart(int x, int y) {
this.x = x;
this.y = y;
}
}
如果您仍然希望/需要整体查看所有文件,可以在这里查看它们:
Snake.java
Board.java
SnakePart.java
Food.java <-无关,但可以防止对Food
对象的困惑
答案 0 :(得分:2)
问题出在主要。
您创建一个board
来托管您的游戏状态,并创建另一个监听键盘的声音。
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);
Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(new Board());
frame.setVisible(true);
board.run(board);
}
应该是:
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);
Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(board);
frame.setVisible(true);
board.run(board);
}
board.run(board)
几乎没有意义,在run
方法的范围内,可以将板交换为this
(以此类推)...