fly_bird.zip

  • L4_178990
    了解作者
  • 14.4MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-10 11:46
    上传日期
用java applet实现的飞翔的小鸟游戏。stupidbird为游戏的主窗体,birdback是游戏的后台。前后台可以设置交互,记录玩家的得分和排名等。其中带有简单的声音特效。
fly_bird.zip
内容介绍
package org.mobiletrain.www; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JPanel; import org.mobiletrain.www.account.ChatLogin; import org.mobiletrain.www.account.LeaderBoard; import org.mobiletrain.www.account.SetSkin; import org.mobiletrain.www.util.NetBaseUtil; import javazoom.jl.decoder.JavaLayerException; public class BirdGame extends JPanel { private BufferedImage imageBg = null, imgCloud = null; private BufferedImage startImg = null; private BufferedImage firstImg, secondImg; private BufferedImage overImg = null, imgTitle = null; private BufferedImage imgScore_Panel = null; private MusicThread music = null; private String name = null;// 记录用户名 private int bestScore; private Bird bird = null; private Column column1 = null, column2 = null; private static int state = -1; private static int score = 0;// 记录分数 private Ground ground = null;// 声明一个地面类对象 private static final int HOME = -1, START = 0, RUNNING = 1, GAMEOVER = 2; private static Boolean isDowning; private Button button = null; private int v; private boolean rate = true; private boolean volume = true; private ChatLogin login = null; private SetSkin ss; private int skinType; private Reward reward1, reward2 = null; private static int life = 0; private static long t1 = 0; AudioPlayer au = null; public static boolean mFlag = true; private BufferedImage life_o = null; private static boolean pause = false; private Hostile hostile = null; /* * 构造方法,当前类进行初始化操作,修饰符+类名+参数 */ public BirdGame(String n) { setLayout(null); this.name = n; bestScore = NetBaseUtil.getScore(n); try {// 读取图片 imageBg = ImageIO.read(getClass().getResource("bg.png")); startImg = ImageIO.read(getClass().getResource("start.png")); overImg = ImageIO.read(getClass().getResource("gameover.png")); imgTitle = ImageIO.read(getClass().getResource("title.png")); firstImg = ImageIO.read(getClass().getResource("medals_1.png")); secondImg = ImageIO.read(getClass().getResource("medals_2.png")); imgScore_Panel = ImageIO.read(getClass().getResource("score_panel.png")); imgCloud = ImageIO.read(getClass().getResource("cloud.png")); // music = new // MusicThread(getClass().getResource(MUSICPATH).toString()); state = HOME; isDowning = false; ground = new Ground(); bird = new Bird(1); column1 = new Column(); column2 = new Column(); button = new Button(); reward1 = new Reward(); reward2 = new Reward(); au = new AudioPlayer(); hostile = new Hostile(); life_o = ImageIO.read(getClass().getResource("life.png")); // 开始按钮 add(button.mJbtn_Play); button.mJbtn_Play.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (login != null) { name = login.getName(); bestScore = NetBaseUtil.getScore(name); } if (ss != null) { skinType = ss.setSkin(); bird.setSkin(skinType); } state = START; } }); // 难度按钮 add(button.mJbtn_Rate); button.mJbtn_Rate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (login != null) { name = login.getName(); bestScore = NetBaseUtil.getScore(name); } if (ss != null) { skinType = ss.setSkin(); bird.setSkin(skinType); } if (button.mJbtn_Rate.getText() == "简单") { button.mJbtn_Rate.setText("困难"); rate = false; } else { button.mJbtn_Rate.setText("简单"); rate = true; } } }); // 排行榜按钮 add(button.mJbtn_Score); button.mJbtn_Score.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (login != null) { name = login.getName(); bestScore = NetBaseUtil.getScore(name); } LeaderBoard ranks = new LeaderBoard(name); ranks.setVisible(true); // System.out.println("name:" + name); } }); // 声音控制按钮 add(button.mJbtn_Volume); button.mJbtn_Volume.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (login != null) name = login.getName(); if (volume) { button.mJbtn_Volume.setIcon(new ImageIcon("src/org/mobiletrain/www/close.png")); if(state == START){ au.stopFree(); mFlag = false; } else if (state == RUNNING){ au.stopRun(); mFlag = false; } volume = false; // 关声音方法 } else { button.mJbtn_Volume.setIcon(new ImageIcon("src/org/mobiletrain/www/volume.png")); volume = true; mFlag = true; // 开声音方法 } } }); // 登录按钮 add(button.log); button.log.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (button.log.getText() == "登录") { login = new ChatLogin(); login.setVisible(true); // if (login.n == 1) // break; // name = login.getName(); // System.out.println("name:" + name); button.log.setText("退出"); // rate = false; } else { if (login != null && login.isVisible()) ; else button.log.setText("登录"); // rate = true; } // getParent().setVisible(false); } }); // 皮肤按钮 add(button.pifu); button.pifu.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (login != null) { name = login.getName(); bestScore = NetBaseUtil.getScore(name); } ss = new SetSkin(); ss.setSkin(); ss.setVisible(true); skinType = ss.getT(); // bird.setSkin(skinType); } }); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); // 1.绘制的资源;2.3.开始绘制起始点坐标;4.当图片发生改变时要通知的对象 g.drawImage(imageBg, 0, 0, getWidth(), getHeight(), null); // Font font = new Font(Font.SERIF, Font.ITALIC, 40); // g.setFont(font); switch (state) { case HOME: // 游戏开始设置界面 g.drawImage(imgTitle, 130, 100, null); g.drawImage(bird.img, 186, 160, null); g.drawImage(imgCloud, 200, 190, null); g.drawImage(imgCloud, 150, 190, null); break; case START:// start g.drawImage(startImg, 0, 0, getWidth(), getHeight(), null); g.drawImage(bird.img, bird.x, bird.y, bird.width, bird.height, null); break; case RUNNING:// running g.drawImage(column1.img, column1.x, column1.y, column1.width, column1.height, null); g.drawImage(column2.img, column2.x, column2.y, column2.width, column2.height, null); g.drawImage(bird.img, bird.x, bird.y, bird.width, bird.height, null); // g.drawString("" + score, 20, 40); g.drawImage(reward1.img, reward1.x, reward1.y, reward1.width, reward1.height, null); g.drawImage(reward2.img, reward2.x, reward2.y, reward2.width, reward2.height, null); g.drawImage(hostile.img, hostile.x, hostile.y, hostile.width, hostile.height, null); break; case GAMEOVER:// gameover g.drawImage(overImg, 118, 100, 210, 60, null
评论
    相关推荐