Java版吃豆游戏及源码-阿里云开发者社区

开发者社区> 科技小能手> 正文

Java版吃豆游戏及源码

简介:
+关注继续查看
这是一个吃豆游戏的Java实现,具体代码较多请下载文件。

下载地址:[url]http://download.csdn.net/source/396925[/url]




package org.loon.test;

import java.awt.Color;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;

/** 

* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2008
* </p>
* <p>
* Company: LoonFramework
* </p>
* <p>
* License: [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* </p>

* @author chenpeng
* @email:[email]ceponline@yahoo.com.cn[/email]
* @version 0.1
*/

class Timer implements Runnable {
        Pacgame ap;

        int duration;

        private Thread myThread;

        private boolean loopf = true;

        private long tickCount;

        public Timer(Pacgame ap, int duration) {
                this.ap = ap;
                this.duration = duration;
        }

        public Timer(Pacgame ap) {
                this(ap, 16);
        }

        public void on() {
                myThread = new Thread(this);
                myThread.start();
        }

        public void off() {
                loopf = false;
        }

        public void run() {
                loopf = true;
                tickCount = System.currentTimeMillis();
                while (loopf) {
                        try {
                                Thread.sleep(duration);
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                        ap.notifyFrame();
                }
        }

        public int frames() {
                long nowTick = System.currentTimeMillis();
                int frame = (int) (nowTick - tickCount) / duration;
                if (frame <= 0) {
                        frame = 1;
                        tickCount = nowTick;
                } else if (frame > 5) {
                        frame = 5;
                        tickCount = nowTick;
                } else {
                        tickCount += frame * duration;
                }
                return frame;
        }
}

public class Pacgame extends Panel implements Runnable {
        /** *//**
         * 
         */

        private static final long serialVersionUID = 1L;

        private static int WIDTH = 256;

        private static int HEIGHT = 224;

        Thread thread;

        //构造一个双缓冲的、可变的image
        VolatileImage offImage;

        Image bg, bg0;

        CGloader loader;

        MazeManager mzmanager;

        MapManager emanager;

        SpriteManager spmanager, chipmanager;

        MonsterSprite redmon, ao, pink, monster;

        PacSprite pac;

        AttractManager atMng;

        Timer timer;

        int key = 0;

        int score;

        int number1;

        boolean loadOK = false;

        boolean isAttr = false;

        public void init() {
                ClassLoader cl = getClass().getClassLoader();
                Toolkit tk = Toolkit.getDefaultToolkit();
                bg0 = tk.createImage(cl.getResource("main.png"));
                bg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB_PRE);
                MediaTracker mt = new MediaTracker(this);
                mt.addImage(bg0, 0);
                try {
                        mt.waitForID(0);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                loader = new CGloader("pacchr.png"this);
                loader.setAlpha(0, 8);

                mzmanager = new MazeManager();

                emanager = new MapManager();

                spmanager = new SpriteManager(4);
                chipmanager = new SpriteManager(5);

                pac = new PacSprite(10, 20, loader);
                redmon = new RedMonster(10, 10, mzmanager, loader);
                redmon.setTarget(pac);
                spmanager.add(redmon);
                ao = new BlueMonster(9, 13, mzmanager, loader);
                ao.setTarget(pac);
                spmanager.add(ao);
                pink = new PinkMonster(10, 13, mzmanager, loader);
                pink.setTarget(pac);
                spmanager.add(pink);
                monster = new Monster(11, 13, mzmanager, loader);
                monster.setTarget(pac);
                spmanager.add(monster);
                for (int i = 0; i < 5; i++) {
                        chipmanager.add(new ChipSprite(i * 16, 0, loader, i * 16, 130, 16,
                                        16));
                }

                atMng = new AttractManager(loader);
                timer = new Timer(this, 16);

                setBackground(Color.black);
                setSize(WIDTH, HEIGHT);

                offImage = createVolatileImage(WIDTH, HEIGHT);

        }

        public void paint(Graphics g) {
                if (!loadOK)
                        return;
                do {
                        int returnCode = offImage.validate(getGraphicsConfiguration());
                        if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE){
                                offImage = createVolatileImage(WIDTH, HEIGHT);
                        }
                        Graphics volG = offImage.getGraphics();
                        volG.drawImage(bg, 0, 0, this);
                        spmanager.draw(volG, this);
                        pac.draw(volG, this);
                        chipmanager.draw(volG, this);
                        if (isAttr)
                                atMng.draw(volG, this);
                        volG.dispose();
                        g.drawImage(offImage, 0, 0, this);
                } while (offImage.contentsLost());
        }

        public void update(Graphics g) {
                paint(g);
        }

        public void start() {
                System.gc();
                if (thread == null) {
                        thread = new Thread(this);
                        thread.start();
                }
        }

        @SuppressWarnings("deprecation")
        public void stop() {
                if (thread != null) {
                        thread.stop();
                        thread = null;
                }
        }

        public void run() {

                while (true) {
                        // MAIN LOOP
                        score = 0;
                        number1 = 3;
                        int stage = 1;
                        esaInit();
                        loadOK = true;
                        while (number1 > 0) {
                                while (gameMain(stage) == 2) {
                                        if (stage == 2)
                                                gameTheater();
                                        stage++;
                                        number1++;// 1:DEAD 2:CLEAR
                                        esaInit();
                                }
                        }
                        // GAME OVER
                        isAttr = true;
                        spmanager.setShow(false);
                        pac.setShow(false);
                        atMng.player = true;
                        atMng.ready = false;
                        atMng.gameover = true;
                        repaint();

                        timer.on();
                        key = 0;
                        while (true) {
                                if (key > 0)
                                        break;
                                waitFrame();
                        }
                        timer.off();
                        atMng.player = false;
                        atMng.gameover = false;
                }

        }

        public void esaInit() {
                Graphics g = bg.getGraphics();
                g.drawImage(bg0, 0, 0, null);
                g.dispose();
                emanager.init(bg);
        }

        public int gameMain(int stage) {
                atMng.init(bg, score, number1);
                spmanager.init();
                pac.init();
                // 游戏状态 1:DEAD 2:CLEAR
                int gamestat = 0;
                int renzoku = 0;
                boolean ispower = false;
                boolean iseyesnd = false;
                Sprite chip;

                isAttr = true;
                int wait;
                if ((score == 0) && (number1 == 3)) {
                        wait = 2000;
                } else {
                        wait = 1000;
                }

                spmanager.setShow(false);
                pac.setShow(false);
                atMng.player = true;
                atMng.ready = true;
                repaint();
                sleep(wait);

                atMng.setBattle(--number1);
                spmanager.setShow(true);
                pac.setShow(true);
                atMng.player = false;
                atMng.ready = true;
                repaint();
                sleep(wait);
                isAttr = false;

                int time = 0;
                timer.on();
                int frame = 1;
                while (gamestat == 0) {
                        for (int l = 0; l < frame; l++) {
                                int score0 = score;
                                if (time == 60 * 55) {
                                        spmanager.setNawabari(false);
                                } else if (time == 60 * 50) {
                                        spmanager.setNawabari(true);
                                } else if (time == 60 * 30) {
                                        spmanager.setNawabari(false);
                                } else if (time == 60 * 25) {
                                        spmanager.setNawabari(true);
                                } else if (time == 60 * 5) {
                                        spmanager.setNawabari(false);
                                }
                                spmanager.run();
                                MonsterSprite hitmon = spmanager.isMonsterHit(pac);
                                if (hitmon != null) {
                                        int state = hitmon.getState();
                                        if (state == 0)
                                                gamestat = 1;
                                        if (state == 1) {
                                                renzoku++;
                                                score = score + (1 << renzoku) * 100;
                                                chip = chipmanager.getSprite(renzoku);
                                                int x = hitmon.getX();
                                                int y = hitmon.getY();
                                                chip.setXY(x, y);
                                                chip.setShow(true);
                                                hitmon.setShow(false);
                                                pac.setShow(false);

                                                try {
                                                        Thread.sleep(50);
                                                } catch (Exception e) {
                                                }

                                                repaint();
                                                for (int i = 0; i < 50; i++) {
                                                        try {
                                                                Thread.sleep(10);
                                                        } catch (Exception e) {
                                                        }
                                                }
                                                chip.setShow(false);
                                                hitmon.setShow(true);
                                                pac.setShow(true);
                                                hitmon.activity();
                                                iseyesnd = true;

                                                if (ispower) {
                                                        if (iseyesnd)
                                                                try {
                                                                        Thread.sleep(10);
                                                                } catch (Exception e) {
                                                                }

                                                }
                                        }
                                }

                                pac.setKey(key);
                                int esa = pac.run(mzmanager, emanager);
                                if (esa > 0) {
                                        int remain = emanager.getRemain();
                                        score += 10;
                                        if (remain <= 0)
                                                gamestat = 2;
                                }
                                if (esa == 2) {
                                        redmon.state();
                                        ao.state();
                                        pink.state();
                                        monster.state();
                                        pac.powerup();
                                        renzoku = 0;
                                        score += 40;
                                        ispower = true;
                                }

                                if (ispower) {
                                        if (!pac.isPower()
                                                        || ((redmon.getState() != 1)
                                                                        && (ao.getState() != 1)
                                                                        && (pink.getState() != 1) && (monster
                                                                        .getState() != 1))) {

                                                ispower = false;
                                        }
                                }
                                if (iseyesnd) {
                                        if ((redmon.getState() != 2) && (ao.getState() != 2)
                                                        && (pink.getState() != 2)
                                                        && (monster.getState() != 2)) {

                                                iseyesnd = false;
                                        }
                                }

                                if (score != score0) {
                                        atMng.setScore(score);
                                }
                                time++;
                                score0 = score;
                                if (gamestat > 0)
                                        break;
                        } // FPS LOOP END

                        repaint();
                        frame = waitFrame();
                        System.gc();
                }
                timer.off();

                try {
                        Thread.sleep(2000);
                } catch (Exception e) {
                }

                return gamestat;
        }

        public void gameTheater() {
                Graphics g = bg.getGraphics();
                g.setColor(Color.black);
                g.fillRect(0, 0, WIDTH, HEIGHT);
                g.dispose();

                int pacx = WIDTH;
                int p;
                int index = 288;
                spmanager.setShow(false);
                pac.init();
                pac.setShow(true);
                redmon.init();
                redmon.setShow(true);

                timer.on();
                int frame = 1;
                int i = 0;
                while (i < 380) {
                        for (int j = 0; j < frame; j++) {
                                p = 2;
                                switch ((i >> 2) % 4) {
                                case 1:
                                        p += 4;
                                        break;
                                case 3:
                                        p = 8;
                                        break;
                                }
                                pac.setP(p);
                                pac.setXY(pacx--, 100);
                                redmon.setXY(index--, 100);
                                i++;
                                if (i >= 360)
                                        break;
                        }
                        repaint();
                        frame = waitFrame();
                }

                pacx = -32;
                redmon.state();
                index = 0;

                i = 0;
                while (i < 380) {
                        for (int j = 0; j < frame; j++) {
                                p = 0;
                                switch ((i >> 2) % 4) {
                                case 1:
                                        p += 4;
                                        break;
                                case 3:
                                        p = 8;
                                        break;
                                }
                                pac.setP(p);
                                pac.setXY(pacx++, 100);
                                redmon.setXY(index++, 100);
                                i++;
                                if (i >= 360)
                                        break;
                        }
                        repaint();
                        frame = waitFrame();
                }

                timer.off();
        }

        public void sleep(int tm) {
                try {
                        Thread.sleep(tm);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public boolean handleEvent(Event e) {
                switch (e.id) {
                case Event.KEY_PRESS:
                case Event.KEY_ACTION:
                        switch (e.key) {
                        case Event.RIGHT:
                                key = 1;
                                break;
                        case Event.DOWN:
                                key = 2;
                                break;
                        case Event.LEFT:
                                key = 4;
                                break;
                        case Event.UP:
                                key = 8;
                                break;
                        }
                        return true;
                case Event.KEY_RELEASE:
                case Event.KEY_ACTION_RELEASE:
                default:
                }
                return false;
        }

        /** *//**
         * fps计算
         * 
         * @return
         */

        private synchronized int waitFrame() {
                try {
                        wait();
                } catch (InterruptedException e) {
                }
                // 返回fps
                return timer.frames();
        }

        public synchronized final void notifyFrame() {
                notifyAll();
        }

        public static void main(String[] args) {
                final Pacgame ap = new Pacgame();
                ap.setSize(WIDTH, HEIGHT);
                Frame frm = new Frame("Java吃豆游戏");
                frm.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                                ap.stop();
                                System.exit(0);
                        }
                });
                frm.addNotify();
                Insets inset = frm.getInsets();
                int w0 = inset.left + inset.right;
                int h0 = inset.top + inset.bottom;
                frm.add(ap);
                frm.setSize(ap.getWidth() + w0, ap.getHeight() + h0);
                frm.setResizable(false);
                frm.setLocationRelativeTo(null);
                frm.setVisible(true);
                ap.requestFocus();
                ap.init();
                ap.start();
        }

}
本文转自 cping 51CTO博客,原文链接:http://blog.51cto.com/cping1982/116833



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
9497 0
java游戏开发杂谈 - 游戏物体
java游戏开发杂谈 - 游戏物体现实生活中,有很多物体,每个物体的长相、行为都不同。 物体存在于不同的空间内,它只在这个空间内发生作用。 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理。
958 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
13186 0
《Java 2D游戏编程入门》—— 1.1 使用FrameRate类
FrameRate类位于javagames.util包中。本书中所开发的工具代码都会放到这个工具包中,随后,我们将把这个包变成一个工具库。这个类用来测量本书中所开发的应用程序的每秒的帧数(frames per seconds,FPS)。
1806 0
23706
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载