编程语言
首页 > 编程语言> > 分享一个坦克发射炮弹击中目标的程序

分享一个坦克发射炮弹击中目标的程序

作者:互联网

一共两辆坦克,发射炮弹会有声音

下面是源程序

package com.cn.testy;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.net.URI;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;
public class CSprjWilliam {

	public static void main(String args[]) {

		JFrame w = new JFrame();

		w.setSize(3000, 3000);
		MyPanel4 mp = new MyPanel4();
		w.add(mp);
		w.addKeyListener(mp);
		mp.addKeyListener(mp);
		Thread t = new Thread(mp);
		t.start();
		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		w.dispose();
		w.setVisible(true);

	}
}

class MyPanel4 extends JPanel implements Runnable, KeyListener {
	int a = 200, b = 0, x1=433, y1=80  ,x=0,y=0;   //x1,y1子弹的位置   x,y坦克位置
	boolean show = false;//子弹
	boolean show1 = true;// 目标
	boolean bShooted = false; //敌方坦克是否被击中
	URI uri;
	URL url; 
	File  file;


	public void paint(Graphics g) {
		super.paint(g);

		Image bgimage = Toolkit.getDefaultToolkit().getImage("src/battle.png");
		g.drawImage(bgimage, 0, 0, 2000, 1500, this);
		g.setColor(new Color(0, 0, 255));

		g.setColor(Color.GRAY);
		g.fillRect(a+50,b+ 0, 20, 30);// 第一辆的履带灰

		g.fillRect(a+82, b+0, 20, 30);
		g.fillRect(a+114, b+ 0, 20, 30);
		g.fillRect(a+146, b+0, 20, 30);
		g.fillRect(a+178, b+0, 20, 30);
		g.fillRect(a+210,b+ 0, 20, 30);
		g.fillRect(a+242,b+ 0, 20, 30);
		g.fillRoundRect(a+250,b+ 0, 30, 30, 15, 15);//第一辆的履带头尾部
		g.fillRoundRect(a+30, b+0, 30, 30,15,15);

		g.setColor(Color.black);
		g.fillRect(a+70, b+0, 12, 30);//第一辆坦克履带黑
		g.fillRect(a+102, b+0, 12, 30);
		g.fillRect(a+134, b+0, 12, 30);
		g.fillRect(a+166, b+0, 12, 30);
		g.fillRect(a+198, b+0, 12, 30);
		g.fillRect(a+230, b+0, 12, 30);

		g.setColor(Color.ORANGE);
		g.fillRect(a+50, b+150, 20, 30);//第一辆坦克履带黄
		g.fillRect(a+82, b+150, 20, 30);
		g.fillRect(a+114, b+150, 20, 30);
		g.fillRect(a+146, b+150, 20, 30);
		g.fillRect(a+178, b+150, 20, 30);
		g.fillRect(a+210,b+ 150, 20, 30);
		g.fillRect(a+242, b+150, 20, 30);

		g.fillRoundRect(a+30,b+150, 30, 30,15,15);
		g.fillRoundRect(a+250, b+150, 30, 30, 15, 15);

		g.setColor(Color.black);
		g.fillRect(a+70, b+150, 12, 30);
		g.fillRect(a+102, b+150, 12, 30);
		g.fillRect(a+134, b+150, 12, 30);
		g.fillRect(a+166, b+150, 12, 30);
		g.fillRect(a+198, b+150, 12, 30);
		g.fillRect(a+230, b+150, 12, 30);

		g.setColor(Color.DARK_GRAY);

		g.fillRoundRect(a+55,b+ 15, 220, 150, 130, 130);
		g.fillRect(a+145, b+80, 300,30 );
		g.fillOval(a+438,b+80 , 30, 30);
		{if (!bShooted && Math.abs(1600-x1)<=90 && Math.abs((y+250)-y1)<=20 * 14) {
			bShooted = true;
			//show1=false;
			show = false; 
			x1=433+a;
			y1=80+b;  //子弹重新置位
			if (show =false) {
				super.paint(g);
				Image cgimage = Toolkit.getDefaultToolkit().getImage("src/test.png");
				g.drawImage(cgimage, 30, 30, 300, 3000, this);

			}
		}

		if (show1 ==true) {
			// 目标
			y+=1;
			g.setColor(Color.RED);
			g.fillRect(x+1600, y+50, 30, 20);//第二辆坦克
			g.fillRect(x+1600, y+82, 30, 20);
			g.fillRect(x+1600, y+114, 30, 20);
			g.fillRect(x+1600, y+146, 30, 20);
			g.fillRect(x+1600, y+178, 30, 20);
			g.fillRect(x+1600, y+210, 30, 20);
			g.fillRect(x+1600, y+242, 30, 20);
			g.fillRoundRect(x+1600,y+250, 30, 30, 15, 15);//第二辆坦克履带头尾部
			g.fillRoundRect(x+1600, y+30, 30, 30,15,15);
			g.fillRect(x+1600, y+70, 30, 12);//第二辆坦克履带
			g.fillRect(x+1600, y+102, 30, 12);
			g.fillRect(x+1600, y+134, 30, 12);
			g.fillRect(x+1600, y+166, 30, 12);
			g.fillRect(x+1600, y+198, 30, 12);
			g.fillRect(x+1600, y+230, 30, 12);
			g.setColor(Color.gray);
			g.fillRect(x+1750, y+50, 30, 20);
			g.fillRect(x+1750, y+82, 30, 20);
			g.fillRect(x+1750, y+114, 30, 20);
			g.fillRect(x+1750, y+146, 30, 20);
			g.fillRect(x+1750, y+178, 30, 20);
			g.fillRect(x+1750, y+210, 30, 20);
			g.fillRect(x+1750, y+242, 30, 20);
			g.fillRoundRect(x+1750,y+30, 30, 30,15,15);
			g.fillRoundRect(x+1750, y+250, 30, 30, 15, 15);


			g.setColor(Color.black);

			g.fillRect(x+1750, y+70, 30, 12);
			g.fillRect(x+1750, y+102, 30, 12);
			g.fillRect(x+1750, y+134, 30, 12);
			g.fillRect(x+1750, y+166, 30, 12);
			g.fillRect(x+1750, y+198, 30, 12);
			g.fillRect(x+1750, y+230, 30, 12);
			g.fillRect(x+1600, y+70, 30, 12);
			g.fillRect(x+1600, y+102, 30, 12);
			g.fillRect(x+1600, y+134, 30, 12);
			g.fillRect(x+1600, y+166, 30, 12);
			g.fillRect(x+1600, y+198, 30, 12);
			g.fillRect(x+1600, y+230, 30, 12);


			g.setColor(Color.DARK_GRAY);
			g.fillRoundRect(x+1615,y+ 55, 150, 220, 130, 130);
			g.fillRect(x+1680, y+145, 30,300 );

			g.fillOval(x
					+1680,y+438 , 30,30);

		}
		}


		if (show == true) {
			g.setColor(new Color(0, 0, 255));
			g.fillOval(x1, y1, 300, 30);//子弹
			x1+=50;			
		}

	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(10);

			} catch (Exception e) {
			}
			repaint();
		}
	}

	public void keyTyped(KeyEvent e) {
	}
	public void sound(){
		try{

          //加载路径
			file=new File("C:/Users/Administrator/Desktop/5972.wav");
			//  uri = file.toURI();
			url =file.toURL(); //解析路径
			AudioClip clip;
			clip = Applet.newAudioClip(url);
			clip.play();
			//aau.loop();
			//aau.stop();
		}catch (Exception e) {
			// TODO: handle exception
		}

	}


	public void keyPressed(KeyEvent e) {

		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			a -=40;
			x1-=40;
		}

		if (e.getKeyCode() == KeyEvent.VK_UP) {
			b -=40;
			y1-=40;
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			a +=40;
			x1+=40;
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			b +=40;
			y1+=40;
		}

		if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
			bShooted = false;
			show1 = true;
			y = 0;
		}

		repaint();
		if (e.getKeyCode() == KeyEvent.VK_SPACE) {
			show = true;
			x1=433+a;
			y1=80+b;
			sound();
		}}


	public void keyReleased(KeyEvent e) {
	}
}

zhupengqq1 发布了70 篇原创文章 · 获赞 1 · 访问量 420 私信 关注

标签:150,12,20,fillRect,30,击中目标,炮弹,import,分享
来源: https://blog.csdn.net/zhupengqq1/article/details/103954099