JAVA-飛行機製作大戦で出会った問題
233059 ワード
JAVA-飛行機製作大戦で出会った問題問題 1.点滅 解決方法 .弾丸は多発できない 解決方法 全コード 1.メインメニュー .モード選択メニュー 3.ゲームメインウィンドウ 4.飛行機類 5.弾丸類 6.爆発類 7.決済画面 8.工具類 9.遊び方窓 今学期javaは最後にプログラムを編ませて、初めてこんなにたくさんの行のプログラムを書いて、収穫が大きいです.
最初のブログを書いて、これからよく書いて、自分が直面した問題と解決方法を記録します.
に質問
1.点滅
解決策
1.デュアルバッファを使用するが効果がない2.JPanelクラスを継承し成功
2.弾丸は多発しない
解決策
弾丸配列を作成し、発火関数で飛行機クラスに書き、すべての絵がゲームメインウィンドウクラスのpaintに移動します.
すべてのコード
1.メインメニュー
2.モード選択メニュー
3.ゲームメインウィンドウ
4.飛行機類
5.弾丸類
6.爆発類
7.決済画面
8.工具類
9.遊び方ウィンドウ
最初のブログを書いて、これからよく書いて、自分が直面した問題と解決方法を記録します.
に質問
1.点滅
解決策
1.デュアルバッファを使用するが効果がない2.JPanelクラスを継承し成功
2.弾丸は多発しない
解決策
弾丸配列を作成し、発火関数で飛行機クラスに書き、すべての絵がゲームメインウィンドウクラスのpaintに移動します.
すべてのコード
1.メインメニュー
package com.lhyltzj.www;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
* ,
*/
public class Welcome extends JFrame{
Image background2=GameUtile.getImage("image/menuback.png");
public Welcome() {
//
this.setSize(800,600);
//
this.setTitle(" ");
//
this.setLocationRelativeTo(null);
//
this.setVisible(true);
JButton startGame=new JButton(" ");
JButton infoGame=new JButton(" ");
JButton endGame=new JButton(" ");
startGame.setBounds(350, 290, 100, 50);
infoGame.setBounds(350, 350, 100, 50);
endGame.setBounds(350, 410, 100, 50);
this.add(startGame);
this.add(infoGame);
this.add(endGame);
this.setLayout(null);
ImageIcon img1=new ImageIcon("src/image/menuback.png");
JLabel la3=new JLabel(img1);
la3.setBounds(0,0,800,600);
this.getLayeredPane().add(la3,new Integer(Integer.MIN_VALUE));
getContentPane().add(la3);
this.setResizable(false);
startGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/* ,
*
*/
dispose();
ChooseModel cm=new ChooseModel();
}
});
infoGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/*
* ,
*/
InfoFrame infoframe=new InfoFrame();
}
});
endGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dispose();
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Welcome welcome=new Welcome();
// GameWindow gw=new GameWindow();
// gw.MyWindow();
//gw.setVisible(true);
}
}
2.モード選択メニュー
package com.lhyltzj.www;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
*
*
*/
public class ChooseModel extends JFrame{
public ChooseModel(){
this.setSize( 500, 400);
this.setTitle(" ");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setLayout(null);
JButton normal=new JButton(" ");
JButton endless=new JButton(" ");
normal.setBounds(200, 100, 100, 50);
endless.setBounds(200, 200, 100, 50);
this.add(normal);
this.add(endless);
ImageIcon img1=new ImageIcon("src/image/chooseback.png");
JLabel la3=new JLabel(img1);
la3.setBounds(0,0,700,800);
this.getLayeredPane().add(la3,new Integer(Integer.MIN_VALUE));
getContentPane().add(la3);
this.setResizable(false);
this.setLayout(null);
normal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/*
*
* false
*/
dispose();
GameWindow gw=new GameWindow();
gw.MyWindow(false);
}
});
endless.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/*
*
* ture
*/
dispose();
GameWindow gw=new GameWindow();
gw.MyWindow(true);
}
});
}
}
3.ゲームメインウィンドウ
package com.lhyltzj.www;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*
*
*/
public class GameWindow extends JPanel {
// JFrame
JFrame frame=new JFrame("Thunder");
int j=4;
// ,ture
public boolean GAMEOVER;
// ,ture
public boolean VECTORY;
//
boolean model;
//
int level=1;
//
int timeNum=0;
//
public int power=0;
//
static int x=250;
static int y=600;
//
public static final int GAME_WIDTH=600;
public static final int GAME_HEIGHT=750;
//
int score=0;
// int enemy_x=110;
// int enemy_y=-20;
//
List<Bullet> bulletarr=new ArrayList<Bullet>();
// List enemybulletarr=new ArrayList();
// ,BOSS,
List<Plane> enemyarr=new ArrayList<Plane>();
//
List<Bomb> bombs=new ArrayList<Bomb>();
//
Plane myplane=new Plane(x, y,true,Plane.Direction.STOP,this,100);
//
Image image=GameUtile.getImage("image/myplane.png");
Image background=GameUtile.getImage("image/background.png");
Image background2=GameUtile.getImage("image/background2.png");
// ,
int back_y=-background.getHeight(null)+background.getHeight(null);
//public GameWindow() {
public void MyWindow(boolean model){
this.setSize(GAME_WIDTH,GAME_HEIGHT);
this.setVisible(true);
this.model=model;
//System.out.println("dsdkskdj");
//GameWindow win=new GameWindow();
// GameWindow frame
frame.add(this);
//win.MyWindow();
//
frame.setSize(GAME_WIDTH,GAME_HEIGHT);
//frame.setAlwaysOnTop(true); //
//
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
frame.setLocationRelativeTo(null);
//
frame.addKeyListener(new KeyMonitor());
//
frame.setVisible(true);
//
frame.setResizable(false);
//
new Thread(new PaintThread()).start();
public void setLevel() {
//
if(model){
for(int i = 0; i < j; i++) {
this.enemyarr.add(new Plane(300+50*i, -200,false,Plane.Direction.D,this,20));
}
j++;
}
//
else{
switch (level) {
//
case 1:
// this.enemyarr.add(new Plane(110,-150,false,Plane.Direction.D,this,1000,true));
for(int i = 0; i < 2; i++) {
this.enemyarr.add(new Plane(300+50*i, -200,false,Plane.Direction.D,this,20));
}
break;
//
case 2:
this.enemyarr.add(new Plane(10, 30,false,Plane.Direction.D,this,20,false,true));
for(int i = 0; i < 5; i++) {
this.enemyarr.add(new Plane(-50+50*i, -50,false,Plane.Direction.D,this,20));
}
break;
//
case 3:
this.enemyarr.add(new Plane(300, 10,false,Plane.Direction.D,this,20,false,true));
for(int i = 0; i < 8; i++) {
this.enemyarr.add(new Plane(-50+50*i, -50,false,Plane.Direction.D,this,20));
}
break;
//
case 4:
this.enemyarr.add(new Plane(100, 200,false,Plane.Direction.D,this,20,false,true));
for(int i = 0; i < 11; i++) {
this.enemyarr.add(new Plane(-50+50*i, -50,false,Plane.Direction.D,this,20));
}
break;
// (BOSS)
case 5:
this.enemyarr.add(new Plane(300, 100,false,Plane.Direction.D,this,20,false,true));
this.enemyarr.add(new Plane(110,-150,false,Plane.Direction.D,this,1000,true));
break;
default:
break;
}
}
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
// return
if(GAMEOVER||VECTORY) {
//myplane.setBlood(100);
return;
}
//
g.drawImage(background, 0,back_y++, null);
g.drawImage(background, 0, back_y-background.getHeight(null), null);
if(back_y==background.getHeight(null)) {
back_y=0;
}
//g.drawImage(background2, 0, back_y+GAME_HEIGHT-background2.getHeight(null), null);
//
myplane.draw(g);
//myplane.hitPlanes(enemyarr);
//
timeNum++;
if(timeNum>0&&timeNum<=20)
{
if(level!=1) {
if(!model) {
g.setFont(new Font(" ",Font.BOLD,100));
g.drawString(" "+(level-1)+" ",50,GAME_HEIGHT/2);
}
}
}
else if(timeNum>20) {
if (enemyarr.size() <= 0) {
setLevel();
level++;
timeNum=0;
}
}
// ,boss
for(int i=0;i<enemyarr.size();i++){
Plane eP=enemyarr.get(i);
//eP.hitPlanes(enemyarr);
eP.draw(g);
// boss, boss ,VECTORY true
if(eP.isBoss()&&!eP.isAlive()){
System.out.println("++++++++++++++++++++++Vectory+++++++++++++++++++");
g.setFont(new Font(" ",Font.BOLD,100));
g.drawString(" VECTORY!",50,GAME_HEIGHT/2);
VECTORY=true;
}
}
// enemyplane.draw(g);
// enemyplane02.draw(g);
//e.draw(g);
//
for(int i=0;i<bombs.size();i++) {
Bomb b=bombs.get(i);
b.draw(g);
}
//buff.draw(g);
//
for(int i=0;i<bulletarr.size();i++){
Bullet tB=bulletarr.get(i);
tB.draw(g);
tB.hitPlane(myplane);
tB.hitPlanes(enemyarr);
}
//
g.setFont(new Font(" ",Font.BOLD,20));
//g.drawString("missiles \t count:"+bulletarr.size(), 10, 50);
//g.drawString("enemy \t count:"+enemyarr.size(), 10, 60);
g.drawString("Score:"+score, 10, 70);
g.drawString("HP "+myplane.getBlood(), 0, GAME_HEIGHT-45);
g.drawString(" "+power, GAME_WIDTH-150, GAME_HEIGHT-45);
// 0,GAMEOVER true
if(myplane.getBlood()<=0) {
g.setFont(new Font(" ",Font.BOLD,100));
g.drawString("GAME OVER",50,GAME_HEIGHT/2);
GAMEOVER=true;
//dispose();
}
}
// private Image offScreenImage=null;
// private Graphics offScreenGraphics;
private Image buffer;
@Override
public void update(Graphics g) {
// TODO Auto-generated method stub
buffer=createImage(getWidth(),getHeight());//
Graphics gBuffer=buffer.getGraphics();//
if(gBuffer!=null)
paint(gBuffer);
else
paint(g);
gBuffer.dispose();
g.drawImage(buffer, 0, 0,null);
}
//
public void Quit() {
//this.setVisible(false);
//System.exit(0);
frame.dispose();
ReturnFrame rf=new ReturnFrame(VECTORY,model);
}
//
public class PaintThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
// ,
while(!GAMEOVER&&!VECTORY) {
try {
Thread.sleep(50);
}catch(Exception e) {
e.printStackTrace();
}
repaint();
}
Quit();
}
}
//
public class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
super.keyPressed(e);
myplane.KeyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
super.keyReleased(e);
myplane.keyReleased(e);
}
}
}
4.飛行機類
package com.lhyltzj.www;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import com.lhyltzj.www.Plane.Direction;
public class Plane {
//
int x,y;
//
int i=0;
int j=0;
//
private boolean isAlive=true;
//
private boolean good;
//private int oldX, oldY;
//int kind;
//
boolean buff;
public boolean isAlive() {
return isAlive;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
//Vector bulletarr=new Vector();
//
public static final int XSPEED=10;
public static final int YSPEED=8;
// public static final int WIDTH=147;
// public static final int HEIGHT=104;
private boolean bL=false,bU=false,bR=false,bD=false;
//
enum Direction{L, LU, U, RU, R, RD, D, LD, STOP};
//
private Direction dir=Direction.STOP;
//
GameWindow gw;
//
private int blood;
// boss
private boolean boss;
public boolean isBoss() {
return boss;
}
public void setBoss(boolean boss) {
this.boss = boss;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
//
Random ran=new Random();
int ranNum=ran.nextInt(4);
private int step=ran.nextInt(12)+3;
Image enemy;
Image enemyA=GameUtile.getImage("image/enemyA.png");
Image enemyB=GameUtile.getImage("image/enemyB.png");
Image enemyC=GameUtile.getImage("image/enemyC.png");
Image enemyD=GameUtile.getImage("image/enemyD.png");
Image enemyboss=GameUtile.getImage("image/BOSS.png");
Image lifeBuff=GameUtile.getImage("image/lifebuff.png");
Image image=GameUtile.getImage("image/myplane.png");
Image img=GameUtile.getImage("image/bullet.png");
Image Eimg=GameUtile.getImage("image/enemybullet.png");
//
public void ranPlane() {
switch(ranNum) {
case 0:
enemy=enemyA;
break;
case 1:
enemy=enemyB;
break;
case 2:
enemy=enemyC;
break;
case 3:
enemy=enemyD;
break;
}
}
// public Plane(int x,int y,int kind,boolean buff) {
// this.x=x;
// this.y=y;
// this.kind=kind;
// this.buff=buff;
// }
public Plane(int x, int y,boolean good) {
super();
this.x = x;
this.y = y;
this.good=good;
}
public Plane(int x,int y,boolean good,Direction dir,GameWindow gw,int blood) {
this(x,y,good);
this.dir=dir;
this.gw=gw;
// this.oldX=x;//
// this.oldY=y;//
this.ranPlane();
this.blood=blood;
}
public Plane(int x,int y,boolean good,Direction dir,GameWindow gw,int blood,boolean boss) {
this(x,y,good,dir,gw,blood);
this.boss=boss;
}
public Plane(int x,int y,boolean good,Direction dir,GameWindow gw,int blood,boolean boss,boolean buff) {
this(x,y,good,dir,gw,blood,boss);
this.buff=buff;
}
public void draw(Graphics g){
//
if(buff) {
g.drawImage(lifeBuff, x, y, null);
//System.out.println("++++++++++++++++++++++++++++++");
}
if(!isAlive) {
// ,
if(!good) {
gw.enemyarr.remove(this);
}
return;
}
if(good&&!buff) {
//
Color c=g.getColor();
g.setColor(Color.red);
g.fillRect(0, gw.GAME_HEIGHT-60, this.getBlood(), 20);
g.setColor(c);
//
g.setColor(Color.blue);
g.fillRect(gw.GAME_WIDTH-gw.power, gw.GAME_HEIGHT-60, gw.GAME_WIDTH, 20);
g.setColor(c);
g.drawImage(image, x, y,null);
}
else if(!good&&!buff) {
if(this.isBoss()==true) {
g.drawImage(enemyboss, x, y,null);
//boss
Color c=g.getColor();
g.setColor(Color.red);
g.fillRect(0,0 , this.getBlood()*enemyboss.getWidth(null)/1000+1, 30);
g.setColor(c);
g.setFont(new Font(" ",Font.BOLD,20));
g.drawString("BOSS HP "+this.getBlood(), 0, 20);
//System.out.println("BOSS ");
}
else if(this.isBoss()==false) {
g.drawImage(enemy, x, y,null);
//
Color c=g.getColor();
g.setColor(Color.red);
g.fillRect(x,y-15 , this.getBlood()*enemy.getWidth(null)/20+1, 10);
g.setColor(c);
}
}
//System.out.println(" "+x+","+y);
move();
}
public void move() {
// TODO Auto-generated method stub
// this.oldX=x;//
// this.oldY=y;//
switch (dir) {
case L:
x-=XSPEED;
break;
case LU:
x-=XSPEED;
y-=YSPEED;
break;
case U:
y-=YSPEED;
break;
case RU:
x+=XSPEED;
y-=YSPEED;
break;
case R:
x+=XSPEED;
break;
case RD:
x+=XSPEED;
y+=YSPEED;
break;
case D:
y+=YSPEED;
break;
case LD:
x-=XSPEED;
y+=YSPEED;
break;
case STOP:
break;
}
//
if(!boss&&!good&&!buff) {
if(x<30){
x=30;
}
if(y<30){
y=30;
}
if(x+enemy.getWidth(null)>gw.GAME_WIDTH){
x=gw.GAME_WIDTH-enemy.getWidth(null);
}
if(y+enemy.getHeight(null)>gw.GAME_HEIGHT){
y=gw.GAME_HEIGHT-enemy.getHeight(null);
}
}
//
else if(good&&!buff) {
if(x<10){
x=10;
}
if(y<10){
y=10;
}
if(x+image.getWidth(null)>gw.GAME_WIDTH){
x=gw.GAME_WIDTH-image.getWidth(null);
}
if(y+image.getHeight(null)>gw.GAME_HEIGHT){
y=gw.GAME_HEIGHT-image.getHeight(null);
}
}
//
if(buff) {
y++;
//if(x)
if(x<0||y<0||x>GameWindow.GAME_WIDTH||y>GameWindow.GAME_HEIGHT){
isAlive=false;
//gw.arr.remove(this);
}
}
// boss,
if(!good&&!buff) {
if(boss) {
if(y>=0) {
// j++;
// if(j%2==0){
// x+=5;
// }
// else{
// x-=5;
// }
dir=Direction.STOP;
}
}
else if(!boss&&!buff) {
Direction[] dirs=Direction.values();
if(step==0) {
step=ran.nextInt(12)+3;
int ranMove=ran.nextInt(dirs.length);
dir=dirs[ranMove];
}
step--;
}
//
if(ran.nextInt(40)>38) {
if(boss) {
BossSkill();
}
else
this.fire();
}
}
}
//
private void locateDirection() {
// TODO Auto-generated method stub
if(bL&&!bU&&!bR&&!bD) dir=Direction.L;
if(bL&&bU&&!bR&&!bD) dir=Direction.LU;
if(!bL&&bU&&!bR&&!bD) dir=Direction.U;
if(!bL&&bU&&bR&&!bD) dir=Direction.RU;
if(!bL&&!bU&&bR&&!bD) dir=Direction.R;
if(!bL&&!bU&&bR&&bD) dir=Direction.RD;
if(!bL&&!bU&&!bR&&bD) dir=Direction.D;
if(bL&&!bU&&!bR&&bD) dir=Direction.LD;
if(!bL&&!bU&&!bR&&!bD) dir=Direction.STOP;
}
//
public void keyReleased(KeyEvent e) {
int key=e.getKeyCode();
if(key==KeyEvent.VK_UP) {
bU=false;
}
if(key==KeyEvent.VK_DOWN) {
bD=false;
}
if(key==KeyEvent.VK_LEFT) {
bL=false;
}
if(key==KeyEvent.VK_RIGHT) {
bR=false;
}
if(key==KeyEvent.VK_Z) {
fire();
}
locateDirection();
}
//
public void KeyPressed(KeyEvent e) {
int key=e.getKeyCode();
if(key==KeyEvent.VK_UP&&y>0) {
//System.out.println("dddd");
bU=true;
}
if(key==KeyEvent.VK_DOWN&&y<615) {
bD=true;
}
if(key==KeyEvent.VK_LEFT&&x>0) {
bL=true;
}
if(key==KeyEvent.VK_RIGHT&&x<480) {
bR=true;
}
if(key==KeyEvent.VK_X) {
//new Thread(new MyBullet()).start();
if(gw.power>=100) {
ultimateSkill();
gw.power=0;
}
}
locateDirection();
}
// private void stay(){ // , ,
// x=oldX;
// y=oldY;
// }
//
public Rectangle getRect(){
if(buff) {
return new Rectangle(x,y,lifeBuff.getWidth(null),lifeBuff.getHeight(null));
}
if(!good) {
if(boss) {
return new Rectangle(x,y,enemyboss.getWidth(null),enemyboss.getHeight(null));
}
return new Rectangle(x,y,enemy.getWidth(null),enemy.getHeight(null));
}
return new Rectangle(x,y,image.getWidth(null),image.getHeight(null));
}
//
public Bullet fire() {
int x = this.x + image.getWidth(null)/2 - img.getWidth(null)/2; //
if(!good) {
x = this.x + enemy.getWidth(null)/2 - img.getWidth(null)/2;
if(boss) {
x = this.x + enemyboss.getWidth(null)/2 - img.getWidth(null)/2;
int y=this.y+enemyboss.getHeight(null)/2-img.getHeight(null)/2;
}
}
//Bullet tB=new Bullet(x-image.getWidth(null)/2,y,dir.LU,good,this.gw,10);
Bullet ttB=new Bullet(x,y,dir,good,this.gw,good?10:5);
//Bullet tttB=new Bullet(x+image.getWidth(null)/2,y,dir.RU,good,this.gw,10);
//gw.bulletarr.add(tB);
gw.bulletarr.add(ttB);
//gw.bulletarr.add(tttB);
return ttB;
}
//
public Bullet ultimateSkill() {
int x = this.x + image.getWidth(null)/2 - img.getWidth(null)/2;
Bullet L1=new Bullet(x-image.getWidth(null)/2,y,dir.LU,good,this.gw,10);
Bullet L2=new Bullet(x,y,dir.LU,good,this.gw,10);
Bullet m=new Bullet(x,y,dir.U,good,this.gw,10);
Bullet R1=new Bullet(x+image.getWidth(null)/2,y,dir.RU,good,this.gw,10);
Bullet R2=new Bullet(x,y,dir.RU,good,this.gw,10);
m.XSPEED=0;
gw.bulletarr.add(m);
L1.XSPEED=5;
gw.bulletarr.add(L1);
L2.XSPEED=10;
gw.bulletarr.add(L2);
R1.XSPEED=5;
gw.bulletarr.add(R1);
R2.XSPEED=10;
gw.bulletarr.add(R2);
//tB.XSPEED=0;
//tB.YSPEED=speed;
return null;
}
//Boss
public Bullet BossSkill() {
int x = this.x + enemyboss.getWidth(null)/2 - Eimg.getWidth(null)/2;
Bullet L1=new Bullet(x-image.getWidth(null)/2,y,dir.LD,good,this.gw,10);
Bullet L2=new Bullet(x,y,dir.LD,good,this.gw,10);
Bullet m=new Bullet(x,y,dir.D,good,this.gw,10);
Bullet R1=new Bullet(x+image.getWidth(null)/2,y,dir.RD,good,this.gw,10);
Bullet R2=new Bullet(x,y,dir.RD,good,this.gw,10);
if(i%2==0) {
m.eXSPEED=0;
gw.bulletarr.add(m);
L1.eXSPEED=-1;
gw.bulletarr.add(L1);
L2.eXSPEED=-2;
gw.bulletarr.add(L2);
R1.eXSPEED=1;
gw.bulletarr.add(R1);
R2.eXSPEED=2;
gw.bulletarr.add(R2);
}
else {
m.eXSPEED=0;
gw.bulletarr.add(m);
L1.eXSPEED=0;
gw.bulletarr.add(L1);
L2.eXSPEED=0;
gw.bulletarr.add(L2);
R1.eXSPEED=0;
gw.bulletarr.add(R1);
R2.eXSPEED=0;
gw.bulletarr.add(R2);
}
i++;
return null;
}
}
5.弾丸類
package com.lhyltzj.www;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.List;
import com.lhyltzj.www.Plane.Direction;
/*
*
*
*
*/
public class Bullet {
int x,y;
private boolean isAlive=true;
public boolean isAlive() {
return isAlive;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
Image img=GameUtile.getImage("image/bullet.png");
Image Eimg=GameUtile.getImage("image/enemybullet.png");
Plane.Direction dir;
//
public static int XSPEED=0;
public static int YSPEED=50;
//
public static int eXSPEED=0;
public static final int eYSPEED=-7;
public int att;
private GameWindow gw;
private boolean good;
public Bullet(int x, int y, Plane.Direction dir) {
super();
this.x = x;
this.y = y;
this.dir = dir;
}
public Bullet(int x,int y ,Plane.Direction dir,boolean good,GameWindow gw,int att){
this(x,y,dir);
this.good=good;
this.gw=gw;
this.att=att;
//this.good=good;
}
public void draw(Graphics g) {
if(!isAlive) {
// ,
gw.bulletarr.remove(this);
return;
}
// if(isAlive==false){
// gw.bulletarr.remove(this);
// return;
// }
//g.drawImage(img, this.x+174, this.y, null);
//g.drawImage(img, this.x+87, this.y, null);
//System.out.println(" "+x+","+y);
if(good) {
//
g.drawImage(img, this.x, this.y, null);
//g.drawImage(img, this.x+50, this.y, null);
mymove();
}
if(!good) {
//
g.drawImage(Eimg, this.x, this.y, null);
enemymove();
}
}
//
void mymove() {
switch (dir) {
case L:
//x-=XSPEED;
y-=YSPEED;
break;
case LU:
x-=XSPEED;
//y-=YSPEED;
y-=YSPEED;
break;
case U:
y-=YSPEED;
break;
case RU:
x+=XSPEED;
//y-=YSPEED;
y-=YSPEED;
break;
case R:
//x+=XSPEED;
y-=YSPEED;
break;
case RD:
//x+=XSPEED;
//y+=YSPEED;
y-=YSPEED;
break;
case D:
//y+=YSPEED;
y-=YSPEED;
break;
case LD:
//x-=XSPEED;
//y+=YSPEED;
y-=YSPEED;
break;
case STOP:
y-=YSPEED;
break;
}
// ,
if(x<0||y<0||x>GameWindow.GAME_WIDTH||y>GameWindow.GAME_HEIGHT){
isAlive=false;
//gw.bulletarr.remove(this);
}
}
//
void enemymove() {
switch (dir) {
case L:
//x-=XSPEED;
y-=eYSPEED;
break;
case LU:
//x-=XSPEED;
//y-=YSPEED;
y-=eYSPEED;
break;
case U:
y-=eYSPEED;
break;
case RU:
//x+=XSPEED;
//y-=YSPEED;
y-=eYSPEED;
break;
case R:
//x+=XSPEED;
y-=eYSPEED;
break;
case RD:
x+=eXSPEED;
//y+=YSPEED;
y-=eYSPEED;
break;
case D:
//y+=YSPEED;
y-=eYSPEED;
break;
case LD:
x-=eXSPEED;
//y+=YSPEED;
y-=eYSPEED;
break;
case STOP:
y-=eYSPEED;
break;
}
if(x<0||y<0||x>GameWindow.GAME_WIDTH||y>GameWindow.GAME_HEIGHT){
isAlive=false;
//gw.bulletarr.remove(this);
}
}
//
public Rectangle getRect(){
if(!good) {
return new Rectangle(x,y,Eimg.getWidth(null),Eimg.getHeight(null));
}
return new Rectangle(x,y,img.getWidth(null),img.getHeight(null));
}
// public boolean hitBuff(Plane buff){
// if(this.good&&this.isAlive&&buff.buff&&this.getRect().intersects(buff.getRect())) {
// gw.myplane.setBlood(100);
// System.out.println("+++++++++++++++++++++++++++");
// return true;
// }
// return false;
// }
//
public boolean hitPlane(Plane p){
if(this.isAlive&&this.getRect().intersects(p.getRect())&&p.isAlive()&&this.good==true&&p.buff==true){
//
gw.myplane.setBlood(100);
System.out.println("++++++++++++++++++++++++++");
p.setAlive(false);
this.isAlive=false;
}
if(this.isAlive&&this.getRect().intersects(p.getRect())&&p.isAlive()&&this.good!=p.isGood()){
// boss
int nextBlood=p.getBlood()-this.att;
if(p.getBlood()>0) {
//
p.setBlood(nextBlood);
//
gw.power+=5;
this.isAlive=false;
}
else if(p.getBlood()<=0) {
//
//p.setPower(nextPower);
p.setAlive(false);
this.isAlive=false;
//
Bomb b=new Bomb(x,y,gw);
gw.bombs.add(b);
if(!p.isGood()) {
//
// ,
gw.score++;
gw.power+=10;
//p.buff=true;
}
}
return true;
}
return false;
}
// ,boss,
public boolean hitPlanes(List<Plane> enemyarr) {
for (int i = 0; i < enemyarr.size(); i++) {
if (hitPlane(enemyarr.get(i))) {
//gw.enemyarr.remove(i);
return true;
}
}
return false;
}
}
6.爆発類
package com.lhyltzj.www;
import java.awt.Color;
import java.awt.Graphics;
public class Bomb {
int x,y;
GameWindow gw;
private boolean isAlive=true;
int[] diameter=new int[]{4,7,12,18,26,32,49,30,14,6};
int step=0;
public Bomb(int x, int y, GameWindow gw) {
super();
this.x = x;
this.y = y;
this.gw = gw;
}
public void draw(Graphics g){
if(!isAlive) {
gw.bombs.remove(this);
return;
}
if(step==diameter.length){
isAlive=false;
step=0;
return;
}
Color c=g.getColor();
g.setColor(Color.orange);
g.fillOval(x, y, diameter[step], diameter[step]);
g.setColor(c);
step++;
}
}
7.決済画面
package com.lhyltzj.www;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class ReturnFrame extends JDialog{
//GameWindow gw;
boolean VECTORY;
//boolean model;
public ReturnFrame(boolean VECTORY,final boolean model) {
this.setSize(800,550);
this.setLocationRelativeTo(null);
this.setVisible(true);
JButton returnMenu=new JButton(" ");
JButton replay=new JButton(" ");
JButton endGame=new JButton(" ");
returnMenu.setBounds(600, 290, 100, 50);
replay.setBounds(600, 350, 100, 50);
endGame.setBounds(600, 410, 100, 50);
this.add(returnMenu);
this.add(replay);
this.add(endGame);
this.setLayout(null);
//this.model=model;
ImageIcon img1;
if(VECTORY) {
this.setTitle(" ");
img1=new ImageIcon("src/image/returnV.png");
}
else {
this.setTitle(" ");
img1=new ImageIcon("src/image/returnD.png");
}
JLabel la3=new JLabel(img1);
la3.setBounds(0,0,800,550);
this.getLayeredPane().add(la3,new Integer(Integer.MIN_VALUE));
getContentPane().add(la3);
this.setResizable(false);
//this.gw=gw;
returnMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//gw.dispose();
//gw.frame.dispose();
dispose();
new Welcome();
}
});
replay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dispose();
GameWindow gw=new GameWindow();
gw.MyWindow(model);
}
});
endGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dispose();
}
});
}
}
8.工具類
package com.lhyltzj.www;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
/*
*
* Image
*/
public class GameUtile {
public static Image getImage(String path) {
URL url=GameUtile.class.getClassLoader().getResource(path);
BufferedImage image=null;
try {
image=ImageIO.read(url);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return image;
}
}
9.遊び方ウィンドウ
package com.lhyltzj.www;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class InfoFrame extends JFrame{
public InfoFrame(){
this.setSize( 300, 600);
this.setTitle(" ");
this.setVisible(true);
this.setLocationRelativeTo(null);
JLabel text1=new JLabel(" , , , , ");
JLabel text2=new JLabel(" 5 , BOSS");
JLabel text3=new JLabel(" 100, HP");
JLabel text4=new JLabel(" , ");
JLabel text5=new JLabel(" , ");
JLabel text6=new JLabel(" 100 , X ");
this.add(text1);
this.add(text2);
this.add(text3);
this.add(text4);
this.add(text5);
this.add(text6);
this.setLayout(new GridLayout(6,1));
}
}