Page 1 of 1

pong in a canvas! -UPDATED-

PostPosted: Thu Apr 21, 2011 10:23 am
by savvy
here we have pong inside a canvas, all you need for this to work is a canvas with a size larger than i think 60x60 (below it and it wont draw the paddles) then paste the below script into a draw event... it will work :) scoring system is done via red lines... if you wish to use this, ask and you will be given permission... please do not claim as your own.
--controls--
w - left paddle up
s - left paddle down
up - right paddle up
down - right paddle down
1 - left cpu on
2 - left cpu off
9 - right cpu on
0 - right cpu off

--script--
Code: Select all
/* savvy's pong in a canvas script, please do not use without my permission...
once my permission is given you may use it as often as you like, but do NOT claim as your own. */

//var declarations
int rcpu;int lcpu;int start;
float lp;
float rp;
float bx;
float by;
float byv;
int balldir;
int i;
int leftscore;
int rightscore;
char* key = GetKeyState();

//drawing
erase(0,0,0,0);
//region
setpen(255,255,255,0,1);
moveto(0,0);
lineto(0,height-2);
lineto(width-2,height-2);
lineto(width-2,0);
lineto(0,0);
setpen(255,255,255,0,width/20);
//left paddle
moveto(width/100+2,lp);
lineto(width/100+2,lp+(height/4));
//right paddle
moveto(width-(width/100)-2,rp);
lineto(width-(width/100)-2,rp+(height/4));
//ball
putpixel(bx,by);
//scoring
setpen(255,0,0,0,width/40);
//left
moveto(1,height/50);
lineto((leftscore*(width/100))+1,height/50);
//right
moveto(width-1,height/50);
lineto(width-((rightscore*(width/100))+1),height/50);

//cpu change
if(key[KEY_1]==1)lcpu=1;
if(key[KEY_2]==1)lcpu=0;
if(key[KEY_9]==1)rcpu=1;
if(key[KEY_0]==1)rcpu=0;

//paddle movement
//left
if(lcpu==0)
{
if(key[KEY_w]==1)
{
    lp-=height/58;
    if(lp<0)lp=0;
}
if(key[KEY_s]==1)
{
    lp+=height/58;
    if(lp+(height/4)>height)lp=height-(height/4);
}
}
//left paddle cpu controls
if(lcpu==1&&balldir==0)
{
    if(lp+((height/4)/2)>by)
    {lp-=height/58;if(lp<0)lp=0;}
    if(lp+((height/4)/2)<by)
    {lp+=height/58;if(lp+(height/4)>height)lp=height-(height/4);}
}
//right
if(rcpu==0)
{
if(key[KEY_UP]==1)
{
    rp-=height/58;;
    if(rp<0)rp=0;
}
if(key[KEY_DOWN]==1)
{
    rp+=height/58;
    if(rp+(height/4)>height)rp=height-(height/4);
}
}
//right paddle cpu controls
if(rcpu==1&&balldir==1)
{
    if(rp+((height/4)/2)>by)
    {rp-=height/58;if(rp<0)rp=0;}
    if(rp+((height/4)/2)<by)
    {rp+=height/58;if(rp+(height/4)>height)rp=height-(height/4);}
}
//ball movement
//y
by+=byv;
//x
if(balldir==0)
{
    if(start==1)bx-=width/40;
    if(start==0)bx-=width/80;
}
if(balldir==1)
{
    if(start==1)bx+=width/40;
    if(start==0)bx+=width/80;
}
//left hit
if(bx<(width/100)+1&&by>lp&&by<lp+(height/4)+(width/20))
{
    balldir=1;
    i=rand(2);
    byv=rand(height/50);
    if(i==0)byv=byv-(byv*2);
    if(start==0)start=1;
}
//right hit
if(bx>width-(width/100)-1&&by>rp&&by<rp+(height/4)+(width/20))
{
    balldir=0;
    i=rand(2);
    byv=rand(height/50);
    if(i==0)byv=byv-(byv*2);
    if(start==0)start=1;
}
//after point center
if(bx<-50||bx>width+50)
{
    if(bx<0)rightscore+=2;
    if(bx>width)leftscore+=2;
    bx=width/2;
    by=height/2;
    balldir=rand(2);
    i=rand(2);
    byv=rand(height/50);
    if(i==0)byv=byv-(byv*2);
    start=0;
}
//bounce of top/bottom
if(by<1)byv=byv-(byv*2);
if(by>height-1)byv=byv-(byv*2);

//end of game scoring
if(rightscore>=10)
{
    rightscore=0;
    leftscore=0;
    setpen(255,255,0,0,100);
    putpixel(width/2,height/2);
}
if(leftscore>=10)
{
    rightscore=0;
    leftscore=0;
    setpen(0,255,255,0,100);
    putpixel(width/2,height/2);
}


enjoy :D

savvy

Re: pong in a canvas! -UPDATED-

PostPosted: Thu Apr 21, 2011 7:50 pm
by Game A Gogo
Great work savvy! This is closer to how games were once made :D

Re: pong in a canvas! -UPDATED-

PostPosted: Sat Apr 23, 2011 11:51 am
by savvy
yes, and you respect it more if you code this way too.

Re: pong in a canvas! -UPDATED-

PostPosted: Sat Apr 23, 2011 10:34 pm
by lcl
Great work man! :D