Join us! Nintendo Enthusiast Forums
dark        
LK4O4 blog header photo
LK4O4's c-blog

"Truth... is the equivalent of love."

Posts 0Blogs 38Following 0Followers 19


 
 

LONG BLOG

Javascript Pong (shortblog)

   0
Posted by: Ryan





I was trying to figure out how to get basic collision detection working from scratch, which is essentially getting boxes to know that they've intersected each other and to react appropriately. And while I was prototyping it out in Javascript, I was able to get some rectangular boxes to collide, and the whole thing looked a bit like Pong.

So I made a simple Pong copycat with just Javascript code and by repositioning <div> tags, and you can play with it here.

Player 1: Move with 'W' and 'S'
Player 2: Move with 'UP' arrow and 'DOWN' arrow
Reset game: Press 'Enter'

Because of the way browser inputs are handled, rapidly tapping the keys works much better than holding them down. Also, the game is only 500 pixels wide because I originally planned to embed it into a post with an iframe.


P.S. If you're interested, my collision detection function is essentially this:

var a = object1.x,
    b = object1.x + object1.width,
    c = object2.x,
    d = object2.x + object2.width,
    e = object1.y,
    f = object1.y + object1.height,
    g = object2.y,
    h = object2.y + object2.height;

if (a > d || c > b || e > h || g > f) {

    // rectangular objects are not colliding

} else {

    // rectangular objects are colliding

}

Note that this code depends on the objects intersecting for at least one frame, so it won't work for really fast objects (or really thin objects).
#Community    #Retro   
Login to vote this up!

LOOK WHO CAME:


LK4O4   
Tubatic   1


 
 

  0 COMMENTS

Please login (or) make a quick account (free)
to view and post comments.



 Login with Twitter

 Login with Dtoid

Three day old threads are only visible to verified humans - this helps our small community management team stay on top of spam

Sorry for the extra step!

 

About LK4O4one of us since 1:47 PM on 05.07.2009

Hello there.

My name is Ryan and I work at a pretty prominent web company. I'm 24 years old, which makes me one of the youngest people at the company (out of over 100 people). I have half of a college degree, a full-time job, and now I've got a place to hang out and talk about awesome video games.

Feel free to talk to me! <3


== Currently Playing ==
Clairvoyance
FTL: Faster Than Light
Slitherlink by Nikoli

== Recently Finished ==
Thomas Was Alone
Fire Emblem: Awakening
FTL: Faster Than Light (Easy)
Hotline Miami
Little Inferno
Aether
Analogue: A Hate Story
Mark of the Ninja
Atom Zombie Smasher
Time Fcuk
Thirty Flights of Loving
Lone Survivor
Might & Magic: Clash of Heroes
Dynamite Jack
Slitherlink by Nikoli
Bastion
TRAUMA
The Legend of Zelda: Skyward Sword
Ghost Trick
Mighty Switch Force
The Legend of Zelda: Four Swords AE
Shin Megami Tensei: Devil Survivor Overclocked
VVVVVV
Mighty Flip Champs
Donkey Kong (Game Boy version)
Escapee GO!
Aura-Aura Climber
Resident Evil: The Mercenaries 3D
Shantae: Risky's Revenge
Dragon Quest Heroes: Rocket Slime
Elite Beat Agents (Normal Difficulty)
Paper Mario
Sonic Colors (Wii version)
BIT.TRIP FATE
Dragon Quest V: Hand of the Heavenly Bride
Mother 3 (Fan Translation)
Muramasa: The Demon Blade (Both Second Endings)
BIT.TRIP RUNNER
Portal (Steam version)
Cave Story (Wii version)
Spartan: Total Warrior (GameCube version)
LostWinds: Winter of the Melodias
BIT.TRIP BEAT
BIT.TRIP VOID
BIT.TRIP CORE
Half-Life 2 (original Xbox version)
The Lost Vikings (Genesis version)
Pokemon HeartGold Version
Bangai-O Spirits (All 160+ Levels Cleared!)
Super Mario 64 DS
The Great Circus Mystery Starring Mickey & Minnie
The World Ends With You
Psychonauts
Braid
Ninja Gaiden Black (Normal Difficulty)
Chrono Trigger
Earthbound

== Systems I Own and Love ==
Sega Genesis
Sega Dreamcast
Xbox
Wii
Game Boy Advance
Nintendo DS Lite
Nintendo 3DS

== 3DS Friend Code ==
5241 1905 5146

== Wii System Code ==
4688 2108 9135 7828

== Tatsunoko vs Capcom ==
4297 4386 7686

== Monster Hunter Tri ==
11821Y
Mii code:4688 2108 9135 7828
3DS Code:5241 1905 5146


 

Around the Community

Featured

Posts

Blogs

Support