刺骨寒江合力托举老人上岸

刺骨寒江合力托举老人上岸,第1张

刺骨寒江合力托举老人上岸

主要对之前学习的面向对象的一些特性进行练习,涉及一些线程等知识就直接复制了。

1.目前不需要掌握的代码

1.1工具类

 public class GameUtil {
    // 工具类最好将构造器私有化。
    // 我把答案卸载注释里
    // a=$(cat 1.fna | grep -v '>' | grep -o 'A' | wc -l); t=$(cat 1.fna | grep -v '>' | grep -o 'T' | wc -l); g=$(cat 1.fna | grep -v '>' | grep -o 'G' | wc -l); c=$(cat 1.fna | grep -v '>' | grep -o 'C' | wc -l); gc=$(($g+$c)); atgc=$(($a+$t+$g+$c)); echo -n 1.fna:; echo "scale=4; $gc / $atgc" | bc; done
    private GameUtil() {
    } 
 
    
    
    public static Image getImage(String path) {
        BufferedImage bi = null;
        try {
            URL u = GameUtil.class.getClassLoader().getResource(path);
            bi = ImageIO.read(u);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bi;
    }
}

1.2动画轮播的类

// 第一个题第一问就是scp空格服务器地址冒号文件地址空格点(点就是下载到当前目录下)
public class Explode {
	double x, y;
	
	static Image[] imgs = new Image[16];
	static {
		for (int i = 0; i < 16; i++) {
			imgs[i] = GameUtil.getImage("images/explode/e" + (i + 1) + ".gif");
			imgs[i].getWidth(null);
		}
	}

	int count;

	public void draw(Graphics g) {
		if (count <= 15) {
			g.drawImage(imgs[count], (int) x, (int) y, null);
			count++;
		}
	}

	public Explode(double x, double y) {
		this.x = x;
		this.y = y;
	}
}

1.3键盘监听类,屏幕刷新的线程

//多线程
	class  PaintThread  extends  Thread  {
		@Override
		public void run() {
			while(true){
				repaint();		//重画
				
				try {
					Thread.sleep(40);   	//1s=1000ms
				} catch (InterruptedException e) {
					e.printStackTrace();
				}		
			}
		}
		
	}
	// ls [1-9]*.fna | while read line; do a=$(cat $line | grep -v '>' | grep -o 'A' | wc -l); t=$(cat $line | grep -v '>' | grep -o 'T' | wc -l); g=$(cat $line | grep -v '>' | grep -o 'G' | wc -l); c=$(cat $line | grep -v '>' | grep -o 'C' | wc -l); gc=$(($g+$c)); atgc=$(($a+$t+$g+$c)); echo -n $line:; echo "scale=4; $gc / $atgc" | bc; done

	//定义键盘监听的内部类
		class   KeyMonitor extends KeyAdapter  {

			@Override
			public void keyPressed(KeyEvent e) {
				plane.addDirection(e);
			}

			@Override
			public void keyReleased(KeyEvent e) {
				plane.minusDirection(e);
			}
			 
			
		}
		
		//双缓冲,降低屏幕的闪烁
		private Image offScreenImage = null;
		 
		public void update(Graphics g) {
		    if(offScreenImage == null)
		        offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGH);//这是游戏窗口的宽度和高度
		     
		    Graphics gOff = offScreenImage.getGraphics();
		    paint(gOff);
		    g.drawImage(offScreenImage, 0, 0, null);
		}

2.游戏物体

先设计一个物体类,其中包括了游戏中所有物体的一些共性,如位置,速度,大小以及检测物体碰撞的一个方法。之后的炮d,飞机等物体直接继承这个物体父类。

public class GameObject {
	Image img;
	double x, y;
	int speed;
	int width,height;
	
	
	public void drawSelf(Graphics g) {
		g.drawImage(img, (int)x, (int)y, null);
	}


	public GameObject(Image img, double x, double y, int speed, int width, int height) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.width = width;
		this.height = height;
	}


	public GameObject(Image img, double x, double y) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
	}
	
	public GameObject() {
		
	}
	
	
	
	public Rectangle getRect() {
		return new Rectangle((int)x, (int)y, width, height);
	}

}

之后是飞机类

public class Plane extends GameObject{

	
	boolean left,right,up,down;
	boolean live = true;
	
	public Plane(Image img,double x,double y) {
		this.img = img;
		this.x = x;
		this.y = y;
		this.speed = 5;
		this.width = img.getWidth(null);
		this.height = img.getHeight(null);

		
	}
	
	
	public void drawSelf(Graphics g) {
				
		if(live) {
			
			g.drawImage(img, (int)x, (int)y, null);

			
			if (left) {
				x-=speed;
			}
			if (right) {
				x+=speed;
			}
			if (up) {
				y-=speed;
			}
			if (down) {
				y+=speed;
			}
		}
		else {
			if (down||up||left||right) {
				live = !live;
			}
		}

	}
	
	
	//按键按下
	public void addDirection(KeyEvent e) {
		switch(e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			left = true;
			break;
		case KeyEvent.VK_RIGHT:
			right = true;
			break;
		case KeyEvent.VK_UP:
			up = true;
			break;
		case KeyEvent.VK_DOWN:
			down = true;
			break;	
		
		}
	}
	//按键抬起
	public void minusDirection(KeyEvent e) {
		switch(e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			left = false;
			break;
		case KeyEvent.VK_RIGHT:
			right = false;
			break;
		case KeyEvent.VK_UP:
			up = false;
			break;
		case KeyEvent.VK_DOWN:
			down = false;
			break;	
		
		}
	}

}

炮d类

public class Shell extends GameObject{
	double degree;
	
	public Shell() {
		x = 30;
		y = 30;
		width = 10;
		height = 10;
		speed = 3;
		
		degree = Math.random()*Math.PI*2;
	}
	
	public void drawSelf(Graphics g) {
		Color c = g.getColor();
		
		g.setColor(Color.YELLOW);
		g.fillOval((int)x, (int)y, width, height);
		
		//随机方向
		x += speed*Math.cos(degree);
		y += speed*Math.sin(degree);
		
		//回d
		if(x<0||x>Constant.GAME_WIDTH-width) {
			degree = Math.PI - degree;
		}
		
		if(y<30||y>Constant.GAME_HEIGH-height) {
			degree = - degree;
		}
		
		g.setColor(c);


	}
	
}

3.窗口类的设计

主要是两个方法,paint方法和窗口启动方法。

paint方法中画出背景图片,飞机,炮d和爆炸效果

public void paint(Graphics g) {		//自动被调用。  g相当于一只画笔
		g.drawImage(bg, 0, 0, null);
	    plane.drawSelf(g);
	    
	    for(int i = 0; i 

窗口启动方法中设置标题,窗口初始位置,启动重画的线程,键盘盘监听等

public  void  launchframe(){
		this.setTitle("Author is Chi");
		this.setVisible(true);
		this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGH);
		this.setLocation(100, 100);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		new PaintThread().start();	//启动重画窗口的线程
		addKeyListener(new KeyMonitor());
		
		for(int i = 0; i 

最后是主方法

public static void main(String[] args) {
		MyGameframe  f = new MyGameframe();
		f.launchframe();
	}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5624251.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存