最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java+JFrame實(shí)現(xiàn)貪吃蛇小游戲

 更新時(shí)間:2022年06月06日 17:21:45   作者:小秋L(fēng)Y  
這篇文章主要為大家詳細(xì)介紹了Java+JFrame實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

分享一個(gè)Java貪吃蛇小游戲,主要用到的是Java的JFrame,適合初學(xué)者。

首先創(chuàng)建一個(gè)合適大小的窗口,我這里是900*720

package com.lzijin;
?
/*
? @Auther: lzijin
?* @Date: 2021/11/5 - 11 - 05 - 22:06
?* @Description: com.lzijin
?* @version: 16.0
?*/
?
import javax.swing.JFrame;
?
public class Start {
? ? public static void main(String[] args) {
? ? ? ? // 創(chuàng)建對(duì)象、標(biāo)題
? ? ? ? JFrame frame = new JFrame("貪吃蛇小游戲");
? ? ? ? // 設(shè)置位置、大小
? ? ? ? frame.setBounds(510, 130, 900, 720);
? ? ? ? // 設(shè)置窗口大小不可改變
? ? ? ? frame.setResizable(false);
? ? ? ? // 設(shè)置關(guān)閉按鈕
? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? // 顯示窗口
? ? ? ? frame.setVisible(true);
? ? }
}

創(chuàng)建好之后大概就是這個(gè)樣子:

 現(xiàn)在我們導(dǎo)入一些素材,輻條,食物,還有蛇的頭,身體等素材。

package com.lzijin;
?
/*
? @Auther: lzijin
?* @Date: 2021/11/5 - 11 - 05 - 22:08
?* @Description: com.lzijin
?* @version: 16.0
?*/
?
import javax.swing.*;
import java.util.Objects;
?
public class Data {
? ? // 獲取頭部素材
? ? public static ImageIcon header = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/header.png")));
?
? ? //獲取頭部素材
? ? public static ImageIcon up = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/up.png")));
? ? public static ImageIcon down = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/down.png")));
? ? public static ImageIcon left = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/left.png")));
? ? public static ImageIcon right = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/right.png")));
?
? ? // 獲取身體素材
? ? public static ImageIcon body_up = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/bodyup.png")));
? ? public static ImageIcon body_right = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/bodyright.png")));
? ? public static ImageIcon body_down = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/bodydown.png")));
? ? public static ImageIcon body_left = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/bodyleft.png")));
?
? ? // 獲取食物素材
? ? public static ImageIcon candy = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/candy.png")));
? ? public static ImageIcon drumsticks = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/drumsticks.png")));
? ? public static ImageIcon hamburger = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/hamburger.png")));
? ? public static ImageIcon twm = new ImageIcon(Objects.requireNonNull(Data.class.getResource("/statics/twm.png")));
?
?
}

素材如下:

頭、身體、食物都是:25+25像素

橫幅:850*50像素

自己可以去PS里面做幾張

現(xiàn)在我們來創(chuàng)建一個(gè)畫板,把蛇畫到畫板上,還要讓他動(dòng)起來。

這里面涉及到的東西稍微有點(diǎn)多,創(chuàng)建一個(gè)畫筆的方法,把圖片素材轉(zhuǎn)換為ImageIcon然后畫到相應(yīng)的位置,蛇向右移動(dòng)一次就是所有身體的X坐標(biāo)加一,向下移就是Y坐標(biāo)加一,其他相同。隨機(jī)函數(shù)生成兩個(gè)隨機(jī)數(shù),其坐標(biāo)就是食物的位置。蛇頭的X坐標(biāo)和Y坐標(biāo)與食物的X坐標(biāo)Y坐標(biāo)重合就說明已經(jīng)吃到食物。蛇頭越過邊界,或者蛇頭與某一個(gè)身體的坐標(biāo)重合,都判定蛇死亡。

package com.lzijin;
?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
?
public class GamePanel extends JPanel implements KeyListener, ActionListener {
? ? /*
? ? ?* 數(shù)據(jù)的定義
? ? ?*/
? ? int length;// 長(zhǎng)度
? ? int[] snakeX = new int[600];
? ? int[] snakeY = new int[500];
? ? String fx;
? ? boolean isStart = false;
? ? Timer timer = new Timer(100, this);
? ? int foodX;
? ? int foodY;
? ? Random random = new Random();
? ? boolean isFail = false;
? ? int score;
? ? Random random_food =new Random();
? ? int food;
?
?
? ? // 構(gòu)造器
? ? public GamePanel() {
? ? ? ? init();
? ? ? ? this.setFocusable(true);
? ? ? ? this.addKeyListener(this);
? ? ? ? timer.start();
? ? }
?
? ? /*
? ? ?* 數(shù)據(jù)的初始化
? ? ?*/
? ? public void init() {
? ? ? ? // 蛇的默認(rèn)長(zhǎng)度
? ? ? ? length = 3;
? ? ? ? snakeX[0] = 100;
? ? ? ? snakeY[0] = 100;
? ? ? ? // 蛇的第一個(gè)身體
? ? ? ? snakeX[1] = 75;
? ? ? ? snakeY[1] = 100;
? ? ? ? // 蛇的第二個(gè)身體
? ? ? ? snakeX[2] = 50;
? ? ? ? snakeY[2] = 100;
? ? ? ? // 默認(rèn)方向
? ? ? ? fx = "R";
? ? ? ? foodX = 25 + 25 * random.nextInt(34);
? ? ? ? foodY = 75 + 25 * random.nextInt(24);
? ? ? ? score = 0;
?
? ? }
?
? ? protected void paintComponent(Graphics g) {
? ? ? ? super.paintComponent(g);// 清屏
? ? ? ? this.setBackground(Color.white);// 背景色
? ? ? ? Data.header.paintIcon(this, g, 25, 11);// 標(biāo)題欄位置
?
?
?
? ? ? ? g.setColor(Color.cyan);
? ? ? ? //畫背景圖片
? ? ? ? g.fillRect(25, 75, 850, 600);// 游戲區(qū)域
?
? ? ? ? // 靜態(tài)蛇 ? ? ? ?// 初始化頭的方向,位置
? ? ? ? switch (fx) {
? ? ? ? ? ? case "R" -> Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
? ? ? ? ? ? case "L" -> Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
? ? ? ? ? ? case "U" -> Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
? ? ? ? ? ? case "D" -> Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
? ? ? ? }
?
? ? ? ? // 默認(rèn)身體
? ? ? ? for (int i = 1; i < length; i++) {
? ? ? ? ? ? switch (fx) {
? ? ? ? ? ? ? ? case "R" -> Data.body_right.paintIcon(this, g, snakeX[i], snakeY[i]);
? ? ? ? ? ? ? ? case "L" -> Data.body_left.paintIcon(this, g, snakeX[i], snakeY[i]);
? ? ? ? ? ? ? ? case "U" -> Data.body_up.paintIcon(this, g, snakeX[i], snakeY[i]);
? ? ? ? ? ? ? ? case "D" -> Data.body_down.paintIcon(this, g, snakeX[i], snakeY[i]);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 畫積分
? ? ? ? g.setColor(Color.BLUE);
? ? ? ? g.setFont(new Font("微軟雅黑", Font.BOLD, 18));
? ? ? ? g.drawString("長(zhǎng)度:" + length, 750, 33);
? ? ? ? g.drawString("分?jǐn)?shù):" + score, 750, 52);
?
? ? ? ? // 畫食物
?
? ? ? ? switch (food) {
? ? ? ? ? ? case 1 -> Data.candy.paintIcon(this, g, foodX, foodY);
? ? ? ? ? ? case 2 -> Data.drumsticks.paintIcon(this, g, foodX, foodY);
? ? ? ? ? ? case 3 -> Data.hamburger.paintIcon(this, g, foodX, foodY);
? ? ? ? ? ? default -> Data.twm.paintIcon(this, g, foodX, foodY);
? ? ? ? }
?
? ? ? ? // 提示是否開始
? ? ? ? if (!isStart) {
? ? ? ? ? ? // 設(shè)置顏色
? ? ? ? ? ? g.setColor(Color.white);
? ? ? ? ? ? // 設(shè)置字體
? ? ? ? ? ? g.setFont(new Font("楷體", Font.BOLD, 40));
? ? ? ? ? ? // 設(shè)置內(nèi)容
? ? ? ? ? ? g.drawString("按下空格請(qǐng)小姐姐吃糖", 250, 300);
? ? ? ? }
? ? ? ? if (isFail) {
? ? ? ? ? ? // 設(shè)置顏色
? ? ? ? ? ? g.setColor(Color.RED);
? ? ? ? ? ? // 設(shè)置字體
? ? ? ? ? ? g.setFont(new Font("微軟雅黑", Font.BOLD, 40));
? ? ? ? ? ? // 設(shè)置內(nèi)容
? ? ? ? ? ? g.drawString("哎呀,小姐姐沒了!", 300, 300);
? ? ? ? }
? ? }
?
? ? @Override
? ? public void keyTyped(KeyEvent e) {
? ? ? ? // 敲擊
?
? ? }
?
? ? /*
? ? ?* 取按下的鍵
? ? ?*/
? ? @Override
? ? public void keyPressed(KeyEvent e) {
? ? ? ? int KeyCode = e.getKeyCode();
? ? ? ? if (KeyCode == KeyEvent.VK_SPACE) {
? ? ? ? ? ? if (isFail) {
? ? ? ? ? ? ? ? isFail = false;
? ? ? ? ? ? ? ? init();// 重新初始化游戲
? ? ? ? ? ? } else {// 暫停游戲
? ? ? ? ? ? ? ? isStart = !isStart;
? ? ? ? ? ? }
?
? ? ? ? ? ? repaint();
? ? ? ? }
? ? ? ? // 控制鍵盤走向
? ? ? ? if (KeyCode == KeyEvent.VK_LEFT) {
? ? ? ? ? ? if (!fx.equals("R"))
? ? ? ? ? ? ? ? fx = "L";
? ? ? ? } else if (KeyCode == KeyEvent.VK_RIGHT) {
? ? ? ? ? ? if (!fx.equals("L"))
? ? ? ? ? ? ? ? fx = "R";
? ? ? ? } else if (KeyCode == KeyEvent.VK_UP) {
? ? ? ? ? ? if (!fx.equals("D"))
? ? ? ? ? ? ? ? fx = "U";
? ? ? ? } else if (KeyCode == KeyEvent.VK_DOWN) {
? ? ? ? ? ? if (!fx.equals("U"))
? ? ? ? ? ? ? ? fx = "D";
? ? ? ? }
?
? ? }
?
? ? @Override
? ? public void keyReleased(KeyEvent e) {
? ? ? ? // 釋放
?
? ? }
?
? ? /*
? ? ?* 定時(shí)器,時(shí)間監(jiān)聽 蛇的移動(dòng) 吃食物 失敗判斷 頁(yè)面刷新
? ? ?*/
? ? @Override
? ? public void actionPerformed(ActionEvent e) {
? ? ? ? // 如果游戲處于開始狀態(tài)
? ? ? ? if (isStart && !isFail) {
? ? ? ? ? ? // 身體移動(dòng)
? ? ? ? ? ? for (int i = length - 1; i > 0; i--) {
? ? ? ? ? ? ? ? snakeX[i] = snakeX[i - 1];
? ? ? ? ? ? ? ? snakeY[i] = snakeY[i - 1];
? ? ? ? ? ? }
? ? ? ? ? ? // 頭部移動(dòng)
? ? ? ? ? ? switch (fx) {
? ? ? ? ? ? ? ? case "R" -> {
? ? ? ? ? ? ? ? ? ? snakeX[0] = snakeX[0] + 25;
? ? ? ? ? ? ? ? ? ? if (snakeX[0] > 850) {// 邊界判斷
? ? ? ? ? ? ? ? ? ? ? ? isFail = true;
? ? ? ? ? ? ? ? ? ? ? ? //snakeX[0] = 25;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case "L" -> {
? ? ? ? ? ? ? ? ? ? snakeX[0] = snakeX[0] - 25;
? ? ? ? ? ? ? ? ? ? if (snakeX[0] < 25) {// 邊界判斷
? ? ? ? ? ? ? ? ? ? ? ? isFail = true;
? ? ? ? ? ? ? ? ? ? ? ? //snakeX[0] = 850;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case "U" -> {
? ? ? ? ? ? ? ? ? ? snakeY[0] = snakeY[0] - 25;
? ? ? ? ? ? ? ? ? ? if (snakeY[0] < 75) {// 邊界判斷
? ? ? ? ? ? ? ? ? ? ? ? isFail = true;
? ? ? ? ? ? ? ? ? ? ? ? //snakeY[0] = 650;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case "D" -> {
? ? ? ? ? ? ? ? ? ? snakeY[0] = snakeY[0] + 25;
? ? ? ? ? ? ? ? ? ? if (snakeY[0] > 650) {// 邊界判斷
? ? ? ? ? ? ? ? ? ? ? ? isFail = true;
? ? ? ? ? ? ? ? ? ? ? ? ?//snakeY[0] = 75;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? // 吃食物
? ? ? ? ? ? if (snakeX[0] == foodX && snakeY[0] == foodY) {
? ? ? ? ? ? ? ? length++;
? ? ? ? ? ? ? ? score += 10;
? ? ? ? ? ? ? ? // 重新生成食物
? ? ? ? ? ? ? ? foodX = 25 + 25 * random.nextInt(34);
? ? ? ? ? ? ? ? foodY = 75 + 25 * random.nextInt(24);
? ? ? ? ? ? ? ? food=1+random_food.nextInt(4);
? ? ? ? ? ? }
? ? ? ? ? ? // 結(jié)束判斷
?
? ? ? ? ? ? for (int i = 1; i < length; i++) {
? ? ? ? ? ? ? ? if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
? ? ? ? ? ? ? ? ? ? isFail = true;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? // 刷新頁(yè)面
? ? ? ? ? ? repaint();
? ? ? ? }
? ? ? ? timer.start();
? ? }
?
}

別忘添加素材圖片:

最后將畫板添加到窗口當(dāng)中

現(xiàn)在我們的貪吃蛇就做好了

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot實(shí)現(xiàn)MyBatis動(dòng)態(tài)創(chuàng)建表的操作語句

    Spring?Boot實(shí)現(xiàn)MyBatis動(dòng)態(tài)創(chuàng)建表的操作語句

    這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)MyBatis動(dòng)態(tài)創(chuàng)建表,MyBatis提供了動(dòng)態(tài)SQL,我們可以通過動(dòng)態(tài)SQL,傳入表名等信息然組裝成建表和操作語句,本文通過案例講解展示我們的設(shè)計(jì)思路,需要的朋友可以參考下
    2024-01-01
  • 史上最佳springboot Locale 國(guó)際化方案

    史上最佳springboot Locale 國(guó)際化方案

    今天給大家分享史上最佳springboot Locale 國(guó)際化方案,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-08-08
  • java構(gòu)造器 默認(rèn)構(gòu)造方法及參數(shù)化構(gòu)造方法

    java構(gòu)造器 默認(rèn)構(gòu)造方法及參數(shù)化構(gòu)造方法

    構(gòu)造器也叫構(gòu)造方法、構(gòu)造函數(shù),是一種特殊類型的方法,負(fù)責(zé)類中成員變量(域)的初始化。構(gòu)造器的用處是在創(chuàng)建對(duì)象時(shí)執(zhí)行初始化,當(dāng)創(chuàng)建一個(gè)對(duì)象時(shí),系統(tǒng)會(huì)為這個(gè)對(duì)象的實(shí)例進(jìn)行默認(rèn)的初始化,下面文章將進(jìn)入講解,需要的朋友可以參考下
    2021-10-10
  • 源碼分析ConcurrentHashMap如何保證線程安全

    源碼分析ConcurrentHashMap如何保證線程安全

    這篇文章將結(jié)合底層源碼為大家詳細(xì)介紹一下ConcurrentHashMap是如何保證線程安全的,文中是示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-06-06
  • 關(guān)于Java中String類字符串的解析

    關(guān)于Java中String類字符串的解析

    這篇文章主要介紹有關(guān)Java中String類字符串的解析,在java中,和C語言一樣,也有關(guān)于字符串的定義,并且有他自己特有的功能,下面就進(jìn)入主題一起學(xué)習(xí)下面文章內(nèi)容吧
    2021-10-10
  • Spring Cloud Gateway入門解讀

    Spring Cloud Gateway入門解讀

    本篇文章主要介紹了Spring Cloud Gateway入門解讀,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • 基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)

    基于SpringBoot上傳任意文件功能的實(shí)現(xiàn)

    下面小編就為大家?guī)硪黄赟pringBoot上傳任意文件功能的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java IO文件過濾器對(duì)命令設(shè)計(jì)模式的使用

    Java IO文件過濾器對(duì)命令設(shè)計(jì)模式的使用

    java io流里面使用到了很多的設(shè)計(jì)模式,最典型的就是裝飾模式,還有命令模式,下面分兩部分來講Java IO文件過濾器對(duì)命令設(shè)計(jì)模式的使用,一起看看吧
    2017-06-06
  • SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn)

    SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot數(shù)據(jù)校驗(yàn)功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 熟練掌握J(rèn)ava8新特性之Stream API的全面應(yīng)用

    熟練掌握J(rèn)ava8新特性之Stream API的全面應(yīng)用

    Stream是Java8的一大亮點(diǎn),是對(duì)容器對(duì)象功能的增強(qiáng),它專注于對(duì)容器對(duì)象進(jìn)行各種非常便利、高效的 聚合操作(aggregate operation)或者大批量數(shù)據(jù)操作。Stream API借助于同樣新出現(xiàn)的Lambda表達(dá)式,極大的提高編程效率和程序可讀性,感興趣的朋友快來看看吧
    2021-11-11

最新評(píng)論

农安县| 西宁市| 南康市| 康保县| 阳朔县| 抚顺市| 翁牛特旗| 冷水江市| 西平县| 镇江市| 永春县| 韶山市| 永新县| 海城市| 宁河县| 盐津县| 方城县| 呼玛县| 通道| 宜君县| 汝阳县| 定襄县| 陆良县| 修水县| 波密县| 宜丰县| 合阳县| 新野县| 望都县| 嘉祥县| 韩城市| 嵊泗县| 中西区| 南阳市| 双柏县| 汝南县| 南川市| 汽车| 上饶市| 南宁市| 高平市|