2 u012374530 u012374530 于 2013.10.09 00:06 提问

急求java高手,帮忙看下面的游戏代码,为何只能运行界面类然后其他都看不见也不能玩

//子弹Bullet类
package hy;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

public class Bullet {
//坐标
public int x,y;
//子弹大小
public int width,height;
//子弹杀伤力
public int kill;
//速度
public int speed;
//子弹图片
public String img;
//是否存活
public boolean isLive=true;
//子弹类型(好坏)
public boolean isGood;

public void drawBullet(Graphics g)
{
    Toolkit tk=Toolkit.getDefaultToolkit();
    Image img1=tk.getImage(Bullet.class.getClassLoader().getResource("hy.image/"+img));
    //画图
    g.drawImage(img1, x,y,width,height,null);
}

}
//Person 类
package hy;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.tools.Tool;

public class Person {
//敌机坐标
private int x,y;
private int width,height;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getKill() {
return kill;
}
public void setKill(int kill) {
this.kill = kill;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
//血量
private int hp;
public Person(int x, int y, int width, int height, int hp, int kill,
String imgurl, int speed) {
super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.hp = hp;
this.kill = kill;
this.imgurl = imgurl;
this.speed = speed;
}
//杀伤力
private int kill;
//定义图片路径
private String imgurl;
//是否存活
public boolean isLive=true;
//速度
private int speed;
public void drawPerson(Graphics g)
{
//定义系统工具类
Toolkit tk=Toolkit.getDefaultToolkit();
//创建图片对象
Image img=tk.getImage(Person.class.getClassLoader().getResource("hy.image/"+imgurl));
g.drawImage(img,x,y,width,height,null);
move();

}
public void move()
{
    y+=speed;
    if(y>=MyFrame.HEIGHT)
    {
        isLive=false;
    }
    //捡查子弹是否出屏幕
    for (int i = 0; i < MyFrame.bulletAll.size(); i++) {
        if(MyFrame.bulletAll.get(i).y<0)
        {
            MyFrame.bulletAll.get(i).isLive=false;
        }
    }
    this.hitPerson();
    this.hitBullet();
}
//反回当前敌机的矩形
public Rectangle getPersonRectangle()
{
    return new Rectangle(x,y,width,height);
}
//判断相撞
public void hitPerson()
{
    //得到敌机矩形
    Rectangle personRec=this.getPersonRectangle();
    //得到主机矩形
    Rectangle myRec=new Rectangle(MyFrame.my_x,MyFrame.my_y,55,60);
    if(myRec.intersects(personRec)==true)
    {
        this.isLive=false;
        //产生爆炸效果
        Explode e=new Explode(x,y);
        //将e放入爆炸效果集合
        MyFrame.explodeAll.add(e);
    }
}
//判断和玩家子弹的相撞
public void hitBullet()
{
    //得到敌机矩形
    Rectangle personRec=this.getPersonRectangle();
    //得到子弹的矩形
    for (int i = 0; i < MyFrame.bulletAll.size(); i++) {
        Rectangle bulletRec=new Rectangle(MyFrame.bulletAll.get(i).x,MyFrame.bulletAll.get(i).y,20,20);
        if(personRec.intersects(bulletRec)==true)
        {
            MyFrame.score+=10;
            this.isLive=false;
            //产生爆炸效果
            Explode e=new Explode(x,y);
            //将e放入爆炸效果集合
            MyFrame.explodeAll.add(e);
        }
    }

}

}
//MyFrame界面类
package hy;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;

public class MyFrame extends Frame {
public static final int WIDTH=900;
public static final int HEIGHT=700;
public static int my_x=450;
public static int my_y=600;
//分数
public static int score=0;

//定义变量,专门表示键盘按下的键
public static boolean a=false;public static boolean s=false;
public static boolean d=false;public static boolean w=false;
public static boolean j=false;
//敌机的集合
public ArrayList<Person> personAll=new ArrayList<Person>();
//存放爆炸效果的集合
public static ArrayList<Explode> explodeAll=new ArrayList<Explode>();
//存放子弹的集合
public static ArrayList<Bullet> bulletAll=new ArrayList<Bullet>();
public MyFrame()
{
    this.setTitle("雷电飞机射击"+score);
    this.setSize(WIDTH,HEIGHT);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.addWindowListener(new MyWindowClose());
    //添加键盘监听器
    this.addKeyListener(new MyKeyDown());
    this.setVisible(true);
    MyThread t=new MyThread();
    t.start();

}
//定义背景Y座标
int bg_y=0;
@Override
public void paint(Graphics g) {
    Toolkit tk=Toolkit.getDefaultToolkit();
    Image bg_img=tk.getImage(MyFrame.class.getClassLoader()
            .getResource("hy.image/bg2.jpg"));
    g.drawImage(bg_img, 0, bg_y, WIDTH, HEIGHT, null);
    bg_y+=10;
    g.drawImage(bg_img, 0, -HEIGHT+bg_y, WIDTH, HEIGHT, null);
    //判断bg_y的值是否超过窗体的高度
    if(bg_y>HEIGHT)
    {
        bg_y=0;
    }
    Image my_img=tk.getImage(MyFrame.class.getClassLoader()
            .getResource("hy.image/my_img.jpg"));
    g.drawImage(my_img, my_x, my_y, 55, 60, null);
    //创建随机对象
    Random rd=new Random();
    if(rd.nextInt(10)==5)
    {
        //创建敌机
        Person person=new Person(rd.nextInt(850), 45, 32, 58, 100, 100, "enemy1.jpg", 15);
        personAll.add(person);

    }
    for (int i = 0; i < personAll.size(); i++) {
        Person person=personAll.get(i);
        if(person.isLive)
        {
            person.drawPerson(g);
        }
        else
            personAll.remove(person);
    }
    //绘画爆炸
    for (int i = 0; i < explodeAll.size(); i++) {
        Explode e=explodeAll.get(i);
        //判断是否是活字弹
        if(e.isLive)
        {
            e.drawExplode(g);
        }
        else {
            explodeAll.remove(e);
        }
    }
    //绘画子弹
    for (int i = 0; i < bulletAll.size(); i++) {
        Bullet bullet=bulletAll.get(i);
        //判断是否是活子弹
        if(bullet.isLive)
        {
            bullet.drawBullet(g);
        }
        else {
            bulletAll.remove(bullet);
        }
    }
    this.setTitle("雷电飞机射击游戏                            分数:"+score);
}
public void move()
{
    if(a)
        MyFrame.my_x-=20;
    if(d)
        MyFrame.my_x+=20;
    if(s)
        MyFrame.my_y+=20;
    if(w)
        MyFrame.my_y-=20;
    if(j)
    {
        Fire();
    }
}
//开火方法
public void Fire()
{
    Bullet bullet=new Bullet();
    bullet.x=my_x+22;
    bullet.y=my_y+30;
    bullet.img="bullet.jpg";
    bullet.speed=40;
    bullet.width=20;
    bullet.height=20;
    bulletAll.add(bullet);
}
Image img=null;
@Override
public void update(Graphics g) {
    if(img==null)
    {
        img=this.createImage(WIDTH, HEIGHT);
    }   
    //利用img创建虚拟画笔
    Graphics gb=img.getGraphics();
    //调用paint方法
    paint(gb);
    //利用真实的画笔g来画图片
    g.drawImage(img, 0,0,WIDTH,HEIGHT,null);
}
public static void main(String[] args) {
    new MyFrame();
}
//内部类,线程类,用来刷新当前窗体S
class MyThread extends Thread
{
    @Override
    public void run() {
        while(true)
        {
            repaint();
            //每时每刻都判断是否要移动飞机 
            move();
            try {
                //让子弹前进,速度要比玩家速度快很多
                for (int i = 0; i < bulletAll.size(); i++) {
                    Bullet bullet=bulletAll.get(i);
                    bullet.y-=bullet.speed;
                }
                Thread.sleep(65);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }           
    }
}

}
//MyWindowClose类
package hy;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyWindowClose extends WindowAdapter

{

@Override 

 public void windowClosing(WindowEvent e) {  

     System.exit(0);  

}  

}

class MyKeyDown extends KeyAdapter

{

 @Override 

public void keyPressed(KeyEvent e) {  

     switch (e.getKeyCode()) {  

    case KeyEvent.VK_A:  

         MyFrame.a=true;           

         break;  

    case KeyEvent.VK_S:  

         MyFrame.s=true;  

         break;  

     case KeyEvent.VK_D:  

         MyFrame.d=true;  

         break;  

    case KeyEvent.VK_W:  

        MyFrame.w=true;  

         break;  

     case KeyEvent.VK_J:  

        MyFrame.j=true;  

         break;  

    default:  

         break;  

     }  

 }  

 @Override 

public void keyReleased(KeyEvent e) {  

    switch (e.getKeyCode()) {  

    case KeyEvent.VK_A:  

        MyFrame.a=false;              

         break;  

    case KeyEvent.VK_S:  

        MyFrame.s=false;  

        break;  

    case KeyEvent.VK_D:  

         MyFrame.d=false;  

         break;  

    case KeyEvent.VK_W:  

         MyFrame.w=false;  

         break;  

     case KeyEvent.VK_J:  

        MyFrame.j=false;  

         break;  

     default:  

         break;  

    }  

}  

}
//Explode爆炸类
package hy;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

public class Explode {
//坐标
private int x;
private int y;
//定义爆炸显示的图片
String img[]={"boom1.jpg","boom2.jpg","boom3.jpg","boom4.jpg","boom5.jpg","boom6.jpg","boom7.jpg","boom8.jpg","boom9.jpg"};
//爆炸的生命
public boolean isLive=true;
//定义下标
private int index=0;
public Explode(int x, int y) {
this.x = x;
this.y = y;
index++;
}
public void drawExplode(Graphics g)
{
//得到工具类
Toolkit tkToolkit=Toolkit.getDefaultToolkit();
//得到图片对象
//得到数组中的图片
String image=img[index];
Image img1=tkToolkit.getImage(Explode.class.getClassLoader().getResource("hy.image/"+image));
g.drawImage(img1, x,y,null);
index++;
if(index>=img.length)
{
index=0;
//生命消失
isLive=false;
}
}
}
//Sound背景音乐类
package hy;
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import javax.swing.JFrame;
import java.net.MalformedURLException;
import java.net.URL;
public class Sound extends JFrame{
public Sound(){
super();
}
public static void main(String args[]) {
try {
URL cb;
File f = new File("hy.music\t.wav"); //引号里面的是音乐文件所在的绝对路径
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
//aau.play();
aau.loop();
//循环播放 aau.play() 单曲 aau.stop()停止播放
Sound frame=new Sound();

//frame.setBounds(0, 0, 300, 200);
//frame.setVisible(true);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
}

1个回答

bailin710258
bailin710258   2013.10.09 08:54

每当在面板上新增或者移除组件后都要重画面板

bailin710258
bailin710258 在面板上新增或者移除组件后都要 repaint()函数吧。 太久没用了记不清,你可以在网上找java坦克大战的源码,参考。
大约 4 年之前 回复
u012374530
u012374530 是要再添加代码吗。可以帮下我吗
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片