?? gamecanvas.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 + -