📄 maze1.java
字号:
StringWidth = FM.stringWidth(EnglishTitle);//取得字符串EnglishTitle的Width
StringHeight = Ascent + Descent;//取得字符串EnglishTitle的Height
X = (width - StringWidth)/2; //设置EnglishTitle开始处的X值
Y = Y + Ascent; //设置EnglishTitle开始处的Y值
g.drawString(EnglishTitle,X,Y);
ImageY = Y + Descent + 30; //设置当前图像开始处的Y值
g.drawImage(currentImage,ImageX+100,ImageY+100,100,100,Game); //绘制当前的心形或星形图片
g.setFont(F3); //设置字体F3
FM = g.getFontMetrics(); //取得FontMetrics类的实例
Ascent = FM.getAscent(); //取得字符串的Ascent
Descent = FM.getDescent(); //取得字符串的Descent
StringWidth = FM.stringWidth(PressEnter); //取得字符串的Width
StringHeight = Ascent + Descent; //取得字符串的Height
X = (width - StringWidth)/2; //设置字符串开始处的X值
Y = ImageY+ImageHeight+Ascent+30+300;//设置字符串开始处的Y值
if(showPressEnter) g.drawString(PressEnter,X,Y);//绘制PressEnter字符串
}
}
class Sprite{ //角色类
int LcX,LcY; //角色的位置
int SizeW,SizeH; //图片的大小
int maze[][]; //迷宫数组
int indexI,indexJ; //角色在数组中的索引值
boolean ml = true,mr =true; //判断角色是否能向左或向右移(这个用于实现NPC怪物虫的左右移动)
public void moveUp(){ //角色向上
if(isPassed(indexI,indexJ,'U')){ //判断是否可以向上移动
LcY -= SizeW; //移动角色的Y坐标
indexI--; //改变角色纵向索引位置
}
}
public void moveDown(){ //角色向下
if(isPassed(indexI,indexJ,'D')){ //判断是否可以向下移动
LcY += SizeW; //移动角色的Y坐标
indexI++; //改变角色纵向索引位置
}
}
public void moveRight(){ //角色向右
if(isPassed(indexI,indexJ,'R')){ //判断是否可以向右移动
LcX += SizeW; //移动角色的X坐标
indexJ++; //改变角色横向索引位置
mr = true; //确定角色可以向右移动
}
else mr = false; //否则的话角色不能向右移动v
}
public void moveLeft(){ //角色向左
if(isPassed(indexI,indexJ,'L')){ //判断是否可以向左移动
LcX -= SizeW; //移动角色的X坐标
indexJ--; //改变角色横向索引位置
ml = true; //确定角色可以向左移动
}
else ml = false; //否则的话角色不能向左移动
}
//判断角色是否可以移动
private boolean isPassed(int i,int j,char d){
boolean pass = false; //先规定角色不能移动
switch(d) //利用键盘选择角色移动方向
{
case 'U': //如果是向上移动
if(maze[i-1][j] == 0||maze[i-1][j] == 3||maze[i-1][j] == 4)//判断角色所在索引位置的上一个位置是否是可以通过的
pass = true; //如果符合条件则角色可以向上移动
break; //跳出循环
case 'D': //如果是向下移动
if(maze[i+1][j] == 0||maze[i+1][j] == 3||maze[i+1][j] == 4)
pass = true;
break;
case 'R': //如果是向右移动
if(maze[i][j+1] == 0||maze[i][j+1] == 3||maze[i][j+1] == 4)
pass = true;
break;
case 'L': //如果是向左移动
if(maze[i][j-1] == 0||maze[i][j-1] == 3||maze[i][j-1] == 4)
pass = true;
break;
default:
pass = false;
}
return pass;//返回角色是否可以移动的信息,以便在主类中进行调用和判断
}
}
class Brother extends Sprite{ //哥哥类
public Brother(int x,int y,int w,int h,int[][] m, int i,int j){
LcX = x;
LcY = y;
SizeW = w;
SizeH = h;
indexI = i;
indexJ = j;
maze = m;
}
}
class Guaishou extends Sprite{ //怪兽类
public Guaishou(int x,int y,int w,int h,int[][] m,int i,int j){
LcX = x;
LcY = y;
SizeW = w;
SizeH = h;
indexI = i;
indexJ = j;
maze = m;
}
}
//主类
public class Maze1 extends Applet implements KeyListener,MouseListener,MouseMotionListener,ActionListener,Runnable{
int ImageWidth,ImageWidthb,ImageHeightb,ImageWidthg,ImageHeightg,ImageX,ImageY,AppletWidth,AppletHeight,
floorW,key,X1,X2;
int I,J;
int scores;
int sx,second,currentsecond,Gamesecond,Gamesecond1;
boolean startnow,gameover,EndGame;
char direction = 'R';
Image character,OffScreen,sky,sky2,blocks1,blocks2;
Image block1[],block2[],chong1[],chong2[],fly[];
int currentImageblock1,currentImageblock2,currentImagechong1,currentImagechong2,currentImagefly;
AudioClip A1,A4,A5;
Thread newThread;
Graphics drawOffScreen;
MediaTracker MT;
Brother s,s1;
Guaishou[] gs = new Guaishou[4];
GameFrame2 G;
int letWidth;
Image Screen,bkImage,PigHead_Normal1,PigHead_Hit;
Graphics awOffScreen;
StartScreen S_Screen,E_Screen,W_Screen;
GregorianCalendar time;
Font F1;
CloseDialog CD;
FontMetrics FM;
String ChineseTitle,E_ChineseTitle,W_ChineseTitle,press1,press2;
String EnglishTitle,E_EnglishTitle,W_EnglishTitle;
int[][] maze ={ {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,1,0,0,2,0,0,0,1,4,4,4,2,2},
{2,0,1,0,0,0,0,0,0,0,1,3,3,3,3,3,2,2},
{2,3,4,4,3,1,1,1,2,0,1,4,1,1,4,4,2,2},
{2,1,0,0,0,0,0,0,0,0,0,1,1,3,3,1,2,2},
{2,1,0,1,0,1,1,0,1,1,0,0,1,3,2,0,2,2},
{2,1,3,3,3,3,1,0,0,0,0,0,0,0,2,2,2,2},
{2,1,4,4,2,4,1,1,1,2,4,1,4,1,2,1,1,2},
{2,1,1,1,1,3,3,3,1,2,3,3,3,1,1,1,4,2},
{2,3,3,3,3,3,3,3,1,3,3,1,3,3,2,1,4,2},
{2,4,2,4,1,0,0,0,0,0,0,0,0,0,2,1,4,2},
{2,1,2,4,4,4,1,1,1,0,1,0,1,1,2,1,4,2},
{2,0,0,0,2,3,3,3,1,1,3,3,3,3,3,0,4,0},
{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
int[][] maze1 = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,1,0,0,2,0,0,0,1,4,4,4,2,2},
{2,0,1,0,0,0,0,0,0,0,1,3,3,3,3,3,2,2},
{2,3,4,4,3,1,1,1,2,0,1,4,1,1,4,4,2,2},
{2,1,0,0,0,0,0,0,0,0,0,1,1,3,3,1,2,2},
{2,1,0,1,0,1,1,0,1,1,0,0,1,3,2,0,2,2},
{2,1,3,3,3,3,1,0,0,0,0,0,0,0,2,2,2,2},
{2,1,4,4,2,4,1,1,1,2,4,1,4,1,2,1,1,2},
{2,1,1,1,1,3,3,3,1,2,3,3,3,1,1,1,4,2},
{2,3,3,3,3,3,3,3,1,3,3,1,3,3,2,1,4,2},
{2,4,2,4,1,0,0,0,0,0,0,0,0,0,2,1,4,2},
{2,1,2,4,4,4,1,1,1,0,1,0,1,1,2,1,4,2},
{2,0,0,0,2,3,3,3,1,1,3,3,3,3,3,0,4,0},
{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
public void init(){
setBackground(Color.black);
EndGame = false;
scores = 0;
CD = new CloseDialog(this,new Frame());
I=J = -1;
ChineseTitle = "迷路天使"; //开始界面中显示的游戏的中文标题
E_ChineseTitle = "游戏结束"; //结束界面中显示的中文字符
W_ChineseTitle = "胜利啦!"; //胜利界面中显示的中文字符
E_EnglishTitle = "GAME OVER";//结束界面中显示的英文字符
EnglishTitle = "lost angle"; //开始界面中显示的游戏的英文标题
W_EnglishTitle = "Congratulations!";//胜利界面中显示的英文字符
press1 = "<<<==请按Enter键开始游戏==>>>";
press2 = "要开始请选择菜单中的再来一次";
F1 = new Font("TimesRoman",Font.BOLD,20);
startnow = gameover = false;
A1 = getAudioClip(getDocumentBase(),"Audio/big big world.midi");//动画进行中的音乐
//A3 = getAudioClip(getDocumentBase(),"Audio/AUDIO1.au");
A4 = getAudioClip(getDocumentBase(),"Audio/prize.au");//吃到灵力果子的音乐
A5 = getAudioClip(getDocumentBase(),"Audio/CRWDBUGL.au");//游戏胜利时的背景音乐
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setBackground(Color.white);
AppletWidth = getSize().width;
AppletHeight = getSize().height;
G = new GameFrame2("lost angle ",AppletWidth,AppletHeight,this);
sx = 0;
currentsecond = -1;
Gamesecond = 0;
currentImageblock1 = currentImageblock2 = currentImagechong1 = currentImagechong2 = currentImagefly = 0;
block1 = new Image[3];
block2 = new Image[4];
chong1 = new Image[2];
chong2 = new Image[2];
fly = new Image[3];
MT = new MediaTracker(this);
blocks1 = getImage(getDocumentBase(),"Images/BLOCK1.gif");
blocks2 = getImage(getDocumentBase(),"Images/BLOCK2.gif");
sky = getImage(getDocumentBase(),"Images/sky03.png"); //取得所需图片
character = getImage(getDocumentBase(),"Images/people.png");
bkImage = getImage(getDocumentBase(),"Images/background.png");
for(int i=0;i<3;i++){
fly[i] = getImage(getDocumentBase(),"Images/fly"+(i+1)+".png");
MT.addImage(fly[i],0);
}
for(int i=0; i<3; i++) {
block1[i] = getImage(getDocumentBase(),"Images/heart"+(i+1)+".png");
MT.addImage(block1[i],0);
}
for(int i=0; i<4; i++) {
block2[i] = getImage(getDocumentBase(),"Images/star"+(i+1)+".png");
MT.addImage(block2[i],0);
}
for(int i=0;i<2;i++){
chong1[i] = getImage(getDocumentBase(),"Images/grub"+(i+1)+".png");
MT.addImage(chong1[i],0);
}
for(int i=0;i<2;i++){
chong2[i] = getImage(getDocumentBase(),"Images/grub"+(i+3)+".png");
MT.addImage(chong2[i],0);
}
MT.addImage(sky,0); //添加到图片监听器中
MT.addImage(character,0);
MT.addImage(blocks1,0);
MT.addImage(blocks2,0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -