Jump to content

here is my 460 bytes pong :)


Ezelia
 Share

Recommended Posts

Also you can save additional 3 bytes defining all 20's as variable at start:

 

<body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;x=h=150,y=10,v=1,z=2;A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>(Math.random()+.5)*200&&(y<B+10?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,10);t.T(b,280,10);',16);"><canvas id=c>

 

452 Bytes total :)

Initially want to reduce lower than 450 but went out of ideas))

Who can do better?

Link to comment
Share on other sites

the AI logic is to follow ball when its X coord is between 100 and a random value between 100 and 300, where 300 is the canvas width.
if the random value is too low, the lower random number generated the less accurate is AI move. so it completely depend on random number generation :)

there is an unneeded semicolon on the end of code :D   451 bytes !

 

<body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;x=h=150,y=10,v=1,z=2;A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>(Math.random()+.5)*200&&(y<B+10?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,10);t.T(b,280,10);',16)"><canvas id=c>
Link to comment
Share on other sites

Wow that is really minimal, I learned a lot from looking at the source comments. I don't think I've ever really seen bitwise operations like that before

 

I removed the bitwise in the minimalized version :)

actually, there is no need to compare X coord to an integer, float values are OK too for comparision :)

Link to comment
Share on other sites

aw yeah, previous semicolon is also not required,and 1 byte from 10's replacing

<body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;x=h=150,y=l=10,v=1,z=2;A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>(Math.random()+.5)*200&&(y<B+l?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,l);t.T(b,280,l)',16)"><canvas id=c>

Achievement unlocked - 449!

 

Next goal -- 440))

 

You should offer this game for non-exculsive licensing 1$ per byte - best price evar!

Link to comment
Share on other sites

here is a new optimisation

 

the value witch determine AI behaviour
(Math.random()+.5)*200

 

can actually be replaced with this

Date.now()%150+150

 

witch can be replaced with this

Date.now()%h+h

 

but we can do better !
since we only need pseudo random number, we can simply do

y*x%h+h

 

at this point I noticed that the AI allways lost the first round, so instead of initializing X with 150 I initialize it with 20 to let AI the time to calculate ball trajectory :)

final code

 

<body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;h=150,y=l=10,v=1,z=2;x=A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>y*x%h+h&&(y<B+l?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:(x>w)?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,l);t.T(b,280,l)',16)"><canvas id=c>

434 bytes !


Edit
one more byte :
instead of initializing y=l=10 I initialize them as y=l=16 and use l in setInterval call

two more byte by replacing this condition (x>w)?a++:0 with this x>w?a++:0
 

<body onkeydown=e=event onload="t=c.getContext('2d');t.T=t.fillText;t.R=t.fillRect;e=a=b=0;w=300;h=150,y=l=16,v=1,z=2;x=A=B=j=20;setInterval('(W=e.which)?A+=(W-39)*2:0;v>0&&x>y*x%h+h&&(y<B+l?B-=2:B+=2);x<8&&y>A&&y<A+j||x>290&&y>B&&y<B+j?v=-v:0;y>h||y<0?z=-z:0;x<0?b++:x>w?a++:0;x>w||x<0?x=h:0;A<0?A=0:(A>130?A=130:0);x+=v;y+=z;t.clearRect(0,0,w,h);t.R(x,y,4,4);t.R(2,A,6,j);t.R(294,B,6,j);t.T(a,j,l);t.T(b,280,l)',l)"><canvas id=c>

431 bytes !

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...