Java GUI编程贪吃蛇小游戏实现1 界面设计

GUI编程贪吃蛇小游戏实现

界面设计

  • 如果页面生成的帧的时间片足够晓,就是动画的效果。
  • 连起来就是动画,拆开就是静态的图片。
  • 需要的技术:键盘监听+定时器

  • 页面素材类
public class Data {
    private static URL headerURL=Data.class.getResource("static/header.png");
    public static ImageIcon header=new ImageIcon(headerURL);

    private static URL upURL=Data.class.getResource("static/up.png");
    private static URL downURL=Data.class.getResource("static/down.png");
    private static URL leftURL=Data.class.getResource("static/left.png");
    private static URL rightURL=Data.class.getResource("static/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);

    private static URL bodyURL=Data.class.getResource("static/body.png");
    public static ImageIcon body=new ImageIcon(bodyURL);

    private static URL foodURL=Data.class.getResource("static/food.png");
    public static ImageIcon food=new ImageIcon(foodURL);
}

  • 游戏的面板
public class GamePanel extends JPanel {

    @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);//默认的游戏界面
    }
}

  • 游戏的主启动类
public class StartGame {
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        frame.setBounds(10,10,900,720);
        frame.setResizable(false);//窗口大小不可变
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        frame.add(new GamePanel());
    }
}
原创文章 31 获赞 31 访问量 835

猜你喜欢

转载自blog.csdn.net/caixuanji/article/details/106122330