微信游戏打飞机游戏制作(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);
    }
}

(0)

相关推荐

  • Java实现打飞机小游戏(附完整源码)

    写在前面 技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习.java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对象编程的过程,纯属娱乐.代码写的很简单,也很容易理解,并且注释写的很清楚了,还有问题,自己私下去补课学习. 效果如下 完整代码 敌飞机 import java.util.Random; 敌飞机: 是飞行物,也是敌人 public class Airplane extends FlyingObject implements Enemy

  • 微信游戏打飞机游戏制作(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

  • Java模拟微信来电提醒示例

    目录 引言 效果图如下 代码思路 总结 引言 前几天在抖音刷到一个老哥做的章鱼哥来电提醒,感觉挺好玩的,于是就用Java搞了一个. 大致流程如下 章鱼哥来电提醒 文本一朗读 点击鼠标特效,紧接着摄像机移动到电脑右下角 中间穿插这章鱼哥朗诵视频(无声,声源是文本内容) 右下角弹出章鱼哥来电提醒 右下角来电倒计时+文本二+文本三 播放章鱼哥朗诵 章鱼哥朗诵视频+zhyg.wav声音 开搞 !! 效果图如下 代码思路 是用java.swing 的 JFrame做的,它原生的按钮太难看,于是想着用Jla

  • java模拟微信抢红包的实例代码

    java简单模拟微信抢红包功能,本例发100元红包,有10个人抢,为了尽可能的公平,每个人的红包金额都要随机(保证结果的不确定性,本例抢红包的次序与红包金额匹配也随机),就是不能出现部分红包偏大部分偏小的情况,在区间0~100上随机生成9个节点,加0和100共11个节点按从小到大排序,各节点之间的差值就是红包金额,保证了红包金额之和等于100. public static void main(String[] args) { // 发100元红包,10人抢 // TODO Auto-genera

  • Java实现两人五子棋游戏(三) 画出棋子

    上一篇文章讲的是Java实现两人五子棋游戏(二) 画出棋盘,已经画好棋盘,接下来要实现控制功能,主要功能: 1)选择棋子 2)画棋子 3)判断胜负 4)交换行棋方 先实现画棋子PART -------------画棋子代码示例如下-------------- 首先,定义一个棋子类,这个类有两个属性,棋子颜色(0-表示黑色,1-表示白色),是否落子(我计划用一个二维数组才存储棋子的落子信息) Chessman.java package xchen.test.simpleGobang; public

  • Java实现两人五子棋游戏(二) 画出棋盘

    本文为大家分享了java画出五子棋游戏棋盘的方法,供大家参考,具体内容如下 棋盘模块: 画五子棋棋盘:19条横线.19条竖线 步骤一:显示棋盘 我有一张名为chessboard.png的棋盘,位置为根目录/res/drawable/chessboard/png,现在我要显示这张图片. DrawChessBoard.java package xchen.test.simpleGobang; import java.awt.Graphics; import java.awt.Image; impor

  • Java实现两人五子棋游戏(七) 屏幕提示信息

    之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘:Java实现两人五子棋游戏(三) 画出棋子:Java实现两人五子棋游戏(四) 落子动作的实现:Java实现两人五子棋游戏(六) 行棋方变换,可以点击查看. 接下来我们要实现的功能是在有五连珠的时候,在屏幕上提示信息.直接在mousePressed函数的isWin部分使用showMessageDialog即可. mousePressed函数代码如下: @Override //当用户按下鼠标按钮时发生 public void mousePr

  • Java实现两人五子棋游戏(六) 行棋方变换

    之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘:Java实现两人五子棋游戏(三) 画出棋子:Java实现两人五子棋游戏(四) 落子动作的实现,可以点击查看. 前面我们已经画好棋盘和棋子,并可以自由落子,且可以判断棋盘中是否有五连珠.接下来的工作是,每次落子后交换行棋方(即将下一棋子颜色由白->黑或黑->白),并将胜出判断增加颜色判断条件(棋盘上存在五连珠->棋盘上存在同色的五连珠). 主模块Main.java和棋子类Chessman.java不变,我们要对DrawChessB

  • Java实现两人五子棋游戏(五) 判断是否有一方胜出

    之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘:Java实现两人五子棋游戏(三) 画出棋子:Java实现两人五子棋游戏(四) 落子动作的实现,可以点击查看. 前面我们已经画好了棋盘.棋子并且可以自由的落子了,那么接下来要实现的功能是判断是否有五连珠(暂时不考虑行棋方). 我们采用遍历棋盘已经落子的位置,查看每个落子点,在它的上下,左右,左下右上,左上右下四个方向的任一方向上是否有五个连续的棋子. 第一步,对棋子类进行改造,之前我们的棋子类只有颜色信息和落子状态,现在要新增一个int型

  • Java实现两人五子棋游戏(四) 落子动作的实现

    之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘:Java实现两人五子棋游戏(三) 画出棋子 前面,我们已经画好的棋盘和棋子,接下来,我们要通过鼠标点击屏幕获取落子位置并落子(先不考虑行棋方和胜负判断). 步骤: 1)捕捉鼠标按下的位置 2)经过坐标变换(由像素位置->0-19的棋盘位置) 3)更新记录棋盘状态的二维数组 4)重新渲染绘制棋盘. -------------落子动作代码示例如下-------------- 一个棋子类Chessman.java package xchen.

  • 基于Java制作一个好玩的打飞机游戏

    目录 1.效果图 2.项目整体构造 3.主类代码展示 4.飞机类代码展示 5.炮弹类代码展示 6.爆炸类代码展示 1.效果图 2.项目整体构造 3.主类代码展示 public class MyGameFrame  extends  Frame {          Image   planeImg  = GameUtil.getImage("images/plane.png");     Image   bg  = GameUtil.getImage("images/bg.j

随机推荐