⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamecanvas.java

📁 用动态数组实现子弹的一个例子,值得参考.
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import java.util.Vector;
import java.io.IOException;
public class gamecanvas  extends Canvas  implements Runnable 
{
	private Image bufferimg;//缓冲
	private Graphics gg;//画笔	
	public int MILLIS_PER_TICK;                               
	boolean sleeping;                           
	gui ccc;                               
	public gamecanvas(gui ci)            
	{                                                   
		ccc = ci;
		bufferimg = Image.createImage(176,208);
		gg = bufferimg.getGraphics();
		init();
	}                                               	
	
	public void start()                                 
	{                                                   
		setFullScreenMode(true);                          		                                 
		sleeping = false;   
		MILLIS_PER_TICK = 33;           
		Thread t = new Thread(this);                    
		t.start();                                                
	}                                                
	   
	   
	public void stop()                                  
	{                                                   
		sleeping = true;                                
	} 
	
	public void run()                                   
	{ 
		try
		{         
			while(!sleeping)                                
			{     
				long startTime = System.currentTimeMillis(); 
				allgo();     
				long timeTake = System.currentTimeMillis() - startTime;
				if (timeTake < MILLIS_PER_TICK) 
				{
					synchronized (this) {
								wait(MILLIS_PER_TICK - timeTake);
								}
				} 
				else 
				{
					Thread.yield();
				}	
			} 
		}catch(InterruptedException ie){}                                      
	}
	
	public synchronized void allgo()
	{
		go();
		draw(gg);
		repaint();
	}
	
	public void go() //游戏逻辑
	{
		addbullet(bullet);
		bullet_go();
	}
	
	public void bullet_go() //子弹飞
	{
		for(int i=0;i<bullet.size();i++)
		{
			bullet b  = (bullet)bullet.elementAt(i);
			b.move();
			checkbullet(bullet,b);	
		}
	}
	
	public void addbullet(Vector bullet)  //添加子弹
	{
		if(System.currentTimeMillis() - current_bulletge >=bullet_ge)
		{
			current_bulletge = System.currentTimeMillis();
			fire = true;
			if(++sort > 1)
				sort = 0;	
		}
		if(fire)
		{
			if(sort == 0)
			{
				if(System.currentTimeMillis()-current_xiaobulletge >=bullet_1ge)
				{
					if(++current_num > bullet_1num)
					{
						current_num = 0;
						fire = false;
						current_xiaobulletge = 0;	
					}
					else
					{
						addbullet_1(bullet);
						current_xiaobulletge = System.currentTimeMillis();	
					}	
				}
			}
			else if(sort == 1)
			{
				if(System.currentTimeMillis()-current_xiaobulletge >=bullet_2ge)
				{
					if(++current_num > bullet_2num)
					{
						current_num = 0;
						fire = false;
						current_xiaobulletge = 0;	
					}
					else
					{
						addbullet_2(bullet);
						current_xiaobulletge = System.currentTimeMillis();	
					}	
				}	
			}
		}	
	}
	
	public void checkbullet(Vector bullet,bullet b)  //检测子弹是否飞出屏幕
	{
		if(b.positionx > 180 || b.positionx < -4 || b.positiony > 212 || b.positiony < -5)
		{
			bullet.removeElement(b);	
		}	
	}
	
	public synchronized void keyPressed(int keyCode)  //按下
	{
		 
	}
	
	public  synchronized void keyReleased(int keyCode) //松开
	{
		 
	}
	
	public void paint(Graphics g)
	{
		g.drawImage(bufferimg,0,0,20); 
	}
	
	public void draw(Graphics g)
	{
		g.setColor(0xffff00);
		g.fillRect(0,0,bufferimg.getWidth(),bufferimg.getHeight());
		g.drawImage(boss,bosspositionx,bosspositiony,20);
		for(int i=0;i<bullet.size();i++)
		{
			bullet b  = (bullet)bullet.elementAt(i);
			b.draw(g);	
		}
	}
	
	private Vector bullet; 
	private Image bullet_1;
	private Image bullet_2;
	private Image boss;
	private final int bosspositionx = 50;
	private final int bosspositiony = 50;
	private final int bullet_ge = 3000;//发子弹时间间隔
	private final int bullet_1ge = 300;//子弹1的时间间隔
	private final int bullet_1num = 3;//子弹1的发次数
	private final int bullet_2ge = 300;//子弹2的时间间隔
	private final int bullet_2num = 5;//子弹2的发次数
	private boolean fire;//是否发子弹
	private int sort;//发哪种子弹
	private long current_bulletge;
	private long current_xiaobulletge;
	private int current_num;
	public void init()
	{
		try
		{
			bullet_1 = Image.createImage("/1.png");
			bullet_2 = Image.createImage("/2.png");
			boss = Image.createImage("/3.png");
		}catch(IOException e){}	
		bullet = new Vector();
	}
	
	
	public void addbullet_1(Vector bullet)   //弹幕1
	{
		int centerx = bosspositionx+boss.getWidth()/2;  //boss中心位置
		int centery = bosspositiony+boss.getHeight()/2;; //boss中心位置
		//r = 10;//半径为10的圆
		//speed = 5;//速度为5
		int position = 10;
		int xieposition = 14;
		int speed = 2;
		int xiespeed = 1;
		bullet.addElement(new bullet(bullet_1, 4, 5, centerx, centery - position, 0, -speed));
		bullet.addElement(new bullet(bullet_1,4,5,centerx+xieposition,centery-xieposition,xiespeed,-xiespeed)); //3是2分之根2再除以2再乘以5的近似值
		bullet.addElement(new bullet(bullet_1,4,5,centerx+position,centery,speed,0));
		bullet.addElement(new bullet(bullet_1,4,5,centerx+xieposition,centery+xieposition,xiespeed,xiespeed)); 
		bullet.addElement(new bullet(bullet_1,4,5,centerx,centery+position,0,speed)); 
		bullet.addElement(new bullet(bullet_1,4,5,centerx-xieposition,centery+xieposition,-xiespeed,xiespeed)); 
		bullet.addElement(new bullet(bullet_1,4,5,centerx-position,centery,-speed,0));
		bullet.addElement(new bullet(bullet_1,4,5,centerx-xieposition,centery-xieposition,-xiespeed,-xiespeed)); 
	}
	
	public void addbullet_2(Vector bullet)   //弹幕2
	{
		int startx1 = bosspositionx;  //boss 
		int starty1 = bosspositiony+boss.getHeight(); //boss 
		int startx2 = bosspositionx+boss.getWidth()-16;  //boss 
		int starty2 = bosspositiony+boss.getHeight(); //boss 
		int speed = 2; 
		bullet.addElement(new bullet(bullet_2,16,16,startx1,starty1,0,speed));
		bullet.addElement(new bullet(bullet_2,16,16,startx2,starty2,0,speed));
	}
	 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -