Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere does the context live in an OOP Javascript Pong game?
    primarykey
    data
    text
    <p>To practice my OOP knowledge I'm making a Pong game in javascript (I know, I know, it's like playing Stairway to Heaven in a guitar shop). I've had several functioning versions of the game by implementing several different techniques including prototypal based OOP and functional style. However I'm not doing this to get a functional game I'm doing it to learn.</p> <p>I'm using the html5 canvas and plain ol' javascript, no frameworks (ok, a little bit of jQuery for the keyboard capture). I had my Pong object which represented my game. Pong had an attribute <code>ctx</code> that contained a reference to the <code>canvas.getContext("2d")</code> context. It also had a <code>player1</code>, <code>player2</code> and <code>ball</code> attribute for holding you know what. When the ball and two players were instantiated the context was passed in to their constructor so that they too could hold a reference to the context for use in their <code>draw(ctx)</code> methods. Pong had a <code>draw()</code> method that would get called using a <code>setInterval(this.draw, 10)</code>. Pong's draw method would call the draw method of the two players and the ball. </p> <p>It doesn't sit well with me that the two players and ball have the context as an attribute. They don't own the context and therefore it shouldn't be an attribute. However the nature of using javascript and the canvas seems to be that this is the best way. Who or what should own the context in this situation? Ideally I wouldn't want the players and ball objects to have a draw object at all. I feel like they should have attributes that describe their geometry and position and a dedicated object should be tasked with rendering them to the screen. This way, if in the future I decided I wanted to use &lt;div&gt;'s instead of the canvas I could just change the rendering object and everything else would be oblivious.</p> <p>I know I'm making a javascript Pong game more complicated than it needs to be but I want to practice the techniques and really grok the concept of OOP but every time I think I've cracked it a completely new problem created by my 'solution' presents itself.</p> <p>EDIT: If it would help if you had a nosy at my code here is an (almost) fully working version:</p> <p>library.js - <a href="http://mikemccabe.me/tests/pong.archive.14.06.11/library.js" rel="nofollow">http://mikemccabe.me/tests/pong.archive.14.06.11/library.js</a></p> <p>pong.js - <a href="http://mikemccabe.me/tests/pong.archive.14.06.11/pong.js" rel="nofollow">http://mikemccabe.me/tests/pong.archive.14.06.11/pong.js</a></p> <p>Try it - out <a href="http://mikemccabe.me/tests/pong.archive.14.06.11/" rel="nofollow">http://mikemccabe.me/tests/pong.archive.14.06.11/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload