Java写个贪吃蛇脚本
时间: 2023-09-09 13:04:19 浏览: 113
javaswing实现贪吃蛇源码
### 回答1:
写一个贪吃蛇脚本需要了解游戏的基本规则和设计思路。下面是一份 Java 贪吃蛇代码的示例:
```
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;
import javax.swing.Timer;
public class Snake implements ActionListener, KeyListener {
public static Snake snake;
public JFrame jframe;
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 over = false, paused;
public Dimension dim;
public Snake() {
dim = Toolkit.getDefaultToolkit().getScreenSize();
jframe = new JFrame("Snake");
jframe.setVisible(true);
jframe.setSize(805, 700);
jframe.setResizable(false);
jframe.setLocation(dim.width / 2 - jframe.getWidth() / 2, dim.height / 2 - jframe.getHeight() / 2);
jframe.add(renderPanel = new RenderPanel());
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.addKeyListener(this);
startGame();
}
public void startGame() {
over = false;
paused = false;
time = 0;
score = 0;
tailLength = 14;
direction = DOWN;
head = new Point(0, -1);
random = new Random();
snakeParts.clear();
cherry = new Point(random.nextInt(79), random.nextInt(66));
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
renderPanel.repaint();
ticks++;
if (ticks % 2 == 0 && head != null && !over && !paused) {
time++;
snakeParts.add(new Point(head.x, head.y));
if (direction == UP) {
if (head.y - 1 >= 0 && noTailAt(head
### 回答2:
好的,以下是一个用Java编写的简单贪吃蛇脚本:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame {
private int width = 300;
private int height = 300;
private int blockSize = 10;
private int delay = 100;
private int[] snakeX;
private int[] snakeY;
private int snakeSize;
private int direction; // 0: 上, 1: 右, 2: 下, 3: 左
private int foodX;
private int foodY;
private boolean gameOver;
public SnakeGame() {
setTitle("贪吃蛇");
setSize(width, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
snakeX = new int[width * height / (blockSize * blockSize)];
snakeY = new int[width * height / (blockSize * blockSize)];
snakeSize = 3;
direction = 1;
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP && direction != 2) {
direction = 0;
} else if (key == KeyEvent.VK_RIGHT && direction != 3) {
direction = 1;
} else if (key == KeyEvent.VK_DOWN && direction != 0) {
direction = 2;
} else if (key == KeyEvent.VK_LEFT && direction != 1) {
direction = 3;
}
}
});
initialize();
}
public void initialize() {
snakeSize = 3;
snakeX[0] = width / 2;
snakeY[0] = height / 2;
snakeX[1] = snakeX[0] - blockSize;
snakeY[1] = snakeY[0];
snakeX[2] = snakeX[1] - blockSize;
snakeY[2] = snakeY[1];
generateFood();
gameOver = false;
runGameLoop();
}
public void generateFood() {
int maxPos = width / blockSize;
foodX = (int) (Math.random() * maxPos) * blockSize;
foodY = (int) (Math.random() * maxPos) * blockSize;
}
public void runGameLoop() {
while (!gameOver) {
update();
repaint();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void update() {
for (int i = snakeSize; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
switch (direction) {
case 0:
snakeY[0] -= blockSize;
break;
case 1:
snakeX[0] += blockSize;
break;
case 2:
snakeY[0] += blockSize;
break;
case 3:
snakeX[0] -= blockSize;
break;
}
if (snakeX[0] == foodX && snakeY[0] == foodY) {
snakeSize++;
generateFood();
}
for (int i = 1; i < snakeSize; i++) {
if (snakeX[i] == snakeX[0] && snakeY[i] == snakeY[0]) {
gameOver = true;
}
}
if (snakeX[0] < 0 || snakeY[0] < 0 || snakeX[0] >= width || snakeY[0] >= height) {
gameOver = true;
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, width, height);
g.setColor(Color.RED);
g.fillRect(foodX, foodY, blockSize, blockSize);
g.setColor(Color.GREEN);
for (int i = 0; i < snakeSize; i++) {
g.fillRect(snakeX[i], snakeY[i], blockSize, blockSize);
}
if (gameOver) {
g.setColor(Color.BLACK);
g.drawString("游戏结束", width / 2 - 30, height / 2);
}
}
public static void main(String[] args) {
new SnakeGame();
}
}
```
这是一个简单的贪吃蛇游戏脚本,使用Java Swing库来实现图形化界面。游戏窗口大小为300x300像素,每个方块大小为10x10像素,延迟时间为100毫秒。玩家可以使用方向键来控制蛇的移动方向。游戏目标是吃到食物,蛇会变长,当蛇头碰到自己的身体或者窗口边界时游戏结束,显示游戏结束的提示信息。
以上代码并非完整的游戏实现,可以根据需要进一步修改和扩展。
### 回答3:
贪吃蛇是一个经典的小游戏,在Java中编写一个贪吃蛇脚本可以通过以下步骤实现。
首先,我们需要创建一个窗口来显示游戏界面。我们可以使用Java的Swing库来创建一个窗口,并在窗口中绘制游戏界面。
接下来,我们需要定义一个蛇的类来表示贪吃蛇。这个类需要包含蛇的长度、位置、移动方向等属性,并提供相应的方法来控制蛇的移动、吃食物等操作。
然后,我们需要定义一个食物的类来表示食物。这个类需要包含食物的位置属性,并提供一个方法来生成新的食物位置。
在游戏开始时,我们需要初始化窗口、蛇和食物对象,并将它们显示在窗口上。然后,我们通过监听键盘事件来控制蛇的移动方向。当蛇吃到食物时,蛇的长度增加,并重新生成一个新的食物位置。当蛇碰到边界或者自己的身体时,游戏结束。
最后,我们需要添加一个游戏循环,使得游戏能够持续运行。在每一次循环中,我们需要更新蛇的位置、判断是否吃到食物、判断游戏是否结束,并在窗口中重新绘制游戏界面。
通过以上步骤,我们可以编写一个简单的贪吃蛇脚本。当然,具体实现还需要根据具体的编程环境和需求进行调整。
阅读全文