什么网站可以做图赚钱,成都十大著名景点,开网络公司做网站挣钱吗,微信小程序免费开店详细步骤文章目录 一、项目介绍二、核心代码三、项目展示四、源码获取 一、项目介绍
贪吃蛇是一款经典的休闲益智游戏,自问世以来便深受广大用户的喜爱。这个游戏的基本玩法是控制一条不断增长的蛇,目标是吃掉屏幕上出现的食物,同时避免撞到边缘或自身。随着游戏的进行,蛇的身体会越长… 文章目录 一、项目介绍二、核心代码三、项目展示四、源码获取 一、项目介绍
贪吃蛇是一款经典的休闲益智游戏,自问世以来便深受广大用户的喜爱。这个游戏的基本玩法是控制一条不断增长的蛇,目标是吃掉屏幕上出现的食物,同时避免撞到边缘或自身。随着游戏的进行,蛇的身体会越长越大,操控难度也越来越高,为玩家带来了挑战性和乐趣。
随着计算机和移动设备的普及,贪吃蛇游戏也逐渐从最初的黑白方块发展成为精美的图形游戏。但是无论视觉效果如何,游戏的核心玩法始终保持不变,这也是贪吃蛇游戏能持续吸引玩家的重要原因。
二、核心代码
启动窗口
public class StartGame {public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {//音乐./*Thread t1 new PlayMusic();t1.start();*/JFrame jf new JFrame();jf.setTitle(贪吃蛇大作战);jf.setBounds(10,10,600,485);jf.setResizable(false);jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);jf.setVisible(true);//正常游戏界面放在面板上jf.add(new GamePanel());}
}存放数据
public class DataCenter {public static URL headerURLDataCenter.class.getResource(/Static/Header.png);public static URL upURLDataCenter.class.getResource(/Static/Up.png);public static URL rightURLDataCenter.class.getResource(/Static/Right.png);public static URL downURLDataCenter.class.getResource(/Static/Down.png);public static URL leftURLDataCenter.class.getResource(/Static/Left.png);public static URL bodyURLDataCenter.class.getResource(/Static/Body.png);public static URL foodURLDataCenter.class.getResource(/Static/Food.png);public static ImageIcon headernew ImageIcon(headerURL);public static ImageIcon upnew ImageIcon(upURL);public static ImageIcon rightnew ImageIcon(rightURL);public static ImageIcon downnew ImageIcon(downURL);public static ImageIcon leftnew ImageIcon(leftURL);public static ImageIcon bodynew ImageIcon(bodyURL);public static ImageIcon foodnew ImageIcon(foodURL);}游戏初始化 //游戏初始化public void init(){length 3;//蛇的长度初始为3String direction;//初始方向向右//脑袋的坐标snakeX[0] 95;snakeY[0] 110;//第一节身体snakeX[1] 70;snakeY[1] 110;//第二节身体snakeX[2] 45;snakeY[2] 110;direct R;//初始方向向右score0;gameState false;//默认还没开始游戏//游戏一开始定时器就启动timer.start();foodX 20 25 * random.nextInt(22);//生成[0-21]的整数foodY 85 25 * random.nextInt(14);//生成[0-13]的整数isFail false;}绘制面板 //绘制面板Overrideprotected void paintComponent(Graphics g) {this.setBackground(Color.WHITE);super.paintComponent(g);//清屏//绘制静态面板//头部图片DataCenter.header.paintIcon(this, g, 20, 8);//游戏面板g.fillRect(20, 85, 548, 355);//画积分g.setColor(Color.WHITE);g.setFont(new Font(微软雅黑,Font.BOLD,20));g.drawString(长度length,450,32);g.drawString(分数score,450,55);//画食物DataCenter.food.paintIcon(this, g, foodX, foodY);//画小蛇头if (direct.equals(U)) {DataCenter.up.paintIcon(this, g, snakeX[0], snakeY[0]);} else if (direct.equals(R)) {DataCenter.right.paintIcon(this, g, snakeX[0], snakeY[0]);} else if (direct.equals(D)) {DataCenter.down.paintIcon(this, g, snakeX[0], snakeY[0]);} else if (direct.equals(L)) {DataCenter.left.paintIcon(this, g, snakeX[0], snakeY[0]);}//画蛇身for (int i 1; i length; i) {DataCenter.body.paintIcon(this, g, snakeX[i], snakeY[i]);}//游戏状态if (gameState false) {g.setColor(new Color(231, 85, 18));g.setFont(new Font(微软雅黑, Font.BOLD, 40));g.drawString(按下空格开始游戏, 126, 265);}if (isFail) {g.setColor(new Color(226, 9, 9));g.setFont(new Font(微软雅黑, Font.BOLD, 40));g.drawString(游戏失败按下空格重新开始, 40, 265);}}三、项目展示
初始面板 开始游戏 游戏失败
四、源码获取
因为页面与源码太多了所以页面与源码只展示了一部分完整源码已经打包了点击下面蓝色链接获取
点我获取源码