Swing-贪吃蛇游戏
1、游戏主启动类
package com.zuoyan.snake;
import javax.swing.*;
/**
* 游戏主启动类
*/
public class StartGame {
public static void main(String[] args) {
JFrame jf = new JFrame();
//游戏面板
jf.add(new GamePanel());
//窗口大小不可变
jf.setResizable(false);
jf.setBounds(500, 200, 900, 720);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
2、游戏面板
package com.zuoyan.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; //初始方向
Boolean isStart; //游戏状态
//食物坐标
int foodx;
int foody;
Random random = new Random();
Boolean isFail; //游戏是否失败
int score; //积分
//定时器
Timer timer = new Timer(100, this);//100毫秒执行一次
//构造器
public GamePanel() {
init();
//获得焦点事件
setFocusable(true);
//获得键盘事件
addKeyListener(this);
}
//初始化方法
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";
isStart = false;
isFail = false;
//随即分配食物坐标
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
score = 0;
timer.start();
}
//绘制面板,游戏中所有的东西都用该画笔来画
@Override
protected void paintComponent(Graphics g) {
//清屏
super.paintComponent(g);
setBackground(Color.WHITE);
//绘制头部广告栏
Data.header.paintIcon(this, g, 25, 11);
g.fillRect(25, 75, 850, 600);
//绘制积分
g.setColor(Color.ORANGE);
g.setFont(new Font("微软雅黑", Font.BOLD, 18));
g.drawString("长度" + length, 800, 35);
g.drawString("分数" + score, 800, 60);
//绘制食物
Data.food.paintIcon(this, g, foodx, foody);
//绘制小蛇
if ("R".equals(fx)) {
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if ("L".equals(fx)) {
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if ("U".equals(fx)) {
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if ("D".equals(fx)) {
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]);
}
//游戏状态
if (isStart == false) {
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("按下空格开始游戏!", 300, 300);
}
//游戏是否失败
if (isFail) {
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("游戏失败!按空格重新开始", 300, 300);
}
}
//键盘监听事件
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
if (isFail) {
//重新开始
isFail = false;
init();
} else {
isStart = !isStart;
}
repaint();
}
//让小蛇移动
if (keyCode == KeyEvent.VK_UP) {
fx = "U";
} else if (keyCode == KeyEvent.VK_DOWN) {
fx = "D";
} else if (keyCode == KeyEvent.VK_LEFT) {
fx = "L";
} else if (keyCode == KeyEvent.VK_RIGHT) {
fx = "R";
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
//事件监听,通过固定事件来刷新,1s=10次
@Override
public void actionPerformed(ActionEvent e) {
if (isStart && isFail == false) {
//吃食物
if (snakeX[0] == foodx && snakeY[0] == foody) {
//长度+1
length++;
//分数+10
score += 10;
//再次随即食物位置
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}
//右移
for (int i = length - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
//走向
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;
}
}
//失败判断,撞到自己算失败
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
isFail = true;
}
}
repaint();
}
timer.start();
}
}
3、数据中心
这些图片是放在Data类同级目录static中的
package com.zuoyan.snake;
import javax.swing.*;
import java.net.URL;
/**
* 数据中心
*/
public class Data {
public static URL headerURL = Data.class.getResource("static/header.png");
public static URL upURL = Data.class.getResource("static/up.png");
public static URL downURL = Data.class.getResource("static/down.png");
public static URL leftURL = Data.class.getResource("static/left.png");
public static URL rightURL = Data.class.getResource("static/right.png");
public static URL bodyURL = Data.class.getResource("static/body.png");
public static URL foodURL = Data.class.getResource("static/food.png");
public static ImageIcon header = new ImageIcon(headerURL);
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);
public static ImageIcon food = new ImageIcon(foodURL);
}