pong in a canvas! -UPDATED-

Post here your demos and examples with the source files.
Forum rules
Always post the games with a screenshot.
The file must have the ged and data files (complete game source)
Use the forum attachment to post the files.
It is always better to use the first post to put the game files

pong in a canvas! -UPDATED-

Postby savvy » Thu Apr 21, 2011 10:23 am

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
Attachments
scrnsht.png
--> For my help, i ask for a simple +1 if it helps! ^-^
--> I dont code, I type art which you dont understand.
--> I keep winning the 3D model challenge at college, teacher says: "you keep winning im not giving you prizes".
User avatar
savvy
 
Posts: 494
Joined: Wed Jun 03, 2009 11:55 am
Location: England
Score: 44 Give a positive score

Re: pong in a canvas! -UPDATED-

Postby Game A Gogo » Thu Apr 21, 2011 7:50 pm

Great work savvy! This is closer to how games were once made :D
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: pong in a canvas! -UPDATED-

Postby savvy » Sat Apr 23, 2011 11:51 am

yes, and you respect it more if you code this way too.
--> For my help, i ask for a simple +1 if it helps! ^-^
--> I dont code, I type art which you dont understand.
--> I keep winning the 3D model challenge at college, teacher says: "you keep winning im not giving you prizes".
User avatar
savvy
 
Posts: 494
Joined: Wed Jun 03, 2009 11:55 am
Location: England
Score: 44 Give a positive score

Re: pong in a canvas! -UPDATED-

Postby lcl » Sat Apr 23, 2011 10:34 pm

Great work man! :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to Game Demos

Who is online

Users browsing this forum: No registered users and 1 guest