刚学不久java,做了一个飞机大战的小小小小游戏,现在把这个思路总结以及代码分享出来。大佬别吐槽(emmmmmm .....
开发环境:jdk1.7
开发工具:eclipese
PlanelJPanel.java
1 package project02; 2 3 import java.awt.BasicStroke; 4 import java.awt.Color; 5 import java.awt.Cursor; 6 import java.awt.Font; 7 import java.awt.Graphics; 8 import java.awt.Graphics2D; 9 import java.awt.Image; 10 import java.awt.Shape; 11 import java.awt.Stroke; 12 import java.awt.event.MouseEvent; 13 import java.awt.event.MouseListener; 14 import java.awt.event.MouseMotionListener; 15 import java.awt.image.BufferedImage; 16 import java.io.File; 17 import java.io.IOException; 18 import java.math.MathContext; 19 import java.net.URL; 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.Iterator; 23 import java.util.List; 24 import java.util.ListIterator; 25 26 import javax.imageio.ImageIO; 27 import javax.swing.JPanel; 28 29 import sun.invoke.util.BytecodeName; 30 31 32 public class PlanelJPanel extends JPanel implements Runnable,MouseListener,MouseMotionListener{ 33 //定义一个图片对象 34 int zdx,zdy; 35 static Image startImg; 36 int cout=0; 37 int score=0; 38 //创建一个放飞机的数组 39 static BufferedImage p[]=new BufferedImage[2]; 40 static BufferedImage game[]=new BufferedImage[2]; 41 //定义图片的坐标 42 int x=0,y=0; 43 //飞机坐标 44 int px=100,py=100; 45 int pc=0; 46 //定义开始游戏的开关 ,当ck等于true表示没有开始游戏; 47 boolean ck=true; 48 //定义子弹图片 49 static Image bImg; 50 //定义奖励子弹 51 static Image award2; 52 static Image blast; 53 int awardx=100; 54 int awardy=100; 55 //定义及奖励的出现的时间; 56 int AwardTime=0; 57 //定义敌人飞机图片数组 58 static BufferedImage Dpanel[]=new BufferedImage[5]; 59 //定义敌人飞机坐标 60 int Djx,Djy=0; 61 List<Dpanel> Djs=new ArrayList<Dpanel>();//把敌人加到集合 62 List<Bullet> bullets=new ArrayList<Bullet>();//把子弹加到集合 63 //定义游戏暂停开关 64 boolean suspend=false; 65 boolean gz=false; 66 boolean tt=false; 67 boolean gameOver=false;//游戏结束按钮 68 //定义线程 69 Thread thread; 70 //静态代码块 71 static { 72 //加载图片 73 try { 74 startImg=ImageIO.read(new File("images/GameInterface/interface_1.png")); 75 p[0]=ImageIO.read(new File("images/1.png")); 76 p[1]=ImageIO.read(new File("images/2.png")); 77 bImg=ImageIO.read(new File("images/bullet/bullet_1.png")); 78 Dpanel[0]=ImageIO.read(new File("images/LittlePlane/plane2.png")); 79 } catch (IOException e) { 80 // TODO 自动生成的 catch 块 81 e.printStackTrace(); 82 } 83 } 84 //构造方法 85 public PlanelJPanel() { 86 addMouseMotionListener(this); 87 addMouseListener(this); 88 thread=new Thread(this); 89 } 90 //画布 91 public void paint(Graphics g) { 92 super.paint(g); 93 g.drawImage(startImg, x, y, null); 94 if(gz==true&&ck==false&&gameOver==false) { 95 Graphics2D g2=(Graphics2D)g; 96 g2.setFont(new Font("宋体", Font.BOLD, 50)); 97 g2.setColor(Color.red); 98 g2.drawString("游戏暂停", 100, 200); 99 } 100 if(gameOver==true&&ck==false) { 101 Graphics2D g2=(Graphics2D)g; 102 g2.setFont(new Font("宋体", Font.BOLD, 50)); 103 g2.setColor(Color.red); 104 g2.drawString("游戏结束!", 100, 200); 105 } 106 if(ck==false) { 107 pc=pc==0?1:0; 108 g.drawImage(p[pc], px, py, null); 109 Graphics2D g2=(Graphics2D)g; 110 g2.setFont(new Font("宋体", Font.BOLD, 20)); 111 g2.setColor(Color.red); 112 g2.drawString("游戏得分"+score, 20,20 ); 113 } 114 //画子弹的方法 115 for (int i = 0; i < bullets.size(); i++) { 116 Bullet bullet=bullets.get(i); 117 bullet.drawBullet(g); 118 //System.out.println(Djs.size()); 119 } 120 //画敌人飞机的方法 121 for (int i = 0; i < Djs.size(); i++) { 122 Dpanel dpanel=Djs.get(i); 123 dpanel.drawDpanel(g); 124 125 // repaint(); 126 } 127 } 128 //线程需要执行的方法 129 public void run() { 130 synchronized (this) { 131 while (true) { 132 cout++; 133 //AwardTime++; 134 awardy++; 135 if(gz) { 136 try { 137 wait(); 138 } catch (InterruptedException e) { 139 // TODO 自动生成的 catch 块 140 e.printStackTrace(); 141 } 142 } 143 if(gameOver) { 144 thread.stop(); 145 } 146 //创建敌人飞机,添加到集合 147 int Djx=(int)(Math.random()*300+50); 148 Dpanel dpanel0=new Dpanel(Dpanel[0], Djx, 0); 149 int Djx1=(int)(Math.random()*300+50); 150 Dpanel dpanel1=new Dpanel(Dpanel[1], Djx1, 0); 151 for (int i = 0; i < Djs.size(); i++) { 152 Dpanel panel=Djs.get(i); 153 panel.DpanelMove(); 154 } 155 if(cout%50==0) { 156 Djs.add(dpanel0); 157 } 158 if(cout%20==0) { 159 Djy+=50; 160 } 161 //创建子弹,并且添加到集合里 162 if(cout%20==0) { 163 zdx=px+p[pc].getWidth()/2-bImg.getWidth(null)/2; 164 zdy= py-p[pc].getHeight()/2+bImg.getHeight(null)/2; 165 Bullet bullet0=new Bullet(zdx,zdy, bImg, 0); 166 Bullet bullet1=new Bullet(zdx,zdy, bImg, 1); 167 Bullet bullet2=new Bullet(zdx, zdy, bImg, 2); 168 bullets.add(bullet0); 169 bullets.add(bullet1); 170 bullets.add(bullet2); 171 if(zdy<=0||zdx<=0||zdx>=400) { 172 bullets.remove(bullet0); 173 bullets.remove(bullet1); 174 bullets.remove(bullet2); 175 } 176 } 177 //飞机的移动 178 for (int i = 0; i <Djs.size(); i++) { 179 Dpanel dpanel=Djs.get(i); 180 } 181 //子弹的移动 182 for (int i = 0; i <bullets.size(); i++) { 183 Bullet bullet=bullets.get(i); 184 if(bullet.direction==0) { 185 bullet.moveBullet1(); 186 } 187 if(bullet.direction==1) { 188 bullet.moveBullet2(); 189 } 190 if(bullet.direction==2) { 191 bullet.moveBullet3(); 192 } 193 } 194 //冒泡遍历子弹和敌人 195 for (int i = 0; i < bullets.size(); i++) { 196 Bullet b1=bullets.get(i); 197 for (int j = 0; j < Djs.size(); j++) { 198 Dpanel b2=Djs.get(j); 199 Crash crash=new Crash(); 200 boolean f=crash.isCollsion(b1, b2); 201 if(f){ 202 score+=5; 203 bullets.remove(i); 204 Djs.remove(j); 205 } 206 } 207 } 208 //敌人和我方飞机的碰撞 209 210 for (int j = 0; j < Djs.size(); j++) { 211 Dpanel b2=Djs.get(j); 212 Crash crash=new Crash(); 213 boolean f1=crash.isCollsion1(px,py,60,80, b2); 214 if(f1){ 215 gameOver=true; 216 } 217 } 218 y++; 219 if(y==0) { 220 y=-5400; 221 } 222 try { 223 Thread.sleep(10); 224 } catch (InterruptedException e) { 225 e.printStackTrace(); 226 } 227 repaint(); 228 } 229 } 230 //定义俩个方法,一个是暂停的方法,一个唤醒的方法。 231 } 232 public synchronized void regame() { 233 notify(); 234 235 } 236 public void mouseDragged(MouseEvent e) { 237 238 } 239 public void mouseMoved(MouseEvent e) { 240 //当鼠标移动到开始游戏的区域 变动鼠标 241 if(gameOver==false){ 242 if(ck &&e.getX()>=130&&e.getX()<261&&e.getY()>391&&e.getY()<=430) { 243 setCursor(new Cursor(Cursor.HAND_CURSOR)); 244 }else if(ck==false){ 245 setCursor(new Cursor(Cursor.HAND_CURSOR)); 246 }else { 247 setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 248 } 249 px=e.getX()-p[pc].getWidth()/2; 250 py=e.getY()-p[pc].getHeight()/2; 251 if(ck==false&&px<=0) { 252 px=0; 253 } 254 if(ck==false&&py<=0) { 255 py=0; 256 } 257 if(ck==false&&px+p[pc].getWidth()>=this.getWidth()) { 258 px=this.getWidth()-p[pc].getWidth(); 259 } 260 if(ck==false&&py+p[pc].getHeight()>=this.getHeight()) { 261 py=this.getHeight()-p[pc].getHeight(); 262 } 263 } 264 265 } 266 267 @Override 268 public void mouseClicked(MouseEvent e) { 269 //鼠标点击开始游戏要进入游戏 270 if(ck && e.getModifiers()==e.BUTTON1_MASK&&e.getX()>=130&&e.getX()<261&&e.getY()>391&&e.getY()<=430) { 271 ck=false; 272 try { 273 startImg=ImageIO.read(new File("images/background/background_2.png")); 274 275 } catch (IOException e1) { 276 // TODO 自动生成的 catch 块 277 e1.printStackTrace(); 278 } 279 y=-5400; 280 // repaint(); 281 thread.start(); 282 } 283 } 284 285 @Override 286 public void mousePressed(MouseEvent e) { 287 // TODO 自动生成的方法存根 288 289 } 290 291 @Override 292 public void mouseReleased(MouseEvent e) { 293 // TODO 自动生成的方法存根 294 295 } 296 297 @Override 298 public void mouseEntered(MouseEvent e) { 299 // 进来重新开始线程 300 regame(); 301 gz=false; 302 } 303 304 @Override 305 public void mouseExited(MouseEvent e) { 306 // 出去窗口暂停线程 307 gz=true; 308 309 } 310 311 }
PlanelJFrame.java
1 package project02; 2 3 import java.awt.Image; 4 5 import javax.swing.JFrame; 6 7 public class PlanelJFrame extends JFrame{ 8 9 public PlanelJFrame() { 10 //设置窗体标题 11 this.setTitle("吃饺子的喵"); 12 //设置窗体的大小 13 this.setSize(400, 600); 14 //设置窗体居中 15 this.setLocationRelativeTo(null); 16 this.setResizable(false); 17 //this.setResizable(false); 18 //关联关闭按钮 19 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 21 //把画布类进行封装成对象 22 PlanelJPanel jpanel=new PlanelJPanel(); 23 this.add(jpanel); 24 25 //让窗体显示 26 this.setVisible(true); 27 } 28 29 public static void main(String[] args) { 30 PlanelJFrame frame=new PlanelJFrame(); 31 32 } 33 }
Crash.java
1 package project02; 2 3 public class Crash { 4 public boolean isCollsion(Bullet bullet,Dpanel dpanel){ //敌机和子弹的碰撞 5 //敌人 50*50 子弹--10*46 6 int x1=bullet.bx; 7 int x2=dpanel.Dx; 8 int y1=bullet.by; 9 int y2=dpanel.Dy; 10 int w1=10; 11 int h1=46; 12 int w2=50; 13 int h2=50; 14 if(x1>=x2&&x1>=x2+w2){ 15 return false; 16 }else if(x1<=x2&&x1+w1<=x2){ 17 return false; 18 }else if(y1>=y2&&y1>=y2+h2){ 19 return false; 20 }else if(y1<=y2&&y1+h1<=y2){ 21 return false; 22 } 23 24 return true; 25 } 26 public boolean isCollsion1(int x1,int y1,int w1,int h1,Dpanel dpanel){ //敌机和我方飞机的碰撞 27 28 int x2=dpanel.Dx; 29 int y2=dpanel.Dy; 30 int w2=50; 31 int h2=50; 32 if(x1>=x2&&x1>=x2+w2){ 33 return false; 34 }else if(x1<=x2&&x1+w1<=x2){ 35 return false; 36 }else if(y1>=y2&&y1>=y2+h2){ 37 return false; 38 }else if(y1<=y2&&y1+h1<=y2){ 39 return false; 40 } 41 42 return true; 43 } 44 45 } 46 47 48 49 50 51
Dpanel.java
1 package project02; 2 3 import java.awt.Graphics; 4 import java.awt.Image; 5 6 public class Dpanel { 7 Image Dpanel;//敌人飞机图片 8 int Dx;//飞机X坐标 9 int Dy;//飞机Y坐标 10 boolean exist=true;//飞机是否存在 11 public Dpanel(Image dpanel, int dx, int dy) { 12 super(); 13 this.Dpanel = dpanel; 14 this.Dx = dx; 15 this.Dy = dy; 16 17 } 18 public void drawDpanel(Graphics g) { 19 g.drawImage(Dpanel, Dx, Dy, null); 20 } 21 public void DpanelMove() { 22 Dy+=5; 23 if(Dy>=600) { 24 exist=false; 25 } 26 } 27 }
Bullet.java
1 package project02; 2 /* 3 * 子弹类中主要写子弹的属性和方法 4 */ 5 6 import java.awt.Graphics; 7 import java.awt.Image; 8 9 public class Bullet{ 10 public int bx,by;//子弹坐标 11 Image bImg;//子弹图片 12 int direction;//子弹方向 13 boolean exist=true; 14 int bsend=10;//速度 15 public Bullet(int bx, int by, Image bImg, int direction) { 16 super(); 17 this.bx = bx; 18 this.by = by; 19 this.bImg = bImg; 20 this.direction = direction; 21 22 } 23 //画子弹的方法 24 public void drawBullet(Graphics g) { 25 g.drawImage(bImg, bx, by, null); 26 } 27 public void moveBullet1() { 28 by-=bsend; 29 if(by<=0) { 30 exist=false; 31 } 32 } 33 public void moveBullet2() { 34 by-=bsend; 35 bx-=bsend/3; 36 if(by<=0) { 37 exist=false; 38 } 39 } 40 public void moveBullet3() { 41 by-=bsend; 42 bx+=bsend/3; 43 if(by<=0) { 44 exist=false; 45 } 46 } 47 48 }
运行效果:
由于有以下没有仔细的算 大概的就写了是图片像素之间的碰撞 效果略差:
原文地址:https://www.cnblogs.com/chijiaozidemiao/p/8652577.html
时间: 2024-10-11 20:21:30