📄 bgame.java
字号:
// Bee 'Game', by R.Capper (c)2003. v. beta 1.0.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class BGame extends Applet implements KeyListener, Runnable
{
// Sound FX
AudioClip s_pickUp;
AudioClip s_level;
AudioClip s_dead;
// Image to draw on for blocks
private Graphics paper;
private Image img;
// Images used in game, an array of gfx, size is pixels, numofgfx is number
// in array
private Image gfx;
private int gfxSize;
private int numOfGfx;
// Holds your block and other room related info
private int yourBlock;
// Size of main screen
private int xsize, ysize;
// Our game room, rm=instance of Room
// roomsize=num of blocks in room (max)
private Room rm;
private int roomSize;
// Current room
private int roomNum;
// levels object contains level data
private Levels doLevel;
// For game state gandling
private int oldGameState;
// Our one and only thread
Thread proc;
private int delay;
private int gameState; // 0=Title Screen, 1=Loading Page, 2=Playing Game
public void init()
{
// Set-up all that needs doing only once
s_pickUp = getAudioClip(getDocumentBase(),"bleep.au");
s_level = getAudioClip(getDocumentBase(),"level.au");
s_dead = getAudioClip(getDocumentBase(),"dead.au");
// Catch keypresses
addKeyListener(this);
// Size of window - this is fixed here, html code must set
// window size of 400 x 300
xsize=380;
ysize=380;
gfxSize=20;
numOfGfx=10;
// Setup levels class
doLevel=new Levels();
// Create images to draw on, these is copied to screen in paint()
img=createImage(xsize,ysize);
// Paper is a Graphics pointer to img
paper=img.getGraphics();
// Set-up a new level
delay=50;
roomNum=1;
startOver();
}
// Next room on level
public void nextRoom()
{
// All the stetting up of things for a new level
gameState=1;
roomNum++;
setBackground(Color.darkGray);
setForeground(Color.green);
paper.setColor(Color.black);
paper.fillRect(0,0,xsize,ysize);
paper.setColor(Color.green);
}
public void startOver()
{
// All the stetting up of things for a new level
// Set-up initial appearance of screen
gameState=0;
oldGameState=0;
setUpRoom(0);
setBackground(Color.darkGray);
setForeground(Color.green);
paper.setColor(Color.black);
paper.fillRect(0,0,xsize,ysize);
paper.setColor(Color.green);
}
// Calls to repaint and screen refreshes redraw image to screen
public void paint(Graphics g)
{
// Draw the main screen
g.drawImage(img, 0, 0, this);
}
public void update(Graphics g)
// Without this, Java paints a rectangle over the screen to del it
// then calls paint (and flickers)
{
paint(g);
}
public void start()
// Start our thread
{
if (proc==null)
{
proc=new Thread(this);
proc.start();
}
}
public void stop()
// Stop our thread
{
if (proc!=null)
{
proc.stop();
proc=null;
}
}
public void run()
// Main process
{
int slp=100; // Used to change delay time on thread
int drBlk=0; // Used to draw room slowly
int lastFlowers=0; // If numflowers has changed, redraw stats
while (true)
{
// Decided how much delay based on state of game
switch (gameState)
{
// Title screen
case 0:
slp=500;
break;
// Loading screen
case 1:
slp=5;
break;
// normal gameplay
case 2:
slp=delay;
break;
// Instructions
case 10:
slp=500;
break;
default:
slp=100;
break;
}
try
{
proc.sleep(slp);
} catch (Exception e) {}
synchronized (this)
{
// Depending on game state
switch (gameState)
{
case 0:
// Do nothing except wait for keypress
// and draw blank.dat room
rm.forceRoom();
paper.setColor(Color.green);
paper.drawString("Press 'i' for help or any key to begin the game",5,343);
break;
case 1:
// Draw room and then switch to animate
setUpRoom(roomNum);
lastFlowers=rm.numFlowers;
rm.forceRoom();
gameState=2;
break;
case 2:
// Normal state of play in mid game
rm.drawRoom();
// Have flowers changed?
if (lastFlowers!=rm.numFlowers)
{
s_pickUp.play();
// Draw number to collect
lastFlowers=rm.numFlowers;
drawStats();
// Collected all flowers?
if (rm.numFlowers<1)
{
s_level.play();
nextRoom();
break;
}
}
// Dead?
if (rm.dead)
{
s_dead.play();
startOver();
}
break;
// Show instructions
case 10:
// Do nothing except wait for keypress
drawInstructions();
break;
default:
break;
}
repaint();
}
}
}
// Load room data using doFile class
// A string is returned and translated into all the blocks
// required for this room
public void setUpRoom(int fn)
{
String levString;
int a;
int x,y;
char s;
// Load gfx for this level set
gfx=getImage(getCodeBase(),"default.jpg");
// Wait for gfx to finish loading
while (!paper.drawImage(gfx,0,0,this));
// Clear screen
paper.setColor(Color.black);
paper.fillRect(0,0,xsize,ysize);
paper.setColor(Color.green);
// Room data appears in this string levString
doLevel.getLevel(fn);
levString=doLevel.levCode;
// Draw room name and clues on screen
paper.drawString(doLevel.levName+" (Room "+roomNum+")",5,332);
paper.drawString(doLevel.levLine1,5,343);
paper.drawString(doLevel.levLine2,5,354);
// Set room size (num of blocks) to maximum possible,
// we reset this value after we reading in room data and
// know more
roomSize=levString.length()-1;
// Create the entities
rm=new Room(roomSize, xsize, paper, gfx);
// 'Set them up'
// Read dtate from string char by char, translating each one
// into a block type at correct location if (and only if) it
// is not a ' ' blank.
a=1;
x=0;
y=0;
rm.numFlowers=0;
for (int j=0; j<roomSize; j++)
{
s=levString.charAt(j);
switch (s)
{
// Pushy Block
case '$':
rm.rmBlock[a].ID=10;
rm.rmBlock[a].xpos=x;
rm.rmBlock[a].ypos=y;
rm.rmBlock[a].moveAble=true;
rm.rmBlock[a].setAnimArray(3, 3, 3, 3, 3, 3, 3, 3, 3, 3);
rm.rmBlock[a].animate();
a++;
break;
// Soluable Block
case '.':
rm.rmBlock[a].ID=5;
rm.rmBlock[a].xpos=x;
rm.rmBlock[a].ypos=y;
rm.rmBlock[a].setAnimArray(17, 17, 17, 17, 17, 17, 17, 17, 17, 17);
rm.rmBlock[a].animate();
a++;
break;
// Bomb
case '!':
rm.rmBlock[a].ID=35;
rm.rmBlock[a].xpos=x;
rm.rmBlock[a].ypos=y;
rm.rmBlock[a].setAnimArray(35, 36, 35, 36, 35, 36, 35, 36, 35, 36);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -