您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家分享的是有关Java如何实现接月饼小游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
设计游戏界面,用swing实现
设计背景
设计得分物体-月饼,碰到加一分
设计障碍物-月亮,碰到会死亡
监听鼠标的左右键,用来控制篮子左右移动
设计积分系统
将resource文件夹设为resource(Project Manage中可以设置),因为要用里面的图片
游戏开始界面:
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("您吃的到的月饼个数为:" + score, 100, 350); g.setColor(Color.BLACK); g.drawString("鼠标点击重玩", 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); } } }
感谢各位的阅读!关于“Java如何实现接月饼小游戏”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。