这是RenderPanel的代码:它抛出一些nullpointerExcepiton,我不知道为什么。它说明了RenderPanel类中每个Cycle的问题。
public class RenderPanel extends JPanel {
public static Color green = new Color(6750054);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Snake snake = Snake.snake;
g.setColor(green);
g.fillRect(0, 0, 700, 600);
g.setColor(Color.BLUE);
for(Point point : snake.snakeParts){
g.fillRect(point.x * Snake.SCALE,point.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
}
g.fillRect(snake.head.x * Snake.SCALE, snake.head.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
g.setColor(Color.RED);
g.fillRect(snake.cherry.x * Snake.SCALE, snake.cherry.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
String string = "Score: " + snake.score + ", Length: " + snake.tailLength + ", Time: " + snake.time / 20;
g.setColor(Color.white);
g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), 10);
string = "Game Over!";
if (snake.gameOver){
g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4);
}
string = "Paused!";
if (snake.paused && !snake.gameOver){
g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4);
}
}
}
蛇程序的代码:
public class Snake implements ActionListener,KeyListener {
public static Snake snake;
JFrame jframe;
public Dimension dim;
public RenderPanel renderPanel;
public Timer timer = new Timer(20, this);
public ArrayList<Point> snakeParts = new ArrayList<Point>();
public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
public int ticks = 0, direction = DOWN, score, tailLength = 10, time;
public Point head, cherry;
public Random random;
public boolean gameOver = false, paused;
public Snake(){
jframe = new JFrame();
dim = Toolkit.getDefaultToolkit().getScreenSize();
jframe.setTitle("Snake");
jframe.add( renderPanel = new RenderPanel());
jframe.setSize(705, 600);
jframe.setLocation(dim.width / 2 - jframe.getWidth() /2, dim.height / 2 - jframe.getHeight() / 2);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.setResizable(false);
jframe.addKeyListener(this);
startGame();
}
public void startGame(){
gameOver = false;
paused = false;
time = 0;
score = 0;
tailLength = 14;
ticks = 0;
direction = DOWN;
head = new Point(0,-1);
random = new Random();
snakeParts.clear();
cherry = new Point(random.nextInt(79), random.nextInt(66));
}
@Override
public void actionPerformed(ActionEvent arg0) {
renderPanel.repaint();
ticks++;
if(ticks % 2 == 0 && head != null && !gameOver && !paused ){
time++;
snakeParts.add(new Point(head.x,head.y));
if(direction == UP){
if(head.y - 1 > 0 && noTailAt(head.x, head.y -1)){
head = new Point(head.x,head.y-1);
}else{
gameOver = true;
}
}
if (direction == DOWN){
if (head.y + 1 < 67 && noTailAt(head.x, head.y + 1)){
head = new Point(head.x, head.y + 1);
}else{
gameOver = true;
}
}
if(direction == LEFT){
if(head.x - 1 > 0 && noTailAt(head.x - 1, head.y)){
head = new Point(head.x - 1,head.y);
}else{
gameOver = true;
}
}
if (direction == DOWN){
if (head.x + 1 < 80 && noTailAt(head.x, head.y + 1)){
head = new Point(head.x + 1, head.y);
}else{
gameOver = true;
}
}
if (snakeParts.size() > tailLength) {
snakeParts.remove(0);
}
if(cherry != null){
if(head.equals(cherry)){
score+=10;
tailLength++;
cherry.setLocation(random.nextInt(79), random.nextInt(66));
}
}
}
}
public boolean noTailAt(int x , int y){
for(Point point : snakeParts){
if(point.equals(new Point(x,y))){
return false;
}
}
return true;
}
public static void main(String[] args){
Snake snake = new Snake();
}
你能帮我吗?