Pong & Object Oriented Programming - Computerphile

Computerphile · Intermediate ·🛡️ AI Safety & Ethics ·10y ago

Key Takeaways

The video demonstrates the strengths of Object Oriented Programming using Pong, with Dr Steve Bagley explaining the concepts, and touches on AI safety and related topics.

Full Transcript

today we're going to talk about objectoriented 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 because it uses so many different terms you talk about objects sending messages to other objects objects doing things and you think they're just bits of code how they doing anything and so on so it can be difficult to get your head around it so Nicholas worth who invented p once described programs as being algorithms Plus data structures now the data structure we can think of is the sort of the data that our programs manipulating whatever that might be and the algorithms that's basically the code that we're running to manipulate that data one way we can think about this is thinking about something like a simple pong game so one of the very early computer games you got back in the70s 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 there's another person controlled and then you had a a ball which was a square block and these literally were square blocks that were filled in on the screen so one player could control this and move it up and down another player can control this and 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 in museums there one at the Computer History Museum down in Cambridge if you want to go and play with it so the pong game seems very very simple but we can use it to sort of think about how the pram would work and so on and things so you've got two bats and a ball and so the data for this would be relatively simple this paddle or bat over here you 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 youd need the same for this one over here and again for the ball you need the ball's X POS and the balls y position so that's the data that our program would represent it doesn't need anything more than that really you perhaps have some representation of the screen but that' 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' change this one's y position to make the bat go down and we could 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're going to write some code very simply to do this so we can say something like if key let's say we're going to use Q to go up is pressed then paddle one y pa equal paddle one y pa+ one we could do something similar for the going down and for the same F we can write bit of program code that manipulate the variables to move the paddles around then we can redraw the screen 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 okay 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 uh pong game we've got available to hold the exposition available to hold the Y position available to hold the ball's X position available 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 program Lang they 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 we 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 we want to update one of the values we can just update that specific value like the Y position that we want to manipulate but there's still a slight issue with this sort of approach makes it easier to refer to things and we can have lots of these data structures referring to different things rather than having separate variables here we could have a position for paddle one and a position for paddle two so we can refer to them and have lots of them so 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 data 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 going to be more than a few hundred lines long as the program grows you're writing something bigger then you'll 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 10 entries rather than 100 in something but the other part of the program still expects 100 and so it writes over the end walks all over memory that's used for something else so you get a crash your program won't work so with objectoriented programming we turn things on their head 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 our program but the other thing we need to think about rather than thinking 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 than having operations to move it up or to move it down we just have an operation that says update your position we let the ball itself work out how it's going 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 and 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 the 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 something like the speed associated with it as well and there just be numerical values perhaps integers floating Point numbers depending on the system that you're using there could also be other objects so we could build an object object out of other objects but that's going to be an 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 we 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 want to be able to draw it on the screen so we'd have a draw op operation as well we might have other operations that we need to make the program work but these aren't things unless we going to want to do to the object like making it move up or down so we might have to have operations allow us to get its position so we might have a get position operation and that might be used say by the ball object so it 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 objectoriented 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 the 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 is 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 have worked out how we return that you can draw it and you perhaps have to give it the screen you 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 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 the way of structuring the program would it be fair to say that's a small program in its own right yes I mean that's that's one way you can think about 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 can get its position and then you'd write another self-contained program for the ball and that would perhaps have 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 of search 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 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 and as I'm sure you've guessed we'd have another type of object to represent the ball which would 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 how we' got an increased speed operation as well so it 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 they have 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 idty 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 so it specifies what operations they have and provides the code for those operations even though it acts on each individual object and it also specifies how the data is stored and so on and then when we create it object we create a new one with its own identity with its own State and then the operations can perform on that individual object if we come to update our program we talked about how that can be a problem with structures how's it easier here then because we've got this defined interface for the object the rest of the program doesn't need to 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 it invokes to move down to make it move down the beauty of this is you can 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 a different class of object and drop that in you could have perhaps a specialist paddle which was two paddles moved up and down in sync with each other and that would just be a different class of objects which you drop in inside and this is part of what we call inheritance in object oriented program which is one of the things which can make it really powerful which we'll look at in another video yeah over a long enough time scale I think that human level artificial intelligence is completely inevitable um if Humanity hangs around for long enough and continues to progress in what it can do

Original Description

Using Pong to demonstrate the strengths of Object Oriented Programming. Dr Steve Bagley explains The Singularity & Friendly AI: https://youtu.be/uA9mxq3gneE Chomsky's Hierarchy: https://youtu.be/224plb3bCog How Bitcoin Works: https://youtu.be/JyxRH18YlpA AI Safety: https://youtu.be/IB1OvoCNnWY http://www.facebook.com/computerphile https://twitter.com/computer_phile This video was filmed and edited by Sean Riley. Computer Science at the University of Nottingham: http://bit.ly/nottscomputer Computerphile is a sister project to Brady Haran's Numberphile. More at http://www.bradyharan.com
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Computerphile · Computerphile · 0 of 60

← Previous Next →
1 Follow the Cookie Trail - Computerphile
Follow the Cookie Trail - Computerphile
Computerphile
2 EXTRA BITS - Follow the Cookie Trail - Computerphile
EXTRA BITS - Follow the Cookie Trail - Computerphile
Computerphile
3 Musical Floppy Drives - Computerphile
Musical Floppy Drives - Computerphile
Computerphile
4 The Hair Algorithm - Computerphile
The Hair Algorithm - Computerphile
Computerphile
5 Getting Sorted & Big O Notation - Computerphile
Getting Sorted & Big O Notation - Computerphile
Computerphile
6 Quick Sort - Computerphile
Quick Sort - Computerphile
Computerphile
7 Hyper History and Cyber War - Computerphile
Hyper History and Cyber War - Computerphile
Computerphile
8 Entropy in Compression - Computerphile
Entropy in Compression - Computerphile
Computerphile
9 Original Elite on the BBC B - Computerphile
Original Elite on the BBC B - Computerphile
Computerphile
10 IP Addresses and the Internet - Computerphile
IP Addresses and the Internet - Computerphile
Computerphile
11 A Career in Video Games - Computerphile
A Career in Video Games - Computerphile
Computerphile
12 Error Detection and Flipping the Bits - Computerphile
Error Detection and Flipping the Bits - Computerphile
Computerphile
13 Programming BASIC and Sorting - Computerphile
Programming BASIC and Sorting - Computerphile
Computerphile
14 Birthplace of the World Wide Web - Computerphile
Birthplace of the World Wide Web - Computerphile
Computerphile
15 Punch Card Programming - Computerphile
Punch Card Programming - Computerphile
Computerphile
16 Programming Paradigms - Computerphile
Programming Paradigms - Computerphile
Computerphile
17 CERN Computing Centre (and mouse farm) - Computerphile
CERN Computing Centre (and mouse farm) - Computerphile
Computerphile
18 Error Correction - Computerphile
Error Correction - Computerphile
Computerphile
19 Home-Made Code - Computerphile
Home-Made Code - Computerphile
Computerphile
20 Security of Data on Disk - Computerphile
Security of Data on Disk - Computerphile
Computerphile
21 Gesture Controls - Computerphile
Gesture Controls - Computerphile
Computerphile
22 How Intelligent is Artificial Intelligence? - Computerphile
How Intelligent is Artificial Intelligence? - Computerphile
Computerphile
23 Encryption and Security Agencies - Computerphile
Encryption and Security Agencies - Computerphile
Computerphile
24 Virtual Machines Power the Cloud - Computerphile
Virtual Machines Power the Cloud - Computerphile
Computerphile
25 Hacking Websites with SQL Injection - Computerphile
Hacking Websites with SQL Injection - Computerphile
Computerphile
26 How Huffman Trees Work - Computerphile
How Huffman Trees Work - Computerphile
Computerphile
27 Cracking Websites with Cross Site Scripting - Computerphile
Cracking Websites with Cross Site Scripting - Computerphile
Computerphile
28 Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Computerphile
29 Texting Cabbage with a Recorder - Computerphile
Texting Cabbage with a Recorder - Computerphile
Computerphile
30 Hashing Algorithms and Security - Computerphile
Hashing Algorithms and Security - Computerphile
Computerphile
31 How YouTube Works - Computerphile
How YouTube Works - Computerphile
Computerphile
32 How NOT to Store Passwords! - Computerphile
How NOT to Store Passwords! - Computerphile
Computerphile
33 A New Golden Age of Video Games - Computerphile
A New Golden Age of Video Games - Computerphile
Computerphile
34 A Universe of Triangles - Computerphile
A Universe of Triangles - Computerphile
Computerphile
35 Cross Site Request Forgery - Computerphile
Cross Site Request Forgery - Computerphile
Computerphile
36 The True Power of the Matrix (Transformations in Graphics) - Computerphile
The True Power of the Matrix (Transformations in Graphics) - Computerphile
Computerphile
37 The Great 202 Jailbreak - Computerphile
The Great 202 Jailbreak - Computerphile
Computerphile
38 EXTRA BITS - Printing and Typesetting History - Computerphile
EXTRA BITS - Printing and Typesetting History - Computerphile
Computerphile
39 Triangles to Pixels - Computerphile
Triangles to Pixels - Computerphile
Computerphile
40 The Problem with Time & Timezones - Computerphile
The Problem with Time & Timezones - Computerphile
Computerphile
41 The Visibility Problem - Computerphile
The Visibility Problem - Computerphile
Computerphile
42 Lights and Shadows in Graphics - Computerphile
Lights and Shadows in Graphics - Computerphile
Computerphile
43 The Penguin Barcode - Computerphile
The Penguin Barcode - Computerphile
Computerphile
44 Typesetters in the '80s - Computerphile
Typesetters in the '80s - Computerphile
Computerphile
45 The Font Magicians - Computerphile
The Font Magicians - Computerphile
Computerphile
46 The Little Mac with the Big Bite - Computerphile
The Little Mac with the Big Bite - Computerphile
Computerphile
47 EXTRA BITS - More on the Original Mac at 30 - Computerphile
EXTRA BITS - More on the Original Mac at 30 - Computerphile
Computerphile
48 XP to Ubuntu with an 8yr old Hacktop - Computerphile
XP to Ubuntu with an 8yr old Hacktop - Computerphile
Computerphile
49 EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
Computerphile
50 EXTRA BITS - Making a Bootable USB in Linux - Computerphile
EXTRA BITS - Making a Bootable USB in Linux - Computerphile
Computerphile
51 EXTRA BITS - Installing Ubuntu Permanently - Computerphile
EXTRA BITS - Installing Ubuntu Permanently - Computerphile
Computerphile
52 The Dawn of Desktop Publishing - Computerphile
The Dawn of Desktop Publishing - Computerphile
Computerphile
53 What is Bootstrapping? - Computerphile
What is Bootstrapping? - Computerphile
Computerphile
54 Reverse Polish Notation and The Stack - Computerphile
Reverse Polish Notation and The Stack - Computerphile
Computerphile
55 Home-Made Z80 Retro Computer - Computerphile
Home-Made Z80 Retro Computer - Computerphile
Computerphile
56 Should Everybody Learn to Code? - Computerphile
Should Everybody Learn to Code? - Computerphile
Computerphile
57 Programming in PostScript - Computerphile
Programming in PostScript - Computerphile
Computerphile
58 Heartbleed, Running the Code - Computerphile
Heartbleed, Running the Code - Computerphile
Computerphile
59 YouTube's Secret Algorithm - Computerphile
YouTube's Secret Algorithm - Computerphile
Computerphile
60 YouTube Search & Discovery - Computerphile
YouTube Search & Discovery - Computerphile
Computerphile

The video teaches Object Oriented Programming using Pong as an example and touches on AI safety, providing a comprehensive introduction to OOP principles and their application in game development. The lesson is useful for understanding how to design and implement simple game systems using OOP. By watching this video, viewers can gain a deeper understanding of AI safety and its relation to game development.

Key Takeaways
  1. Create a Pong game using Object Oriented Programming
  2. Define classes and objects for game elements
  3. Implement collision detection and response
  4. Apply AI safety principles to game development
💡 Object Oriented Programming can be used to create simple and efficient game systems, and AI safety is an important consideration in game development.

Related Reads

📰
The AI That Can’t See the Monster in the Room
Learn why replacing human cybersecurity professionals with AI is flawed and how the future of the profession will evolve
Medium · AI
📰
Keeping Patient Voice Data Safe: HIPAA‑Compliant AI for PTs
Learn how to keep patient voice data safe with HIPAA-compliant AI for physical therapists, ensuring secure automation of soap note generation and insurance billing codes
Dev.to AI
📰
AI Safety Showdown
Explore the capabilities and limitations of various AI models, including GPT, Gemini, Qwen, Grok, Nano Banana, and Seedream, to understand AI safety concerns
Medium · AI
📰
The AI Jailbreak Problem Isn’t Gone Yet
The AI jailbreak problem persists, posing risks to LLM security and frontier models, and understanding its implications is crucial for AI safety
Medium · AI
Up next
Big Tech Is Turning Its Own Workers Into AI Training Data
AI Uncovered
Watch →