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