• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

verynice

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

java雷电游戏

[文件] Bullet.java ~ 649B    下载(221)

01 package com.ruxia.frame;
02  
03 import java.awt.Graphics;
04 import java.awt.Image;
05 import java.awt.Toolkit;
06  
07 public class Bullet {
08     //坐标
09     public int x,y;
10     //子弹大小
11     public int width,height;
12     //子弹杀伤力
13     public int kill;
14     //速度
15     public int speed;
16     //子弹图片
17     public String img;
18     //是否存活
19     public boolean isLive=true;
20     //子弹类型(好坏)
21     public boolean isGood;
22      
23     public void drawBullet(Graphics g)
24     {
25         Toolkit tk=Toolkit.getDefaultToolkit();
26         Image img1=tk.getImage(Bullet.class.getClassLoader().getResource("image/"+img));
27         //画图
28         g.drawImage(img1, x,y,width,height,null);
29     }
30 }

[文件] Explode.java ~ 921B    下载(114)

01 package com.ruxia.frame;
02  
03 import java.awt.Graphics;
04 import java.awt.Image;
05 import java.awt.Toolkit;
06  
07 public class Explode {
08    //坐标
09     private int x;
10     private int y;
11     //定义爆炸显示的图片
12     String img[]={"boom1.jpg","boom2.jpg","boom3.jpg","boom4.jpg","boom5.jpg","boom6.jpg","boom7.jpg","boom8.jpg","boom9.jpg"};
13     //爆炸的生命
14     public boolean isLive=true;
15     //定义下标
16     private int index=0;
17     public Explode(int x, int y) {
18         this.x = x;
19         this.y = y;
20     }
21     public void drawExplode(Graphics g)
22     {
23         //得到工具类
24         Toolkit tkToolkit=Toolkit.getDefaultToolkit();
25         //得到图片对象
26         //得到数组中的图片
27         String image=img[index];
28         Image img1=tkToolkit.getImage(Explode.class.getClassLoader().getResource("image/"+image));
29         g.drawImage(img1, x,y,null);
30         index++;
31         if(index>=img.length)
32         {
33             index=0;
34             //生命消失
35             isLive=false;
36         }
37     }
38 }

[文件] MyFrame.java ~ 6KB    下载(116)

001 package com.ruxia.frame;
002  
003 import java.awt.Frame;
004 import java.awt.Graphics;
005 import java.awt.Image;
006 import java.awt.Toolkit;
007 import java.awt.event.KeyAdapter;
008 import java.awt.event.KeyEvent;
009 import java.awt.event.WindowAdapter;
010 import java.awt.event.WindowEvent;
011 import java.util.ArrayList;
012 import java.util.Random;
013  
014 import javax.swing.JFrame;
015  
016 public class MyFrame extends Frame {
017     public static final  int WIDTH=900;
018     public static final  int HEIGHT=700;
019     public static int my_x=450;
020     public static int my_y=600;
021     //分数
022     public static int score=0;
023      
024     //定义变量,专门表示键盘按下的键
025     public static boolean a=false;public static boolean s=false;public staticboolean d=false;public static boolean w=false;public static boolean j=false;
026     //敌机的集合
027     public ArrayList<Person> personAll=new ArrayList<Person>();
028     //存放爆炸效果的集合
029     public static ArrayList<Explode> explodeAll=new ArrayList<Explode>();
030     //存放子弹的集合
031     public static ArrayList<Bullet> bulletAll=new ArrayList<Bullet>();
032     public MyFrame()
033     {
034         this.setTitle("雷电"+score);
035         this.setSize(WIDTH,HEIGHT);
036         this.setLocationRelativeTo(null);
037         this.setResizable(false);
038         this.addWindowListener(new MyWindowClose());
039         //添加键盘监听器
040         this.addKeyListener(new MyKeyDown());
041         this.setVisible(true);
042         MyThread t=new MyThread();
043         t.start();
044          
045     }
046     //定义背景Y座标
047     int bg_y=0;
048     @Override
049     public void paint(Graphics g) {
050         Toolkit tk=Toolkit.getDefaultToolkit();
051         Image bg_img=tk.getImage(MyFrame.class.getClassLoader().getResource("image/bg2.jpg"));
052         g.drawImage(bg_img, 0, bg_y, WIDTH, HEIGHT, null);
053         bg_y+=10;
054         g.drawImage(bg_img, 0, -HEIGHT+bg_y, WIDTH, HEIGHT, null);
055         //判断bg_y的值是否超过窗体的高度
056         if(bg_y>HEIGHT)
057         {
058             bg_y=0;
059         }
060         Image my_img=tk.getImage(MyFrame.class.getClassLoader().getResource("image/my_img.jpg"));
061         g.drawImage(my_img, my_x, my_y, 55, 60, null);
062         //创建随机对象
063         Random rd=new Random();
064         if(rd.nextInt(10)==5)
065         {
066             //创建敌机
067             Person person=new Person(rd.nextInt(850), 45, 32, 58, 100, 100,"enemy1.jpg", 15);
068             personAll.add(person);
069              
070         }
071         for (int i = 0; i < personAll.size(); i++) {
072             Person person=personAll.get(i);
073             if(person.isLive)
074             {
075                 person.drawPerson(g);
076             }
077             else
078                 personAll.remove(person);
079         }
080         //绘画爆炸
081         for (int i = 0; i < explodeAll.size(); i++) {
082             Explode e=explodeAll.get(i);
083             //判断是否是活字弹
084             if(e.isLive)
085             {
086                 e.drawExplode(g);
087             }
088             else {
089                 explodeAll.remove(e);
090             }
091         }
092         //绘画子弹
093         for (int i = 0; i < bulletAll.size(); i++) {
094             Bullet bullet=bulletAll.get(i);
095             //判断是否是活子弹
096             if(bullet.isLive)
097             {
098                 bullet.drawBullet(g);
099             }
100             else {
101                 bulletAll.remove(bullet);
102             }
103         }
104         this.setTitle("雷电                            分数:"+score);
105     }
106     public void move()
107     {
108         if(a)
109             MyFrame.my_x-=20;
110         if(d)
111             MyFrame.my_x+=20;
112         if(s)
113             MyFrame.my_y+=20;
114         if(w)
115             MyFrame.my_y-=20;
116         if(j)
117         {
118             Fire();
119         }
120     }
121     //开火方法
122     public void Fire()
123     {
124         Bullet bullet=new Bullet();
125         bullet.x=my_x+22;
126         bullet.y=my_y+30;
127         bullet.img="bullet.jpg";
128         bullet.speed=40;
129         bullet.width=20;
130         bullet.height=20;
131         bulletAll.add(bullet);
132     }
133     Image img=null;
134     @Override
135     public void update(Graphics g) {
136         if(img==null)
137         {
138             img=this.createImage(WIDTH, HEIGHT);
139         }  
140         //利用img创建虚拟画笔
141         Graphics gb=img.getGraphics();
142         //调用paint方法
143         paint(gb);
144         //利用真实的画笔g来画图片
145         g.drawImage(img, 0,0,WIDTH,HEIGHT,null);
146     }
147     public static void main(String[] args) {
148         new MyFrame();
149     }
150     //内部类,线程类,用来刷新当前窗体S
151     class MyThread extends Thread
152     {
153         @Override
154         public void run() {
155             while(true)
156             {
157                 repaint();
158                 //每时每刻都判断是否要移动飞机
159                 move();
160                 try {
161                     //让子弹前进,速度要比玩家速度快很多
162                     for (int i = 0; i < bulletAll.size(); i++) {
163                         Bullet bullet=bulletAll.get(i);
164                         bullet.y-=bullet.speed;
165                     }
166                     Thread.sleep(65);
167                 } catch (InterruptedException e) {
168                     // TODO Auto-generated catch block
169                     e.printStackTrace();
170                 }
171                  
172             }          
173         }
174     }
175 }
176 //这个类的作用是关闭窗体
177 class MyWindowClose extends WindowAdapter
178 {
179     @Override
180     public void windowClosing(WindowEvent e) {
181         System.exit(0);
182     }
183 }
184 class MyKeyDown extends KeyAdapter
185 {
186     @Override
187     public void keyPressed(KeyEvent e) {
188         switch (e.getKeyCode()) {
189         case KeyEvent.VK_A:
190             MyFrame.a=true;        
191             break;
192         case KeyEvent.VK_S:
193             MyFrame.s=true;
194             break;
195         case KeyEvent.VK_D:
196             MyFrame.d=true;
197             break;
198         case KeyEvent.VK_W:
199             MyFrame.w=true;
200             break;
201         case KeyEvent.VK_J:
202             MyFrame.j=true;
203             break;
204         default:
205             break;
206         }
207     }
208     @Override
209     public void keyReleased(KeyEvent e) {
210         switch (e.getKeyCode()) {
211         case KeyEvent.VK_A:
212             MyFrame.a=false;           
213             break;
214         case KeyEvent.VK_S:
215             MyFrame.s=false;
216             break;
217         case KeyEvent.VK_D:
218             MyFrame.d=false;
219             break;
220         case KeyEvent.VK_W:
221             MyFrame.w=false;
222             break;
223         case KeyEvent.VK_J:
224             MyFrame.j=false;
225             break;
226         default:
227             break;
228         }
229     }
230 }

[文件] Person.java ~ 3KB    下载(111)

view source
print?
001 package com.ruxia.frame;
002  
003 import java.awt.Graphics;
004 import java.awt.Image;
005 import java.awt.Rectangle;
006 import java.awt.Toolkit;
007  
008 import javax.tools.Tool;
009  
010 public class Person {
011     //敌机坐标
012     private int x,y;
013     private int width,height;
014     public int getX() {
015         return x;
016     }
017     public void setX(int x) {
018         this.x = x;
019     }
020     public int getY() {
021         return y;
022     }
023     public void setY(int y) {
024         this.y = y;
025     }
026     public int getWidth() {
027         return width;
028     }
029     public void setWidth(int width) {
030         this.width = width;
031     }
032     public int getHeight() {
033         return height;
034     }
035     public void setHeight(int height) {
036         this.height = height;
037     }
038     public int getHp() {
039         return hp;
040     }
041     public void setHp(int hp) {
042         this.hp = hp;
043     }
044     public int getKill() {
045         return kill;
046     }
047     public void setKill(int kill) {
048         this.kill = kill;
049     }
050     public String getImgurl() {
051         return imgurl;
052     }
053     public void setImgurl(String imgurl) {
054         this.imgurl = imgurl;
055     }
056     public int getSpeed() {
057         return speed;
058     }
059     public void setSpeed(int speed) {
060         this.speed = speed;
061     }
062     //血量
063     private int hp;
064     public Person(int x, int y, int width, int height, int hp, int kill,
065             String imgurl, int speed) {
066         super();
067         this.x = x;
068         this.y = y;
069         this.width = width;
070         this.height = height;
071         this.hp = hp;
072         this.kill = kill;
073         this.imgurl = imgurl;
074         this.speed = speed;
075     }
076     //杀伤力
077     private int kill;
078     //定义图片路径
079     private String imgurl;
080     //是否存活
081     public  boolean isLive=true;
082     //速度
083     private int speed;
084     public void drawPerson(Graphics g)
085     {
086         //定义系统工具类
087         Toolkit tk=Toolkit.getDefaultToolkit();
088         //创建图片对象
089         Image img=tk.getImage(Person.class.getClassLoader().getResource("image/"+imgurl));
090         g.drawImage(img,x,y,width,height,null);
091         move();
092          
093     }
094     public void move()
095     {
096         y+=speed;
097         if(y>=MyFrame.HEIGHT)
098         {
099             isLive=false;
100         }
101         //捡查子弹是否出屏幕
102         for (int i = 0; i < MyFrame.bulletAll.size(); i++) {
103             if(MyFrame.bulletAll.get(i).y<0)
104             {
105                 MyFrame.bulletAll.get(i).isLive=false;
106             }
107         }
108         this.hitPerson();
109         this.hitBullet();
110     }
111     //反回当前敌机的矩形
112     public Rectangle getPersonRectangle()
113     {
114         return new Rectangle(x,y,width,height);
115     }
116     //判断相撞
117     public void hitPerson()
118     {
119         //得到敌机矩形
120         Rectangle personRec=this.getPersonRectangle();
121         //得到主机矩形
122         Rectangle myRec=new Rectangle(MyFrame.my_x,MyFrame.my_y,55,60);
123         if(myRec.intersects(personRec)==true)
124         {
125             this.isLive=false;
126             //产生爆炸效果
127             Explode e=new Explode(x,y);
128             //将e放入爆炸效果集合
129             MyFrame.explodeAll.add(e);
130         }
131     }
132     //判断和玩家子弹的相撞
133     public void hitBullet()
134     {
135         //得到敌机矩形
136         Rectangle personRec=this.getPersonRectangle();
137         //得到子弹的矩形
138         for (int i = 0; i < MyFrame.bulletAll.size(); i++) {
139             Rectangle bulletRec=newRectangle(MyFrame.bulletAll.get(i).x,MyFrame.bulletAll.get(i).y,20,20);
140             if(personRec.intersects(bulletRec)==true)
141             {
142                 MyFrame.score+=10;
143                 this.isLive=false;
144                 //产生爆炸效果
145                 Explode e=new Explode(x,y);
146                 //将e放入爆炸效果集合
147                 MyFrame.explodeAll.add(e);
148             }
149         }
150          
151     }
152 }

posted on 2012-12-04 11:03  verynice  阅读(696)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3