Pong & Object Oriented Programming – Computerphile

Pong & Object Oriented Programming – Computerphile

Today we’re going to talk about object
oriented programming, it’s one of the most common ways that software is written
these days certainly for things like applications on desktops and mobile
devices. The problem with object oriented programming though is that it can be quite difficult
to get your head around at first ’cause it uses so many different terms, you talk about objects sending messages to other objects, objects doing things and you think “Well they’re just bits of code, how are they doing anything?” and so on so it can be difficult to get your head around it. For Nicholas Wirth, who invented Pascal, once
described programs as being algorithms plus data structures. Now the data structures we can think of as the sort of the data that our program’s manipulating, whatever that might be. And the algorithm – that’s basically the
code they we’re running to manipulate that data. One way we can think about this is thinking about something like a simple pong game, sort of the very early computer games
you got back in the seventies. Pong was a very simple game that you played on
your TV. It had – so that’s your TV screen – you had a little bat on this side which one
person controlled; you had another bat on this side that another person controlled and then you had a ball which was a square block. These literally were square blocks that were filled in on the screen. So one player could control this and move it up and down, and the other player could control this to move it up and down, and the ball would bounce between effectively trying to be a sort of tennis game. The graphics were state of the art for the time, and you can still find Pongs lying around mainly
museums. There’s one at the Computer History Museum down in Cambridge if you were to go and play with it. So the Pong game’s very, very simple but we can use it to sort of
think about how the probe would work and so on and things. So you’ve got two bats and a
ball. So the data for this would be relatively simple. This paddle or
bat over here – you’d need to know where it is, so you need its position; and we’ll call that its y position. And you’d probably keep track of its x position as well just to make
it easier to draw things. And you’d need the same for this one over here and
again for the ball you’d need the ball’s x pos., and the ball’s y position. So that’s
the data that our program would represent. It doesn’t need anything more than that really. You’d perhaps have some representation of screen but that’d be handled by the
computer, so we’ll ignore that for now. And we’d be able to write programs
which would use this data, manipulate it to run the game. So if a key was pressed, we would change the y position to make the bat go up; or if a different key was pressed, we’d change this one’s y position to make the bat go down. And we can change this to make
the ball sort of bounce off the edge of the screen, so it looks like the ball’s
moving around. So we’ve got to write some code very simply to do this. So we could say something like, if he – alright, so we’ve got to use Q to go up – is pressed then paddle 1 y pos equals paddle 1 y pos plus 1. And we could do something similar for the going down for the same paddles – we could write a basic program code that manipulate the variables to move the paddles around. Then we can redraw the screen; and so we redraw the screen. It looks like the things are moving. Actually all we’ve
done is change the variables’ data and then redraw the screen. This is OK, and
for a simple program like this, this approach would work and anything else would
perhaps be over the top. But often you would find that you’d have lots of data
that you’d want to group together. So for example with our pong game, we’ve got a variable to hold the x position; a variable to hold the y position; a variable to
hold the ball’s x position; a variable to hold the ball’s y position; and so on. And we might actually say we’re talking a lot of the
time about points on things. So we actually want to group them together. So we can
use in various programs that have the facility to group things together as what we call a data structure. And we can then refer to this position either as a whole, so you can
set the position to be the same as another position; or we can refer to the
individual things inside it. So we can pass it into a function, say, to draw the paddle, just passing the whole position in there; but if you want to update one of the
values, we can just update that specific value about the y position that we want to manipulate. But there’s still a slight issue with this sort of approach. It makes it easy to
refer to things and we could have lots of these data structures referring to different things. Rather than having separate variables here, we could have a position
for paddle 1; and a position for paddle 2. So we can refer to them and have lots of them. We can have one data structure which we can then have many different things for. So we can actually group the data together, which is quite useful. But the problem with all of this is that
the date is completely open and we can write code anywhere in our program that
can manipulate it. For a simple program like Pong, having the data open is not a
problem. Your program is probably not gonna be more than a few hundred lines
long. As the program grows, you’re writing something bigger, then you have lots of
files accessing the data and you might change some things and forget to update part of the program. So for example, you might update one
part of the program to only allow ten entries rather the hundred and something, but the other part of the problem still expects a hundred, and so it writes over the end, walks all over memory that’s used for something else. That’s how you get a crash; your program won’t work. So, with object oriented programming we turn things on their heads slightly. Rather than thinking of the program in terms of the code we write and how it modifies the data, we think rather
in terms of objects. So, what would an object be? Well, using our Pong example – the objects in this game would
be – well we’ve got two paddles in here; so we’d have objects that would represent the
paddles; and we’ve got another object here, which is the ball. So we can sort of see what might be an object in a program. But the other thing we need to think about, rather think about writing code that
manipulates the data, we need to think about what these objects can do. What
operations can we perform on them? So for example, on our paddle, we might have operations which make it move up. We might have an operation that make it move down.
And that would be the same for both paddles. And we could ask the object to
perform that operation based on input from the keyboard. So the ball object would be
similar. Rather having operations to move it up to move it down, we just have
an operation says update your position. We let the ball itself work out how
its going to move around the screen. The other thing we might have is
an operation to detect whether the ball has collided with a paddle. In this case,
we’d say to the ball, Have you collided with this other object? So we’d have the two
objects working together to work out whether they’ve collided. Now I’ve described
that and I’ve talked about objects working with each other; objects having
operations. How does this all fit down to the way it works with a computer? Well, to do that, we need to think about what an object is. And actually, an object is made up of three things. So let’s look at the paddle as an example to start with, and we’ll think about the ball in a minute. It’s got some state. And that’s things like its
x position and its y position. But you might also if you – taking this further – you might
have to be like the speed associated with it as well. And they’ll just be
numerical values, perhaps integers, floating point numbers, depending on the system they’re using. There could also be other
objects, or we could build an object out of other objects, but that’s gonna be
advanced. We’ll look at that later on. So what we’ve got here is no different from the
data structures we talked about earlier. We’ve got some data and we’ve encapsulated
it together, and we can refer to it as a whole. The other thing that makes it
different is that we also have the operations, and these are the things that
the object can do. So if you think about our paddle, we said it would be able to move up, and we said it would be able to move down. We also said that you might be able to
draw it on the screen so we’d have a draw operation as well. We might have other operations that we need to make the program work, but these aren’t things that we’re necessarily going to want do to the object like making it move up or down. So we might have to have operations in ours to get its position. So we might have a get position operation. And that might be used, say, by the ball object we can detect whether it’s collided with it or not. And it’s having these operations which makes a difference between a simple data structure and an object. We think about the state as being the center of the
object. So we’ve got that state. That’s our x position and our y position. We’ll keep it simple for now for the paddle. When we think about things in an object oriented way,
we say, No, there’s a defined interface that can manipulate this data. And so you have
this sort of ring fence around the data. So you’ve got this sort of move up operation, the move down, the draw operation and the get position operation. And the only way
this data here can be accessed or manipulated by calling these operations. And these create what we call the interface to the object. You can get its
position, which will give you an xy coordinate. We’ll have more to have it return that. You can draw it, and you’d perhaps have to give it the screen you’d want it to be drawn on. You can get it to move down, and you can get it to move up. and so we now write our programs not in terms of manipulating the data,
but in terms of telling this object to move up or to move down. What actually
happens is that when the move up operation is performed, there’s a bit of code that we
specify here which updates the y variable and then it returns back to
whatever called it to continue working. So it’s actually a way of thinking about, and a way of
structuring, the program. [Offscreen] Would it be fair to say that’s a small program in its own right? Yes; I mean, that’s one way you could think about it. The object is a small self-contained program that’s in its own right. So it’s got a
move up, and a move down, it can be drawn and you get its position. And then you write another self-contained program for the ball, and that would perhaps have a similar state, but it would have different operations associated with it. So an object is actually made up of three things. We’ve seen the state; we’ve seen the operations; but the third thing
we have is actually not part of the object as such. But we also have identity. And this is simply saying that we can have many objects of the same type. So we can have one paddle, like this; but we can also – and I’ll draw a smaller one for speed – we can also have another one with its state and the operations around it. And this can represent the other paddle. So we’ve got two objects with the identical interfaces:
one to represent the paddle on the left; one to represent the paddle on the right.
So the only thing that can alter the data is the operation on the object of a particular identity. So if we wanted to access it, we say to an object with this identity, perform this operation; and that would then go and
modify the state of that object. As I’m sure you’ve guessed we’d have
another type of object to represent the ball, which should also have an x and a y position. And it would have a different set of operations around it. So, some of them
might be similar. Things like, we might have a draw operation. But we might have a collide operation instead, and then this time update positions – that’s really got an increase speed operation as well – let’s have a speed value in there. The two objects that represent the paddle of the same type. They have the same state
associated with them, the same operations associated with them. But this one is of
a different type. It’s got different state – it’s got an xy position and speed, and it
also has a different set of operations associated with it. And of course it has its own identity, and if we wanted to, we could have two balls in the game. And generally, in most object oriented languages, when we write these things, we define what we call a class, which is
basically the blueprint for building objects that it specifies, what operations they have; and provides the code for those operations. Even though it acts on each individual object, it also specifies how the data is stored and so on. And then when we create an object, we create a new one with its own identity,
with its own state, and then the operations can perform on that individual object. [Offscreen] If we come to update our program, you talked about how that can be a problem with structures. [Offscreen] How’s it easier here, then? Because we’ve got this
defined interface for the object, the rest of the program doesn’t know
anything about how that object is implemented. So it doesn’t need to know there’s a y coordinate or an x coordinate in there. All it needs to know is, to make it move up, it invokes the move-up operation on the object; or invokes the move down to make it move down. The beauty of this is you have lots of different classes of objects that
implement this interface, and then you can drop in whichever one you want. So you could have a paddle which is wider, which would just be different class of object,
and drop that in. We could have perhaps a specialist paddle which was two paddles that moved up and down in sync with each other. And that’d just be a different class of object which you’d drop in inside. And this is part of what we call inheritance in object oriented
programming is one of the things which can make it really powerful, which we’ll look at in another video. Over a long enough time scale, I think that human level artificial intelligence is completely inevitable. Um, if your auntie hangs around for long enough and continues to progress in what it can do…

100 thoughts on “Pong & Object Oriented Programming – Computerphile”

  1. I taught myself how to program basic C++ and sometimes do stuff for fun. Some time ago I made a basic Pong on the Windows CMD. This video will be interesting!

  2. Syed Mazahir Rizvi

    a little demo code in C or Java or any suitable language would be much appreciated. It would help in consolidating the concept. Great video though! cheers.

  3. 2:28 We could literally stop here, represent the paddles and the ball as maps, and represent their movement on the canvas (new paddles at their new positions) as functions of those maps. Then a game function that uses tail recursion to loop endlessly and call those functions. That's it. No need for OOP, no need to worry about memory at all!

  4. I have been using OOP for more than 10 years and I pretty much know everything there is to know about it. So tell me again, why am I watching this video?

  5. I'm pretty glad that web front-end development is moving away from OOP to stuff like React and Elm… encapsulated state is pretty nasty to reason about in large scale codebases.

  6. I like to think of objects as something like a television. You interface with the TV through a remote and can change channels etc. However you don't need to know all the work the TV is doing internally to display an image and change the channel etc All you need to know are the functions of the remote..

  7. I think the statement at around 11:50 that the rest of the program doesn't need to know anything about the object's behaviour should be prefixed by "for a single threaded program" ….

  8. Ever wondered why games need to be patched so much lately?
    Ever wondered why there are bugs in games that either cant be patched or take ages to be patched?
    Programming is becoming so abstract that people find it hard to pinpoint the location of a bug.
    Along with this programmers are regularly using 3rd party code/libs which itself may be buggy.
    Give me native C any day and I'll be able to tell you exactly how it works.

  9. Can you do an introduction about AOP? Wikipedia said it is patented by Xerox. And there isn't much information or resources out there.

  10. Hello help me to buy a leptop for Game Off-On line and softwars
    RAM 8GO DDR3L 1600MHZ Hard drive 1000 GO ,WIFI /BLUETOOTH/ WEB CAM
    Gpu :AMD RADEON HD 8870M 2GO
    CPU INTEL CORE I7 5500U 2.70GHZ ,RAM 8GO DDR3 1600MHZ
    ECRAN 15.6” HD LED
    they have the same price

  11. Thank you Dr Steve, your video was clear and very helpful. However, there is one thing that is confusing me?
    Given that both the paddle and the ball have an X,Y position, does the:-

    paddle look out for whether it has hit the ball or
    the ball look out for whether it has hit the paddle?

    Perhaps they are equally valid?

    Many thanks,


  12. Steams Nightcore™

    We had to program a version of object oriented pong in my computer class using java. It was good fun, but very difficult, so when I saw this video, I had to watch.

  13. Ironically, the original Pong arcade game was implemented entirely using logic gates. No CPU or software was used in the implementation.

  14. Bad example to choose.

    Pong – or at least pong clones – were done with hard-wired TTL logic with a few up-down counters and a clock. It was so simple a copy could be made in a shortish evening on the kitchen table. Nary a computer in sight.

  15. If you want to learn more about OOP, i'd suggest going with a language such as Java, or C#. C++ is fine, but it's not exactly beginner friendly (Considering you are a beginner).

  16. I'm looking in the comment section. I see a bunch of programmer wannabes talking about object oriented programming like they actually know more than they really do XD

  17. I have never once in all my days on god's green earth, heard somebody call that thing a bat O.o

    but really, people wonder why modern games tend to have more bugs in them (or at least we are more aware of it), when they've got millions and millions of strings of code all interacting with each other :O

  18. 8:28 – not exactly true. In many programing languages, if the attribute is public, you can access the attributes without functions.

  19. Am I the only one who gets annoyed when videos end with "…and we'll look that in another|next video"? And then you have to wait for days or weeks for it…. -_-

  20. Good explanation though it leaves out one of the main reasons for Object Oriented Programing, to allow software mills to turn programmers into modular, expendable, commodities. aka objects.

  21. I tried to write the pong game exactly the way he decribed it using javascript and put it on jsfiddle. It's very crude though. https://jsfiddle.net/za6Lhb3z/

  22. I feel like there is no way I could have understood this video if I didn't already have basic understanding of 'object oriented programming'. I think these videos have to appeal to ~90% of the viewers, but all it did is explain how to program a pong game to a person who already has some idea of how to do it.

    (This is assuming the video meant to explain 'Object Oriented Programming' to the general public)

  23. Xan-Kun Clark-Davis

    PONG was completely analog! If they had used "OOD", it would have taken another two decades beforde they could build one 😉

  24. A drawing object which can paint itself is the classical example of OOP. Once you go beyond the academic environment, you will learn that things fall apart and OOP in itself is not a sufficient answer for complexity.

  25. Like most tools, OOP has its occasions when it's the right tool for the job, and others where it doesn't really fit. Some parts of your program might be good candidates for it, others, not really. If it's right for that part, then use it. If not, use some other approach.

    Games are an example of where you might use both object oriented and procedural programming. The sound and graphics systems will be procedural, while the myriad of things in the game world will be objects.

  26. class Message:
        def send(self,recipient):
            # wait, should a message send itself or should I create a MessageSender class??

  27. I've never understood what OOP is not paralellizable. Why can't you just run each of the mini-programs–the object–independently of one another? That way you could have as many threads as you have objects. Divide up the threads to different cores, and there we go.

    I'd really like to see an episode on this. And why moving to something that is much harder to understand–functional programming–is supposedly the solution.

  28. I find OOP really beneficial. I have never had any problems with it. Without OOP you just end up with functions, that are encapsulated in namespaces with structures, which pretty much are objects anyway. Say you wanted to make 1000 balls in a pong game, is that even possible without OOP??

  29. You neglected to mention an essential property of the ball: its direction of movement, ie, it's a vector. Also, if it were a real ball, it would have mass, momentum, and a surface area with a coefficient of friction, generating a constantly decelerating force that would interact with the downward force of gravity to continuously alter its direction and velocity. And an even more real ball would have rotational inertia, etc. OOP makes it possible to encapsulate all of this data and processing into a single block of code. As you pointed out, OOP may be overkill for Pong, but not for more realistic simulations involving numerous entities with similar properties. For a more immediate example of OOP, the screen image we are reading is generated from literally hundreds of objects each with its own properties and methods.

  30. You neglected to mention an essential property of the ball: its direction of movement, ie, it's a vector. Also, if it were a real ball, it would have mass, momentum, and a surface area with a coefficient of friction, generating a constantly decelerating force that would interact with the downward force of gravity to continuously alter its direction and velocity. And an even more real ball would have rotational inertia, etc. OOP makes it possible to encapsulate all of this data and processing into a single block of code. As you pointed out, OOP may be overkill for Pong, but not for more realistic simulations involving numerous entities with similar properties. For a more immediate example of OOP, the screen image we are reading is generated from literally hundreds of objects each with its own properties and methods.

  31. Say I was programming a checkers game in object oriented Design, how could the piece know if it jumped another piece.assuming that the pieces know their location.

  32. Pong isn't really the greatest thing to use to demonstrate OOP. Games generally use the ECS, and not OOP.

  33. This has to be the worst and overcomplicated explanation of object oriented programming. It's not this complicated folks, honestly!

  34. You forgot to preface the video with a disclaimer warning viewers that OOP, just like communism, despite being a wonderful whimsical theoretical ideology, never works IRL because of human factor on one side and bureaucratic fascism on the other.

  35. I'm in the middle of redesigning my first proper Python program (a recordable looper for guitar/keyboard etc.) using classes. I typically have upwards of 20 loops running at once and before this I was making them all be nested dictionaries of variables and (ironically) classes coded by another person. It worked but was a horrible mess, and I only wrote them that way because I didn't understand Python classes enough to test them out. It's taking a bit of time to rewrite the program but it's so so so worth it. Code is so much easier to read and operate and is in some cases half the length.

  36. c++ is the most excellent language because it has both straight forward programming and object orient programming. Choose Your Destiny!

  37. 2:35 of course, this is not what this video is about, but I would be interested in a video about that subject.

    the way it is shown there, to just adjust the position linearly while the key is pressed, is a kind of naive approach which won't work out as a proper replacement for the paddle controllers where you could turn the knobs and determine yourself how far and how fast you turn it.

    however, now you want to make a Pong game that is playable and fun with key presses, which only can be ON or OFF, nothing inbetween.

    usually, the way it is attempted to "solve" this, is by adding some kind of acceleration, so if you only tap the key quickly, it moves just a little bit to allow for fine movement, however the longer you hold it down, the faster it will go.

    however, what kind of function to use for this acceleration? there are many possibilities.

    i think this really is an "unsolvable" problem, meaning there is no "proper" way to solve it, but a lot of ways one can go about it which might be "good enough".
    is there one that is the best?
    are there differing opinions?

  38. What is your get opinion of putting get in front of all accessors. This practice comes from Java Beans, but makes the grammar very strange. All of your nouns (and adjectives) turn into verbs: “Get would you like fries with that?“, “What is your get height?”, “if get name equals "root" then …”

  39. By the way, the screen would not be only represented by the computer, because what happens to the ball when it goes off the top/bottom of the screen?

  40. I like to the analogy of Space Invaders and Galaxian. Space Invaders could be programmed using a bunch of arrays, x-y values and speed. Galaxian needed a simple AI for the groups to break off and swoop down. That would be far easier using OO.

  41. I'll always prefer straight C. I can do things similar to OOP with it without a bunch of convoluted code. Many of the concerns about straight C coding over OOP you get with C++ just never come up for me. I have more problems with a misplaced semicolon than anything else like pointers (which C++ programmers seem to have an irrational fear of). For pong, I can place structs and functions to do with paddles in their own files, say paddle.c and paddle.h and it is all completely contained without the headaches and slowdowns that come with C++ or other high level languages.

    Maybe someday I will start coding with OOP like C++, I'm just not there yet. One thing I did like about C++ were vectors, so I wrote my own vector code for C which gave me the same functionality. 🙂

    I did write a pong game in C++ one time just to demonstrate classes for someone that challenged me to do it. It didn't take long to write but after I was done, I didn't like how it looked. I don't know, I have never "accidentally" altered the code of something I should not have. It seems as though OOP is a solution to a non-existent problem.

  42. Kylefassbinderful

    I really appreciate how you visually explained Object Oriented Programming. This is the first time I'm starting to grasp it.

  43. Then in base, of Object Oriented Programming, the shapes or models are the objects or shapes inside shapes are objects of other objects and whatever is then in manipulate are the operations on the objects or something, correct if wrong. Where the object is a program in itself as well and each object has its own identity with own set of rules. A class is a blueprint to all of that also and stuff is and then the computer interprets and then does compile if it makes sense or whatnot.

  44. All you need to take away from OOP is the idea of associating a function with a type, so that you can call type_instance.function() easily. Everything else is more trouble than its worth. Especially classical inheritance.

    And I'm saying this as a game dev. If I can make a whole game with has-a relationships rather than is-a's then you certainly can for business applications.

  45. I always hear people talking about how hard objects and classes are to people, but I always thought it was just super intuitive. Maybe I just found a good Java starting article (the first programming language I ever used classes in was Java, which is bassically forced object oriented programming [anyone who has ever used Java knows exactly what I mean]), but it always seemed really easy to me. IDK.

  46. 8:40 "We write the code not in terms of data manipulation, but in terms of telling this object to move up or down."
    This could be easily done with procedural language + contexts.
    Still, I don't see why migrate to OOP.

  47. having a deterministic size for the objects make garbage collection much easier to automate. IE making them strongly typed objects – especially if all your primitives are derived from a single base object.

  48. it's easy to fall into the OOP mindset. In theory it sounds like a great idea re-usable code relating to real world objects. But in reality it gets way over-engineered. Why write a class when a simple function will do? I go back to my old C code and I still find that easier to read than the Java code Ive written over the years.

  49. ►What r U Waiting◄ ☺◘♪☼

    never use inheritance always use composition! just google "composition over inheritance" before you go to that historical damned road of getting excited about inheritance.

  50. So much hate for OOP. I actually really like the style of thinking behind oop . Just the fact that you can write a class once and not have to think about the details behind what each method does. It just does what you want it do as an entity.

    plus it really helps in organizing your code instead of having each method like a block of 100 lines of code , each method can be less than 20 let's say because you have entities and helper methods that'll take care of most things for you.

    It might not be perfect for all use cases , but for most of the things I've made solutions for it's been helpful. Plus unit testing is like a piece of cake when you have classes.

    From what I noticed people who hate it , generally are either wanna be pseudo coders who use python to write scripts , functional programmers ( kudos to you and I respect your opinion) , or C/Assembler programs who think that any form of abstraction is nonperformant and slow.

Leave a Reply

Your email address will not be published. Required fields are marked *