code
/*
kevin zhang's superpong game
info tech 11
block d
feb-apr 2004
*/
import java.applet.*;
import java.awt.*;
public class superpong extends Applet implements Runnable
{
//////////////////////////
///////////////////////////////////
////// Ultimate variables of Ultimateness ////////
int z=101;
int zstep=0;
////// Ultimate variables of Ultimateness
////////
//////////////////////////////////
////////////////////////
//borders
int xl=150;
int xr=250;
int yt=150;
int yb=250;
//ball
int diameter=10;
int bx=200;
int by=200;
int bdx=0;
int bdy=0;
int flash=0;
//paddles
int px=0;
int py=0;
int pd=10;
//computer paddle
int cx=151;
int cy=151;
int cd=1;
int score1=0;
int score2=0;
int time=272;
int timecount=1;
int timecut=0;
int w0=(int)(Math.random()*1);
int w00=(int)(Math.random()*1);
int w1=(int)(Math.random()*3);
int w2=(int)(Math.random()*3);
int w3=(int)(Math.random()*3);
int w4=(int)(Math.random()*3);
int black=500;
int intro=1;
int congranum=0;
int losenum=0;
int score3=0;
// delay DO NOT DELETE
int delay=12;
Image buffer;
Image back;
Image paddle1;
Image start;
Image congra;
Image lose;
Graphics bufferG;
Thread running;
public void init()
{buffer=this.createImage(500,500);
bufferG=buffer.getGraphics();
back= this.getImage(this.getCodeBase(), "back.gif");
paddle1= this.getImage(this.getCodeBase(), "paddle.gif");
start= this.getImage(this.getCodeBase(), "start.gif");
congra= this.getImage(this.getCodeBase(), "congra.gif");
lose= this.getImage(this.getCodeBase(), "lose.gif");
}
////////////////////////////////////////////////////////////////////////
public void paint (Graphics g)
{
//g.drawImage(back,0,0,this);
//bufferG.setColor(Color.black);
//bufferG.fillRect(0,0,700,700);
bufferG.drawImage(back,0,0,this);
bufferG.setColor(Color.red);
bufferG.drawRect(cx,cy,25,25);
bufferG.drawRect(cx+1,cy+1,23,23);
bufferG.drawRect(cx+2,cy+2,21,21);
bufferG.drawLine(cx+12,cy,cx+12,cy+25);
bufferG.drawLine(cx,cy+12,cx+25,cy+12);
bufferG.setColor(Color.blue);
bufferG.drawRect((int)((400-z)/2),(int)((400-z)/2),z,z);
bufferG.setColor(Color.yellow);
bufferG.drawString("PLAYER "+score1+" COMPUTER "+score2,20,425);
bufferG.setColor(Color.red);
bufferG.fillOval(bx,by,diameter,diameter);
bufferG.setColor(Color.white);
if (z>=400)
{if (flash==1)
{bufferG.fillRect(px,py,100,100);
}
}
if (z<=100)
{if (flash==2)
{bufferG.fillRect(cx,cy,30,30);
}
}
bufferG.setColor(Color.green);
bufferG.drawRect(px,py,100,100);
bufferG.drawRect(px+1,py+1,98,98);
bufferG.drawRect(px+2,py+2,96,96);
bufferG.drawRect(px+3,py+3,94,94);
bufferG.drawRect(px+4,py+4,92,92);
bufferG.drawLine(px,py+50,px+100,py+50);
bufferG.drawLine(px,py+51,px+100,py+51);
bufferG.drawLine(px+50,py,px+50,py+100);
bufferG.drawLine(px+51,py,px+51,py+100);
bufferG.setColor(Color.yellow);
bufferG.drawString("Time Remaining: ",17,447);
bufferG.fillRect(114,439,time,6);
if (black==500)
{bufferG.setColor(Color.black);
bufferG.fillRect(0,0,black,black);
if (intro==1)
{
bufferG.setColor(Color.white);
bufferG.drawImage(start,0,0,this);
}
}
if (congranum==1)
{bufferG.drawImage(congra,0,0,this);
}
if (losenum==1)
{bufferG.drawImage(lose,0,0,this);
bufferG.drawString("Score: "+ score3,220,170);
}
g.drawImage(buffer,0,0,this);
}
////////////////////////////////////////////////////////////////////////
public void update(Graphics g)
{paint(g);
}
public void start(){
if (running==null)
{running = new Thread(this,"running");
running.start();
}
}
public void stop(){
if (running!=null && running.isAlive()){
running.stop();
}
running=null;
}
public boolean mouseMove(Event e, int mousex, int mousey){
if (mousex<=350&&mousey<=350&&mousex>=50&&mousey>=50)
{px=mousex-50;
py=mousey-50;
}
if (mousex>350&&mousey<=350&&mousey>=50)
{py=mousey-50;
}
if (mousex<50&&mousey<=350&&mousey>=50)
{py=mousey-50;
}
if (mousey>350&&mousex<=350&&mousex>=50)
{px=mousex-50;
}
if (mousey<50&&mousex<=350&&mousex>=50)
{px=mousex-50;
}
return true;
}
////////////////////////////////////////////////////////////////////////
public boolean keyDown(Event e, int key){
if (e.id==Event.KEY_PRESS && key=='s'){
w0=(int)(Math.random()*10);
w00=(int)(Math.random()*10);
w1=(int)(Math.random()*3);
w2=(int)(Math.random()*3);
zstep=2;
if (w0>5)
{bdx=w1;
}
if (w0<=5)
{bdx=-w1;
}
if (w00>5)
{bdy=w2;
}
if (w00<=5)
{bdy=-w2;
}
black=501;
intro=2;
timecut=1;
return true;
}if (congranum==1)
{
if (e.id==Event.KEY_PRESS && key=='c'){
z=101;
zstep=1;
xl=150;
xr=250;
yt=150;
yb=250;
diameter=10;
bx=200;
by=200;
bdx=0;
bdy=0;
flash=0;
//paddles
px=0;
py=0;
pd=10;
//computer paddle
cx=151;
cy=151;
score1=0;
score2=0;
time=272;
timecount=1;
timecut=0;
congranum=0;
return true;
}
}
if (e.id==Event.KEY_PRESS && key=='r'){
z=101;
zstep=0;
xl=150;
xr=250;
yt=150;
yb=250;
diameter=10;
bx=200;
by=200;
bdx=0;
bdy=0;
flash=0;
//paddles
px=0;
py=0;
pd=10;
//computer paddle
cx=151;
cy=151;
cd=2;
score1=0;
score2=0;
time=272;
timecount=1;
timecut=0;
black=500;
intro=1;
losenum=0;
congranum=0;
return true;
}
else return false;
}
////////////////////////////////////////////////////////////////////////
public void run() {
for (; ; )
{ /////////////////////////////////
if (score1==4)
{congranum=1;
cd=cd+1;
score1=0;
score2=0;
}
if (score2==4)
{losenum=1;
}
/////////////////////////////////////////////////// RUN stuff starts
timecount++;
if (timecount==5)
{time=time-timecut;
timecount=1;
}
////////////////////////////////z
z=z+zstep;
if (z>=400)
{
if (bx+20>=px&&bx+20<=px+100&&by+20>=py&&by+20<=py+100)
{zstep=-(zstep+1);
if (bdx<0)
{bdx=bdx-1;
}
else
bdx=bdx+1;
if (bdy<0)
{bdy=bdy-1;
}
else
bdy=bdy+1;
flash=1;
}
else
{
score2=score2+1;
z=101;
zstep=2;
bx=160;
by=160;
w0=(int)(Math.random()*10);
w00=(int)(Math.random()*10);
w1=(int)(Math.random()*3);
w2=(int)(Math.random()*3);
if (w0>5)
{bdx=w1;
}
if (w0<=5)
{bdx=-w1;
}
if (w00>5)
{bdy=w2;
}
if (w00<=5)
{bdy=-w2;
}
}
}
if (z<=100)
{
if (bx+5>=cx&&bx+5<=cx+30&&by+5>=cy&&by+5<=cy+30)
{
zstep=-zstep;
flash=2;
}
else
{
score1=score1+1;
z=399;
cx=183;
cy=183;
zstep=-2;
bx=200;
by=200;
w0=(int)(Math.random()*10);
w00=(int)(Math.random()*10);
w1=(int)(Math.random()*3);
w2=(int)(Math.random()*3);
if (w0>5)
{bdx=w1;
}
if (w0<=5)
{bdx=-w1;
}
if (w00>5)
{bdy=w2;
}
if (w00<=5)
{bdy=-w2;
}
}
}
////////////////////////////////z
if (cx<220)
{if (bx+(int)(diameter/2)>cx+15)
{cx=cx+cd;
}
}
if (cx>150)
{if (bx+(int)(diameter/2)<cx+15)
{cx=cx-cd;
}
}
if (cy<220)
{if (by+(int)(diameter/2)>cy+15)
{cy=cy+cd;
}
}
if (cy>150)
{if (by+(int)(diameter/2)<cy+15)
{cy=cy-cd;
}
}
//////////////////computer paddle
/*if ((int)(bx+diameter/2)>150&&(int)(bx+diameter/2)<225&&(int)(by+diameter/2)>150&&(int)(by+diameter/2)<225)
{cd=5;
}
else
{cd=0;
}
if ((int)(bx+diameter/2)>cx+12)
{cx=cx+cd;
}
if ((int)(bx+diameter/2)<cx+12)
{cx=cx-cd;
}
if ((int)(by+diameter/2)>cy+12)
{cy=cy+cd;
}
if ((int)(by+diameter/2)<cy+12)
{cy=cy-cd;
}
*/
//////////////////compaddle
///movement
if (zstep>0)
{if (bx<=xl)
{bdx=-(bdx);
}
if (bx>=xr-diameter)
{bdx=-(bdx);
}
if (by<=yt)
{
bdy=-(bdy);
}
if (by>=yb-diameter)
{
bdy=-(bdy);
}
}
if (zstep<0)
{if (bx<=xl)
{bx=xl+1;
bdx=-(bdx);
}
if (bx>=xr-diameter)
{bx=xr-diameter-2;
bdx=-(bdx);
}
if (by<=yt)
{by=yt+1;
bdy=-bdy;
}
if (by>=(yb-diameter))
{
bdy=-bdy;
by=(yb-diameter-2);
}
}
///////////
bx=bx+bdx;
by=by+bdy;
////////movement
diameter=(int)(z/10);
////////cordinates
xl=(int)((400-z)/2);
xr=(int)((400-z)/2+z);
yt=(int)((400-z)/2);
yb=(int)((400-z)/2+z);
////////cordinates
/////////////////////////////////////////////////// RUN stuuf ends here
repaint();
try{running.sleep(delay);}
catch(InterruptedException e) {;}
}}}