📄 pacman.java
字号:
}
if (count==0)
{
if ((screendata[pos]&15)==15)
{
ghostdx[i]=0;
ghostdy[i]=0;
}
else
{
ghostdx[i]=-ghostdx[i];
ghostdy[i]=-ghostdy[i];
}
}
else
{
count=(int)(Math.random()*count);
if (count>3) count=3;
ghostdx[i]=dx[count];
ghostdy[i]=dy[count];
}
}
ghostx[i]=ghostx[i]+(ghostdx[i]*ghostspeed[i]);
ghosty[i]=ghosty[i]+(ghostdy[i]*ghostspeed[i]);
DrawGhost(ghostx[i]+1,ghosty[i]+1);
if (pacmanx>(ghostx[i]-12) && pacmanx<(ghostx[i]+12) &&
pacmany>(ghosty[i]-12) && pacmany<(ghosty[i]+12) && ingame)
{
if (scared)
{
score+=10;
ghostx[i]=7*blocksize;
ghosty[i]=7*blocksize;
}
else
{
dying=true;
deathcounter=64;
}
}
}
}
public void DrawGhost(int x, int y)
{
if (ghostanimpos==0 && !scared)
{
goff.drawImage(ghost1,x,y,this);
}
else if (ghostanimpos==1 && !scared)
{
goff.drawImage(ghost2,x,y,this);
}
else if (ghostanimpos==0 && scared)
{
goff.drawImage(ghostscared1,x,y,this);
}
else if (ghostanimpos==1 && scared)
{
goff.drawImage(ghostscared2,x,y,this);
}
}
public void MovePacMan()
{
int pos;
short ch;
if (reqdx==-pacmandx && reqdy==-pacmandy)
{
pacmandx=reqdx;
pacmandy=reqdy;
viewdx=pacmandx;
viewdy=pacmandy;
}
if (pacmanx%blocksize==0 && pacmany%blocksize==0)
{
pos=pacmanx/blocksize+nrofblocks*(int)(pacmany/blocksize);
ch=screendata[pos];
if ((ch&16)!=0)
{
screendata[pos]=(short)(ch&15);
score++;
}
if ((ch&32)!=0)
{
scared=true;
scaredcount=scaredtime;
screendata[pos]=(short)(ch&15);
score+=5;
}
if (reqdx!=0 || reqdy!=0)
{
if (!( (reqdx==-1 && reqdy==0 && (ch&1)!=0) ||
(reqdx==1 && reqdy==0 && (ch&4)!=0) ||
(reqdx==0 && reqdy==-1 && (ch&2)!=0) ||
(reqdx==0 && reqdy==1 && (ch&8)!=0)))
{
pacmandx=reqdx;
pacmandy=reqdy;
viewdx=pacmandx;
viewdy=pacmandy;
}
}
// Check for standstill
if ( (pacmandx==-1 && pacmandy==0 && (ch&1)!=0) ||
(pacmandx==1 && pacmandy==0 && (ch&4)!=0) ||
(pacmandx==0 && pacmandy==-1 && (ch&2)!=0) ||
(pacmandx==0 && pacmandy==1 && (ch&8)!=0))
{
pacmandx=0;
pacmandy=0;
}
}
pacmanx=pacmanx+pacmanspeed*pacmandx;
pacmany=pacmany+pacmanspeed*pacmandy;
}
public void DrawPacMan()
{
if (viewdx==-1)
DrawPacManLeft();
else if (viewdx==1)
DrawPacManRight();
else if (viewdy==-1)
DrawPacManUp();
else
DrawPacManDown();
}
public void DrawPacManUp()
{
switch(pacmananimpos)
{
case 1:
goff.drawImage(pacman2up,pacmanx+1,pacmany+1,this);
break;
case 2:
goff.drawImage(pacman3up,pacmanx+1,pacmany+1,this);
break;
case 3:
goff.drawImage(pacman4up,pacmanx+1,pacmany+1,this);
break;
default:
goff.drawImage(pacman1,pacmanx+1,pacmany+1,this);
break;
}
}
public void DrawPacManDown()
{
switch(pacmananimpos)
{
case 1:
goff.drawImage(pacman2down,pacmanx+1,pacmany+1,this);
break;
case 2:
goff.drawImage(pacman3down,pacmanx+1,pacmany+1,this);
break;
case 3:
goff.drawImage(pacman4down,pacmanx+1,pacmany+1,this);
break;
default:
goff.drawImage(pacman1,pacmanx+1,pacmany+1,this);
break;
}
}
public void DrawPacManLeft()
{
switch(pacmananimpos)
{
case 1:
goff.drawImage(pacman2left,pacmanx+1,pacmany+1,this);
break;
case 2:
goff.drawImage(pacman3left,pacmanx+1,pacmany+1,this);
break;
case 3:
goff.drawImage(pacman4left,pacmanx+1,pacmany+1,this);
break;
default:
goff.drawImage(pacman1,pacmanx+1,pacmany+1,this);
break;
}
}
public void DrawPacManRight()
{
switch(pacmananimpos)
{
case 1:
goff.drawImage(pacman2right,pacmanx+1,pacmany+1,this);
break;
case 2:
goff.drawImage(pacman3right,pacmanx+1,pacmany+1,this);
break;
case 3:
goff.drawImage(pacman4right,pacmanx+1,pacmany+1,this);
break;
default:
goff.drawImage(pacman1,pacmanx+1,pacmany+1,this);
break;
}
}
public void DrawMaze()
{
short i=0;
int x,y;
bigdotcolor=bigdotcolor+dbigdotcolor;
if (bigdotcolor<=64 || bigdotcolor>=192)
dbigdotcolor=-dbigdotcolor;
for (y=0; y<scrsize; y+=blocksize)
{
for (x=0; x<scrsize; x+=blocksize)
{
goff.setColor(mazecolor);
if ((screendata[i]&1)!=0)
{
goff.drawLine(x,y,x,y+blocksize-1);
}
if ((screendata[i]&2)!=0)
{
goff.drawLine(x,y,x+blocksize-1,y);
}
if ((screendata[i]&4)!=0)
{
goff.drawLine(x+blocksize-1,y,x+blocksize-1,y+blocksize-1);
}
if ((screendata[i]&8)!=0)
{
goff.drawLine(x,y+blocksize-1,x+blocksize-1,y+blocksize-1);
}
if ((screendata[i]&16)!=0)
{
goff.setColor(dotcolor);
goff.fillRect(x+11,y+11,2,2);
}
if ((screendata[i]&32)!=0)
{
goff.setColor(new Color(224,224-bigdotcolor,bigdotcolor));
goff.fillRect(x+8,y+8,8,8);
}
i++;
}
}
}
public void ShowIntroScreen()
{
String s;
goff.setFont(largefont);
goff.setColor(new Color(0,32,48));
goff.fillRect(16, scrsize/2 - 40, scrsize-32,80);
goff.setColor(Color.white);
goff.drawRect(16, scrsize/2 - 40, scrsize-32,80);
if (showtitle)
{
s="Java PacMan";
scared=false;
goff.setColor(Color.white);
goff.drawString(s,(scrsize-fmlarge.stringWidth(s)) / 2 +2, scrsize/2 - 20 +2);
goff.setColor(new Color(96,128,255));
goff.drawString(s,(scrsize-fmlarge.stringWidth(s)) / 2, scrsize/2 - 20);
s="(c)2000 by Brian Postma";
goff.setFont(smallfont);
goff.setColor(new Color(255,160,64));
goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 + 10);
s="b.postma@hetnet.nl";
goff.setColor(new Color(255,160,64));
goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 + 30);
}
else
{
goff.setFont(smallfont);
goff.setColor(new Color(96,128,255));
s="'S' to start game";
goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 - 10);
goff.setColor(new Color(255,160,64));
s="Use cursor keys to move";
goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 + 20);
scared=true;
}
count--;
if (count<=0)
{ count=screendelay; showtitle=!showtitle; }
}
public void DrawScore()
{
int i;
String s;
goff.setFont(smallfont);
goff.setColor(new Color(96,128,255));
s="Score: "+score;
goff.drawString(s,scrsize/2+96,scrsize+16);
for (i=0; i<pacsleft; i++)
{
goff.drawImage(pacman3left,i*28+8,scrsize+1,this);
}
}
public void CheckScared()
{
scaredcount--;
if (scaredcount<=0)
scared=false;
if (scared && scaredcount>=30)
mazecolor=new Color(192,32,255);
else
mazecolor=new Color(32,192,255);
if (scared)
{
screendata[7*nrofblocks+6]=11;
screendata[7*nrofblocks+8]=14;
}
else
{
screendata[7*nrofblocks+6]=10;
screendata[7*nrofblocks+8]=10;
}
}
public void CheckMaze()
{
short i=0;
boolean finished=true;
while (i<nrofblocks*nrofblocks && finished)
{
if ((screendata[i]&48)!=0)
finished=false;
i++;
}
if (finished)
{
score+=50;
DrawScore();
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
}
if (nrofghosts < maxghosts)
nrofghosts++;
if (currentspeed<maxspeed)
currentspeed++;
scaredtime=scaredtime-20;
if (scaredtime<minscaredtime)
scaredtime=minscaredtime;
LevelInit();
}
}
public void run()
{
long starttime;
Graphics g;
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
g=getGraphics();
while(true)
{
starttime=System.currentTimeMillis();
try
{
paint(g);
starttime += 40;
Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}
}
}
public void start()
{
if (thethread == null) {
thethread = new Thread(this);
thethread.start();
}
}
public void stop()
{
if (thethread != null) {
thethread.stop();
thethread = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -