javaweb项目笔记一:贪吃蛇游戏

源码

1.存放数据模块

package com.tanglei.snake;

import javax.swing.*;
import java.net.URL;

/**
 * @author tanglei
 * @create 2021-05-08 14:56
 * 存放外部数据
 */
public class Data {
          
   
    //头部的图片 URL:定位图片地址
    public static URL headURL = Data.class.getResource("/statics/header.png");
    public static ImageIcon header =new ImageIcon(headURL);
    //
    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);

}

2.贪吃蛇核心模块

3.主函数模块代码

package com.tanglei.snake;

import javax.swing.*;

/**
 * @author tanglei
 * @create 2021-05-08 14:42
 */
public class StartGames {
          
   
    public static void main(String[] args) {
          
   
        //1.绘制窗口用 JFrame类
        JFrame frame = new JFrame("tl-贪吃蛇小游戏");
        frame.setBounds(10,10,900,720);//界面大小
        frame.setResizable(false);//窗口大小不能改变
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭事件
        //2.面板JPanel可以加入到jframe
        frame.add(new GamePanel());
        frame.setVisible(true);//窗口是否展现出来
    }
}

4.说明

经验分享 程序员 微信小程序 职场和发展