使用Java实现一个简单的贪吃蛇小游戏

一. 准备工作
首先获取贪吃蛇小游戏所需要的头部、身体、食物以及贪吃蛇标题等图片。

然后,创建贪吃蛇游戏的Java项目命名为snake_game,并在这个项目里创建一个文件夹命名为images,将图片素材导入文件夹。

再在src文件下创建两个包,一个命名为com.snake.view用来存放所有的图形界面类;一个命名为com.snake.controller用来存放启动的入口类(控制类)。

最后,分别在两个包下创建两个类命名为SnakeStart和SnakeJPanel。

准备工作完毕。

二.代码呈现
        1.主体代码

    2.构造启动类 

三.游戏测试 
设置贪吃蛇运行速度(数字越大越慢):

游戏界面:

好的,下面是一个简单贪吃蛇小游戏Java代码: ``` import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SnakeGame extends JFrame implements ActionListener { private static final long serialVersionUID = -4272056262246670918L; private JPanel gamePanel; private JButton startButton, stopButton; private Timer timer; private int delay = 200; // 蛇移动的速度 private int score = 0; // 得分 private int snakeLength = 3; // 蛇的长度 private int[] snakeX = new int[100]; // 蛇身体的X坐标 private int[] snakeY = new int[100]; // 蛇身体的Y坐标 private int foodX; // 食物的X坐标 private int foodY; // 食物的Y坐标 private int direction = 1; // 蛇的方向,1表示向上,2表示向下,3表示向左,4表示向右 public SnakeGame() { super("贪吃蛇小游戏"); gamePanel = new JPanel(); gamePanel.setLayout(null); gamePanel.setPreferredSize(new Dimension(400, 400)); gamePanel.setBackground(Color.WHITE); getContentPane().add(gamePanel, BorderLayout.CENTER); startButton = new JButton("开始游戏"); startButton.setBounds(50, 420, 100, 30); startButton.addActionListener(this); gamePanel.add(startButton); stopButton = new JButton("结束游戏"); stopButton.setBounds(250, 420, 100, 30); stopButton.addActionListener(this); gamePanel.add(stopButton); timer = new Timer(delay, this); initGame(); } private void initGame() { score = 0; snakeLength = 3; snakeX[0] = 100; snakeY[0] = 100; snakeX[1] = 75; snakeY[1] = 100; snakeX[2] = 50; snakeY[2] = 100; foodX = (int) (Math.random() * 380 / 10) * 10; foodY = (int) (Math.random() * 380 / 10) * 10; direction = 1; } private void paintSnake(Graphics g) { for (int i = 0; i < snakeLength; i++) { if (i == 0) { g.setColor(Color.RED); } else { g.setColor(Color.BLACK); } g.fillRect(snakeX[i], snakeY[i], 10, 10); } } private void paintFood(Graphics g) { g.setColor(Color.GREEN); g.fillOval(foodX, foodY, 10, 10); } private boolean isEatFood() { if (snakeX[0] == foodX && snakeY[0] == foodY) { return true; } else { return false; } } private boolean isGameOver() { if (snakeX[0] < 0 || snakeX[0] > 390 || snakeY[0] < 0 || snakeY[0] > 390) { return true; } for (int i = 1; i < snakeLength; i++) { if (snakeX[i] == snakeX[0] && snakeY[i] == snakeY[0]) { return true; } } return false; } private void moveSnake() { for (int i = snakeLength - 1; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } if (direction == 1) { snakeY[0] -= 10; } else if (direction == 2) { snakeY[0] += 10; } else if (direction == 3) { snakeX[0] -= 10; } else if (direction == 4) { snakeX[0] += 10; } } private void createNewFood() { foodX = (int) (Math.random() * 380 / 10) * 10; foodY = (int) (Math.random() * 380 / 10) * 10; for (int i = 0; i < snakeLength; i++) { if (snakeX[i] == foodX && snakeY[i] == foodY) { createNewFood(); break; } } } public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { initGame(); timer.start(); gamePanel.requestFocus(); } else if (e.getSource() == stopButton) { timer.stop(); } else if (e.getSource() == timer) { moveSnake(); if (isGameOver()) { timer.stop(); JOptionPane.showMessageDialog(gamePanel, "游戏结束,得分为:" + score, "游戏结束", JOptionPane.INFORMATION_MESSAGE); } else if (isEatFood()) { score += 10; snakeLength++; createNewFood(); } gamePanel.repaint(); } } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP && direction != 2) { direction = 1; } else if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != 1) { direction = 2; } else if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != 4) { direction = 3; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != 3) { direction = 4; } } public static void main(String[] args) { SnakeGame snakeGame = new SnakeGame(); snakeGame.setSize(400, 500); snakeGame.setResizable(false); snakeGame.setLocationRelativeTo(null); snakeGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); snakeGame.setVisible(true); } } ``` 这个代码使用Java Swing界面库来实现界面,使用一个定时器来控制蛇的移动,使用了键盘事件来控制蛇的方向。在界面上有开始游戏和结束游戏两个按钮,还有得分的显示。在游戏开始时,初始化蛇的位置、长度、方向和食物的位置。在定时器的每次触发中,先判断游戏是否结束,然后再判断蛇是否吃到了食物,如果吃到了就增加得分和蛇的长度,并重新生成食物。最后更新界面,重绘蛇和食物。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值