import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class Bounce_Ball extends java.applet.Applet implements Runnable{ Font f=new Font("TimesRoman",Font.PLAIN,10); Font f2=new Font("TimesRoman",Font.BOLD,40); Thread runThread; URL imgURL,imgURL2; Image ball, bar[]=new Image[5], target; Image offscreenImg; Graphics offscreenG; int ax[]={7,9,11,12,10}, ay[]={7,9,11,12,10}; int axx[]={5,6,8,10,8}, ayy[]={5,6,8,10,8}; int x=0, y=0, xx=200, yy=275; int s=0, az[]={100,200,500,0,1000}; int l=0, zz[]={2500,10000,25000,50000,100000}; int b=0, c=0, d=0, barx[]={71,59,43,35,49}; int sz=0, tx=0, ty=24, atx=8, p[]={50,50,45,40,45}; public void init() { String file_path="/~cwarner/java/Bounce-Ball/"; try { imgURL=new URL("http","grove.ufl.edu",80,file_path+"ball.jpg"); ball=getImage(imgURL); imgURL=new URL("http","grove.ufl.edu",80,file_path+"target.jpg"); target=getImage(imgURL); file_path="/~cwarner/java/Bounce-Ball/bar"; imgURL2=new URL("http","grove.ufl.edu",80,file_path+".jpg"); bar[0]=getImage(imgURL2); imgURL2=new URL("http","grove.ufl.edu",80,file_path+"2.jpg"); bar[1]=getImage(imgURL2); imgURL2=new URL("http","grove.ufl.edu",80,file_path+"3.jpg"); bar[2]=getImage(imgURL2); imgURL2=new URL("http","grove.ufl.edu",80,file_path+"4.jpg"); bar[3]=getImage(imgURL2); imgURL2=new URL("http","grove.ufl.edu",80,file_path+"5.jpg"); bar[4]=getImage(imgURL2); } catch (Exception e) { int pts=0;} offscreenImg=createImage(480,300); offscreenG=offscreenImg.getGraphics(); } public void start() { if (runThread==null) { runThread=new Thread(this); runThread.start(); } } public void stop() { if (runThread!=null) { runThread.stop(); runThread=null; } } public void run() { while (b==0) { try{Thread.sleep(50);} catch (InterruptedException e) {} } try {Thread.sleep(2500);} catch (InterruptedException e) {} while (true) { try {Thread.sleep(p[l]);} catch (InterruptedException e) {} x=x+ax[l]; y=y+ay[l]; if (x>464) ax[l]=-1*ax[l]; if (x<1) ax[l]=-1*ax[l]; if (y<1) ay[l]=-1*ay[l]; if (y>=yy-14 && y<=yy-(15-ay[l]) && x>=xx-8 && x<=xx+barx[l]) { ay[l]=-1*ay[l]; s=s+az[l]; if (l==4) { if (ay[4]>0) ay[4]=(int)Math.floor(Math.random()*5)+8; if (ax[4]>0) ax[4]=(int)Math.floor(Math.random()*5)+8; if (ay[4]<0) ay[4]=(int)Math.floor(Math.random()*5)-12; if (ax[4]<0) ax[4]=(int)Math.floor(Math.random()*5)-12; } } if (l==3) { sz=sz-1; tx=tx+atx; if (tx>=480) tx=-16; if (sz<0 && y>=ty-14 && y<=ty-(15-ay[l]) && x>tx-15 && x=zz[l] && y<240 && l<4) { l=l+1; x=(int)x/8; y=0; xx=200; repaint(); try{Thread.sleep(750);} catch (InterruptedException e) {} } repaint(); } } public boolean keyDown(Event evt, int key) { if (key==Event.LEFT) xx=xx-axx[l]; if (key==Event.RIGHT) xx=xx+axx[l]; if ((char)key=='*') c=1; if (c==1 && s==0) { if ((char)key=='2') { s=2500;} if ((char)key=='3') { l=1; s=10000;} if ((char)key=='4') { l=2; s=25000;} if ((char)key=='5') { l=3; s=50000;} } repaint(); return true; } public boolean mouseDown(Event evt, int xm, int ym) { if (b==0) b=1; return true; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { setBackground(Color.white); if (d==0) { for (int j=1; j<5; j++) { offscreenG.drawImage(bar[j],0,0,this); } offscreenG.drawImage(target,0,0,this); d=1; } offscreenG.setColor(Color.black); offscreenG.fillRect(0,0,480,300); offscreenG.setFont(f); offscreenG.setColor(Color.white); offscreenG.drawString("Score",20,12); offscreenG.drawString(""+s,20,24); offscreenG.drawString("Level",420,12); offscreenG.drawString(""+(l+1),448,12); offscreenG.drawImage(ball,x,y,this); offscreenG.drawImage(bar[l],xx,yy,this); if (l==3) offscreenG.drawImage(target,tx,ty,this); if (y>320) { offscreenG.setFont(f2); offscreenG.drawString("Game Over",120,120); stop(); } g.drawImage(offscreenImg,0,0,this); } }