一、设置窗口,包括但不限于窗口标题、可见、窗口可关闭,固定大小、设置大小
package learn_snake; /* * @author MK * @date 2023年3月15日 */ import javax.swing.JFrame; import javax.swing.WindowConstants; public class SankeDemo { public static void main(String[] args) { JFrame frame = new JFrame(); //frame.add(frame, new SankePanel()); frame.add(new SankePanel() ); frame.setTitle("贪吃蛇大作战"); frame.setResizable(false); frame.setVisible(true); frame.setBounds(10,10,900,720); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
二、创建一个面板类
-
设置面板,颜色(black)
-
这个面板是画图面板,paintComponent
-
创建一个存放素材的文件夹
-
记住,所有的素材都是画上去的。
-
if判断蛇头的朝向
-
for循环添加蛇的身体
-
if 判断游戏的状态,并添加一个欢迎的字符串
package learn_snake; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Iterator; /* * @author MK * @date 2023年3月15日 */ import javax.swing.JPanel; import javax.xml.crypto.Data; public class SankePanel extends JPanel { // int length; int snakeX[] = new int[600]; int snakeY[] = new int[500]; String fx;//蛇头的朝向 boolean isStart; //构造器 public SankePanel() { // TODO Auto-generated constructor stub init(); } //蛇的初始位置 //x坐标和y坐标 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; } @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g);//清屏 this.setBackground(Color.white); //画上头部广告区 Date.header.paintIcon(this, g, 25, 11); g.fillRect(25,75,850,600); //画上蛇 //Date.body.paintIcon(this, g, snakeX[0], snakeY[0]); //用for循环进行蛇身体的添加 for (int i = 1; i < length; i++) { Date.body.paintIcon(this, g, snakeX[i], snakeY[i]); } //用if判断蛇头的朝向 if (fx.equals("r")) { Date.right.paintIcon(this, g, snakeX[0], snakeY[0]); }else if (fx.equals("l")) { Date.left.paintIcon(this, g, snakeX[0], snakeY[0]); }else if (fx.equals("u")) { Date.up.paintIcon(this, g, snakeX[0], snakeY[0]); }else if (fx.equals("d")) { Date.down.paintIcon(this, g, snakeX[0], snakeY[0]); } //用if判断游戏的状态 if (isStart==false) { g.setColor(Color.white); g.setFont(new Font("微软雅黑",Font.BOLD,40)); g.drawString("按下空格开始游戏",300,300); } } }
三、创建数据类
-
定义出窗口的头部图片(用来存放数据),数据中心(data类)
-
定义出蛇的身体图片
package learn_snake; /* * @author MK * @date 2023年3月16日 */ import java.net.URL; import javax.swing.ImageIcon; import javax.xml.crypto.Data; public class Date { //画上头部 public static URL headerUrl = Data.class.getResource("/learn_snake/header.png"); public static ImageIcon header = new ImageIcon(headerUrl); //画身体 public static URL bodyUrl = Data.class.getResource("/learn_snake/body.png"); public static ImageIcon body = new ImageIcon(bodyUrl); public static URL upUrl = Data.class.getResource("/learn_snake/up.png"); public static ImageIcon up = new ImageIcon(upUrl); public static URL downUrl = Data.class.getResource("/learn_snake/down.png"); public static ImageIcon down = new ImageIcon(downUrl); public static URL leftUrl = Data.class.getResource("/learn_snake/left.png"); public static ImageIcon left = new ImageIcon(leftUrl); public static URL rightUrl = Data.class.getResource("/learn_snake/right.png"); public static ImageIcon right = new ImageIcon(rightUrl); }
总结:
标签:ImageIcon,Java,一之,snakeX,贪吃蛇,static,import,new,public From: https://www.cnblogs.com/mkdance/p/17227434.html