import java.util.*; import java.net.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.*; public class UsagiShoot { // public static SoundSource shotSound = null; // public static SoundSource explodeSound = null; public static final int SCREENW = 640; public static final int SCREENH = 480; public static final int ENEMY_EMARGE_PERIOD = 50; public static final float USAGI_VELOCITY = 3; public static final int ENEMY_SHOOT_WAIT = 50; public static final float ENEMY_BULLET_VELOCITY = 3; public static VirtualKey upKey = new VirtualKey(); public static VirtualKey downKey = new VirtualKey(); public static VirtualKey leftKey = new VirtualKey(); public static VirtualKey rightKey = new VirtualKey(); public static VirtualKey shootKey = new VirtualKey(); public static ArrayList spriteList = new ArrayList(); public static Usagi usagi = null; public static UsagiBullet tamas[] = { new UsagiBullet(), new UsagiBullet(), new UsagiBullet(),new UsagiBullet(), new UsagiBullet(), new UsagiBullet(), new UsagiBullet(), new UsagiBullet(), new UsagiBullet(),new UsagiBullet(), new UsagiBullet(), new UsagiBullet(), new UsagiBullet(), new UsagiBullet(), new UsagiBullet(),new UsagiBullet(), new UsagiBullet(), new UsagiBullet() }; public static Enemy enemies[] = { new Enemy(), new Enemy(), new Enemy(), new Enemy(), new Enemy(), new Enemy(), new Enemy(), new Enemy(), new Enemy(), new Enemy() }; public static EnemyBullet enemybullets[] = { new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet(), new EnemyBullet() }; public static Explosion explosions[] = { new Explosion(), new Explosion(), new Explosion(), new Explosion(), new Explosion() }; static JPanel usaPanel = null; static JPanel getPanel() { if(usaPanel==null) { usaPanel = new UsaPanel(); } return usaPanel; } public static void main(String args[]) { new UsaFrame().setVisible(true); } public static int poissonRand(int lambda) { int x; double v, vlimit; x = 0; v = 1; vlimit = Math.exp(-lambda); while(true) { v = v * Math.random(); if(v < vlimit) break; x = x + 1; } return x; } } class UsaFrame extends JFrame implements KeyListener { public UsaFrame() { setTitle("UsagiShoot"); getContentPane().add(UsagiShoot.getPanel()); setDefaultCloseOperation(EXIT_ON_CLOSE); addKeyListener(this); pack(); new Thread((Runnable)UsagiShoot.getPanel()).start(); } // KeyListener public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) UsagiShoot.leftKey.down(); if(e.getKeyCode()==KeyEvent.VK_RIGHT) UsagiShoot.rightKey.down(); if(e.getKeyCode()==KeyEvent.VK_UP) UsagiShoot.upKey.down(); if(e.getKeyCode()==KeyEvent.VK_DOWN) UsagiShoot.downKey.down(); if(e.getKeyCode()==KeyEvent.VK_SPACE) UsagiShoot.shootKey.down(); } public void keyReleased(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_LEFT) UsagiShoot.leftKey.up(); if(e.getKeyCode()==KeyEvent.VK_RIGHT) UsagiShoot.rightKey.up(); if(e.getKeyCode()==KeyEvent.VK_UP) UsagiShoot.upKey.up(); if(e.getKeyCode()==KeyEvent.VK_DOWN) UsagiShoot.downKey.up(); if(e.getKeyCode()==KeyEvent.VK_SPACE) UsagiShoot.shootKey.up(); } } class UsaPanel extends JPanel implements Runnable { public int waitCount = 0; public UsaPanel() { setSize(UsagiShoot.SCREENW, UsagiShoot.SCREENH); init(); } Dimension size = null; public Dimension getPreferredSize() { if(size==null) { size = new Dimension(UsagiShoot.SCREENW, UsagiShoot.SCREENH); } return size; } public Dimension getMinimumSize() { return getPreferredSize(); } public void init() { // UsagiShoot.shotSound = new SoundSource("data/tama1.wav"); // UsagiShoot.explodeSound = new SoundSource("data/bom34.wav"); waitCount = UsagiShoot.poissonRand(UsagiShoot.ENEMY_EMARGE_PERIOD); UsagiShoot.usagi = new Usagi(); UsagiShoot.usagi.loadImage("data/small_usagi2.gif"); UsagiShoot.usagi.centerImage(); UsagiShoot.usagi.x = UsagiShoot.SCREENW / 4; UsagiShoot.usagi.y = UsagiShoot.SCREENH / 2; Option opt = new Option(); opt.setTarget(UsagiShoot.usagi); opt.loadImage("data/star.gif"); opt.centerImage(); Option opt2 = new Option(); opt2.setTarget(opt); opt2.loadImage("data/star.gif"); opt2.centerImage(); for(int i=0 ; i x || this.x+20 < x || this.y-10 > y || this.y+30 < y) { return false; } return true; } public void explode() { active = false; // UsagiShoot.explodeSound.play(); for(int i=0 ; i UsagiShoot.SCREENW) active = false; // 衝突判定 // 敵全部 for(int i=0 ; i UsagiShoot.SCREENW || x < 0) active = false; if(y > UsagiShoot.SCREENH || y < 0) active = false; // 衝突判定 // うさぎ if(UsagiShoot.usagi.intersected(x, y)) { UsagiShoot.usagi.explode(); active = false; } } } public void draw(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1)); if(active) super.draw(g); } public boolean shoot(float x, float y, float vx, float vy) { if(active) return false; this.x = x; this.y = y; velX = vx; velY = vy; active = true; return true; } } class Option extends Sprite { Sprite target = null; float targetPrevPosX=0; float targetPrevPosY=0; float[] xs = new float[15]; float[] ys = new float[15]; int traceCount = 0; boolean moveFlag = false; public void setTarget(Sprite sprite) { target = sprite; for(int i=0 ; i0) { shootWaitCount--; if(shootWaitCount==0) { float vx; float vy; vx = (UsagiShoot.usagi.x - x); vy = (UsagiShoot.usagi.y - y); double distance = Math.sqrt(vx*vx+vy*vy); vx/=distance; vy/=distance; vx*=UsagiShoot.ENEMY_BULLET_VELOCITY; vy*=UsagiShoot.ENEMY_BULLET_VELOCITY; for(int i=0 ; i x || this.x+30 < x || this.y-30 > y || this.y+30 < y) { return false; } return true; } public void explode() { active = false; // UsagiShoot.explodeSound.play(); for(int i=0 ; i1) { t=1; phase = PHASE1; } color = new Color(1.f, (float)(1.5-t>1.0 ? 1.0 : 1.5-t), 0); break; case PHASE1: t -= 0.01; color = new Color(1.f, (float)(1.5-t>1.0 ? 1.0 : 1.5-t), (float)(1.5-t>1.0 ? 1.0 : 1.5-t)); if(t<0.5) { t=0; phase = PHASE2; waitcount = 0; } break; case PHASE2: break; } } public void phase0(Graphics g) { r = t*maxR; Graphics2D g2 = (Graphics2D)g; g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1)); g.setColor(color); g.fillOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r)); } public void phase1(Graphics g) { r = t*maxR; Graphics2D g2 = (Graphics2D)g; g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)t)); g.setColor(color); g.fillOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r)); } } class Sprite { private Image image = null; public float x = 0; public float y = 0; public float offsetX = 0; public float offsetY = 0; public void loadImage(String filename) { URL url = this.getClass().getResource("/" + filename); image = Toolkit.getDefaultToolkit().getImage(url); } public void centerImage() { if(image!=null) { int img_w, img_h; while(true) { img_w = image.getWidth(null); if(img_w!=-1) break; try { Thread.sleep(100); } catch(InterruptedException e) {} } while(true) { img_h = image.getHeight(null); if(img_h!=-1) break; try { Thread.sleep(100); } catch(InterruptedException e) {} } offsetX = (float)(-img_w/2); offsetY = (float)(-img_h/2); } } public void frameProc() {} public void draw(Graphics g) { g.drawImage(image, (int)(x+offsetX), (int)(y+offsetY), null); } } class VirtualKey { char prev = 0; char current = 0; public boolean isDown() { return current==1; } public boolean isUp() { return current==0; } public boolean hasDownEdge() { if(current!=prev && current==1) return true; return false; } public boolean hasUpEdge() { if(current!=prev && current==0) return true; return false; } public void down() { if(current==1) return; current = 1; } public void up() { current = 0; } public void update() { prev = current; } }