Infinite Cubes 2 - OpenGL with PyOpenGL Python and PyGame - 9

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

The video demonstrates how to create infinite cubes in a 3D field using PyOpenGL and PyGame, covering topics such as modifying the set_vertices function, defining camera clipping planes, and optimizing game performance. It utilizes tools like PyOpenGL and PyGame to achieve this.

Full Transcript

everybody was going on welcome to another PI game with PI OpenGL and obviously Python tutorial video in this video what we talk about is how we can take our block generations and turn them even into an even better infinite possibilites sort of idea so right now we have a working version of infinite block creation so this will just continue happening but we always have a couple of problems that we can notice right away and some one of the major problems is blocks just kind of appear out of nowhere and so that's no good also we can avoid all blocks by simply scrolling up like this and then we not don't worry so we need kind of handling to handle both of these events so let's go and get started so the first thing that I'd like to us to do is let's go ahead and modify the new or basically set vertices function here and you'll see that we have max distance min distance and now we are going to add two more parameters the first one whoops the first one will be a camera underscore X and we'll set that default to zero and then we're going to have camera underscore Y and again we'll set that to a default of 0 and this will be will be passing through where are we in the 3d environment then we can make boxes basically that correspond to where we are next we see first of all let's go ahead and come down here and well right away we want to we basically flip camera x and y so camera underscore x equals negative 1 times the int of camera X and then we have camera underscore y equals negative 1 times the int of camera underscore Y so we're just flipping those around now now like what I was saying before you need to have enough movement on either side of the camera erna possibility for cubes on either side of the camera up-down left-right to account for the person literally just pressing and holding their arrow key just constantly moving over to the side to avoid cubes so we need to kind of account for this so now we're going to do is x-value change and we're going to say x-value change equals camera um well I it's a random dot R and range of camera underscore X - 75 - camera underscore X + 75 so where is the X and then 75 units on either either side so now we'll take this let's just copy-paste and instead of camera X that's camera Y and that's good enough um z change we don't need to change anything there so now we've basically we've got set vertices that is is done okay so now what we want to do is we're going to come down into our main loop here and we want to do a few things first of all we've been continuing to like kind of place ourselves a little randomly here and um to an extent I guess that doesn't really matter but let's just for simplicity's sake go ahead and just replace that with zero zero we were doing that whenever we were looping the main the main loop you know 10 times and having us being like a random location so the cube looked like it looked like it was coming randomly so uh so we've done that the next thing that we're going to go ahead and do is you've got X move Y move and now let's go ahead and do it corrects we'll say equals zero and cur underscore Y we'll say equals zero then what we'll go ahead and do is we'll come down here where do we have the x and y move so we've got GL translate and all that and the other thing that we want to do is we're going to Aker underscore X plus equals x move and then cur underscore y plus equals y move so we'll modify corrects and curve y which is the current location subsequently whenever we go to define here this cube Dyck set vertices we've got new max int camera Z and really we don't we don't want the camera um let's see I think what we'll do is first of all the new max so the new maximum is the new like how far out can we go we'll do basically camera Z - max distance times - so basically added this right so now the new max is basically that where are we now - you know or moving out the max distance times - so that's the maximum distance for a new cube and now we have we already know what new max is because that's what we just said right we've just redefined what new max is and then the minimum distance right so the closest to the camera possible is actually going to be camera Z - max distance and so the reason why we're doing that is because when we were saying like you know the cube can be anywhere between the cameras view and the max distance was cubes we're just popping up in our face and we can't have that so now cubes will not pop up in our face they will all pop past just past the clipping plane or it well just pass the clipping paint - you know basically - clipping planes away so there's that and then now finally we have to pass the cameras x and y-coordinates so we'll just say cur underscore X comma cur underscore Y and that should basically be basically be it the other thing I wouldn't mind having us do right now is let's just go ahead and comment out pygame duck time dot wait there's really no reason to have that and basically because we're pretty much like going to be like right on the line of heavy processing so I just don't think what we'll need a timer there so anyways let's go ahead and save and run it and see see what we come up with here hopefully no no major errors yet so as you can see cubes are kind of spawning one major problem that we have at the moment well first of all we don't have any cubes yet there we go beginning to wonder if we would get new cubes so we've got some new cubes coming so let me move this over a little bit here but we can see they kind of seem to be coming in groups which kind of makes sense because they're all being made past the clipping plane and then once there's no more we kind of have some more now there's a couple things that we could do like we could cut we could go up sorry I have this running on a different monitor and then ignored it so let's go ahead and close out of this first of all past a cube can go away we don't need to be printing that out anymore and I'm going to get rid of delete list and then basically all that since we don't need it um right I was going to add more cubes so for X and range let's try a 75 see if we can do that without lag it's still a little laggy for me part of the problem is just the distance that these are traveling at any one time let me speed up the distance so it's moving at 0.5 let's go ahead and move it let's go move in twos yeah it's still pretty still pretty jittery for me and but let's go ahead and test a couple of things so let's first of all let's move oh wow the player move needs to be a lot quicker I suppose since we're you know trying to tweak things this really just brings up the point that we need to have some more constants here so up here is where we're going to say we're going to go ahead and say game underscore speed will say that's two so game speed will be basically down here where is it where we move so GL translate right X move Y move replace two with game speeds so whatever we set game speed to will be the speed of the game and then we'll say a direct Direction speed we'll say that's a two as well so then we can take Direction speed copy that and then basically we can come down here where you have X move Y moves a direction speed and then Direction speed and this will be negative one times direction speed and again Direction speed negative one times that and then Direction speed and so now we can modify these these values a little easier and then we'll make that 50 so 50 cubes game speeds 2 Direction speed is 2 let's run it all right not bad on my computer so we'll stick with that and then now let's go ahead and try to move up okay so we're just constantly moving up here and we're still you know we don't have as many cubes as we would like necessarily and we're actually being able to avoid basically all cubes but the concept of infinity is clearly working the question is are we still going to have people able to cheat just by doing this like will they ever run into a cube so in theory we might need to generate the cubes further or we can make the game go a little faster but something would have to give okay so anyways that's going to conclude this video so we've kind of covered a lot of things but you know the movement where are we generating new cubes kind of off the screen so we can keep the player moving they can basically go infinitely in any direction and there will still always be cubes coming their way so that's that if you guys have any questions or comments on this video feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time

Original Description

In this OpenGL programming tutorial with Python and PyOpenGL we cover how to create infinite cubes in the player's 3D field no matter where they are using a dictionary. To do this, we delete any cubes that have passed and only show new cubes when necessary. We also show cubes far enough in either direction so that the player can constantly fly to the left or right and still have cubes in their way. PyOpenGL playlist: http://youtu.be/R4n4NyDG2hI?list=PLQVvvaa0QuDdfGpqjkEJSeWKGCP31__wD PyGame with Python 3 Playlist: http://www.youtube.com/playlist?list=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO Python: Python.org PyOpenGL: Either use http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl or http://pyopengl.sourceforge.net/ PyGame: Either use http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame or http://pygame.org/download.shtml sample code: http://pythonprogramming.net/opengl-pyopengl-python-pygame-tutorial/ http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches how to create infinite cubes in a 3D field using PyOpenGL and PyGame, covering topics such as modifying the set_vertices function and defining camera clipping planes. It provides a comprehensive guide on how to optimize game performance and implement essential game development principles.

Key Takeaways
  1. Modify the set_vertices function to add camera_X and camera_Y parameters
  2. Flip camera X and Y by multiplying with -1
  3. Calculate x-value change and y-value change based on camera position
  4. Replace random location with fixed location in the main loop
  5. Update x_move and y_move variables in the main loop
  6. Define camera clipping planes to prevent cubes from popping up in the face
  7. Add a timer to control the speed of cube spawning
  8. Modify game speed and direction speed to improve performance
  9. Use constants to make code more readable and maintainable
💡 Using camera clipping planes and optimizing game performance are crucial for creating a seamless and engaging 3D gaming experience.

Related Reads

📰
Beyond 11,000: Navigating the Unified MCP Server Catalog That's Reshaping AI Tooling
Learn to navigate the Unified MCP Server Catalog with over 11,000 tools, reshaping AI tooling and streamlining development workflows
Dev.to · Robert Pelloni
📰
Could AI Save the English Major?
Explore how AI can enhance the English major curriculum and improve student learning outcomes
Medium · AI
📰
Six AI Tools Advancing Mental Health Care Access
Discover six AI tools revolutionizing mental health care access, enabling remote screenings and personalized support
Dev.to AI
📰
Runway Can’t Win on ‘Best Model’ Anymore. So It Stopped Trying To.
Runway launches Media Router, a tool that automatically selects the best image, shifting focus from 'best model' to practical solutions
Medium · AI
Up next
Clicky AI Telugu 🔥 Control Your Computer with Voice | Best Free AI Tool 2026
Withmesravani_
Watch →