你用JAVA写一个贪吃蛇小程序
时间: 2023-08-31 15:34:33 浏览: 68
### 回答1:
很抱歉,作为AI语言模型,我无法编写出实际的代码。以下是一份Java贪吃蛇游戏的基本代码示例供您参考:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JPanel implements ActionListener {
private final int WIDTH = 400;
private final int HEIGHT = 350;
private final int SQUARE_SIZE = 10;
private final int MAX_SQUARES = (WIDTH*HEIGHT)/(SQUARE_SIZE*SQUARE_SIZE);
private final int DELAY = 140;
// the coordinates of the snake's body blocks
private int[] x = new int[MAX_SQUARES];
private int[] y = new int[MAX_SQUARES];
// the current length of the snake
private int length = 3;
// the current direction of the snake
private char direction = 'R';
private Timer timer;
private boolean inGame = true;
public SnakeGame(){
addKeyListener(new MyKeyAdapter());
setBackground(Color.black);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
initGame();
}
private void initGame(){
length=3;
x[0] = 30;
y[0] = 50;
x[1] = 20;
y[1] = 50;
x[2] = 10;
y[2] = 50;
direction = 'R';
timer = new Timer(DELAY, this);
timer.start();
}
private void checkApple() {
if (x[0] == appleX && y[0] == appleY) {
length++;
locateApple();
}
}
private void move(){
for(int i = length-1; i > 0; i--){
x[i] = x[i-1];
y[i] = y[i-1];
}
switch(direction){
case 'U':
y[0] -= SQUARE_SIZE;
break;
case 'D':
y[0] += SQUARE_SIZE;
break;
case 'L':
x[0] -= SQUARE_SIZE;
break;
case 'R':
x[0] += SQUARE_SIZE;
break;
}
}
private void checkCollision(){
// check if the snake has collided with the borders of the game
if(x[0] < 0 || x[0] > WIDTH || y[0] < 0 || y[0] > HEIGHT){
inGame = false;
}
// check if the snake has collided with itself
for(int i=1; i<length; i++){
if(x[0] == x[i] && y[0] == y[i]){
inGame = false;
}
}
// stop the game timer if the snake has collided
if(!inGame){
timer.stop();
}
}
private void locateApple() {
appleX = (int) (Math.random() * (WIDTH - SQUARE_SIZE) / SQUARE_SIZE) * SQUARE_SIZE;
appleY = (int) (Math.random() * (HEIGHT - SQUARE_SIZE) / SQUARE_SIZE) * SQUARE_SIZE;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
private void draw(Graphics g){
if(inGame){
g.setColor(Color.red);
g.fillRect(appleX, appleY, SQUARE_SIZE, SQUARE_SIZE);
g.setColor(Color.green);
for(int i=0; i<length; i++){
g.fillRect(x[i], y[i], SQUARE_SIZE, SQUARE_SIZE);
}
Toolkit.getDefaultToolkit().sync();
}
else{
gameOver(g);
}
}
private void gameOver(Graphics g){
g.setColor(Color.red);
g.setFont(new Font("TimesRoman", Font.BOLD, 30));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics.stringWidth("Game Over"))/2, HEIGHT/2);
}
public void actionPerformed(ActionEvent e){
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class MyKeyAdapter extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
if(direction != 'R'){
direction = 'L';
}
break;
case KeyEvent.VK_UP:
if(direction != 'D'){
direction = 'U';
}
break;
case KeyEvent.VK_RIGHT:
if(direction != 'L'){
direction = 'R';
}
break;
case KeyEvent.VK_DOWN:
if(direction != 'U'){
direction = 'D';
}
break;
}
}
}
public static void main(String[] args){
JFrame f = new JFrame("Snake Game");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SnakeGame game = new SnakeGame();
f.add(game);
f.pack();
f.setVisible(true);
}
}
```
### 回答2:
贪吃蛇是一种经典的游戏,我们可以用Java编写一个简单的贪吃蛇小程序。
首先,我们需要创建一个Snake类来表示蛇,Snake类中包括蛇的坐标、长度和移动方向等属性。蛇的移动方向可以用一个枚举类型来表示,例如:上、下、左、右。
然后,我们需要创建一个Game类来表示游戏界面,Game类中包括蛇、食物、得分等属性。我们可以使用一个二维数组来表示游戏地图,每个元素表示一个方块,蛇身和食物分别用不同的数字表示。
接下来,我们需要实现游戏逻辑。在每一帧里,程序会检测用户的输入,并根据用户的输入来改变蛇的移动方向。同时,我们还需要判断蛇是否吃到了食物,如果吃到了食物,则蛇的长度加一,并在地图上生成新的食物。我们还需要判断蛇是否碰到了自己的身体或撞墙,如果碰到了,则游戏结束。
最后,我们需要实现游戏界面的绘制。我们可以通过使用Java的绘图功能,将游戏地图和蛇画在一个窗口上,每次更新时重新绘制。
通过以上步骤,我们就可以用Java写一个简单的贪吃蛇小程序了。当然,我们还可以增加一些额外的功能,如难度选择、计时器等,以增加游戏的趣味性和挑战性。
### 回答3:
贪吃蛇是一款经典的游戏,在Java中编写贪吃蛇小程序可以通过使用Java的图形化界面库来实现。以下是一个简单的贪吃蛇小程序的实现思路。
首先,我们需要定义贪吃蛇的各个组成部分,包括蛇的坐标点、蛇的移动方向、食物的位置等。可以使用数组或链表来表示蛇的坐标点,使用枚举类型或整数来表示蛇的移动方向,使用坐标点来表示食物的位置。
其次,我们需要实现蛇的移动。蛇的移动可以通过改变蛇的坐标点来实现,每次移动时,将蛇头按照当前移动方向向前移动一格,并在蛇的头部新增一个坐标点,然后删除蛇尾的坐标点。
再次,我们需要检测碰撞。碰撞可以分为两种情况:蛇头是否碰到了蛇身或边界;蛇头是否碰到了食物。如果蛇头碰到了蛇身或边界,游戏结束,如果蛇头碰到了食物,蛇的长度增加,并生成新的食物。
最后,我们需要设计游戏界面。可以使用Java的图形化界面库来创建游戏窗口,将贪吃蛇的各个组成部分在游戏界面中进行绘制,同时使用键盘监听来获取玩家的输入,控制蛇的移动方向。
通过以上步骤,我们就可以实现一个简单的贪吃蛇小程序。当然,还可以增加更多的功能,例如计分、难度调整等,以增加游戏的趣味性和挑战性。