微信游戏打飞机游戏制作(java模拟微信打飞机游戏)
package com.beancore.ui;
import java.awt.borderlayout;
import java.awt.container;
import java.awt.dimension;
import java.awt.toolkit;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.ioexception;
import java.util.arraylist;
import java.util.collections;
import java.util.date;
import java.util.list;
import javax.sound.sampled.lineunavailableexception;
import javax.sound.sampled.unsupportedaudiofileexception;
import javax.swing.box;
import javax.swing.boxlayout;
import javax.swing.imageicon;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import com.beancore.config.config;
import com.beancore.config.imageconstants;
import com.beancore.entity.score;
import com.beancore.util.fileutil;
import com.beancore.util.imageloader;
import com.beancore.util.images;
import com.beancore.util.soundplayer;
public class mainframe extends jframe implements actionlistener {
private static final long serialversionuid = 1l;
private imageloader imgloader;
private gameloadingpanel gameloadingpanel;
private gameplayingpanel gameplayingpanel;
private popupmenupanel popupmenupanel;
private top10scorepanel popupscorepanel;
private helpdialog helpdialog;
private soundplayer achievementsoundplayer;
private list<score> scorelist;
public mainframe() throws ioexception, lineunavailableexception, unsupportedaudiofileexception {
try {
this.scorelist = fileutil.readscore(config.score_file);
} catch (exception e) {
this.scorelist = new arraylist<score>();
}
this.loadimage();
this.initsoundplayer();
this.initcomponents();
this.setbackgroundimage();
}
/**
* load the image part from the whole image, shoot_background.png shoot.png
* are both extracted from the weixin apk file
* */
private void loadimage() throws ioexception {
this.imgloader = new imageloader(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 = new imageloader(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 = new imageloader(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);
}
private void initcomponents() {
this.settitle("shoot plane - www.java1234.com");
this.seticonimage(new imageicon(config.logo_img).getimage());
this.setsize(config.main_frame_width, config.main_frame_height);
dimension d = 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);
}
private void initsoundplayer() throws lineunavailableexception, unsupportedaudiofileexception, ioexception {
achievementsoundplayer = new soundplayer(config.achievement_audio);
}
private void setbackgroundimage() {
imageicon bgimgicon = new imageicon(images.shoot_background_img);
jlabel bglabel = new jlabel(bgimgicon);
this.getlayeredpane().add(bglabel, new integer(integer.min_value));
bglabel.setbounds(0, 0, bgimgicon.geticonwidth(), bgimgicon.geticonheight());
((jpanel) this.getcontentpane()).setopaque(false);
}
private void popupmenupanel() {
container c = this.getcontentpane();
c.removeall();
this.repaint();
if (this.popupmenupanel == null) {
this.popupmenupanel = new popupmenupanel(this);
}
boxlayout boxlayout = new boxlayout(c, boxlayout.y_axis);
c.setlayout(boxlayout);
c.add(box.createverticalglue());
c.add(this.popupmenupanel);
c.add(box.createverticalglue());
this.validate();
}
public void loadgame() throws lineunavailableexception, unsupportedaudiofileexception, ioexception {
container c = this.getcontentpane();
c.removeall();
this.repaint();
if (this.gameloadingpanel == null) {
this.gameloadingpanel = new gameloadingpanel();
}
boxlayout boxlayout = new boxlayout(c, boxlayout.y_axis);
c.setlayout(boxlayout);
c.add(box.createverticalglue());
c.add(this.gameloadingpanel);
c.add(box.createverticalglue());
this.gameloadingpanel.loadinggame();
this.startgame();
}
private void startgame() throws lineunavailableexception, unsupportedaudiofileexception, ioexception {
container c = this.getcontentpane();
c.removeall();
this.repaint();
borderlayout borderlayout = new borderlayout();
c.setlayout(borderlayout);
this.gameplayingpanel = new gameplayingpanel();
c.add(this.gameplayingpanel, borderlayout.center);
this.gameplayingpanel.startgame();
long starttime = system.currenttimemillis();
while (this.gameplayingpanel.getmyplane().isalive()) {
try {
thread.sleep(config.game_panel_repaint_interval);
} catch (interruptedexception e) {
e.printstacktrace();
}
}
long endtime = system.currenttimemillis();
// add to score list
this.addscore(this.gameplayingpanel.getscore(), endtime - starttime);
int option = joptionpane.showconfirmdialog(this, "game over, score:" + this.gameplayingpanel.getscore()
+ ", start again?", "game over", joptionpane.yes_no_option);
switch (option) {
case joptionpane.yes_option:
loadgame();
break;
case joptionpane.no_option:
stopgame();
break;
}
}
private void addscore(int score, long lastmilliseconds) throws ioexception {
score s = new score(new date(system.currenttimemillis()), score, lastmilliseconds);
int size = this.scorelist.size();
if (this.scorelist.contains(s)) {
return;
}
if (size < config.max_score_count) {
this.scorelist.add(s);
} else {
score lastscore = 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);
}
public void stopgame() {
popupmenupanel();
}
@override
public void actionperformed(actionevent e) {
string actioncmd = e.getactioncommand();
if (actioncmd.equals(popupmenupanel.start_game_button)) {
startgameaction();
} else if (actioncmd.equals(popupmenupanel.top_10_scores_button)) {
this.achievementsoundplayer.play();
popupscorepanel(this.scorelist);
} else if (actioncmd.equals(popupmenupanel.exit_game_button)) {
exitgameaction();
} else if (actioncmd.equals(popupmenupanel.help_button)) {
helpaction();
} else if (actioncmd.equals(top10scorepanel.ok_button)) {
this.popupmenupanel();
}
}
private void popupscorepanel(list<score> sortedscorelist) {
container c = this.getcontentpane();
c.removeall();
this.repaint();
if (this.popupscorepanel == null) {
this.popupscorepanel = new top10scorepanel(this);
}
this.popupscorepanel.loadscore(sortedscorelist);
boxlayout boxlayout = new boxlayout(c, boxlayout.y_axis);
c.setlayout(boxlayout);
c.add(box.createverticalglue());
c.add(this.popupscorepanel);
c.add(box.createverticalglue());
this.validate();
}
private void startgameaction() {
new thread(new startgameactionclass()).start();
}
class startgameactionclass implements runnable {
@override
public void run() {
try {
loadgame();
} catch (exception e) {
e.printstacktrace();
}
}
}
private void exitgameaction() {
system.exit(0);
}
private void helpaction() {
if (this.helpdialog == null) {
this.helpdialog = new helpdialog();
}
this.helpdialog.setvisible(true);
}
}
上一篇: 简析ASP.NET网站的创建与发布过程
下一篇: Java 虚拟机(JVM)之基本概念详解