📄 pintu.java
字号:
import java.applet.*;import java.awt.*;import java.awt.event.*;import java.net.*;public class pintu extends Applet implements MouseListener,KeyListener,Runnable{ Image[] m_Image=new Image[9]; //9个用来装入每个拼图的图片对象 Image m_ImgAll; //总的大图片 int m_nImageNo[][]=new int[3][3]; ////标志现在各个拼图的排列情况 final int NO_IMAGE=-1; //此位置没有拼图 final int IMAGE_WIDTH=120; //每张拼图的宽 final int IMAGE_HEIGHT=120; ////每张拼图的高 final int DIRECTION_UP=1; final int DIRECTION_DOWN=2; final int DIRECTION_LEFT=3; final int DIRECTION_RIGHT=4; final int DIRECTION_NONE=-1; final int DELTAX=120; //标志提示信息区的宽度 boolean bWantStartNewGame=false; //游戏是否结束,是否需要开始新游戏 int nStep=0; //已经走的步数 int nTime=0; //已经玩过的时间,以秒为单位 Thread thTimer; //计时器线程 int nScore=0; //玩家所得的分数 int m_nNumOfImg=0; //拼图底图所使用的图片的个数 String m_sImgName[]=new String[9]; //记录拼图底图的名字 boolean bOnShowAll=false; //预览的开关 AudioClip m_audioClip1,m_audioClip2; //装载要播放的声音对象 public void init() { String param=getParameter("NumOfImg"); try { m_nNumOfImg=Integer.parseInt(param); } catch(Exception e) { m_nNumOfImg=1; System.out.println("Can't convert the param's name to int."); } for(int i=0;i<m_nNumOfImg;i++) { m_sImgName[i]=getParameter("Image"+(i+1))+".jpg"; System.out.println(m_sImgName[i]); if(m_sImgName[i]==null)m_sImgName[i]="pintu.jpg"; } System.out.println(param); MediaTracker mediaTracker=new MediaTracker(this); //建立一个监视器// for(int i=0;i<9;i++)// {// int nTemp=i+1;// m_Image[i]=getImage(getDocumentBase(),"img/pintu.jpg"); m_ImgAll=getImage(getDocumentBase(),"img/"+m_sImgName[0]); //装载总的大图片// m_Image[i]=getImage(getDocumentBase(),"img/"+nTemp+".JPG");// mediaTracker.addImage(m_Image[i],i); mediaTracker.addImage(m_ImgAll,1);// } try { mediaTracker.waitForID(1); } catch(Exception e) { System.out.println("图片装载出错"); } if(mediaTracker.isErrorAny())System.out.println("图片装载出错"); for(int i=0;i<9;i++) { m_Image[i]=createImage(IMAGE_WIDTH,IMAGE_HEIGHT); Graphics g=m_Image[i].getGraphics(); int nRow=i%3; int nCol=i/3; g.drawImage(m_ImgAll,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,nRow*IMAGE_WIDTH,nCol*IMAGE_HEIGHT,(nRow+1)*IMAGE_WIDTH,(nCol+1)*IMAGE_HEIGHT,this); System.out.println("init "+i);// m_ImgAll.getGraphics().clipRect(1,1,120,120);// g.copyArea(1,1,120,120,0,0); } System.out.println("init over"); thTimer=new Thread(this); //为线程分配内存空间 thTimer.start(); //开始线程 initData(); m_audioClip1=getAudioClip(getCodeBase(),"au/move.au"); m_audioClip2=getAudioClip(getCodeBase(),"au/notmove.au"); addMouseListener(this); addKeyListener(this); } public void initImageAgain(int nImgNo) { //nImgNo为要装载的图像是第几个图像。 if(nImgNo>m_nNumOfImg) { showStatus("你要的图片不存在!!"); return; }// System.out.println(param); MediaTracker mediaTracker=new MediaTracker(this); //建立一个监视器// for(int i=0;i<9;i++)// {// int nTemp=i+1;// m_Image[i]=getImage(getDocumentBase(),"img/pintu.jpg"); m_ImgAll=getImage(getDocumentBase(),"img/"+m_sImgName[nImgNo]); //装载总的大图片// m_Image[i]=getImage(getDocumentBase(),"img/"+nTemp+".JPG");// mediaTracker.addImage(m_Image[i],i); mediaTracker.addImage(m_ImgAll,1);// } try { mediaTracker.waitForAll(); } catch(Exception e) { System.out.println("图片装载出错"); } for(int i=0;i<9;i++) { m_Image[i]=createImage(IMAGE_WIDTH,IMAGE_HEIGHT); Graphics g=m_Image[i].getGraphics(); int nRow=i%3; int nCol=i/3; g.drawImage(m_ImgAll,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,nRow*IMAGE_WIDTH,nCol*IMAGE_HEIGHT,(nRow+1)*IMAGE_WIDTH,(nCol+1)*IMAGE_HEIGHT,this); System.out.println("dfsdfdfdsdf"+i);// m_ImgAll.getGraphics().clipRect(1,1,120,120);// g.copyArea(1,1,120,120,0,0); } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.setColor(Color.white); //将当前颜色变为白色 g.fillRect(0,0,DELTAX,IMAGE_HEIGHT*3); //填充左边的提示信息区域 g.setFont(new Font("宋体",Font.PLAIN,15)); //设置字体 g.setColor(Color.blue); //设置颜色 g.drawString("步数:"+nStep,5,20); g.drawString("现有图片"+m_nNumOfImg+"张",5,60); g.drawString("请按1-"+m_nNumOfImg+"键改变图片",5,100); //在坐标(10,20)画出字串,来显示现在走了多少步。 g.setColor(Color.white); if(bOnShowAll) { int x=DELTAX; int y=0; g.drawImage(m_ImgAll,x,y,this); return; } for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { int x=i*IMAGE_WIDTH+DELTAX; int y=j*IMAGE_HEIGHT; if(m_nImageNo[i][j]==NO_IMAGE) g.fill3DRect(x,y,IMAGE_WIDTH,IMAGE_HEIGHT,true); else { g.drawImage(m_Image[m_nImageNo[i][j]],x,y,this); g.drawRect(x,y,IMAGE_WIDTH,IMAGE_HEIGHT); } } } checkStatus(); if(bWantStartNewGame) { //如果游戏结束,玩家将拼图的顺序排对之后 nScore=1000-nStep*10-nTime; g.setColor(Color.blue); g.drawString("请按任意键重新开始",5,140); g.setColor(Color.red);// g.setColor(Color.blue); g.setFont(new Font("宋体",Font.PLAIN,40)); g.drawString("你赢了"+nScore+"分",70+DELTAX,160); g.drawString("祝贺你!",110+DELTAX,210); transferScore(nScore); } } public pintu() { } public void initData() { int[] nHasDistrib=new int[9]; for(int i=0;i<9;i++)nHasDistrib[i]=0; for(int j=0;j<3;j++) { for(int i=0;i<3;i++) { int nCount=j*3+i; int nImgNo=-1; do { nImgNo=(int)(Math.random()*9); }while(nHasDistrib[nImgNo]==1); //1代表已经分配了这张图片 m_nImageNo[i][j]=nImgNo; nHasDistrib[nImgNo]=1; System.out.println("test.."); } } m_nImageNo[(int)(Math.random()*3)][(int)(Math.random()*3)]=NO_IMAGE; nStep=0; nTime=0; //清空计时器 } public int directionCanMove(int nCol,int nRow) { if((nCol-1)>=0) if(m_nImageNo[nRow][nCol-1]==NO_IMAGE) return DIRECTION_UP; if((nCol+1)<=2) if(m_nImageNo[nRow][nCol+1]==NO_IMAGE) return DIRECTION_DOWN; if((nRow-1)>=0) if(m_nImageNo[nRow-1][nCol]==NO_IMAGE) return DIRECTION_LEFT; if((nRow+1)<=2) if(m_nImageNo[nRow+1][nCol]==NO_IMAGE) return DIRECTION_RIGHT; return DIRECTION_NONE; } public void move(int nCol,int nRow,int nDirection) { switch(nDirection) { case DIRECTION_UP: m_nImageNo[nRow][nCol-1]=m_nImageNo[nRow][nCol]; m_nImageNo[nRow][nCol]=NO_IMAGE; break; case DIRECTION_DOWN: m_nImageNo[nRow][nCol+1]=m_nImageNo[nRow][nCol]; m_nImageNo[nRow][nCol]=NO_IMAGE; break; case DIRECTION_LEFT: m_nImageNo[nRow-1][nCol]=m_nImageNo[nRow][nCol]; m_nImageNo[nRow][nCol]=NO_IMAGE; break; case DIRECTION_RIGHT: m_nImageNo[nRow+1][nCol]=m_nImageNo[nRow][nCol]; m_nImageNo[nRow][nCol]=NO_IMAGE; break; } } public boolean move(int nDirection) { int nNoImageCol=-1; int nNoImageRow=-1; int i=0; int j=0; while (i<3 && nNoImageRow==-1) { while (j<3 && nNoImageCol==-1) { if(m_nImageNo[i][j]==NO_IMAGE) { nNoImageRow=i; nNoImageCol=j; } j++; } j=0; i++; } //以上判断哪个拼图可以往方向nDirection移动 //可以移动的拼图的位置为第nNoImageCol行,第nNoImageRow列。 System.out.println(nNoImageCol+",,"+nNoImageRow); switch(nDirection) { case DIRECTION_UP: if(nNoImageCol==3)return false; m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow][nNoImageCol+1]; m_nImageNo[nNoImageRow][nNoImageCol+1]=NO_IMAGE; break; case DIRECTION_DOWN: if(nNoImageCol==0)return false; m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow][nNoImageCol-1]; m_nImageNo[nNoImageRow][nNoImageCol-1]=NO_IMAGE; break; case DIRECTION_LEFT: if(nNoImageRow==3)return false; m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow+1][nNoImageCol]; m_nImageNo[nNoImageRow+1][nNoImageCol]=NO_IMAGE; break; case DIRECTION_RIGHT: if(nNoImageRow==0)return false; m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow-1][nNoImageCol]; m_nImageNo[nNoImageRow-1][nNoImageCol]=NO_IMAGE; break; } return true; } public void mouseClicked(MouseEvent e) { //Invoked when the mouse has been clicked on a component. if(bOnShowAll)return; if(bWantStartNewGame) { initData(); repaint(); bWantStartNewGame=false; return; } int nX=e.getX()-DELTAX; int nY=e.getY(); int nCol=nY/IMAGE_HEIGHT; int nRow=nX/IMAGE_WIDTH; System.out.println("col:"+nCol+" row:"+nRow); int nDirection=directionCanMove(nCol,nRow); if(nDirection!=DIRECTION_NONE) { move(nCol,nRow,nDirection); nStep++; m_audioClip1.play(); repaint(); } else { m_audioClip2.play(); } } public void mouseEntered(MouseEvent e) { //Invoked when the mouse has been clicked on a component. } public void mouseExited(MouseEvent e) { //Invoked when the mouse has been clicked on a component. } public void mousePressed(MouseEvent e) { //Invoked when the mouse has been clicked on a component. } public void mouseReleased(MouseEvent e) { //Invoked when the mouse has been clicked on a component. } public void keyPressed(KeyEvent e) { //Invoked when a key has been pressed. if(bOnShowAll) { if(e.getKeyCode()==KeyEvent.VK_Y) { bOnShowAll=false; repaint(); } return; } System.out.println("press key"+e.getKeyCode()+" " +e.getKeyText(e.getKeyCode())); System.out.println(KeyEvent.VK_LEFT); if(bWantStartNewGame) { initData(); bWantStartNewGame=false; repaint(); return; } int nDirection=DIRECTION_NONE; switch(e.getKeyCode()) { case KeyEvent.VK_DOWN: nDirection=DIRECTION_DOWN; break; case KeyEvent.VK_UP: nDirection=DIRECTION_UP; break; case KeyEvent.VK_LEFT: System.out.println("left111..."); nDirection=DIRECTION_LEFT; break; case KeyEvent.VK_RIGHT: System.out.println("left..."); nDirection=DIRECTION_RIGHT; break; case KeyEvent.VK_F1: //F1键按下,重新开始游戏 initData(); //init(); repaint(); return; case KeyEvent.VK_1: case KeyEvent.VK_2: case KeyEvent.VK_3: case KeyEvent.VK_4: case KeyEvent.VK_5: case KeyEvent.VK_6: case KeyEvent.VK_7: case KeyEvent.VK_8: case KeyEvent.VK_9: int nImgNo=e.getKeyCode()-KeyEvent.VK_1; if(nImgNo<m_nNumOfImg) { System.out.println(nImgNo); initImageAgain(nImgNo); initData(); repaint(); } return; case KeyEvent.VK_Y: if(bOnShowAll) bOnShowAll=false; else bOnShowAll=true; repaint(); return; default: return; } boolean bCanMove=move(nDirection); if(bCanMove) { nStep++; m_audioClip1.play(); repaint(); } else { m_audioClip2.play(); } } public void keyReleased(KeyEvent e) { //Invoked when a key has been pressed. } public void keyTyped(KeyEvent e) { //Invoked when a key has been pressed. } public void checkStatus() { boolean bWin=true; //定义成员,默认值为真 int nCorrectNum=0; for(int j=0;j<3;j++) { for(int i=0;i<3;i++) { if(m_nImageNo[i][j]!=nCorrectNum && m_nImageNo[i][j]!=NO_IMAGE) bWin=false; nCorrectNum++; } } //比较拼图是否都放到了正确的位置上,如果,有一个没有放到正确位置上,则游戏就不能结束。 if(bWin)bWantStartNewGame=true; } public static void main(String[] args) { pintu pintu1=new pintu(); pintu1.init(); pintu1.start(); } public void run() { while(Thread.currentThread()==thTimer) { try { thTimer.sleep(990); String sTemp="你玩了"+nTime+"秒的时间,"; if(nTime>200)sTemp=sTemp+"时间用的很长了,你可要加油啦!"; else sTemp=sTemp+"别紧张,慢慢来。"; showStatus(sTemp); if(!bWantStartNewGame)nTime++; } catch(Exception e) { } } } public void transferScore(int nScore) { String sLocation="http://162.105.101.107/saveScore.php3?score="; nScore=(nScore/2)*10+nScore%4; sLocation=sLocation+nScore; URL url=null; try { url=new URL(sLocation); } catch(Exception e){} getAppletContext().showDocument(url); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -