文章目录
GUI编程基础03贪吃蛇
import javax.swing.*;
//游戏的主启动类
public class StartGame {
public static void main(String[] args) {
JFrame jFrame = new JFrame("贪吃蛇");
jFrame.setBounds(10,10,900,720);
jFrame.setResizable(false);//设置窗口大小不可变
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.add(new GamePanel());
jFrame.setVisible(true);
}
}
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 direction;//蛇头方向
boolean isStart = false;//游戏是否开始
Timer timer = new Timer(100, this);// 定时器
//定义一个食物
int foodX;
int foodY;
Random random = new Random();
boolean isFail = false;//死亡判断
int score;//积分系统
//构造器
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;//第二个身体坐标
direction = "R";//蛇头向右
//食物坐标
foodX = 25 + 25 * random.nextInt(34);
foodY = 75 + 25 * random.nextInt(24);
score = 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);//绘制游戏区域
//画一条静态的小蛇
if (direction.equals("L")) {//蛇头(左)
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (direction.equals("R")) {//蛇头(右)
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (direction.equals("U")) {//蛇头(上)
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
} else if (direction.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]);//蛇的长度通过length控制
}
//画食物
Data.food.paintIcon(this, g, foodX, foodY);
//画积分
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑", Font.BOLD, 18));
g.drawString("长度:" + length, 750, 35);
g.drawString("积分:" + score, 750, 50);
//游戏提示:是否开始
if (isStart == false) {
//画一个文字
g.setColor(Color.WHITE);//设置画笔颜色
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("按下空格键开始游戏", 300, 300);
}
//失败提示
if (isFail == true) {
//画一个文字
g.setColor(Color.RED);//设置画笔颜色
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("游戏失败,按下空格键重新开始", 200, 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_LEFT) {
direction = "L";//左
} else if (keyCode == KeyEvent.VK_RIGHT) {
direction = "R";//右
} else if (keyCode == KeyEvent.VK_UP) {
direction = "U";//上
} else if (keyCode == KeyEvent.VK_DOWN) {
direction = "D";//下
}
}
//定时器,监听时间:帧
@Override
public void actionPerformed(ActionEvent e) {
//如果游戏处于开始状态,并且游戏没有结束
if (isStart && isFail == false) {
//身体移动
for (int i = length - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
//通过控制方向让头部移动
if (direction.equals("R")) {//右移
snakeX[0] = snakeX[0] + 25;
//边界判断
if (snakeX[0] > 850) {
snakeX[0] = 25;
}
} else if (direction.equals("L")) {//左移
snakeX[0] = snakeX[0] - 25;
//边界判断
if (snakeX[0] < 25) {
snakeX[0] = 850;
}
} else if (direction.equals("U")) {//上移
snakeY[0] = snakeY[0] - 25;
//边界判断
if (snakeY[0] < 75) {
snakeY[0] = 650;
}
} else if (direction.equals("D")) {//下移
snakeY[0] = snakeY[0] + 25;
//边界判断
if (snakeY[0] > 650) {
snakeY[0] = 75;
}
}
//如果蛇头和食物重合(吃到食物)
if (snakeX[0] == foodX && snakeY[0] == foodY) {
length++;//长度+1
score = score + 10;//吃到食物+10
//重新生成食物坐标
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;
}
}
repaint();//刷新界面
}
timer.start();//定时器启动
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
import javax.swing.*;
import java.net.URL;
//数据类
public class Data {
//头部图片
public static URL headerurl = Data.class.getResource("header.png");
public static ImageIcon header = new ImageIcon(headerurl);
// 贪吃蛇头部
public static URL upurl = Data.class.getResource("up.png");
public static URL downurl = Data.class.getResource("down.png");
public static URL lefturl = Data.class.getResource("left.png");
public static URL righturl = Data.class.getResource("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("body.png");
public static ImageIcon body = new ImageIcon(bodyurl);
//食物
public static URL foodurl = Data.class.getResource("food.png");
public static ImageIcon food = new ImageIcon(foodurl);
}