游戏效果
创建、设置窗体
package com.game;
import javax.swing.*;
import java.awt.*;
public class GameStart {
public static void main(String[] args) {
// 創建一個窗體
JFrame jf = new JFrame();
// 設置標題
jf.setTitle("牛逼貪吃🐍🐏 by你爸爸");
// 設置窗體彈出坐標以及窗體大小
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//默認電腦寬度
int height = Toolkit.getDefaultToolkit().getScreenSize(). height;//默認電腦高度
// jf.setBounds(50,50,800,800);
jf.setBounds((width-800)/2,(height-800)/2,800,800);//這樣會讓窗體在正中央顯示
//窗體解除隱藏效果
jf.setVisible(true);
// 設置窗體不可改變大小
jf.setResizable(false);
// 設置窗體關閉,程序即關閉
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //WindowConstants.EXIT_ON_CLOSE == 3
GamePanel g =new GamePanel();
jf.add(g);
}
}
//2021年9月7日17:46:15
游戏面板及游戏操作
package com.game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.Scanner;
//GamePanel繼承了JPanel才具備面板的功能
public class GamePanel extends JPanel {
//定義🐍的長度
int length;
//一個數組存儲🐍的x坐標
int[] snakeX = new int[200];
// 一個數組存儲🐍的y坐標
int[] snakeY = new int[200];
//定義蛇頭方向
String direction;//direction是方向的意思
//定義游戲是否已經開始,只有兩個狀態
boolean isStart;
//加入定时器
Timer time;
//加入食物坐标
int FoodX;
int FoofY;
//分数
int grade;
//蛇的状态
boolean isDie;
public void init(){ /*init初始化設置*/
length = 3;//初始長度
//蛇頭
snakeX[0] = 175;
snakeY[0] = 175;
//蛇身1
snakeX[1] = 150;
snakeY[1] = 175;
//蛇身2
snakeX[2] = 125;
snakeY[2] = 175;
//初始方向
direction = "R";
isStart = false;
//初始食物坐标
FoodX = 225;
FoofY = 600;
//初始分数0
grade = 0;
//初始蛇状态
isDie = false;
}
public GamePanel(){
init();//爲了init初始化以後能用,單獨列出init方法
//把焦點聚集在面板上
this.setFocusable(true);
//加入監聽
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
if(isDie==true)
{
isDie=false;
isStart = true;
init();
}
else{
isStart = !isStart;
}
repaint();//重繪,調用paintComponent(Graphics g)
}
if(e.getKeyCode() == KeyEvent.VK_UP)
{
direction = "U";
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
direction = "R";
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
direction = "B";
}
if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
direction = "L";
}
}
});
time = new Timer(100, new ActionListener() {
//ActionListener() 是事件监听,每100ms监听
@Override
public void actionPerformed(ActionEvent e) {
if(isStart == true)
{
//让蛇身从后往前走,后一个占用前一个的位置
for(int i = length-1;i > 0;i--)
{
snakeX[i]=snakeX[i-1];
snakeY[i]=snakeY[i-1];
}
//蛇头动
if(direction.equals("R"))
{
snakeX[0]+=25;
}
if(direction.equals("U"))
{
snakeY[0]-=25;
}
if(direction.equals("B"))
{
snakeY[0]+=25;
}
if(direction.equals("L"))
{
snakeX[0]-=25;
}
if(snakeX[0]>750)
{
snakeX[0] = 25;
}
if(snakeX[0]<25)
{
snakeX[0] = 725;
}
if(snakeY[0]>725)
{
snakeY[0] = 75;
}
if(snakeY[0]<75){
snakeY[0] = 725;
}
//吃食物
if(snakeX[0]==FoodX&&snakeY[0]==FoofY)
{
length++;//蛇身增长
//随机更改食物坐标
FoodX = (new Random().nextInt(30)+1)*25;
FoofY = (new Random().nextInt(23)+3)*25;
//积分+
grade++;
}
//判断蛇是否死亡
for(int i=1;i<length;i++)
{
if(snakeX[i]==snakeX[0]&&(snakeY[i]==snakeY[0]))
{
isStart = false;
isDie = true;
}
}
repaint();
}
}
});
time.start();//启动定时器
}
// paintComponent這個方法比較特殊,相當於圖形版的Main函數
//自動調用畫筆 Graphics g
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(120,205,135));
images.headerImg.paintIcon(this,g,10,5); //this是指此面板,g是指畫筆工具
//更改畫筆顔色
g.setColor(new Color(147, 181, 158));
//畫一個矩形
g.fillRect(10,55,775,700);
//畫蛇頭
if(direction.equals("R"))
{
images.headRImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
if(direction.equals("U"))
{
images.headUImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
if(direction.equals("B"))
{
images.headBImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
if(direction.equals("L"))
{
images.headLImg.paintIcon(this,g,snakeX[0],snakeY[0]);
}
//畫蛇身
for(int i=1;i<length;i++)
{
images.bodyImg.paintIcon(this,g,snakeX[i],snakeY[i]);
}
//暫停顯示字樣
if(isStart == false&&isDie==false)
{
g.setColor(new Color(226, 15, 15));
g.setFont(new Font("微軟雅黑",Font.BOLD,25));
g.drawString("点击空格开始游戏",300,350);
}
if(!isStart == false)
{
g.setColor(new Color(255, 255, 255));
g.setFont(new Font("微軟雅黑",Font.BOLD,20));
g.drawString("分数:"+grade+"",700,32);
}
if(isDie == true)
{
g.setColor(new Color(255, 255, 255));
g.setFont(new Font("微軟雅黑",Font.BOLD,25));
g.drawString("已死亡,按空格重新开始",300,350);
}
images.shitImg.paintIcon(this,g,FoodX,FoofY);
}
}
封装游戏需要的图片
package com.game;
import javax.swing.*;
import java.net.URL;
public class images {
// 将图片路径封装成对象
public static URL header = images.class.getResource("/Images/header.png");
// 将图片封装为对象
public static ImageIcon headerImg = new ImageIcon(header);
// 将图片路径封装成对象
public static URL headU = images.class.getResource("/Images/headU.png");
// 将图片封装为对象
public static ImageIcon headUImg = new ImageIcon(headU);
// 将图片路径封装成对象
public static URL headR = images.class.getResource("/Images/headR.png");
// 将图片封装为对象
public static ImageIcon headRImg = new ImageIcon(headR);
// 将图片路径封装成对象
public static URL headB = images.class.getResource("/Images/headB.png");
// 将图片封装为对象
public static ImageIcon headBImg = new ImageIcon(headB);
// 将图片路径封装成对象
public static URL headL = images.class.getResource("/Images/headL.png");
// 将图片封装为对象
public static ImageIcon headLImg = new ImageIcon(headL);
// 将图片路径封装成对象
public static URL body = images.class.getResource("/Images/body.png");
// 将图片封装为对象
public static ImageIcon bodyImg = new ImageIcon(body);
// 将图片路径封装成对象
public static URL shit = images.class.getResource("/Images/shit.png");
// 将图片封装为对象
public static ImageIcon shitImg = new ImageIcon(shit);
}
图片素材