package com.akita.snake;
import javax.swing.*;
public class StartGames {
public static void main(String[] args) {
//绘制静态窗口
JFrame jFrame = new JFrame("快乐的贪吃蛇");
//设置界面大小
jFrame.setBounds(250, 150, 900, 720);
jFrame.setResizable(false); //设置窗口大小不可以关闭
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭事件,可以点击关闭
jFrame.setVisible(true); //设置窗口可见
}
}
package com.akita.snake;
import javax.swing.*;
import java.net.URL;
public class Data {
//头部的图片 URL:定位图片的地址 ImageIcon:图片
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 ImageIcon up = new ImageIcon(upURL);
public static URL downURL = Data.class.getResource("/statics/down.png");
public static ImageIcon down = new ImageIcon(downURL);
public static URL leftURL = Data.class.getResource("/statics/left.png");
public static ImageIcon left = new ImageIcon(leftURL);
public static URL rightURL = Data.class.getResource("/statics/right.png");
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);
}
定义一个面板
package com.akita.snake;
import javax.swing.*;
import java.awt.*;
public class GamePanel extends JPanel {
//画板:画界面,画蛇
//Graphics:画笔
@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);
}
}
将上述的面板添加到主窗口
jFrame.add(new GamePanel());
//画一条静态的小蛇
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]); //蛇的身体的绘制
}
搭配timer类
Timer timer = new Timer(100, this);
这里使得当前类实现ActionListener接口,并重写actionPerformed方法,在此方法内部进行不断的移动刷新操作,方法内部代码如下:
//如果游戏处于开始状态
if (isStart) {
//右移
for (int i = length - 1; i > 0; i--) { //除了脑袋身体全部向前移动
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
snakeX[0] = snakeX[0] + 25;
//刷新界面
this.repaint();
}
timer.start(); //让时间监听器的时间动起来
//通过控制方向让头部移动
if (fx.equals("R")) {
snakeX[0] = snakeX[0] + 25;
//边界判断
if (snakeX[0] > 850) {
snakeX[0] = 25;
}
} else if (fx.equals("L")) {
snakeX[0] = snakeX[0] - 25;
//边界判断
if (snakeX[0] < 25) {
snakeX[0] = 850;
}
} else if (fx.equals("U")) {
snakeY[0] = snakeY[0] - 25;
//边界判断
if (snakeY[0] < 75) {
snakeY[0] = 650;
}
} else if (fx.equals("D")) {
snakeY[0] = snakeY[0] + 25;
//边界判断
if (snakeY[0] > 650) {
snakeY[0] = 75;
}
}
使用随机数生成食物的坐标
//初始化食物
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
判断小蛇吃食物的逻辑
//如果蛇的头部与食物重合那么就吃到了食物
if (snakeX[0] == foodx && snakeY[0] == foody) {
//长度加一
length++;
//重新生成食物
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}
//结束判断
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
isFail = true;
}
}