贪吃蛇
此游戏主要包括三个类:
- 主启动类:GameStart
- 游戏面板类:GamePanel
- 数据中心类:Data
话不多说,直接开始
主启动类:GameStart
package com.teng.Snake;
import javax.swing.*;
//游戏的主启动类
public class GameStart{
public static void main(String[] args) {
JFrame jFrame = new JFrame("小梁学Java之贪吃蛇游戏");
jFrame.add(new GamePanel());//添加面板
jFrame.setVisible(true);
jFrame.setResizable(false);//设置窗口大小不可变
jFrame.setBounds(100,100,915,720);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
游戏面板类:GamePanel
package com.teng.Snake;
import com.sun.deploy.security.SandboxSecurity;
import javafx.scene.input.KeyCode;
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 {
//定义蛇的数据
int length;//定义蛇的长度
int[] snakeX = new int[600];//蛇的X坐标
int[] snakeY = new int[500];//蛇的Y坐标
String fx;//定义方向
int score;//定义当前长度
int top;//定义最高长度
//定义食物坐标
int foodx;
int foody;
Random random = new Random();
boolean isStart = false;//设置游戏初始状态为等待开始
boolean isOver = false;//判定游戏失败,默认为否
boolean isStop = false;//判定游戏暂停,默认为否
Timer timer = new Timer(100,this);//监听对象为本窗口,且100ms执行一次
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);//添加键盘监听事件
timer.start();//游戏开始的时候定时器就启动
}
//初始化方法
public void init(){
length = 3;
snakeX[0] = 100;snakeY[0] = 100;//蛇的头部;
snakeX[1] = 75;snakeY[1] = 100;//蛇的第一节身体;
snakeX[2] = 50;snakeY[2] = 100;//蛇的第二节身体;
fx = "R";
foodx = 25+25*random.nextInt(34);
foody = 75+25*random.nextInt(24);
score = 0;
}
//绘制面板
@Override
protected void paintComponent(Graphics g) {//游戏中的东西都用这支画笔来画
super.paintComponent(g);//清屏
Data.head.paintIcon(this,g,25,11);//顶部的广告栏
g.fillRect(25,75,850,600);//游戏界面
this.setBackground(Color.white);
//计分板
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("当前长度为:"+score,650,30);
g.drawString("历史最高为:"+top,650,53);
//绘制蛇
//通过“fx”变量来判断蛇头的初始化方向
if (fx.equals("R"))
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
else if(fx.equals("L"))
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
else if(fx.equals("U"))
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
else if(fx.equals("D"))
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
//蛇的食物
Data.food.paintIcon(this,g,foodx,foody);
if(isStart == false && isOver == false && isStop == false){
g.setColor(Color.RED);
g.setFont(new Font("华文行楷",Font.BOLD,50));
g.drawString("按下空格键开始游戏",220,300);
}
if(isStart == false && isOver == false && isStop == true){
g.setColor(Color.RED);
g.setFont(new Font("华文行楷",Font.BOLD,50));
g.drawString("游戏已暂停,按下空格键继续游戏",50,300);
}
if(isOver == true){
g.setColor(Color.RED);
g.setFont(new Font("华文行楷",Font.BOLD,50));
g.drawString("游戏结束,按下空格重新开始",120,300);
}
}
//键盘监听
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获取按下的按钮码
if(keyCode == KeyEvent.VK_SPACE){//如果按下的是空格
if(isOver) {
isOver = false;
isStop = false;
}
else {
isStart = !isStart;
isStop = true;
}
}
repaint();//重画
//小蛇移动
if(keyCode == KeyEvent.VK_UP) fx = "U";
if(keyCode == KeyEvent.VK_DOWN) fx = "D";
if(keyCode == KeyEvent.VK_LEFT) fx = "L";
if(keyCode == KeyEvent.VK_RIGHT) fx = "R";
}
//事件监听——通过固定的事件来刷新
@Override
public void actionPerformed(ActionEvent e) {
if(isStart == true && isOver == false){//如果游戏是开始状态
//移动
for (int i = length-1; i >0 ; i--) {
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
//控制走向
if(fx.equals("U")){
snakeY[0] = snakeY[0]-25;
//边界判断
if (snakeY[0]<=50)
snakeY[0] = 650;
}
else if(fx.equals("D")){
snakeY[0] = snakeY[0]+25;
if (snakeY[0]>=675)
snakeY[0] = 75;
}
else if(fx.equals("L")){
snakeX[0] = snakeX[0]-25;
if (snakeX[0]<0)
snakeX[0] = 825;
}
else if(fx.equals("R")){
snakeX[0] = snakeX[0]+25;
if (snakeX[0]>850)
snakeX[0] = 25;
}
//蛇的头部碰到食物时长度加一并且刷新食物
while(snakeX[0]==foodx&&snakeY[0]==foody){
length ++;
score ++;
if(score>=top) top = score;
foodx = 25+25*random.nextInt(34);
foody = 75+25*random.nextInt(24);
}
//判定游戏结束
for (int i = 1; i <= length-1; i++) {
if(snakeX[0]==snakeX[i] && snakeY[0]==snakeY[i]){
isOver = true;
init();
}
}
repaint();
}
timer.start();//定时器开启
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
数据中心类:Data
package com.teng.Snake;
import javax.swing.*;
import java.net.URL;
//数据中心
public class Data {
public static URL headURL = Data.class.getResource("statics/head.png");
public static URL foodURL = Data.class.getResource("statics/food.png");
public static URL upURL = Data.class.getResource("statics/up.png");
public static URL downURL = Data.class.getResource("statics/down.png");
public static URL leftURL = Data.class.getResource("statics/left.png");
public static URL rightURL = Data.class.getResource("statics/right.png");
public static URL bodyURL = Data.class.getResource("statics/body.png");
public static ImageIcon head = new ImageIcon(headURL);
public static ImageIcon food = new ImageIcon(foodURL);
public static ImageIcon up = new ImageIcon(upURL);
public static ImageIcon down = new ImageIcon(downURL);
public static ImageIcon left = new ImageIcon(leftURL);
public static ImageIcon right = new ImageIcon(rightURL);
public static ImageIcon body = new ImageIcon(bodyURL);
}
运行图:
游戏初始界面:
游戏运行界面:
游戏暂停界面:
游戏结束界面:
总结
- 贪吃蛇游戏汇总了GUI编程所学知识
- 游戏还可以进行大量优化,比如失败判定加一个边界、食物不会刷新到小蛇身体的坐标、游戏难度分级,甚至还想加个数据库记录最高分哈哈哈哈(日后慢慢来嘿嘿)
最后附上游戏源码:
链接:https://pan.baidu.com/s/1bH87OmWSssTtLUacN3KThw
提取码:gqas