java贪吃蛇代码eclipse
时间: 2023-09-05 17:06:56 浏览: 68
以下是一个简单的Java贪吃蛇代码示例,可以在Eclipse中运行:
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Snake extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 500, HEIGHT = 500;
private Thread thread;
private boolean running;
private Node head, apple;
private ArrayList<Node> snake;
private Random r;
private int xCoor = 10, yCoor = 10;
private int size = 5;
private boolean right = true, left = false, up = false, down = false;
private int ticks = 0;
public Snake() {
setFocusable(true);
addKeyListener(this);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
r = new Random();
snake = new ArrayList<Node>();
start();
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void tick() {
if (snake.size() == 0) {
head = new Node(xCoor, yCoor, 10);
snake.add(head);
}
if (apple == null) {
int xCoor = r.nextInt(49);
int yCoor = r.nextInt(49);
apple = new Node(xCoor, yCoor, 10);
}
for (int i = 0; i < snake.size(); i++) {
if (xCoor == snake.get(i).getxCoor() && yCoor == snake.get(i).getyCoor()) {
if (i != snake.size() - 1) {
stop();
}
}
}
if (xCoor < 0 || xCoor > 49 || yCoor < 0 || yCoor > 49) {
stop();
}
ticks++;
if (ticks > 250000) {
if (right) xCoor++;
if (left) xCoor--;
if (up) yCoor--;
if (down) yCoor++;
ticks = 0;
head = new Node(xCoor, yCoor, 10);
snake.add(head);
if (snake.size() > size) {
snake.remove(0);
}
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.RED);
g.fillRect(apple.getxCoor() * apple.getDimension(), apple.getyCoor() * apple.getDimension(), apple.getDimension(), apple.getDimension());
for (int i = 0; i < snake.size(); i++) {
g.setColor(Color.GREEN);
g.fillRect(snake.get(i).getxCoor() * snake.get(i).getDimension(), snake.get(i).getyCoor() * snake.get(i).getDimension(), snake.get(i).getDimension(), snake.get(i).getDimension());
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Snake snake = new Snake();
frame.add(snake);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Snake");
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
@Override
public void run() {
while (running) {
tick();
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
right = true;
}
if (key == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
}
if (key == KeyEvent.VK_UP && !down) {
left = false;
right = false;
up = true;
}
if (key == KeyEvent.VK_DOWN && !up) {
left = false;
right = false;
down = true;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
private class Node {
private int xCoor, yCoor, dimension;
public Node(int xCoor, int yCoor, int dimension) {
this.xCoor = xCoor;
this.yCoor = yCoor;
this.dimension = dimension;
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
public int getDimension() {
return dimension;
}
public void setDimension(int dimension) {
this.dimension = dimension;
}
}
}
```
注意:此代码仅供参考,可能不是最优解决方案。
阅读全文