scr.java
来自「litwizi j2me」· Java 代码 · 共 1,821 行 · 第 1/3 页
JAVA
1,821 行
//-------------------------------------------------------------------------
// The Little Wizard, by Ralph Capper (c)2003. v. Alpha 1.3
//
// This program is intended to be 'freeware' and 'open source'. You may
// use this program and source / distribute it / modify it / publish it
// under the following conditions:
//
// 1. No profit is made from this program / source
// 2. Original credit is given to the author and a link and / or email
// address provided when used: www.ralpharama.com / ralph@ralpharama.com
//
// If you like this applet and / or want to use it on your website you may
// want to consider donating $1 or similar to me: paypal: ralph@ralpharama.com
// or email me and ask for my address to send through the post - thanks.
//-------------------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class scr extends Applet implements KeyListener, Runnable
{
// Screen size
private int xsize;
private int ysize;
// Image to draw on for blocks
private Image img;
private Image title;
private Image gfx;
private Image gfx2;
// Size of our window
private int xscalesize;
private int yscalesize;
private int xleft;
private int xright;
private int ytop;
private int ybottom;
private int titsize;
// Images
private Image scoImg;
private Graphics paper;
private Graphics scores;
// Sound FX
AudioClip s_pickUp;
AudioClip s_pain;
AudioClip s_dead;
AudioClip s_message;
AudioClip s_spell;
AudioClip s_banish;
AudioClip s_explode;
AudioClip s_won;
// Our Message1
private String msg1;
private String msg2;
// In-game vars
private int gold;
private int food;
private int health;
private int walking;
private int gameState;
private int traderMsgNum;
private cauldron ourCauldron;
private boolean nearCauldron;
// Items class used to return IDs and names etc of obejcts
private items item;
// Used for scrolling image
private int xOffset;
private int yOffset;
private int xOrigin;
private int yOrigin;
// Our hero
private ent ourEnt;
// Inventory
private inv inventory;
// Speed of game
private int speed;
private int speed2;
// Messaging - trade is for big messages
// publicMsg is for messages that are shown only once
private bigMsg trade;
private boolean publicMsg[];
// Our level
private lev curLev;
// Our level from a file
private File curFile;
// Our one and only thread
Thread proc;
// *** INIT ***
public void init()
{
// Catch keypresses
addKeyListener(this);
// We can scale the window we view to be larger if we like
xsize=240;
ysize=240;
xscalesize=200;
yscalesize=200;
int a=Integer.parseInt(getParameter("xsize"));
int b=Integer.parseInt(getParameter("ysize"));
if (a!=0) xscalesize=a;
if (b!=0) yscalesize=b;
titsize=(int)ysize/3;
xleft=5;
ytop=5;
xright=xleft+xscalesize;
ybottom=ytop+yscalesize;
// Create images to draw on, this is copied to screen in paint()
img=createImage(xsize,ysize);
title=getImage(getCodeBase(),"title.jpg");
gfx=getImage(getCodeBase(),"scr.jpg");
gfx2=getImage(getCodeBase(),"scr2.jpg");
// Scoreboard is size of view window
scoImg=createImage(xscalesize,titsize);
// Paper is a Graphics pointer to img
// scores is a graphics pointer to scoImg
setBackground(Color.gray);
setForeground(Color.white);
paper=img.getGraphics();
paper.setFont(new Font("", Font.PLAIN, 10));
scores=scoImg.getGraphics();
scores.setFont(new Font("", Font.PLAIN, 10));
scores.setColor(Color.black);
scores.fillRect(0,0,xscalesize,titsize);
// Trader - used for displaying big messages
trade=new bigMsg(paper, gfx);
publicMsg=new boolean[20];
for (int i=0; i<20; i++) {publicMsg[i]=false;}
doInit();
}
public void doInit()
{
// Messages and scores etc, note, walking is a counter used to decrease food
gold=0;
food=20;
health=100;
msg1="(Version 1.3 - Feb 2004)";
msg2="* Click in window and press any key *";
walking=0;
traderMsgNum=1;
ourCauldron=new cauldron();
nearCauldron=false;
// Items class for getting names of objects
item=new items();
// Starting position for window view
xOffset=20;
yOffset=20;
xOrigin=7;
yOrigin=8;
// Starting position for our ent
ourEnt=new ent(xOrigin*20,yOrigin*20,98);
ourEnt.dirL=false;
ourEnt.dirR=false;
ourEnt.dirU=false;
ourEnt.dirD=false;
ourEnt.moves=true;
ourEnt.moveSpeed=4;
// Inventory
inventory=new inv(scores, gfx);
// Speed of game, default = 40ms sleep
speed=20;
speed2=20;
updateScores();
inventory.drawInv();
gameState=-2;
}
// What takes time is loading the pictures and sounds, so
// show something whilst we wait.
public void loader()
{
paper.setColor(Color.black);
paper.fillRect(0,0,xsize,ysize);
paper.setColor(Color.green);
paper.drawString("Loading . . . Please Wait",65,100);
// Get our sound FX
s_pickUp = getAudioClip(getDocumentBase(),"bleep.au");
s_pain = getAudioClip(getDocumentBase(),"pain.au");
s_dead = getAudioClip(getDocumentBase(),"dead.au");
s_message = getAudioClip(getDocumentBase(),"message.au");
s_spell = getAudioClip(getDocumentBase(),"spell.au");
s_banish = getAudioClip(getDocumentBase(),"banish.au");
s_explode = getAudioClip(getDocumentBase(),"explode.au");
s_won = getAudioClip(getDocumentBase(),"won.au");
}
public int ticker(int t)
{
paper.setColor(Color.black);
paper.fillRect(65,110,150,10);
paper.setColor(Color.blue);
paper.fillRect(65,110,t,10);
t++;
if (t>130) t=0;
// Wait for gfx to finish loading
if (paper.drawImage(gfx,-200,0,this) &
paper.drawImage(gfx2,-200,0,this) &
paper.drawImage(title,20,20,this)) gameState=0;
repaint();
return t;
}
// *** PAINT calls
// Calls to repaint and screen refreshes redraw image to screen
public void paint(Graphics g)
{
// Draw the main screen
g.drawImage(img, xleft, ytop, xright, ybottom, xOffset, yOffset, xOffset+200, yOffset+200, this);
g.drawImage(scoImg, xleft, ybottom+5, 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);
}
// *** THREAD ***
public void start()
{
if (proc==null)
{
proc=new Thread(this);
proc.start();
}
}
public void stop()
{
proc = null;
}
// Main thread procedure
public void run()
{
int t=0;
long startTime; // Starting time of program, in milliseconds.
long endTime; // Time when computations are done, in milliseconds.
double time; // Time difference, in seconds.
startTime = System.currentTimeMillis();
Thread thisThread = Thread.currentThread();
while (proc == thisThread)
{
// Time Calculator
endTime = System.currentTimeMillis();
time = (endTime - startTime) / 1000.0;
speed2=speed-((int)time);
if (speed2<5) speed2=5;
if (speed2>100) speed2=100;
try
{
proc.sleep(speed2);
}
catch (Exception e) {}
// Time Calculator
startTime = System.currentTimeMillis();
synchronized (this)
{
// gameState=1, Normal Play
if (gameState==1)
{
// Delete us
delEnt(ourEnt);
// Move us if required
if (doDir(ourEnt))
{
if (walking++>100)
{
walking=0;
food--;
if (food<1)
{
food=0;
health--;
if (health<0)
{
s_dead.play();
doInit();
}
}
updateScores();
}
}
// Adjust screen if we need to
scrollScreen();
// Draw us
drawEnt(ourEnt);
// Move any ents in the room
moveOthers();
}
// gameState=0, Title Screen
if (gameState==0)
{
}
// gameState>99 - We're talking with a trader - we will press Y or N to buy
// information
if (gameState>99)
{
// Yes, we buy information
if (gameState==101)
{
if (gold-traderMsgNum>-1)
{
gold-=traderMsgNum;
trade.doMsg(traderMsgNum++, 0, 0);
updateScores();
}
else
{
trade.doMsg(107, 0, 0);
}
s_message.play();
gameState=99;
}
}
// gameState=-2, Load first time
if (gameState==-2)
{
gameState=-1;
loader();
}
// gameState=-1, Wait for gfx
if (gameState==-1)
{
ticker(t);
}
// Refresh level
repaint();
}
}
}
// *** DRAW / DEL ents on screen
// Draw an ent on screen (assume it on on screen)
public void drawEnt(ent e)
{
curLev.doDraw(getXscr(e.x),getYscr(e.y), e.i, e.flip);
}
// Delete an ent on screen (assume it on on screen)
public void delEnt(ent e)
{
paper.fillRect(getXscr(e.x),getYscr(e.y), 20, 20);
}
// *** Move all Ents on the current screen
public void moveOthers()
{
// Loop through array of ents currently on screen
for (int i=0; i<curLev.numInRm; i++)
{
// Only do this if we're in normal game state
if (gameState==1)
{
ent a=curLev.movInRm[i];
delEnt(a);
// If our ent is off screen, remove him.
if (offScreen(i))
{
setAsBlock(a);
curLev.removeFromRm(i);
}
// If not, we'd better move him etc...
else
{
// If thing is frozen, don't move, but countdown frozen ent, or remove it
if (a.freeze!=0)
{
// Minus freeze = dead and remove them
if (a.freeze<0)
{
curLev.removeFromRm(i);
a.z=0;
a.i=0;
}
else
{
a.freeze--;
}
}
else
{
// Move our nasty and if required, change dir
if (!doDir(a))
{
int z;
switch (a.z)
{
// **** BAT - Moves left / right ****
case 87:
if (a.dirL)
{
a.dirL=false;
a.dirR=true;
}
else
{
a.dirL=true;
a.dirR=false;
}
break;
// **** BAT - Moves up / down ****
case 88:
if (a.dirU)
{
a.dirU=false;
a.dirD=true;
}
else
{
a.dirU=true;
a.dirD=false;
}
break;
// **** DEMON - Vaguely follows you
case 94:
if (a.x < ourEnt.x)
{
a.dirR=true;
a.dirL=false;
}
if (a.x > ourEnt.x)
{
a.dirL=true;
a.dirR=false;
}
if (a.y < ourEnt.y)
{
a.dirD=true;
a.dirU=false;
}
if (a.y > ourEnt.y)
{
a.dirU=true;
a.dirD=false;
}
break;
// **** GRUNT - Vaguely follows you
case 102:
if (a.x < ourEnt.x)
{
a.dirR=true;
a.dirL=false;
}
if (a.x > ourEnt.x)
{
a.dirL=true;
a.dirR=false;
}
if (a.y < ourEnt.y)
{
a.dirD=true;
a.dirU=false;
}
if (a.y > ourEnt.y)
{
a.dirU=true;
a.dirD=false;
}
break;
// **** DEVIL - Very vaguely follows you
case 92:
if (a.x < ourEnt.x)
{
a.dirR=true;
a.dirL=false;
}
if (a.x > ourEnt.x)
{
a.dirL=true;
a.dirR=false;
}
if (a.y < ourEnt.y)
{
a.dirD=true;
a.dirU=false;
}
if (a.y > ourEnt.y)
{
a.dirU=true;
a.dirD=false;
}
// Either follows, or sometimes random
z=(int)(6*Math.random())+1;
if (z<4) break;
// **** Default - wanders around ****
default:
z=(int)(6*Math.random())+1;
switch (z)
{
case 1:
a.dirU=true;
a.dirD=false;
break;
case 2:
a.dirR=true;
a.dirL=false;
break;
case 3:
a.dirD=true;
a.dirU=false;
break;
case 4:
a.dirL=true;
a.dirR=false;
break;
case 5:
a.dirL=true;
a.dirR=false;
a.dirU=false;
a.dirD=false;
break;
case 6:
a.dirR=true;
a.dirL=false;
a.dirU=false;
a.dirD=false;
break;
}
break;
} // End case
}
// **** DEATH - Follows you nastily (So does nice zombie)
if (a.z==85 || a.z==106)
{
if (a.x < ourEnt.x)
{
a.dirR=true;
a.dirL=false;
}
if (a.x > ourEnt.x)
{
a.dirL=true;
a.dirR=false;
}
if (a.y < ourEnt.y)
{
a.dirD=true;
a.dirU=false;
}
if (a.y > ourEnt.y)
{
a.dirU=true;
a.dirD=false;
}
}
// *** - Plant creature - moves randomly all the time - 'judders'
if (a.z==100)
{
int z=(int)(4*Math.random())+1;
switch (z)
{
case 1:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?