用Java写一个只有一关的简单贪吃蛇小游戏
时间: 2023-08-22 22:03:25 浏览: 42
好的,下面是一个简单的贪吃蛇小游戏实现,使用Java语言编写:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class SnakeGame extends JFrame implements ActionListener{
private final int WIDTH = 400;
private final int HEIGHT = 400;
private final int UNIT_SIZE = 10;
private final int GAME_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE;
private final int DELAY = 75;
private int[] x = new int[GAME_UNITS];
private int[] y = new int[GAME_UNITS];
private int bodyParts = 6;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
private Random random;
public SnakeGame(){
setTitle("Snake Game");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setBackground(Color.BLACK);
addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame(){
random = new Random();
newApple();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void paint(Graphics g){
super.paint(g);
if(running){
for(int i=0; i<bodyParts; i++){
if(i == 0){
g.setColor(Color.GREEN);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}else{
g.setColor(new Color(45, 180, 0));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
g.setColor(Color.WHITE);
g.setFont(new Font("Ink Free", Font.BOLD, 20));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: " + (bodyParts-6), (WIDTH - metrics.stringWidth("Score: " + (bodyParts-6))) / 2, g.getFont().getSize());
}else{
gameOver(g);
}
}
public void newApple(){
appleX = random.nextInt((int)(WIDTH/UNIT_SIZE)) * UNIT_SIZE;
appleY = random.nextInt((int)(HEIGHT/UNIT_SIZE)) * UNIT_SIZE;
}
public void move(){
for(int i=bodyParts; i>0; i--){
x[i] = x[i-1];
y[i] = y[i-1];
}
switch(direction){
case 'U':
y[0] -= UNIT_SIZE;
break;
case 'D':
y[0] += UNIT_SIZE;
break;
case 'L':
x[0] -= UNIT_SIZE;
break;
case 'R':
x[0] += UNIT_SIZE;
break;
}
}
public void checkApple(){
if(x[0] == appleX && y[0] == appleY){
bodyParts++;
newApple();
}
}
public void checkCollisions(){
for(int i=bodyParts; i>0; i--){
if(x[0] == x[i] && y[0] == y[i]){
running = false;
}
}
if(x[0] < 0 || x[0] >= WIDTH || y[0] < 0 || y[0] >= HEIGHT){
running = false;
}
if(!running){
timer.stop();
}
}
public void gameOver(Graphics g){
g.setColor(Color.WHITE);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics1 = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics1.stringWidth("Game Over")) / 2, HEIGHT / 2);
g.setColor(Color.WHITE);
g.setFont(new Font("Ink Free", Font.BOLD, 20));
FontMetrics metrics2 = getFontMetrics(g.getFont());
g.drawString("Score: " + (bodyParts-6), (WIDTH - metrics2.stringWidth("Score: " + (bodyParts-6))) / 2, g.getFont().getSize());
}
public void actionPerformed(ActionEvent e){
if(running){
move();
checkApple();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter{
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
if(direction != 'R')
direction = 'L';
break;
case KeyEvent.VK_RIGHT:
if(direction != 'L')
direction = 'R';
break;
case KeyEvent.VK_UP:
if(direction != 'D')
direction = 'U';
break;
case KeyEvent.VK_DOWN:
if(direction != 'U')
direction = 'D';
break;
}
}
}
public static void main(String[] args){
new SnakeGame().setVisible(true);
}
}
```
运行以上代码,即可开始玩贪吃蛇小游戏。按键盘的方向键控制贪吃蛇的移动方向,吃到苹果得分。游戏结束后会显示得分和“Game Over”字样。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)