实例介绍
【实例简介】学习java入门知识点集合
【实例截图】
【核心代码】
package com.tarena.shoot; /**扩展 (完整版) * 1.子弹与敌人撞上后,子弹消失 * 2.大飞机----打3次才死,死后得40分 * 3.大黄蜂----打5次才死,死后得60分,并且获得奖励:life 1,doubleFire * */ import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Arrays; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class ShootGame extends JPanel{ private static final long serialVersionUID = 1L; public static final int WIDTH = 400; public static final int HEIGHT = 654; public static BufferedImage background; public static BufferedImage start; public static BufferedImage pause; public static BufferedImage gameover; public static BufferedImage airplane; public static BufferedImage bee; public static BufferedImage bullet; public static BufferedImage hero0; public static BufferedImage hero1; public static BufferedImage bigyellowbee; public static BufferedImage bossairplane; Hero hero = new Hero(); FlyingObject[] flyings = {}; Bullet[] bullets = {}; private int state = 0; public static final int START = 0; public static final int PAUSE = 1; public static final int RUNNING = 2; public static final int GAMEOVER = 3; static { try { background = ImageIO.read(ShootGame.class.getResource("background.png")); start = ImageIO.read(ShootGame.class.getResource("start.png")); pause = ImageIO.read(ShootGame.class.getResource("pause.png")); gameover = ImageIO.read(ShootGame.class.getResource("gameover.png")); airplane = ImageIO.read(ShootGame.class.getResource("airplane.png")); bee = ImageIO.read(ShootGame.class.getResource("bee.png")); bullet = ImageIO.read(ShootGame.class.getResource("bullet.png")); hero0 = ImageIO.read(ShootGame.class.getResource("hero.png")); hero1 = ImageIO.read(ShootGame.class.getResource("hero.png")); bigyellowbee = ImageIO.read(ShootGame.class.getResource("bigyellowbee.png")); bossairplane = ImageIO.read(ShootGame.class.getResource("bossairplane.png")); } catch (IOException e) { e.printStackTrace(); } } public FlyingObject nextOne(){ Random ran = new Random(); int type = ran.nextInt(20); if(type<=1){ return new Bee(); }else if(type == 2){ return new BigYellowBee(); }else if(type == 3){ return new BossAirplane(); }else{ return new Airplane(); } } public void stepAction(){ for(int i = 0;i<flyings.length;i ){ flyings[i].step(); } for(int i = 0;i<bullets.length;i ){ bullets[i].step(); } hero.step(); } int flyEnteredIndex = 0; public void enteredAction(){ flyEnteredIndex ; if(flyEnteredIndex%50==0){ FlyingObject f = nextOne(); flyings = Arrays.copyOf(flyings,flyings.length 1); flyings[flyings.length-1] = f; } } int shootIndex = 0; public void shootAction(){ shootIndex ; if(shootIndex%30==0){ Bullet[] bs = hero.shoot(); bullets = Arrays.copyOf(bullets,bullets.length bs.length); System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length); } } public void outOfBoundsAction(){ int index = 0; FlyingObject[] flyingLives = new FlyingObject[flyings.length]; for(int i = 0;i<flyings.length;i ){ FlyingObject f = flyings[i]; if(!f.outOfBounds()){ flyingLives[index] = f; index ; } } flyings = Arrays.copyOf(flyingLives,index); index = 0; Bullet[] bulletLives = new Bullet[bullets.length]; for(int i = 0;i<bullets.length;i ){ Bullet b = bullets[i]; if(!b.outOfBounds()){ bulletLives[index] = b; index ; } } bullets = Arrays.copyOf(bulletLives,index); } public void bangAction(){ int index = -1;//被撞的子弹的下标 for(int i = 0;i<bullets.length;i ){ if(bang(bullets[i])){ index = i; } if(index != -1){//删除被撞的子弹 Bullet temp = bullets[index]; bullets[index] = bullets[bullets.length-1]; bullets[bullets.length-1] = temp; bullets = Arrays.copyOf(bullets,bullets.length-1); } } } int score = 0; public boolean bang(Bullet b){ boolean flag = false; int index = -1; for(int i = 0;i<flyings.length;i ){ FlyingObject f = flyings[i]; if(f instanceof Airplane || f instanceof Bee){//判断撞上的是敌机还是小蜜蜂 if(f.shootBy(b)){ index = i; flag = true; break; } } if(f instanceof BossAirplane){//撞上的是Boss机 if(f.shootBy(b)){ BossAirplane ba = (BossAirplane)f; ba.substractLife(); if(ba.getLife()==0){ index = i; break; } flag = true; } } if(f instanceof BigYellowBee){//撞上的是大黄蜂 if(f.shootBy(b)){ BigYellowBee byb = (BigYellowBee)f; byb.substractLife(); if(byb.getLife()==0){ index = i; break; } flag = true; } } } if(index!=-1){ FlyingObject one = flyings[index]; if(one instanceof BigYellowBee){ BigYellowBee byb = (BigYellowBee)one; score = byb.getScore(); hero.addDoubleFire(); hero.addLife(); } if(one instanceof BossAirplane){ BossAirplane ba = (BossAirplane)one; score = ba.getScore(); } if(one instanceof Enemy){ Enemy e = (Enemy)one; score = e.getScore(); } if(one instanceof Award){ Award a = (Award)one; int type = a.getType(); switch(type){ case Award.DOUBLE_FIRE: hero.addDoubleFire(); break; case Award.LIFE: hero.addLife(); break; } } FlyingObject temp = flyings[index]; flyings[index] = flyings[flyings.length-1]; flyings[flyings.length-1] = temp; flyings = Arrays.copyOf(flyings,flyings.length-1); } return flag; } public void checkGameOverAction(){ if(isGameOver()){ state = GAMEOVER; } } public boolean isGameOver(){ for(int i = 0;i<flyings.length;i ){ FlyingObject f = flyings[i]; if(hero.hit(f)){ hero.clearDoubleFire(); hero.substractLife(); FlyingObject temp = flyings[i]; flyings[i] = flyings[flyings.length-1]; flyings[flyings.length-1] = temp; flyings = Arrays.copyOf(flyings,flyings.length-1); } } return hero.getLife()<=0; } public void action(){ MouseAdapter ma = new MouseAdapter(){ public void mouseMoved(MouseEvent e){ if(state == RUNNING){ int x = e.getX(); int y = e.getY(); hero.moveTo(x, y); } } public void mouseEntered(MouseEvent e){ if(state == PAUSE){ state = RUNNING; } } public void mouseExited(MouseEvent e){ if(state == RUNNING){ state = PAUSE; } } public void mouseClicked(MouseEvent e){ switch(state){ case START: state = RUNNING; break; case GAMEOVER: state = START; hero = new Hero(); bullets = new Bullet[0]; flyings = new FlyingObject[0]; score = 0; break; } } }; this.addMouseListener(ma); this.addMouseMotionListener(ma); Timer timer = new Timer(); int intevel = 10; timer.schedule(new TimerTask(){ @Override public void run() { if(state == RUNNING){ stepAction(); enteredAction(); shootAction(); outOfBoundsAction(); bangAction(); checkGameOverAction(); } repaint(); } }, intevel,intevel); } public void paint(Graphics g){ g.drawImage(background,0,0,null); paintHero(g); paintFlyingObjects(g); paintBullets(g); paintScoreAndLife(g); paintState(g); } public void paintHero(Graphics g){ g.drawImage(hero.image,hero.x,hero.y,null); } public void paintFlyingObjects(Graphics g){ for(int i = 0;i<flyings.length;i ){ FlyingObject f = flyings[i]; g.drawImage(f.image, f.x, f.y,null); } } public void paintBullets(Graphics g){ for(int i = 0;i<bullets.length;i ){ Bullet b = bullets[i]; g.drawImage(b.image, b.x, b.y,null); } } public void paintScoreAndLife(Graphics g){ g.setColor(new Color(0xFF0000)); g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20)); g.drawString("SCORE:" score,15,25); g.drawString("LIFE:" hero.getLife(),15,45); } public void paintState(Graphics g){ switch(state){ case START: g.drawImage(start,0,0,null); break; case PAUSE: g.drawImage(pause,0,0,null); break; case GAMEOVER: g.drawImage(gameover,0,0,null); break; } } @SuppressWarnings("static-access") public static void main(String[] args) { JFrame jf = new JFrame("shoot"); ShootGame game = new ShootGame(); jf.add(game); jf.setSize(WIDTH,HEIGHT); jf.setAlwaysOnTop(true); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); jf.setVisible(true); game.action(); } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论