个人推荐:
📢📢📢 前些天发现了一个蛮有意思的人工智能学习网站,8个字形容一下
"通俗易懂,风趣幽默"
,感觉非常有意思,忍不住分享一下给大家。
点击跳转到教程。
1:运行效果
2:项目框架
3:代码
3.1 Data.java
package com.oneprogrammerofloveduduofcat.snake;
import java.net.URL;
import javax.swing.ImageIcon;
public class Data {
//头部的图片 URL
public static URL headerURL=Data.class.getResource("/statics/header.png");
public static ImageIcon header=new ImageIcon(headerURL);
//贪吃蛇头部
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 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 URL bodyUrl=Data.class.getResource("/statics/body.png");
public static ImageIcon body=new ImageIcon(bodyUrl);
//食物
public static URL foodUrl=Data.class.getResource("/statics/food.png");
public static ImageIcon food=new ImageIcon(foodUrl);
}
3.2 GamePanel.Java
package com.oneprogrammerofloveduduofcat.snake;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements KeyListener,ActionListener{
private snake snake;//定义贪吃蛇对象
private boolean isStart=false;//游戏是否开始
private Timer snakeTime=new Timer(100,this);//游戏定时器,让贪吃蛇动起来
//定义食物
private Random random=new Random();//随机生成食物
private int foodx;//食物X轴坐标
private int foody;//食物Y轴坐标
private int socre=0;//分数
//画板:画界面,画蛇
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
this.setBackground(Color.white);//设置游戏面板为黑色
//绘制头部的广告栏
Data.header.paintIcon(this, g, 25, 11);
g.fillRect(25, 75, 850, 600);//绘制游戏区域
//让贪吃蛇动起来
switch (snake.getFx()) {
case "R":
Data.right.paintIcon(this, g, snake.getSnakex()[0], snake.getSnakey()[0]);
break;
case "L":
Data.left.paintIcon(this, g, snake.getSnakex()[0], snake.getSnakey()[0]);
break;
case "U":
Data.up.paintIcon(this, g, snake.getSnakex()[0], snake.getSnakey()[0]);
break;
case "D":
Data.down.paintIcon(this, g, snake.getSnakex()[0], snake.getSnakey()[0]);
break;
default:
break;
}
//加载身体
for (int i = 1; i < snake.getLenth(); i++) {
Data.body.paintIcon(this, g, snake.getSnakex()[i], snake.getSnakey()[i]);
}
//生成食物
Data.food.paintIcon(this, g, foodx, foody);
//画积分
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,16));
g.drawString("长度:"+snake.getLenth(), 750, 30);
g.drawString("分数:"+socre, 750, 50);
//游戏提示是否开始
if (!isStart) {
g.setColor(Color.white);//设置画笔的颜色
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("按下空格开始游戏", 300, 300);
}
//失败提醒
if (snake.getIsFail()) {
g.setColor(Color.red);//设置画笔的颜色
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("游戏结束,按下空格重新开始", 300, 300);
}
}
public GamePanel() {
//游戏初始化
init();
}
private void init() {
snake=new snake();//实例化贪吃蛇对象
foodx=25+25*random.nextInt(34); //初始化食物X坐标
foody=75+25*random.nextInt(24);//初始化食物Y坐标
//获取键盘的监听事件
this.setFocusable(true);
this.addKeyListener(this);
snakeTime.start();//开启定时器
this.socre=0;//初始化分数
}
//定时器监听
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int newResultX[] = snake.getSnakex(),newResultY[] = snake.getSnakey();
//如果游戏处于开始状态
if (isStart && !snake.getIsFail()) {
//判断是否吃到食物
if (foodx==newResultX[0]&&foody==newResultY[0]) {
//长度加1
snake.setLenth(snake.getLenth()+1);
//分数+10
socre+=10;
//食物位置更新
newResultX[snake.getLenth()-1]=foodx-1;
newResultY[snake.getLenth()-1]=foody-1;
//初始化食物的位置
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
}
//默认右移
for (int i =snake.getLenth()-1; i >0; i--) {
newResultX[i]=newResultX[i-1];
newResultY[i]=newResultY[i-1];
}
//通过方向控制蛇头
switch (snake.getFx()) {
case "R":
newResultX[0]+=25;
//边界判断
if (newResultX[0]>850) {
newResultX[0]=25;
}
break;
case "L":
newResultX[0]-=25;
//边界判断
if (newResultX[0]<25) {
newResultX[0]=850;
}
break;
case "U":
newResultY[0]-=25;
//边界判断
if (newResultY[0]<75) {
newResultY[0]=650;
}
break;
case "D":
newResultY[0]+=25;
//边界判断
if (newResultY[0]>650) {
newResultY[0]=75;
}
break;
default:
break;
}
//初始化蛇的位置
snake.setSnakex(newResultX);
snake.setSnakey(newResultY);
//蛇死掉了
for (int i = 1; i < snake.getLenth(); i++) {
if (snake.getSnakex()[0]==snake.getSnakex()[i]&&snake.getSnakey()[0]==snake.getSnakey()[i]) {
snake.setIsFail(true);
}
}
//刷新页面
repaint();
}
snakeTime.start();
}
//键盘按下未释放
@Override
public void keyPressed(KeyEvent e) {
//获取键盘的键
int keyCode=e.getKeyCode();
if (keyCode==KeyEvent.VK_SPACE) {
if (snake.getIsFail()) {
snake.setIsFail(false);
init();
}else{
isStart=!isStart;
}
repaint();//刷新界面
}
//键盘控制走向
if (keyCode==KeyEvent.VK_LEFT) {
snake.setFx("L");
}else if (keyCode==KeyEvent.VK_RIGHT) {
snake.setFx("R");
}else if (keyCode==KeyEvent.VK_UP) {
snake.setFx("U");
}else if (keyCode==KeyEvent.VK_DOWN) {
snake.setFx("D");
}
}
//键盘按下释放
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
//键盘监听
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
3.3 snake.Java
package com.oneprogrammerofloveduduofcat.snake;
public class snake {
private int lenth;//蛇的长度
private int[]snakex=new int[600];//蛇的x坐标
private int[]snakey=new int[600];//蛇的y坐标
private String fx="R";//R:右 L:左 U:上 D:下
private boolean isFail=false;
//贪吃蛇默认参数
public void init() {
this.lenth=3;
snakex[0]=100;snakey[0]=100;
snakex[1]=75;snakey[1]=100;
snakex[2]=50;snakey[2]=100;
}
public snake(int lenth,int[]snakex,int[]snakey) {
// TODO Auto-generated constructor stub
this.lenth=lenth;
this.snakex=snakex;
this.snakey=snakey;
init();
}
public snake() {
// TODO Auto-generated constructor stub
init();
}
public int getLenth() {
return lenth;
}
public void setLenth(int lenth) {
this.lenth = lenth;
}
public int[] getSnakex() {
return snakex;
}
public void setSnakex(int[] snakex) {
this.snakex = snakex;
}
public int[] getSnakey() {
return snakey;
}
public void setSnakey(int[] snakey) {
this.snakey = snakey;
}
public String getFx() {
return fx;
}
public void setFx(String fx) {
this.fx = fx;
}
public boolean getIsFail() {
return isFail;
}
public void setIsFail(boolean isFail) {
this.isFail = isFail;
}
}
3.4 StartGamers.Java
package com.oneprogrammerofloveduduofcat.snake;
import javax.swing.JFrame;
public class StartGamers {
public static void main(String[] args) {
JFrame frame=new JFrame("Javad实战案例-贪吃蛇小游戏");//绘制一个静态窗口 JFrame
frame.setBounds(10,10,900,720);//设置界面的大小
frame.setResizable(false);//窗体大小不可以改变
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置游戏关闭事件
frame.add(new GamePanel()); //将窗体中加入面板
frame.setVisible(true);//显示面板
}
}
3.5 图片素材
4:代码讲解来源于B站up主 [遇见狂神]
5:JavaSE总结
观看地址:点击观看