Java实现简易GUI贪吃蛇小游戏
本文实例为大家分享了Java实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下
主启动类
import javax.swing.*; /** * @author The Setting sun * @create 2021-09-11 15:19 */ //主启动类 public class StartGame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setResizable(false);//窗口大小不可变 frame.setSize(900, 720);//窗口大小 frame.setLocationRelativeTo(null);//窗口居中 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗口关闭事件 frame.add(new GamePanel()); frame.setVisible(true);//窗口可见 } }
资源类(图标)
import javax.swing.*; import java.net.URL; /** @author The Setting sun @create 2021-09-11 15:40 */ public class Data { public static URL headerURL = Data.class.getResource("img/header.png"); public static ImageIcon header = new ImageIcon(headerURL); public static URL upURL = Data.class.getResource("img/tx.png"); public static ImageIcon up = new ImageIcon(upURL); public static URL downURL = Data.class.getResource("img/down.png"); public static ImageIcon down = new ImageIcon(downURL); public static URL rightURL = Data.class.getResource("img/right.png"); public static ImageIcon right = new ImageIcon(rightURL); public static URL leftURL = Data.class.getResource("img/left.png"); public static ImageIcon left = new ImageIcon(leftURL); public static URL bodyURL = Data.class.getResource("img/body.png"); public static ImageIcon body = new ImageIcon(bodyURL); public static URL foodURL = Data.class.getResource("img/food.png"); public static ImageIcon food = new ImageIcon(foodURL); }
程序类
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; /** * @author The Setting sun * @create 2021-09-11 15:24 */ //游戏面板 public class GamePanel extends JPanel implements KeyListener, ActionListener { //蛇 int length;//蛇的长度 int[] snakeX = new int[600];//蛇的x坐标 25*25 int[] snakeY = new int[500];//蛇的y坐标 25*25 String orient;//初始方向 //食物 int foodX; int foodY; Random random = new Random(); boolean isStart = false;//游戏当前状态:默认为停止 boolean isFail = false;//游戏是否失败了,默认为没有 int score;//积分 //定时器 ms为单位 Timer timer = new Timer(140, this);//100毫秒刷新一次 //构造器 public GamePanel() { init();//初始化 setFocusable(true);//获得焦点 addKeyListener(this);//获得键盘监听事件 timer.start();//游戏一开始定时器就启动 } //初始化 public void init() { length = 3;//初始长度为3 snakeX[0] = 100; snakeY[0] = 100;//头的坐标 snakeX[1] = 75; snakeY[1] = 100;//第1个身体的坐标 snakeX[2] = 50; snakeY[2] = 100;//第2个身体的坐标 orient = "R";//初始方向向右 //让食物随机分布在界面上 foodX = 25 + 25 * random.nextInt(34); foodY = 75 + 25 * random.nextInt(24); score = 0;//积分 } //绘制面板,游戏中的东西都用这个来画 @Override protected void paintComponent(Graphics g) { super.paintComponent(g);//清屏 setBackground(Color.pink); //绘制静态面板 Data.header.paintIcon(this, g, 25, 11); g.fillRect(25, 75, 850, 600); //绘制积分面板 g.setColor(Color.GREEN); g.setFont(new Font("微软雅黑", Font.BOLD, 16)); g.drawString("长度:" + length, 750, 35); g.drawString("积分:" + score, 750, 50); //绘制食物 Data.food.paintIcon(this, g, foodX, foodY); //绘制蛇 switch (orient) { case "R": Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通过判断转变方向 break; case "L": Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通过判断转变方向 break; case "U": Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通过判断转变方向 break; case "D": Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通过判断转变方向 break; } //动态添加身体 for (int i = 1; i < length; i++) { Data.body.paintIcon(this, g, snakeX[i], snakeY[i]); } // Data.body.paintIcon(this,g,snakeX[1],snakeY[1]);//第1个身体的坐标 // Data.body.paintIcon(this,g,snakeX[2],snakeY[2]);//第2个身体的坐标 //游戏状态 if (isStart == false) { g.setColor(Color.WHITE); g.setFont(new Font("微软雅黑", Font.BOLD, 40)); g.drawString("按下空格开始游戏", 300, 300); } if (isFail) { g.setColor(Color.RED); g.setFont(new Font("微软雅黑", Font.BOLD, 40)); g.drawString("犹豫就会败北!", 300, 300); } } //键盘监听器 @Override public void keyPressed(KeyEvent e) {//按下 int keyCode = e.getKeyCode();//获得键盘按键码 if (keyCode == KeyEvent.VK_SPACE) {//按下空格 if (isFail) { //重新开始 isFail = false; init(); } else { isStart = !isStart;//取反 repaint(); } } //修复了一个蛇可以反向移动的bug if (orient.equals("U") && keyCode == KeyEvent.VK_DOWN){ keyCode = KeyEvent.VK_UP; } if (orient.equals("D") && keyCode == KeyEvent.VK_UP){ keyCode = KeyEvent.VK_DOWN; } if (orient.equals("L") && keyCode == KeyEvent.VK_RIGHT){ keyCode = KeyEvent.VK_LEFT; } if (orient.equals("R") && keyCode == KeyEvent.VK_LEFT){ keyCode = KeyEvent.VK_RIGHT; } //控制蛇改变方向移动 switch (keyCode) { case KeyEvent.VK_UP: orient = "U"; break; case KeyEvent.VK_DOWN: orient = "D"; break; case KeyEvent.VK_LEFT: orient = "L"; break; case KeyEvent.VK_RIGHT: orient = "R"; break; } } //事件监听:需要通过固定事件来刷新,1秒10次(帧率) @Override public void actionPerformed(ActionEvent e) { if (isStart && isFail == false) {//如果游戏是开始状态,就让蛇动起来 //吃食物 if (snakeX[0] == foodX && snakeY[0] == foodY) { //长度+1 length++; score += 10; //再次让食物随机出现 foodX = 25 + 25 * random.nextInt(34); foodY = 75 + 25 * random.nextInt(24); } //身体移动 for (int i = length - 1; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } //走向 switch (orient) { case "R": snakeX[0] = snakeX[0] + 25; //边界判断 if (snakeX[0] > 850) { snakeX[0] = 25; } break; case "L": snakeX[0] = snakeX[0] - 25; //边界判断 if (snakeX[0] < 25) { snakeX[0] = 850; } break; case "U": snakeY[0] = snakeY[0] - 25; //边界判断 if (snakeY[0] < 75) { snakeY[0] = 650; } break; case "D": snakeY[0] = snakeY[0] + 25; //边界判断 if (snakeY[0] > 650) { snakeY[0] = 75; } break; } //失败判断,撞到自己就GG for (int i = 1 ; i < length; i++) { if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){ isFail = true; } } repaint();//刷新页面 } timer.start();//定时器开启 } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)