推箱子(1)——Java

第一部分:实现小人出现在画布上。

1.构建窗口
新建class-GameFrame,构建窗体。

构造窗体:构造方法,每一个类都有自己的构造方法,没有返回值,也不能写void,方法名与类名一致。
设置窗口的名字、尺寸、关闭方法、窗口的可见度。

	GamePanel gp; 
	public GameFrame(){
    	 this.setSize(500,500);
    	 gp=new  GamePanel();
     	 this.add(gp);
    	 this.setTitle("推箱子");
    	 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setVisible(true);
     }

2.新建Run调用GrameFrame
3.构造画布GamePanel
将画布添加到窗口
public GamePanel(){
		player=new GamePlayer(200,200);
}

public void paint(Graphics g){
		player.render(g);
}
4.构造小人
绘制小人,渲染到屏幕上
移动,设置小人坐标


//绘制自己 渲染到屏幕上
public void render(Graphics g){
       g.drawImage(image,x,y,null);
}

//移动
public void movePlayer(int dx,int dy){
	   x=x+dx;
	   y=y+dy;        

}
5.运行Run即出现小人在画布上。
GameFrame gf=new GameFrame();



猜你喜欢

转载自blog.csdn.net/qinran_585/article/details/80027388