Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)q'jiu'jie

package com.attempt.snake;


import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.image.BufferedImage;

import java.util.Arrays;

import java.util.Random;



import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;




public class GluttonousSnake extends JPanel implements Runnable {



// 窗体

public static final int width = 675;// 面板宽

public static final int height = 540;// 面板高


// 规定

private int state = START;

private static final int START = 0;

private static final int RUNNING = 1;

private static final int GAME_OVER = 3;


// 图像

public static BufferedImage start;// 开始背景

public static BufferedImage game_over;// 结束背景

public static BufferedImage background;// 运行背景

public static BufferedImage drumstick;// 鸡腿

public static BufferedImage snakeBody;// 蛇身

public static BufferedImage snakeHeart_u;// 蛇头上

public static BufferedImage snakeHeart_d;// 蛇头下

public static BufferedImage snakeHeart_r;// 蛇头右

public static BufferedImage snakeHeart_l;// 蛇头左


// 数据

private int score = 0;// 得分

private int speed = 50;// 变换速率

private int count = 0;// 次数(用来改变蛇的速度)

int Ux = 0;// 记忆吃鸡腿前一步的最后一节蛇身的位置x

int Uy = 0;// 记忆吃鸡腿前一步的最后一节蛇身的位置y

// 对象


private Snake snakeHead = new Snake();// 蛇头

private All snakes[] = { snakeHead };// 整条蛇

private int direction = 1;

Drumstick drumsticks = new Drumstick(270, 270);


// private Timer timer;// 时间线程


// 静态块初始化图片

static {

try {

start = ImageIO

.read(GluttonousSnake.class.getResource("start.png"));

background = ImageIO.read(GluttonousSnake.class

.getResource("background.png"));

game_over = ImageIO.read(GluttonousSnake.class

.getResource("game_over.png"));

drumstick = ImageIO.read(GluttonousSnake.class

.getResource("drumstick.png"));

snakeBody = ImageIO.read(GluttonousSnake.class

.getResource("SnakeBody.png"));

snakeHeart_u = ImageIO.read(GluttonousSnake.class

.getResource("SnakeHeart_u.png"));

snakeHeart_d = ImageIO.read(GluttonousSnake.class

.getResource("SnakeHeart_d.png"));

snakeHeart_r = ImageIO.read(GluttonousSnake.class

.getResource("SnakeHeart_r.png"));

snakeHeart_l = ImageIO.read(GluttonousSnake.class

.getResource("SnakeHeart_l.png"));

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}


// 游戏开始

public void action() {

this.addMouseListener(new MouseAdapter() {


@Override

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

if (state == START) {

if (e.getX() >= 275 && e.getX() <= 417 && e.getY() >= 464

&& e.getY() <= 488) {

state = RUNNING;

}

}

if (state == GAME_OVER) {

if (e.getX() >= 249 && e.getX() <= 426 && e.getY() >= 264

&& e.getY() <= 353) {

speed = 50;

snakes = Arrays.copyOf(snakes, 1);

snakeHead = new Snake();

snakes[0] = snakeHead;

direction = 1;

count = 0;

score = 0;

state = RUNNING;

}

}

}

});

}


// 重写Runable线程中的run方法

@Override

public void run() {

// TODO Auto-generated method stub


count++;

this.addKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

// TODO Auto-generated method stub

switch (e.getKeyCode()) {

case KeyEvent.VK_UP:

if (direction != 1)

direction = 2;

break;

case KeyEvent.VK_DOWN:


if (direction != 2)

direction = 1;

break;

case KeyEvent.VK_LEFT:


if (direction != 3)

direction = 4;

break;

case KeyEvent.VK_RIGHT:

if (direction != 4)

direction = 3;

break;


default:

break;

}

}

});

if (state == RUNNING) {

if (nolive()) {

state = GAME_OVER;

}

if (count % speed == 0) {


moveTo();

snakeHead.step(direction);

eatDrumstick();


}


}

repaint();

// 蛇的速度就会增加

if (count % 1000 == 0 && speed > 2) {

speed -= 2;

}

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}


// 判死

public boolean nolive() {

if (snakeHead.hitOneself(snakes) && snakes.length > 1) {

return true;

} else if (snakeHead.outOfBound(width, height)) {

return true;

} else {

return false;

}


}


// 蛇移动

public void moveTo() {

if (snakes.length > 1) {

for (int i = snakes.length - 1; i > 0; i--) {

snakes[i].x = snakes[i - 1].x;

snakes[i].y = snakes[i - 1].y;

}

}

}


// 蛇吃到鸡腿的方法

public void eatDrumstick() {

int panduan = 0;

if ((snakeHead.getX() + snakeHead.getImageWidth() / 2) <= (drumsticks

.getX() + drumsticks.getImageWidth() / 2)

&& (snakeHead.getX() + snakeHead.getImageWidth() / 2) >= (drumsticks

.getX() - drumsticks.getImageWidth() / 2)

&& (snakeHead.getY() + snakeHead.getImageHeight() / 2) <= (drumsticks

.getY() + drumsticks.getImageHeight() / 2)

&& (snakeHead.getY() + snakeHead.getImageHeight() / 2) >= (drumsticks

.getY() - drumsticks.getImageHeight() / 2)) {

panduan = 1;

score += drumsticks.getScore();

snakes = Arrays.copyOf(snakes, snakes.length + 1);

snakes[snakes.length - 1] = drumsticks;

snakes[snakes.length - 1].x = Ux;

snakes[snakes.length - 1].y = Uy;

RandDrumstick();

}

if (panduan == 0) {

Ux = snakes[snakes.length - 1].x;

Uy = snakes[snakes.length - 1].y;

}

panduan = 0;

}


// 鸡腿去向

public void RandDrumstick() {

int x;

int y;

Random random = new Random();

x = 45 + random.nextInt(13) * 45;

y = 45 + random.nextInt(10) * 45;

drumsticks = new Drumstick(x, y);

}


// 重写画笔方法

public void paint(Graphics g) {

new Thread(this).start();

// 状态

paintState(g);

if (state == RUNNING) {


// 画蛇

paintSnake(g);

// 鸡腿

g.drawImage(drumstick, drumsticks.getX(), drumsticks.getY(), null);

// 分数

paintScore(g);

}

}


// 画蛇

public void paintSnake(Graphics g) {

if (snakes.length > 1) {


for (int i = 1; i < snakes.length; i++) {

g.drawImage(snakeBody, snakes[i].getX(), snakes[i].getY(), null);

}

g.drawImage(snakeHead.getImage(), snakeHead.getX(),

snakeHead.getY(), null);

} else {

g.drawImage(snakeHead.getImage(), snakeHead.getX(),

snakeHead.getY(), null);

}

}


// 分数

public void paintScore(Graphics g) {

int x = 10; //

int y = 25; //

Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14); // 字体

g.setColor(new Color(0x3A3B3B));

g.setFont(font); // 设置字体

g.drawString("SCORE:" + score, x, y); // 分数

}


// 游戏状态

public void paintState(Graphics g) {

switch (state) {

case START: // 开始״̬

g.drawImage(start, 0, 0, null);

break;

case RUNNING: // 游戏״̬

g.drawImage(background, 0, 0, null);

break;

case GAME_OVER: // 游戏״̬

g.drawImage(game_over, 0, 0, null);

break;

}

}


public static void main(String[] args) {

JFrame frame = new JFrame();

GluttonousSnake game = new GluttonousSnake();

frame.add(game); // 将面板添加到JFrame中

frame.setSize(width, height); // 设置大小

frame.setAlwaysOnTop(true); // 设置其总在最上

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默认关闭操作

frame.setLocationRelativeTo(null); // 设置窗体初始位置

frame.setVisible(true); // 尽快调用paint

game.setFocusable(true);

game.action();

}

}

具体的代码

都是假的
浏览 2155回答 1
1回答

都是假的

将数组换成list依然会报错
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java