代码模块组成
一.游戏的主启动类
package com.Hailin.snake;
import javax.swing.*;
//游戏的主启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame("王海林的贪吃蛇游戏");
frame.setBounds(10,10,900,720);
frame.setResizable(false);//窗口不可变
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//游戏界面放在面板上
frame.add(new GamePanel());
frame.setVisible(true);
}
}
- 二.游戏的面板
package com.Hailin.snake;
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坐标 25*25
int[] snakeY = new int[500];//蛇的Y坐标 25*25
String fx;//用来判断小蛇行驶的方向
//食物的坐标
int foodX;
int foodY;
Random random = new Random();
//积分展示
int score;//得分
//游戏当前的状态: 开始 停止
boolean isStart = false;//默认是停止
//游戏状态判定
boolean isFail = false;
//定时器 以毫秒为单位
Timer timer = new Timer(100,this);//100毫秒执行一次
//初始化方法
public void init(){
length = 3;//初始化蛇的长度为3
snakeX[0] = 300;snakeY[0] = 300;//蛇头的坐标
snakeX[1] = 275;snakeY[1] = 300;//第一节身体的坐标
snakeX[2] = 250;snakeY[2] = 300;//第二节身体的坐标
fx = "right";//初始定义蛇头的方向向右
foodX = 25+25*random.nextInt(34);
foodY = 75+25*random.nextInt(24);
score = 0;
}
//构造器
public GamePanel(){
init();
//获得焦点和键盘事件
this.setFocusable(true);//获得焦点事件
this.addKeyListener(this);//键盘监听事件
timer.start();//游戏一开始定时器就启动
}
//绘制面板
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
//绘制静态面板
this.setBackground(Color.WHITE);//设置背景颜色
Date.header.paintIcon(this,g,25,11);//用g画笔将广告画到固定位置
g.fillRect(25,75,850,600);//默认的游戏界面大小
//画积分
g.setColor(Color.black);//设置字体颜色
g.setFont(new Font("微软雅黑",Font.BOLD,18));//设置字体
g.drawString("得分"+score,750,35);
g.drawString("长度"+length,750,50);
//把蛇画到面板上去
if (fx.equals("right")){
Date.right.paintIcon(this,g,snakeX[0],snakeY[0]);//头部的方向判断
}else if (fx.equals("left")){
Date.left.paintIcon(this,g,snakeX[0],snakeY[0]);//头部的方向判断
}else if (fx.equals("up")){
Date.up.paintIcon(this,g,snakeX[0],snakeY[0]);//头部的方向判断
}else if (fx.equals("down")){
Date.down.paintIcon(this,g,snakeX[0],snakeY[0]);//头部的方向判断
}
//将食物显示在桌面上
Date.food.paintIcon(this,g,foodX,foodY);
//画身体
for (int i = 1; i < length; i++) {
Date.body.paintIcon(this,g,snakeX[i],snakeY[i]);
}
//游戏状态
if (isStart == false){
g.setColor(Color.GREEN);//设置字体颜色
g.setFont(new Font("微软雅黑",Font.BOLD,30));//设置字体
g.drawString("轻按空格开始游戏",300,300);
}
//失败界面设置
if (isFail){
g.setColor(Color.RED);//设置字体颜色
g.setFont(new Font("微软雅黑",Font.BOLD,50));//设置字体
g.drawString("游戏结束,轻按空格重新开始游戏",100,200);
}
}
//键盘按压监听事件
@Override
public void keyPressed(KeyEvent keyEvent) {
//获得键盘按键是哪个
int keyCode = keyEvent.getKeyCode();
//按下的为空格键
if(keyCode == KeyEvent.VK_SPACE) {
if (isFail) {
//重新开始
isFail = false;
init();
} else {
isStart = !isStart;//取反
}
repaint();//重新绘制
}
//小蛇移动
if(keyCode==KeyEvent.VK_UP){
fx = "up";
}else if(keyCode==KeyEvent.VK_DOWN){
fx = "down";
}else if(keyCode==KeyEvent.VK_LEFT){
fx = "left";
}else if(keyCode==KeyEvent.VK_RIGHT){
fx = "right";
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
}
@Override
public void keyReleased(KeyEvent keyEvent) {
}
//事件监听----需要通过固定事件来刷新
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(isStart && isFail == false){
//如果游戏开始,就让小蛇移动
//移动
for (int i = length-1; i >0 ; i--) {
//后一节移到前一节的位置
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
//走向
if(fx.equals("right")){
snakeX[0] = snakeX[0]+25;
if ((snakeX[0]==850)) {
isFail = true;
} //边界判断
}else if (fx.equals("left")){
snakeX[0] = snakeX[0]-25;
if ((snakeX[0]==25)){
isFail = true;
} //边界判断
}else if (fx.equals("up")){
snakeY[0] = snakeY[0]-25;
if ((snakeY[0]==75)){
isFail = true;
} //边界判断
}else if (fx.equals("down")){
snakeY[0] = snakeY[0]+25;
if ((snakeY[0]==650)){
isFail = true;
} //边界判断
}
//失败的判断条件
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
isFail = true;
}
}
repaint();//页面重画
//吃食物
if (snakeX[0] == foodX && snakeY[0] == foodY){
length++;//长度加1
score += 10;//得分加10
//再重新生成食物坐标
foodX = 25+25*random.nextInt(34);
foodY = 75+25*random.nextInt(24);
}
}
timer.start();//定时器开启
}
}
三.数据模块
package com.Hailin.snake;
import javax.swing.*;
import java.net.URL;
public class Date {
//数据中心
//相对路径
//绝对路径 / 相对于当前项目
public static URL headerURL = Date.class.getResource("statics/header.png");
public static ImageIcon header = new ImageIcon(headerURL);
public static URL upURL = Date.class.getResource("statics/up.png");
public static ImageIcon up = new ImageIcon(upURL);
public static URL downURL = Date.class.getResource("statics/down.png");
public static ImageIcon down = new ImageIcon(downURL);
public static URL rightURL = Date.class.getResource("statics/right.png");
public static ImageIcon right = new ImageIcon(rightURL);
public static URL leftURL = Date.class.getResource("statics/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL bodyURL = Date.class.getResource("statics/body.png");
public static ImageIcon body = new ImageIcon(bodyURL);
public static URL foodURL = Date.class.getResource("statics/food.png");
public static ImageIcon food = new ImageIcon(foodURL);
}