您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Java如何實現(xiàn)接月餅小游戲的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
設(shè)計游戲界面,用swing實現(xiàn)
設(shè)計背景
設(shè)計得分物體-月餅,碰到加一分
設(shè)計障礙物-月亮,碰到會死亡
監(jiān)聽鼠標(biāo)的左右鍵,用來控制籃子左右移動
設(shè)計積分系統(tǒng)
將resource文件夾設(shè)為resource(Project Manage中可以設(shè)置),因為要用里面的圖片
游戲開始界面:
public class Start { public static void main(String[] args) throws URISyntaxException { new EatGame(); } }
public class EatGame extends JFrame implements ActionListener, MouseListener { private final int WIDTH = 400; private final int HEIGHT = 800; private int score; private int ticks; private boolean gameOver = false; private GameControl control; private List<Moon> moons; private List<Cake> cakes; private Timer timer; private ChangeE changeE; /** * Constructor for objects of class FallingGame */ public EatGame() throws URISyntaxException { changeE = new ChangeE(150, 650, "player.jpg", WIDTH); moons = new ArrayList<>(); cakes = new ArrayList<>(); addMoonAndCake(); control = new GameControl(changeE, moons, cakes); timer = new Timer(20, this); //add keybinds control.addAction("Left", -20, KeyEvent.VK_LEFT); control.addAction("Right", 20, KeyEvent.VK_RIGHT); //add components add(control); addMouseListener(this); control.addMouseListener(this); setTitle("吃月餅"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setResizable(false); setVisible(true); timer.start(); } /** * 添加月亮和月餅 */ public void addMoonAndCake() { Random r = new Random(); int x, y; x = 60 + (r.nextInt(35) * 2); y = r.nextInt(20) + moons.size() * 30; Moon moon1 = new Moon(x, y, "moon.png"); moons.add(moon1); x = 190 + r.nextInt(35) * 2; y = r.nextInt(20) + moons.size() * 30; Moon moon2 = new Moon(x, y, "moon.png"); moons.add(moon2); x = 80 + (r.nextInt(35) * 2); y = r.nextInt(20) + moons.size() * 50; Cake cake = new Cake(x, y, "cake.png"); cakes.add(cake); } /** * 更新窗口 */ private void updateFrame() { ticks++; for(int i = 0; i < moons.size(); i++) { Moon moon = moons.get(i); if(ticks % 25 == 0 && moon.getSpeed() < 10) { moon.setSpeed(moon.getSpeed() + 2); } } Iterator<Moon> moonIterator = moons.iterator(); while (moonIterator.hasNext()) { Moon moon = moonIterator.next(); // 超出屏幕 if(moon.getY() > HEIGHT) { moonIterator.remove(); } else moon.move(); } Iterator<Cake> cakeIterator = cakes.iterator(); while (cakeIterator.hasNext()) { Cake cake = cakeIterator.next(); // 超出屏幕 if(cake.getY() > HEIGHT) { cakeIterator.remove(); } else cake.move(); } if(moons.size() == 0) { addMoonAndCake(); } } private boolean checkCollision() { Rectangle rectangle = (Rectangle) changeE.getShape(); for(Moon moon : moons) { Ellipse2D circle = (Ellipse2D) moon.getShape(); // 判斷是否與圓形相撞 if (circle.intersects(rectangle)) { gameOver = true; } } Iterator<Cake> cakeIterator = cakes.iterator(); while (cakeIterator.hasNext()) { Cake cake = cakeIterator.next(); Ellipse2D circle = (Ellipse2D) cake.getShape(); if (circle.intersects(rectangle)) { score ++; // 得分 cakeIterator.remove(); } } return gameOver; } public void actionPerformed(ActionEvent e) { if(gameOver) { timer.stop(); control.drawEnd(control.getGraphics(), score); } else { //continue with game updateFrame(); checkCollision(); control.repaint(); } } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { if(gameOver) { //reset game moons.clear(); control.removeAll(); control.updateUI(); score = 0; changeE.setX(150); changeE.setY(650); addMoonAndCake(); timer.start(); repaint(); gameOver = false; } } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }
/** * 畫面繪制 */ public class GameControl extends JPanel { private InputMap inputMap; private ChangeE myChangeE; private List<Moon> moons; private List<Cake> cakes; public GameControl(ChangeE changeE, List<Moon> moons, List<Cake> cakes) { this.myChangeE = changeE; this.moons = moons; this.cakes = cakes; setBackground(Color.white); inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // 繪制背景 ImageIcon icon=new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("background.png")); Image img=icon.getImage(); g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); // 繪制玩家 g.drawImage(myChangeE.getImage(), myChangeE.getX(), myChangeE.getY(), this); // 繪制月亮 for(Moon moon : moons) { g.drawImage(moon.getImage(), moon.getX(), moon.getY(), this); } // 繪制月餅 for (Cake cake : cakes) { g.drawImage(cake.getImage(), cake.getX(), cake.getY(), this); } } public void drawEnd(Graphics g, int score) { g.setColor(Color.WHITE); g.fillRect(50, 200, 300, 300); g.setColor(Color.RED); g.drawString("祝你中秋快樂,闔家歡樂!", 100, 300); g.setColor(Color.BLUE); g.drawString("您吃的到的月餅個數(shù)為:" + score, 100, 350); g.setColor(Color.BLACK); g.drawString("鼠標(biāo)點擊重玩", 100, 400); } public void addAction(String name, int deltaX, int keyCode) { MoveAction moveAction = new MoveAction(name, deltaX); inputMap.put(KeyStroke.getKeyStroke(keyCode, 0), name); getActionMap().put(name, moveAction); } private class MoveAction extends AbstractAction implements ActionListener { private int myDeltaX; public MoveAction(String name, int deltaX) { super(name); myDeltaX = deltaX; } public void actionPerformed(ActionEvent e) { myChangeE.move(myDeltaX); } } }
感謝各位的閱讀!關(guān)于“Java如何實現(xiàn)接月餅小游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。