/* Remake of the classic pipes game This version by Stewart Wilcox */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; public class pipes extends Applet implements MouseListener,MouseMotionListener,Runnable{ public String getAppletInfo(){ return "Pipes remake.\n" +"By Stewart Wilcox\n" +"Parameters:\n" +"tilesize = width in pixels of tiles (default 40)\n" +"watersize = width of water stream in pixels (default 6)\n" +"waterspeed = time (in milliseconds) for water to move 1 pixel (default 40)\n" +"smoothness = frames for water to move 1 pixel (default 2, increase to remove dots from water stream)\n" +"countdown = number of seconds before water starts flowing (default 20)\n" +"points = points for having water flow through a square (default 10)\n" +"bonus = bonus points for having water enter a square again (default 30)\n" +"penalty = points deducted for replacing a tile (default 1)\n" +"imagestem = stem name of images used to display tiles (default 'brick')"; } int TILESZ; int WATERSZ; int framerate; int sl; int countdown; int points,bonus,penalty; final int dx[]={0,0,1,-1}; //NSEW final int dy[]={-1,1,0,0}; int w,h,width,height; Image backim; Graphics backgr; tile[][] board; boolean[][] free; tile[] next; int nnext; tile blank,source; tile[] alltiles; int ntiles; int mx,my; Thread t=null; int ready=-1; int score; int wx,wy,wi,wo,wpos; private int param(String name,int def){ String s=getParameter(name); return (s==null)?def:Integer.parseInt(s); } public void init(){ // get parameters TILESZ=param("tilesize",40); WATERSZ=param("watersize",6); sl=param("smoothness",2); framerate=param("waterspeed",40)/sl; countdown=param("countdown",20); points=param("points",10); bonus=param("bonus",30); penalty=param("penalty",1); tile.stem=getParameter("imagestem"); if(tile.stem==null) tile.stem="brick"; // calculate field size and setup backbuffer width=getWidth(); height=getHeight(); backim=createImage(width,height); backgr=backim.getGraphics(); width-=TILESZ+5; height-=20; w=width/TILESZ; h=height/TILESZ; height=h*TILESZ; mx=w;my=0; addMouseMotionListener(this); addMouseListener(this); // create tiles and setup field blank=new brick(this,""); source=new brick(this,"_s"); alltiles=new tile[9]; ntiles=0; for(int j=1;j<4;++j) for(int k=0;k=w||wy<0||wy>=h||(wo=board[wx][wy].out(wi))==-1){ ready=-2; fade(); repaint(); t.sleep(1000); ready=-1; }else{ wo^=1; score+=free[wx][wy]?points:bonus; free[wx][wy]=false; } } }else{ // count down to water release t.sleep(1000); --ready; } repaint(); }catch(InterruptedException e){ System.out.println(e); } } //--------------------------------------------------- drawing void fade(){ backgr.setColor(new Color(1.f,1.f,1.f,.5f)); backgr.fillRect(0,0,width+TILESZ+5,height); } void draw(Graphics g,int i,int j,tile t,boolean border){ g.drawImage(t.pic,i*TILESZ,j*TILESZ,this); if(border){ g.setColor(Color.blue); g.drawRect(i*TILESZ,j*TILESZ,TILESZ-1,TILESZ-1); } } void draw(int i,int j){draw(backgr,i,j,board[i][j],false);} void drawnext(){ for(int i=0;i0) g.drawString("Water is on its way... "+ready,10,height+11); } g.drawString("Score: "+score,width-40,height+11); } } abstract class tile{ abstract public int out(int in); Image pic; static String stem; } class brick extends tile{ brick(pipes main,String im){ pic=main.getimage(stem+im+".gif"); } public int out(int in){return -1;} } class onepipe extends tile{ int a,b; onepipe(pipes main,int _a,int _b){ a=_a; b=_b; pic=main.getimage(stem+"_"+Integer.toString(a)+Integer.toString(b)+".gif"); } public int out(int in){ if(in==a) return b; if(in==b) return a; return -1; } } class twopipe extends tile{ int a; twopipe(pipes main,int _a){ a=_a; pic=main.getimage(stem+"_t"+Integer.toString(a)+".gif"); } public int out(int in){ return in^a; } }