帧:图片的时间片足够小就是动画,一般正常一秒30帧或60帧,连在一起就是动画,拆分开就是静态图片
键盘监听
定时器Timer
数据中心Data
package com.yao.snake;
import javax.swing.*;
import java.net.URL;
//数据中心
public class Data {
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);
}
开始游戏StartGame
package com.yao.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);
}
}
游戏界面GamePanel
package com.yao.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.Objects;
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[600];//蛇的Y坐标 25*25
String direction;
String olddirection;
//当前游戏的状态
boolean isStart = false;//默认为false
//食物坐标
int foodX;
int foodY;
Random random = new Random();
boolean isFail = false;//游戏是否失败
//定时器
Timer timer = new Timer(100,this);
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;//身体1的坐标
snakeX[2]=50;snakeY[2]=100;//身体2的坐标
direction="R";//初始方向向右
//把食物随机分布在界面上
foodX = 25+25*random.nextInt(34);
foodY = 75+25*random.nextInt(24);
for (int i = 1; i < length; i++) {
if (foodX==snakeX[i]&&foodY==snakeY[i]) {
foodX = 25+25*random.nextInt(34);
foodY = 75+25*random.nextInt(24);
}
}
}
@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);//默认游戏界面
//绘制长度和分数
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));//设置字体
g.drawString("长度:"+length,750,30);
g.drawString("分数:"+((length-3)*10),750,50);
//绘制食物
Data.food.paintIcon(this,g,foodX,foodY);
for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);//身体的坐标
}
//绘制初始小蛇
switch (direction) {//判断蛇头方向
case "R":
Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);//脑袋的坐标蛇头向右
break;
case "L":
Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);//脑袋的坐标蛇头向左
break;
case "U":
Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);//脑袋的坐标蛇头向上
break;
case "D":
Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);//脑袋的坐标蛇头向下
break;
}
//游戏状态
if (!isStart){
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("按下空格开始游戏",300,350);
}
if (isFail){
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
g.drawString("游戏失败,按下空格重新开始",200,350);
}
}
//键盘监听事件
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获得键盘按键
if (keyCode==KeyEvent.VK_SPACE){
if (isFail){
//重新开始
isFail=false;
timer.start();
init();
}else {
isStart = !isStart;
if (!isStart) timer.stop();
else timer.start();
repaint();
}
}
if (keyCode == KeyEvent.VK_W&&!olddirection.equals("D")){
direction="U";
}else if (keyCode == KeyEvent.VK_S&&!olddirection.equals("U")){
direction="D";
}else if (keyCode == KeyEvent.VK_A&&!olddirection.equals("R")){
direction="L";
}else if (keyCode == KeyEvent.VK_D&&!olddirection.equals("L")){
direction="R";
}
}
//监听事件--需要通过固定事件来刷新,1s=10次
@Override
public void actionPerformed(ActionEvent e) {
if (isStart&&!isFail){//如果游戏状态为开始,就让小蛇动起来
//吃食物
if (snakeX[0]==foodX&&snakeY[0]==foodY){
length+=1;//长度+1
//再次生成食物
foodX = 25+25*random.nextInt(34);
foodY = 75+25*random.nextInt(24);
for (int i = 1; i < length; i++) {
if (foodX==snakeX[i]&&foodY==snakeY[i]) {
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 (Objects.equals(direction, "U")){
snakeY[0]-=25;//将小蛇的头向右移动一格
olddirection="U";
if (snakeY[0]<75) snakeY[0]=650;
}else if (Objects.equals(direction, "D")){
snakeY[0]+=25;//将小蛇的头向右移动一格
olddirection="D";
if (snakeY[0]>650) snakeY[0]=75;
}else if (Objects.equals(direction, "L")){
snakeX[0]-=25;//将小蛇的头向右移动一格
olddirection="L";
if (snakeX[0]<25) snakeX[0]=850;
}else if (Objects.equals(direction, "R")){
snakeX[0]+=25;//将小蛇的头向右移动一格
olddirection="R";
if (snakeX[0]>850) snakeX[0]=25;
}
//失败判断,撞到自己就算失败
for (int i = 1; i < length; i++) {
if (snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]) {
isFail=true;
timer.stop();
}
}
repaint();//重新绘制界面
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
header.png
body.png
food.png
up.png
down.png
left.png
right.png
2021.11.22
不在能够后退,食物不会再生成到自己身上。