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: http://www.apache.org/licenses/LICENSE-2.0
* </p>
*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @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();
}
}
链接地址:http://blog.csdn.net/cping1982/archive/2008/03/27/2222593.aspx
|
|
CALENDER
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 |
|
常用链接
留言簿(2)
随笔档案(3)
文章档案(9)
相册
收藏夹(18)
good sit
搜索
最新评论
阅读排行榜
评论排行榜
Powered By: 博客园 模板提供:沪江博客
|