编写JAVA贪吃蛇代码
时间: 2023-11-14 19:07:26 浏览: 136
非常感谢您的提问!我可以为您提供一些编写JAVA贪吃蛇代码的思路和建议。首先,您需要决定使用哪种GUI库来构建图形用户界面。常见的GUI库包括Swing和JavaFX。接下来,您需要思考如何表示贪吃蛇和食物的位置和方向,并实现它们的运动和碰撞检测。最后,您需要考虑如何通过键盘输入控制贪吃蛇的方向。如果您需要更具体的帮助和代码实现,请告诉我,我会尽力为您提供帮助。
相关问题
Java编写贪吃蛇的代码
好的,以下是一个简单的Java贪吃蛇游戏的代码示例:
```java
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 SnakeGame 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 = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private Apple apple;
private ArrayList<Apple> apples;
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 SnakeGame() {
setFocusable(true);
addKeyListener(this);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
r = new Random();
snake = new ArrayList<BodyPart>();
apples = new ArrayList<Apple>();
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) {
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
}
ticks++;
if (ticks > 250000) {
if (right)
xCoor++;
if (left)
xCoor--;
if (up)
yCoor--;
if (down)
yCoor++;
ticks = 0;
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
if (snake.size() > size) {
snake.remove(0);
}
}
if (apples.size() == 0) {
int xCoor = r.nextInt(49);
int yCoor = r.nextInt(49);
apple = new Apple(xCoor, yCoor, 10);
apples.add(apple);
}
for (int i = 0; i < apples.size(); i++) {
if (xCoor == apples.get(i).getxCoor() && yCoor == apples.get(i).getyCoor()) {
size++;
apples.remove(i);
i++;
}
}
for (int i = 0; i < snake.size(); i++) {
if (xCoor == snake.get(i).getxCoor() && yCoor == snake.get(i).getyCoor()) {
if (i != snake.size() - 1) {
System.out.println("Game Over!");
stop();
}
}
}
if (xCoor < 0 || xCoor > 49 || yCoor < 0 || yCoor > 49) {
System.out.println("Game Over!");
stop();
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.GREEN);
for (int i = 0; i < snake.size(); i++) {
g.fillRect(snake.get(i).getxCoor() * 10, snake.get(i).getyCoor() * 10, snake.get(i).getSize(), snake.get(i).getSize());
}
g.setColor(Color.RED);
for (int i = 0; i < apples.size(); i++) {
g.fillOval(apples.get(i).getxCoor() * 10, apples.get(i).getyCoor() * 10, apples.get(i).getSize(), apples.get(i).getSize());
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
SnakeGame game = new SnakeGame();
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Snake Game");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@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) {
}
}
class BodyPart {
private int xCoor, yCoor, size;
public BodyPart(int xCoor, int yCoor, int size) {
this.xCoor = xCoor;
this.yCoor = yCoor;
this.size = size;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
public int getxCoor() {
return xCoor;
}
public int getyCoor() {
return yCoor;
}
public int getSize() {
return size;
}
}
class Apple {
private int xCoor, yCoor, size;
public Apple(int xCoor, int yCoor, int size) {
this.xCoor = xCoor;
this.yCoor = yCoor;
this.size = size;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
public int getxCoor() {
return xCoor;
}
public int getyCoor() {
return yCoor;
}
public int getSize() {
return size;
}
}
```
这个示例代码中实现了一个简单的贪吃蛇游戏,包括蛇的移动、食物的生成、碰撞检测等基本功能。你可以根据自己的需求进行修改和扩展。
用java语言编写贪吃蛇代码
好的,以下是一个简单的Java语言贪吃蛇代码示例:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class SnakeGame extends JPanel implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private int x[] = new int[ALL_DOTS];
private int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public SnakeGame() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
loadImages();
initGame();
}
private void loadImages() {
ImageIcon iid = new ImageIcon("dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("head.png");
head = iih.getImage();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2,
HEIGHT / 2);
}
private void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if(!inGame) {
timer.stop();
}
}
private void locateApple() {
Random rand = new Random();
int r = rand.nextInt(RAND_POS);
apple_x = ((r * DOT_SIZE));
r = rand.nextInt(RAND_POS);
apple_y = ((r * DOT_SIZE));
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
}
}
```
这段代码实现了一个基本的贪吃蛇游戏,使用Java语言编写。其中包含了贪吃蛇的移动、吃苹果、游戏结束等逻辑。你可以在Java编译器中编译并运行该代码。
阅读全文