📄 hog puncher.txt
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;
public class mogura extends Applet implements Runnable{
Graphics g,ig;
MediaTracker mt;
Image image;
Image mogura[];
AudioClip audio;
int holeStatus[][];
int Direction[][];
int moguraWidth=63;
int moguraHeight=63;
int mogCount;
int time;
int score;
int hiscore;
Random random;
boolean exitFlag;
volatile boolean mouseButtonFlag;
volatile int mouseX;
volatile int mouseY;
Thread thread;
final int sleepTime=20;
public void init() {
resize(370,252);
image=createImage(370,252);
g =getGraphics();
ig =image.getGraphics();
ig.setColor(Color.yellow);
ig.fillRect(0,0,370,252);
ig.setFont(new Font("TimesRoman",Font.BOLD,16));
mt=new MediaTracker(this);
audio=getAudioClip(getDocumentBase(),"audio/mog.au");
random=new Random();
holeStatus=new int[4][4];
Direction=new int[4][4];
mogura=new Image[10];
for(int i=0;i<10;i++){
mogura[i]=getImage(getDocumentBase(),"images/mog"+i+".gif");
mt.addImage(mogura[i],0);
}
mouseButtonFlag=false;
}
public void start(){
if(thread==null){
thread=new Thread(this);
thread.start();
}
}
public void stop(){
if(thread!=null){
thread.stop();
thread=null;
}
}
public void paint(Graphics dmy){
g.drawImage(image,0,0,this);
}
public boolean mouseDown(Event e,int x,int y){
mouseX=x;
mouseY=y;
mouseButtonFlag=true;
return true;
}
public void run(){
exitFlag=false;
try{
mt.waitForID(0);
}catch(InterruptedException e){
return;
}
for(;;){
initTitle();
repaint();
waitMouseButton();
if(exitFlag)break;
initGame();
runGame();
if(exitFlag)break;
initGameover();
repaint();
waitMouseButton();
if(exitFlag)break;
}
}
void waitMouseButton(){
mouseButtonFlag=false;
while(mouseButtonFlag==false){
try{thread.sleep(sleepTime);
}catch(InterruptedException e){
exitFlag=true;
break;
}
}
}
void initTitle(){
if(mt.isErrorID(0)){
ig.setColor(Color.red);
ig.fillRect(0,0,370,252);
ig.setColor(Color.yellow);
ig.setFont(new Font("TimesRoman",Font.BOLD,24));
ig.drawString("Error",150,130);
return;
}
if(mt.checkID(0)){
ig.setColor(Color.yellow);
ig.fillRect(0,0,370,252);
ig.setColor(Color.red);
ig.setFont(new Font("TimesRoman",Font.BOLD,24));
ig.drawString("-- The Mogura --",105,70);
ig.setFont(new Font("TimesRoman",Font.BOLD,16));
ig.drawImage(mogura[4],130,90,this);
ig.drawImage(mogura[7],193,90,this);
ig.setColor(Color.green);
ig.drawString("Push mouse button to start !",100,180);
}else{
ig.setColor(Color.red);
ig.setFont(new Font("TimesRoman",Font.PLAIN,20));
ig.drawString("Image Loading...",70,120);
}
}
void initGameover(){
ig.setColor(Color.yellow);
ig.fillRect(0,0,370,252);
ig.drawImage(mogura[9],0,0,this);
ig.setFont(new Font("TimesRoman",Font.PLAIN,20));
ig.setColor(Color.blue);
ig.drawString("Your score is "+score+" !",100,200);
if(score>hiscore){
hiscore=score;
ig.drawString("This is new Hiscore "+hiscore+" !",80,230);
}
}
void initGame(){
mouseButtonFlag=false;
score=0;
time=300;
mogCount=0;
}
void initMog(){
int x,y,dx,dy;
for(y=dy=0;y<4;y++,dy+=moguraHeight)
for(x=dx=0;x<4;x++,dx+=moguraWidth){
holeStatus[y][x]=0;
ig.drawImage(mogura[holeStatus[y][x]],dx,dy,this);
}
}
void drawMogura(){
int level=(int)(30-time/10);
double BmogRatio=Math.sqrt(level*3)*0.015;
double GmogRatio=Math.sqrt(level*3)*0.008;
int MaxMog=(int)(Math.sqrt(level)+1);
double RetRatio=Math.sqrt(level/2)*0.3;
int x,y,dx,dy;
for(y=dy=0;y<4;y++,dy+=moguraHeight)
for(x=dx=0;x<4;x++,dx+=moguraWidth){
switch(holeStatus[y][x]){
case 0:
if(Math.random()<BmogRatio&&mogCount<MaxMog){
holeStatus[y][x]=1;
Direction[y][x]=1;
}else
if(Math.random()<GmogRatio&&mogCount<MaxMog){
holeStatus[y][x]=5;
Direction[y][x]=1;
}
break;
case 1:
holeStatus[y][x]=holeStatus[y][x]+Direction[y][x];
if(holeStatus[y][x]==4)holeStatus[y][x]=0;
if(mouseY>=dy&&mouseY<=dy+moguraHeight){
if(mouseX>=dx&&mouseX<=dx+moguraWidth)
holeStatus[y][x]=4;
}
break;
case 2:
holeStatus[y][x]=holeStatus[y][x]+Direction[y][x];
if(holeStatus[y][x]==4)holeStatus[y][x]=0;
if(mouseY>=dy&&mouseY<=dy+moguraHeight){
if(mouseX>=dx&&mouseX<=dx+moguraWidth)
holeStatus[y][x]=4;
}
break;
case 3:
if(mouseY>=dy&&mouseY<=dy+moguraHeight){
if(mouseX>=dx&&mouseX<=dx+moguraWidth)
holeStatus[y][x]=4;
}
if(Math.random()<RetRatio){
Direction[y][x]=-1;
holeStatus[y][x]=holeStatus[y][x]+Direction[y][x];
}
break;
case 5:
holeStatus[y][x]=holeStatus[y][x]+Direction[y][x];
if(holeStatus[y][x]==4)holeStatus[y][x]=0;
if(mouseY>=dy&&mouseY<=dy+moguraHeight){
if(mouseX>=dx&&mouseX<=dx+moguraWidth)
holeStatus[y][x]=8;
}
break;
case 6:
holeStatus[y][x]=holeStatus[y][x]+Direction[y][x];
if(holeStatus[y][x]==4)holeStatus[y][x]=0;
if(mouseY>=dy&&mouseY<=dy+moguraHeight){
if(mouseX>=dx&&mouseX<=dx+moguraWidth)
holeStatus[y][x]=8;
}
break;
case 7:
if(mouseY>=dy&&mouseY<=dy+moguraHeight){
if(mouseX>=dx&&mouseX<=dx+moguraWidth)
holeStatus[y][x]=8;
}
if(Math.random()<RetRatio){
Direction[y][x]=-1;
holeStatus[y][x]=holeStatus[y][x]+Direction[y][x];
}
break;
case 4:
score=score+10;
audio.play();
holeStatus[y][x]=0;
break;
case 8:
score=score-40;
audio.play();
holeStatus[y][x]=0;
break;
}
ig.drawImage(mogura[holeStatus[y][x]],dx,dy,this);
}
}
void putMog(){
int i,j;
mogCount=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++){
if(holeStatus[j][i]>0&&holeStatus[j][i]<4)
mogCount++;
}
}
void runGame(){
ig.setColor(Color.yellow);
ig.fillRect(0,0,370,252);
initMog();
for(;;){
time--;
drawMogura();
putMog();
gameStatus();
pause(100);
if(time==0){
return;
}
g.drawImage(image,0,0,this);
}
}
void gameStatus(){
ig.setColor(Color.yellow);
ig.fillRect(moguraWidth*4+5,0,370,220);
ig.setColor(Color.blue);
ig.setFont(new Font("TimesRoman",Font.BOLD,16));
ig.drawString("SCORE",270,50);
ig.drawString(Integer.toString(score),290,70);
ig.drawString("TIME",270,100);
if(time<21)
ig.setColor(Color.red);
ig.drawString(Integer.toString(time),290,120);
ig.setColor(Color.blue);
ig.drawString("HISCORE",270,150);
ig.drawString(Integer.toString(hiscore),290,170);
}
void pause(int pauseTime){
try{Thread.sleep(pauseTime);
}catch(InterruptedException e){
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -