做app推廣上哪些網(wǎng)站嗎2022年今天新聞聯(lián)播
文章目錄
- 一、項目介紹
- 二、核心代碼
- 三、項目展示
- 四、源碼獲取
一、項目介紹
貪吃蛇是一款經(jīng)典的休閑益智游戲,自問世以來便深受廣大用戶的喜愛。這個游戲的基本玩法是控制一條不斷增長的蛇,目標是吃掉屏幕上出現(xiàn)的食物,同時避免撞到邊緣或自身。隨著游戲的進行,蛇的身體會越長越大,操控難度也越來越高,為玩家?guī)砹颂魬?zhàn)性和樂趣。
隨著計算機和移動設備的普及,貪吃蛇游戲也逐漸從最初的黑白方塊發(fā)展成為精美的圖形游戲。但是無論視覺效果如何,游戲的核心玩法始終保持不變,這也是貪吃蛇游戲能持續(xù)吸引玩家的重要原因。
二、核心代碼
啟動窗口
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("貪吃蛇大作戰(zhàn)");jf.setBounds(10,10,600,485);jf.setResizable(false);jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);jf.setVisible(true);//正常游戲界面放在面板上jf.add(new GamePanel());}
}
存放數(shù)據(jù)
public class DataCenter {public static URL headerURL=DataCenter.class.getResource("/Static/Header.png");public static URL upURL=DataCenter.class.getResource("/Static/Up.png");public static URL rightURL=DataCenter.class.getResource("/Static/Right.png");public static URL downURL=DataCenter.class.getResource("/Static/Down.png");public static URL leftURL=DataCenter.class.getResource("/Static/Left.png");public static URL bodyURL=DataCenter.class.getResource("/Static/Body.png");public static URL foodURL=DataCenter.class.getResource("/Static/Food.png");public static ImageIcon header=new ImageIcon(headerURL);public static ImageIcon up=new ImageIcon(upURL);public static ImageIcon right=new ImageIcon(rightURL);public static ImageIcon down=new ImageIcon(downURL);public static ImageIcon left=new ImageIcon(leftURL);public static ImageIcon body=new ImageIcon(bodyURL);public static ImageIcon food=new ImageIcon(foodURL);}
游戲初始化
//游戲初始化public void init(){length = 3;//蛇的長度,初始為3String direction;//初始方向向右//腦袋的坐標snakeX[0] = 95;snakeY[0] = 110;//第一節(jié)身體snakeX[1] = 70;snakeY[1] = 110;//第二節(jié)身體snakeX[2] = 45;snakeY[2] = 110;direct = "R";//初始方向向右score=0;gameState = false;//默認還沒開始游戲//游戲一開始定時器就啟動timer.start();foodX = 20 + 25 * random.nextInt(22);//生成[0-21]的整數(shù)foodY = 85 + 25 * random.nextInt(14);//生成[0-13]的整數(shù)isFail = false;}
繪制面板
//繪制面板@Overrideprotected void paintComponent(Graphics g) {this.setBackground(Color.WHITE);super.paintComponent(g);//清屏//繪制靜態(tài)面板//頭部圖片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("分數(shù):"+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]);}//游戲狀態(tà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);}}
三、項目展示
初始面板
開始游戲
游戲失敗
四、源碼獲取
因為頁面與源碼太多了,所以頁面與源碼只展示了一部分,完整源碼已經(jīng)打包了,點擊下面藍色鏈接獲取!
點我獲取源碼