Pong, Python & PyGame 01 - Computerphile

Computerphile · Intermediate ·📐 ML Fundamentals ·6y ago

Key Takeaways

This video demonstrates creating a Pong-like game using Python and PyGame, covering game development basics and Python programming fundamentals.

Full Transcript

hey Isaac so last time we look to sketching out some of the ideas of this this game I'm gonna not call it pong a squash pong game that you can implement with the PI game library where do we go from here so basically what we did in the previous video was to basically try to understand how the screen will look like the things that are going to be fixed and the objects that are gonna be moving around and it was all about defining the coordinates of different walls and the paddle and the ball and everything right and also we were thinking about okay these two objects the ball and the paddle they will be moving and they will have some attributes so basically the coordinates and where in which they are and also with they will have some functionality they will be able to move and to show an update their position right so this is what we did and this how we define it initially right so we know the ball will have coordinates X Y and some class variables like the color and the radius of that ball actually I just realized that the color should be something that the problem should also have probably if you want to write so now what we're going to be doing is to implement that using the library called pi game I've done my homework before this whenever you use a library like this you need to do your own homework you need to go to the documentation and read it carefully and try to find which functions are useful for you for whatever your purposes are as on that I'm gonna show you quickly on the screen the PI game website so here you have plenty of classes definitions and functionality so you have a class to define the color display to draw to take care of events to draw stuff with phones images anything you like really so you can do very cool games using all functionality I'm gonna stick to something very simple in this video we're gonna start off by importing the library right so if you're going to be using this you need to install the library in your computer right but whenever you have done that you need to import PI again and the very very first thing that you need to do is to initiate this so basically you need to call a function code in it if you're using one IDE like I'm doing in this video I'm using a spider you can actually do cool things you can just run it here and it's just saying hello from PI game community right so it's not doing much that's what we do when we run this piece of code but in this case so whenever you do program it typically you only get that console right and then you get output there but now we're gonna change that we want to be able to draw a screen right and to do that you need to use one function from PI game and you can look up on the documentation for this but we're gonna basically go at this play and we're gonna have to set the mode and the mode is not anything else but how big the screen will look like so we could do manually here just coordinates for example 1200 600 with in hive and that will create a screen so I'm gonna run it something very cool about this idea is I can just select a bit of code and run it shift control and then you create a blank window interesting uh actually it is black on my operating system it doesn't have to be black in your operating systems why because whenever we call this function what is happening under the hood is that this is making what we call a system code so this library is talking to the operating system and telling hey can you please create a screen for me in which I'm going to be doing the drawing right but it's not the user actually doing that directly it's the communication with the operating system so that's why it depends where you run the library they behavior might change slightly right so whenever you run this you could store the output of that function on a variable why I want to do that because I know this function will return something called a surface which is where I'm gonna be drawing stuff so one thing is the display and the surface is the black part that was there and which I'm going to be drawing things right something I don't like from here shown from programming point of view from the programming point of view you will never want to do this otherwise Torsen and I will kill you if we see that your hard-coding values here right so normally you have some variables in which you store those values and then that makes your program way more flexible so if you allow me what I'm going to do is to put a comment here and saying here are gonna be my variable and the first thing I want to do is to draw the main scenario right that's what I want to do down here and for now I'm gonna define variables with which is 1200 for now and hi this is gonna be 600 yep so now instead of using this values here how called it you put it here with an hi okay cool huh we thought we haven't done much we spent like two minutes years to get the assorted we haven't done much and I want to to draw something right so I want to do the first rectangle the first wall at the top right and for that what you need is a function that is within the class draw in PI again so this one is within draw and then you need to use rectangle and then you might be wondering well how do I use this function well well you have to do once again is to go on the API on the documentation and really a quick trick that you could do here is if you want to you can go here to the console and then you could write help and then the name of the function and then you will have what the function is expecting so expecting something called a surface color and then the rectangle itself which is a different type of object so basically initially I have to say I'm gonna draw this on the surface that I just created when initialize in the display then I have to define the color right of that one right so this is in a different class that we call color it understand me if I say I wanted white as simple as that and then we actually have to draw the rectangle which is in a tie game dot correct and then you have to provide two things for that one right once again I do know how this library works perfectly so now you need to know what are the coordinates of the top-left corner and then you need to indicate the width and the height of that rectangle so we actually did the numbers before we're doing it once again so the coordinate the initial coordinate the top-left corner of that rectangle is basically C no zero right I'm gonna put the double zero zero for that one and then you need to indicate in a dot in a different taboo you need to indicate the width and how big we want that border so the width is gonna be the same width as we define here because it's all the way on the screen but then we want to draw that for a few pixels you were saying 5 or 10 or 30 well do not guess probably the best thing to do is to define another variable border we say for example 20 right and then we are going to be using that and in case we want to change the values we can do it at the top of the program right so if you do this and now we run this code and then you look for pi again you will not see anything and you will be all disappointed that you will say what why why it doesn't work well pi game is a bit funny so you I did the drawing but I didn't tell pi gang I did it I just basically said I use this function yes but you need to tell the surface that they're having changes how you do that well you need to call a different function that is called flip so you need to do PI game dot display because you want to tell the display to refresh and then we flip it and whenever I run this I should be able to say it now on top as you can see here on the video right so it's that border good enough we want it bigger oh we can see why to the top yeah oh it I didn't see it first yeah okay we can make it bigger if you want to it's really up to you right let's leave it like that for now I can only see one line though is that right align across yes so only one line so look we have to write a full instruction just to draw that rectangle there right so now we you what you need to do is to draw the other three right but I'm not gonna be doing this in the video I have precooked this free and I'm gonna just copy paste with your permission because otherwise it's gonna take a long time and actually I was doing something funny before I put the color here directly when calling this function rectangle but now I have done that three times so what we could do is to create a variable in which we kind of store what is it gonna be the foreground color and what is gonna be the background color so here you can store the result of calling this pygame color wine you can put it in this variable and now our code will be much nicer just by using FG color in that way so there you go if now you do this and you run this code you will have two three walls tada yeah perfect yeah very good look at this now I'm gonna try to close this window what's happening it doesn't work it will never work why because we haven't told the operating system what to do we haven't told our program what to do when the user is clicking this cross so the first thing you need to do is to handle this and the only way to close this is to using a function called Pi game dot quit by the way this function will not work in all operating systems easily so if you're using a Mac you need something else that I I will not be covering here but if you do that yes so that disappeared now so that's again but if I run the whole game now it's gonna be doing the following look boom you're showing it and closing it that's not good is it it's quitting itself right so it's basically creating the display drawing the three walls and then saying no and have to quit but that's not what I want what I want is to check if the user has actually click on that cross and then I will close the program never before so what we need to do for that is to create a loop an infinite loop that will be running until the user says otherwise so we're gonna have a loop that is going to be true forever until something happens and for things happening my gang has a different class called events and it's a way to control what is happening how the user is interacting with that window right so there is a bit of detail that I'm gonna skip here but basically you need to call a class on the event and pool events and whenever you've got an event that is called quit that is basically when the user has click on that cross the program will stop never earlier so basically we need to check here a condition and check the type of event that we've got and whenever it that is equal to quit which is a predefined variable within PI game whenever that happens you break the loop otherwise the program will continue here and will not do anything at all just waiting for the user to do something so that while true means that it will carry on doing everything above it well that's a very good question so I'm drawing everything right it Droste it and then it doesn't do anything just basically stays here just looping here doing nothing waiting for the user to do something or waiting for us to write something within this while loop and that's the way we have to somehow stop this window from closing and it will only close when I now click on that one tada works right beautiful all right so so far we have actually just design how the game will look like we haven't even drawn yet the ball or the problem and that take us quite a bit of time just to understand how the display works how to put the walls right and how to make the program to stay there and wait for the user to do something and to play or to draw the ball so this is what we're going to do for now just to draw the environment and in the next video I'm gonna show you how to create the ball how to move it around and how to put the bubble and move it around as well okay I put before only two parameters but now we have two more which is the speed that we want this to go let's say on the horizontal axis and the vertical one so initially for example I'm gonna say that I just want the ball to move horizontally not vertically so the one

Original Description

The second part of Dr Isaac Triguero's walkthrough of creating a Pong-like game in Python, using PyGame. Part 00 : https://youtu.be/JRLdbt7vK-E Part 10 : https://youtu.be/Nk3Och0I4ZY Thorsten & Isaac's Python programming book: https://bit.ly/2p3r5IT https://www.facebook.com/computerphile https://twitter.com/computer_phile This video was filmed by Sean Riley and Isaac Triguero and edited by Sean Riley. Computer Science at the University of Nottingham: https://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

This video teaches how to create a Pong-like game using Python and PyGame, covering game development basics and Python programming fundamentals. It is part of a series and assumes some prior knowledge of Python. By following along, viewers can learn how to build a simple game and implement game logic in Python.

Key Takeaways
  1. Install PyGame
  2. Create a new Python project
  3. Initialize PyGame
  4. Set up the game window
  5. Implement game logic
  6. Add user input handling
💡 Using PyGame can simplify the process of creating games in Python, allowing developers to focus on game logic and development.

Related AI Lessons

Beyond the Elephant: On Manifolds, Projections, and the Hidden Assumptions of Neural Geometry
Learn how neural geometry relies on manifolds, projections, and hidden assumptions to understand complex data, and why it matters for AI development
Medium · AI
Beyond the Elephant: On Manifolds, Projections, and the Hidden Assumptions of Neural Geometry
Learn how neural geometry relies on manifolds, projections, and hidden assumptions to understand complex data, and why it matters for advancing AI research
Medium · Data Science
Beyond the Elephant: On Manifolds, Projections, and the Hidden Assumptions of Neural Geometry
Explore the geometric assumptions underlying neural networks and their implications on manifold learning and projections
Medium · Deep Learning
Beyond the Elephant: On Manifolds, Projections, and the Hidden Assumptions of Neural Geometry
Learn about the hidden assumptions of neural geometry and how manifolds and projections impact neural network performance
Medium · LLM
Up next
Machine Learning Project for Final Year Students | ML Project Idea @FameWorldEducationalHub
FAME WORLD EDUCATIONAL HUB
Watch →