微信游戏打飞机游戏制作(java模拟微信打飞机游戏)
packagecom.beancore.ui;
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Date;
importjava.util.List;
importjavax.sound.sampled.LineUnavailableException;
importjavax.sound.sampled.UnsupportedAudioFileException;
importjavax.swing.Box;
importjavax.swing.BoxLayout;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importcom.beancore.config.Config;
importcom.beancore.config.ImageConstants;
importcom.beancore.entity.Score;
importcom.beancore.util.FileUtil;
importcom.beancore.util.ImageLoader;
importcom.beancore.util.Images;
importcom.beancore.util.SoundPlayer;
publicclassMainFrameextendsJFrameimplementsActionListener{
privatestaticfinallongserialVersionUID=1L;
privateImageLoaderimgLoader;
privateGameLoadingPanelgameLoadingPanel;
privateGamePlayingPanelgamePlayingPanel;
privatePopupMenuPanelpopupMenuPanel;
privateTop10ScorePanelpopupScorePanel;
privateHelpDialoghelpDialog;
privateSoundPlayerachievementSoundPlayer;
privateList<Score>scoreList;
publicMainFrame()throwsIOException,LineUnavailableException,UnsupportedAudioFileException{
try{
this.scoreList=FileUtil.readScore(Config.SCORE_FILE);
}catch(Exceptione){
this.scoreList=newArrayList<Score>();
}
this.loadImage();
this.initSoundPlayer();
this.initComponents();
this.setBackgroundImage();
}
/**
*Loadtheimagepartfromthewholeimage,shoot_background.pngshoot.png
*arebothextractedfromtheweixinapkfile
**/
privatevoidloadImage()throwsIOException{
this.imgLoader=newImageLoader(Config.SHOOT_BACKGROUND_IMG);
Images.GAME_LOADING_IMG1=this.imgLoader.getImage(ImageConstants.GAME_LOADING_PLANE_1_POS_X,
ImageConstants.GAME_LOADING_PLANE_1_POS_Y,ImageConstants.GAME_LOADING_PLANE_1_WIDTH,
ImageConstants.GAME_LOADING_PLANE_1_HEIGHT);
Images.GAME_LOADING_IMG2=this.imgLoader.getImage(ImageConstants.GAME_LOADING_PLANE_2_POS_X,
ImageConstants.GAME_LOADING_PLANE_2_POS_Y,ImageConstants.GAME_LOADING_PLANE_2_WIDTH,
ImageConstants.GAME_LOADING_PLANE_2_HEIGHT);
Images.GAME_LOADING_IMG3=this.imgLoader.getImage(ImageConstants.GAME_LOADING_PLANE_3_POS_X,
ImageConstants.GAME_LOADING_PLANE_3_POS_Y,ImageConstants.GAME_LOADING_PLANE_3_WIDTH,
ImageConstants.GAME_LOADING_PLANE_3_HEIGHT);
Images.SHOOT_BACKGROUND_IMG=this.imgLoader.getImage(ImageConstants.GAME_BACKGROUND_IMG_POS_X,
ImageConstants.GAME_BACKGROUND_IMG_POS_Y,ImageConstants.GAME_BACKGROUND_IMG_WIDTH,
ImageConstants.GAME_BACKGROUND_IMG_HEIGHT);
Images.GAME_LOADING_TEXT_IMG=this.imgLoader.getImage(ImageConstants.GAME_LOADING_TEXT_IMG_POS_X,
ImageConstants.GAME_LOADING_TEXT_IMG_POS_Y,ImageConstants.GAME_LOADING_TEXT_IMG_WIDTH,
ImageConstants.GAME_LOADING_TEXT_IMG_HEIGHT);
this.imgLoader=newImageLoader(Config.SHOOT_IMG);
Images.YELLOW_BULLET_IMG=this.imgLoader.getImage(ImageConstants.YELLOW_BULLET_POS_X,
ImageConstants.YELLOW_BULLET_POS_Y,ImageConstants.YELLOW_BULLET_WIDTH,
ImageConstants.YELLOW_BULLET_HEIGHT);
Images.BLUE_BULLET_IMG=this.imgLoader.getImage(ImageConstants.BLUE_BULLET_POS_X,
ImageConstants.BLUE_BULLET_POS_Y,ImageConstants.BLUE_BULLET_WIDTH,ImageConstants.BLUE_BULLET_HEIGHT);
Images.MY_PLANE_IMG=this.imgLoader.getImage(ImageConstants.MY_PLANE_POS_X,ImageConstants.MY_PLANE_POS_Y,
ImageConstants.MY_PLANE_WIDTH,ImageConstants.MY_PLANE_HEIGHT);
Images.MY_PLANE_FLYING_IMG=this.imgLoader.getImage(ImageConstants.MY_PLANE_FLYING_POS_X,
ImageConstants.MY_PLANE_FLYING_POS_Y,ImageConstants.MY_PLANE_FLYING_WIDTH,
ImageConstants.MY_PLANE_FLYING_HEIGHT);
Images.SMALL_PLANE_IMG=this.imgLoader.getImage(ImageConstants.SMALL_PLANE_POS_X,
ImageConstants.SMALL_PLANE_POS_Y,ImageConstants.SMALL_PLANE_WIDTH,ImageConstants.SMALL_PLANE_HEIGHT);
Images.BIG_PLANE_IMG=this.imgLoader.getImage(ImageConstants.BIG_PLANE_POS_X,ImageConstants.BIG_PLANE_POS_Y,
ImageConstants.BIG_PLANE_WIDTH,ImageConstants.BIG_PLANE_HEIGHT);
Images.BOSS_PLANE_IMG=this.imgLoader.getImage(ImageConstants.BOSS_PLANE_POS_X,
ImageConstants.BOSS_PLANE_POS_Y,ImageConstants.BOSS_PLANE_WIDTH,ImageConstants.BOSS_PLANE_HEIGHT);
Images.BOMB_IMG=this.imgLoader.getImage(ImageConstants.BOMB_POS_X,ImageConstants.BOMB_POS_Y,
ImageConstants.BOMB_WIDTH,ImageConstants.BOMB_HEIGHT);
Images.CAUGHT_BOMB_IMG=this.imgLoader.getImage(ImageConstants.CAUGHT_BOMB_POS_X,
ImageConstants.CAUGHT_BOMB_POS_Y,ImageConstants.CAUGHT_BOMB_WIDTH,ImageConstants.CAUGHT_BOMB_HEIGHT);
Images.DOUBLE_LASER_IMG=this.imgLoader.getImage(ImageConstants.DOUBLE_LASER_POS_X,
ImageConstants.DOUBLE_LASER_POS_Y,ImageConstants.DOUBLE_LASER_WIDTH,
ImageConstants.DOUBLE_LASER_HEIGHT);
Images.SMALL_PLANE_FIGHTING_IMG=this.imgLoader.getImage(ImageConstants.SMALL_PLANE_FIGHTING_POS_X,
ImageConstants.SMALL_PLANE_FIGHTING_POS_Y,ImageConstants.SMALL_PLANE_FIGHTING_WIDTH,
ImageConstants.SMALL_PLANE_FIGHTING_HEIGHT);
Images.SMALL_PLANE_KILLED_IMG=this.imgLoader.getImage(ImageConstants.SMALL_PLANE_KILLED_POS_X,
ImageConstants.SMALL_PLANE_KILLED_POS_Y,ImageConstants.SMALL_PLANE_KILLED_WIDTH,
ImageConstants.SMALL_PLANE_KILLED_HEIGHT);
Images.SMALL_PLANE_ASHED_IMG=this.imgLoader.getImage(ImageConstants.SMALL_PLANE_ASHED_POS_X,
ImageConstants.SMALL_PLANE_ASHED_POS_Y,ImageConstants.SMALL_PLANE_ASHED_WIDTH,
ImageConstants.SMALL_PLANE_ASHED_HEIGHT);
Images.BIG_PLANE_FIGHTING_IMG=this.imgLoader.getImage(ImageConstants.BIG_PLANE_FIGHTING_POS_X,
ImageConstants.BIG_PLANE_FIGHTING_POS_Y,ImageConstants.BIG_PLANE_FIGHTING_WIDTH,
ImageConstants.BIG_PLANE_FIGHTING_HEIGHT);
Images.BIG_PLANE_HITTED_IMG=this.imgLoader.getImage(ImageConstants.BIG_PLANE_HITTED_POS_X,
ImageConstants.BIG_PLANE_HITTED_POS_Y,ImageConstants.BIG_PLANE_HITTED_WIDTH,
ImageConstants.BIG_PLANE_HITTED_HEIGHT);
Images.BIG_PLANE_BADDLY_WOUNDED_IMG=this.imgLoader.getImage(ImageConstants.BIG_PLANE_BADDLY_WOUNDED_POS_X,
ImageConstants.BIG_PLANE_BADDLY_WOUNDED_POS_Y,ImageConstants.BIG_PLANE_BADDLY_WOUNDED_WIDTH,
ImageConstants.BIG_PLANE_BADDLY_WOUNDED_HEIGHT);
Images.BIG_PLANE_KILLED_IMG=this.imgLoader.getImage(ImageConstants.BIG_PLANE_KILLED_POS_X,
ImageConstants.BIG_PLANE_KILLED_POS_Y,ImageConstants.BIG_PLANE_KILLED_WIDTH,
ImageConstants.BIG_PLANE_KILLED_HEIGHT);
Images.BIG_PLANE_ASHED_IMG=this.imgLoader.getImage(ImageConstants.BIG_PLANE_ASHED_POS_X,
ImageConstants.BIG_PLANE_ASHED_POS_Y,ImageConstants.BIG_PLANE_ASHED_WIDTH,
ImageConstants.BIG_PLANE_ASHED_HEIGHT);
Images.BOSS_PLANE_FIGHTING_IMG=this.imgLoader.getImage(ImageConstants.BOSS_PLANE_FIGHTING_POS_X,
ImageConstants.BOSS_PLANE_FIGHTING_POS_Y,ImageConstants.BOSS_PLANE_FIGHTING_WIDTH,
ImageConstants.BOSS_PLANE_FIGHTING_HEIGHT);
Images.BOSS_PLANE_HITTED_IMG=this.imgLoader.getImage(ImageConstants.BOSS_PLANE_HITTED_POS_X,
ImageConstants.BOSS_PLANE_HITTED_POS_Y,ImageConstants.BOSS_PLANE_HITTED_WIDTH,
ImageConstants.BOSS_PLANE_HITTED_HEIGHT);
Images.BOSS_PLANE_BADDLY_WOUNDED_IMG=this.imgLoader.getImage(ImageConstants.BOSS_PLANE_BADDLY_WOUNDED_POS_X,
ImageConstants.BOSS_PLANE_BADDLY_WOUNDED_POS_Y,ImageConstants.BOSS_PLANE_BADDLY_WOUNDED_WIDTH,
ImageConstants.BOSS_PLANE_BADDLY_WOUNDED_HEIGHT);
Images.BOSS_PLANE_KILLED_IMG=this.imgLoader.getImage(ImageConstants.BOSS_PLANE_KILLED_POS_X,
ImageConstants.BOSS_PLANE_KILLED_POS_Y,ImageConstants.BOSS_PLANE_KILLED_WIDTH,
ImageConstants.BOSS_PLANE_KILLED_HEIGHT);
Images.BOSS_PLANE_ASHED_IMG=this.imgLoader.getImage(ImageConstants.BOSS_PLANE_ASHED_POS_X,
ImageConstants.BOSS_PLANE_ASHED_POS_Y,ImageConstants.BOSS_PLANE_ASHED_WIDTH,
ImageConstants.BOSS_PLANE_ASHED_HEIGHT);
Images.SCORE_IMG=this.imgLoader.getImage(ImageConstants.SCORE_IMG_POS_X,ImageConstants.SCORE_IMG_POS_Y,
ImageConstants.SCORE_IMG_WIDTH,ImageConstants.SCORE_IMG_HEIGHT);
this.imgLoader=newImageLoader(Config.FONT_IMG);
Images.X_MARK_IMG=this.imgLoader.getImage(ImageConstants.X_MARK_POS_X,ImageConstants.X_MARK_POS_Y,
ImageConstants.X_MARK_WIDTH,ImageConstants.X_MARK_HEIGHT);
Images.NUMBER_0_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_0_POS_X,ImageConstants.NUMBER_0_POS_Y,
ImageConstants.NUMBER_0_WIDTH,ImageConstants.NUMBER_0_HEIGHT);
Images.NUMBER_1_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_1_POS_X,ImageConstants.NUMBER_1_POS_Y,
ImageConstants.NUMBER_1_WIDTH,ImageConstants.NUMBER_1_HEIGHT);
Images.NUMBER_2_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_2_POS_X,ImageConstants.NUMBER_2_POS_Y,
ImageConstants.NUMBER_2_WIDTH,ImageConstants.NUMBER_2_HEIGHT);
Images.NUMBER_3_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_3_POS_X,ImageConstants.NUMBER_3_POS_Y,
ImageConstants.NUMBER_3_WIDTH,ImageConstants.NUMBER_3_HEIGHT);
Images.NUMBER_4_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_4_POS_X,ImageConstants.NUMBER_4_POS_Y,
ImageConstants.NUMBER_4_WIDTH,ImageConstants.NUMBER_4_HEIGHT);
Images.NUMBER_5_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_5_POS_X,ImageConstants.NUMBER_5_POS_Y,
ImageConstants.NUMBER_5_WIDTH,ImageConstants.NUMBER_5_HEIGHT);
Images.NUMBER_6_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_6_POS_X,ImageConstants.NUMBER_6_POS_Y,
ImageConstants.NUMBER_6_WIDTH,ImageConstants.NUMBER_6_HEIGHT);
Images.NUMBER_7_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_7_POS_X,ImageConstants.NUMBER_7_POS_Y,
ImageConstants.NUMBER_7_WIDTH,ImageConstants.NUMBER_7_HEIGHT);
Images.NUMBER_8_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_8_POS_X,ImageConstants.NUMBER_8_POS_Y,
ImageConstants.NUMBER_8_WIDTH,ImageConstants.NUMBER_8_HEIGHT);
Images.NUMBER_9_IMG=this.imgLoader.getImage(ImageConstants.NUMBER_9_POS_X,ImageConstants.NUMBER_9_POS_Y,
ImageConstants.NUMBER_9_WIDTH,ImageConstants.NUMBER_9_HEIGHT);
}
privatevoidinitComponents(){
this.setTitle("ShootPlane-www.java1234.com");
this.setIconImage(newImageIcon(Config.LOGO_IMG).getImage());
this.setSize(Config.MAIN_FRAME_WIDTH,Config.MAIN_FRAME_HEIGHT);
Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds((d.width-Config.MAIN_FRAME_WIDTH)/2,(d.height-Config.MAIN_FRAME_HEIGHT)/2,
Config.MAIN_FRAME_WIDTH,Config.MAIN_FRAME_HEIGHT);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
privatevoidinitSoundPlayer()throwsLineUnavailableException,UnsupportedAudioFileException,IOException{
achievementSoundPlayer=newSoundPlayer(Config.ACHIEVEMENT_AUDIO);
}
privatevoidsetBackgroundImage(){
ImageIconbgImgIcon=newImageIcon(Images.SHOOT_BACKGROUND_IMG);
JLabelbgLabel=newJLabel(bgImgIcon);
this.getLayeredPane().add(bgLabel,newInteger(Integer.MIN_VALUE));
bgLabel.setBounds(0,0,bgImgIcon.getIconWidth(),bgImgIcon.getIconHeight());
((JPanel)this.getContentPane()).setOpaque(false);
}
privatevoidpopupMenuPanel(){
Containerc=this.getContentPane();
c.removeAll();
this.repaint();
if(this.popupMenuPanel==null){
this.popupMenuPanel=newPopupMenuPanel(this);
}
BoxLayoutboxLayout=newBoxLayout(c,BoxLayout.Y_AXIS);
c.setLayout(boxLayout);
c.add(Box.createVerticalGlue());
c.add(this.popupMenuPanel);
c.add(Box.createVerticalGlue());
this.validate();
}
publicvoidloadGame()throwsLineUnavailableException,UnsupportedAudioFileException,IOException{
Containerc=this.getContentPane();
c.removeAll();
this.repaint();
if(this.gameLoadingPanel==null){
this.gameLoadingPanel=newGameLoadingPanel();
}
BoxLayoutboxLayout=newBoxLayout(c,BoxLayout.Y_AXIS);
c.setLayout(boxLayout);
c.add(Box.createVerticalGlue());
c.add(this.gameLoadingPanel);
c.add(Box.createVerticalGlue());
this.gameLoadingPanel.loadingGame();
this.startGame();
}
privatevoidstartGame()throwsLineUnavailableException,UnsupportedAudioFileException,IOException{
Containerc=this.getContentPane();
c.removeAll();
this.repaint();
BorderLayoutborderLayout=newBorderLayout();
c.setLayout(borderLayout);
this.gamePlayingPanel=newGamePlayingPanel();
c.add(this.gamePlayingPanel,BorderLayout.CENTER);
this.gamePlayingPanel.startGame();
longstartTime=System.currentTimeMillis();
while(this.gamePlayingPanel.getMyPlane().isAlive()){
try{
Thread.sleep(Config.GAME_PANEL_REPAINT_INTERVAL);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
longendTime=System.currentTimeMillis();
//addtoscorelist
this.addScore(this.gamePlayingPanel.getScore(),endTime-startTime);
intoption=JOptionPane.showConfirmDialog(this,"GameOver,Score:"+this.gamePlayingPanel.getScore()
+",StartAgain?","GameOver",JOptionPane.YES_NO_OPTION);
switch(option){
caseJOptionPane.YES_OPTION:
loadGame();
break;
caseJOptionPane.NO_OPTION:
stopGame();
break;
}
}
privatevoidaddScore(intscore,longlastMilliSeconds)throwsIOException{
Scores=newScore(newDate(System.currentTimeMillis()),score,lastMilliSeconds);
intsize=this.scoreList.size();
if(this.scoreList.contains(s)){
return;
}
if(size<Config.MAX_SCORE_COUNT){
this.scoreList.add(s);
}else{
ScorelastScore=this.scoreList.get(size-1);
if(s.compareTo(lastScore)>0){
this.scoreList.remove(lastScore);
this.scoreList.add(s);
}
}
Collections.sort(this.scoreList);
Collections.reverse(this.scoreList);
FileUtil.writeScore(scoreList,Config.SCORE_FILE);
}
publicvoidstopGame(){
popupMenuPanel();
}
@Override
publicvoidactionPerformed(ActionEvente){
StringactionCmd=e.getActionCommand();
if(actionCmd.equals(PopupMenuPanel.START_GAME_BUTTON)){
startGameAction();
}elseif(actionCmd.equals(PopupMenuPanel.TOP_10_SCORES_BUTTON)){
this.achievementSoundPlayer.play();
popupScorePanel(this.scoreList);
}elseif(actionCmd.equals(PopupMenuPanel.EXIT_GAME_BUTTON)){
exitGameAction();
}elseif(actionCmd.equals(PopupMenuPanel.HELP_BUTTON)){
helpAction();
}elseif(actionCmd.equals(Top10ScorePanel.OK_BUTTON)){
this.popupMenuPanel();
}
}
privatevoidpopupScorePanel(List<Score>sortedScoreList){
Containerc=this.getContentPane();
c.removeAll();
this.repaint();
if(this.popupScorePanel==null){
this.popupScorePanel=newTop10ScorePanel(this);
}
this.popupScorePanel.loadScore(sortedScoreList);
BoxLayoutboxLayout=newBoxLayout(c,BoxLayout.Y_AXIS);
c.setLayout(boxLayout);
c.add(Box.createVerticalGlue());
c.add(this.popupScorePanel);
c.add(Box.createVerticalGlue());
this.validate();
}
privatevoidstartGameAction(){
newThread(newStartGameActionClass()).start();
}
classStartGameActionClassimplementsRunnable{
@Override
publicvoidrun(){
try{
loadGame();
}catch(Exceptione){
e.printStackTrace();
}
}
}
privatevoidexitGameAction(){
System.exit(0);
}
privatevoidhelpAction(){
if(this.helpDialog==null){
this.helpDialog=newHelpDialog();
}
this.helpDialog.setVisible(true);
}
}
相关文章
- 推荐几个IDEA插件,Java开发者撸码利器
- java反射菜鸟教程_Java反射
- 刷完3个Java教程后不再犹豫
- java编写一个学生类和教师类,Java创建一个学生类[通俗易懂]
- java面试强基(7)
- java锁:第四章:读写锁
- Java 程序控制结构(3)
- Java集合面试题_java是什么
- 并发多线程学习(五)Java线程的状态及主要转化方法
- 【Java】JavaMail实现收发邮件——(一)相关概念概述
- Java学习笔记之九java二维数组及其多维数组的内存应用拓展延伸详解编程语言
- java 数据结构与算法—递归详解编程语言
- Java Connection.createStatement()方法:创建一个Statement对象
- Linux上部署Java项目实践指南(linux部署java项目)
- Oracle与Java: 未来前瞻(oracle与java)
- 玩转Linux:Java开发入门指南(linux上开发java)
- 策略Java实现Redis过期策略实践(redisjava过期)
- 使用Java和MySQL框架创建动态Web应用(javamysql框架)
- Java调用Redis实现高性能数据存储(java调用redis)
- 使用Redis存储Java对象(redis存java对象)
- 时间解决Java中Redis键值对过期时间设置问题(redisjava过期)
- Java与Linux:前景迷人的新科技(java和linux前景)
- Java在Linux下促进发展的力量(java linux)
- java中的Integer的toBinaryString()方法实例
- Java中集合和数组的排序方式小结