Random Cube Position - OpenGL with PyOpenGL Python and PyGame - 5
Key Takeaways
This video tutorial covers how to start a cube in a random position and enable press-and-hold functionality for arrow keys in an OpenGL project using PyOpenGL and PyGame.
Full Transcript
everybody what is going on welcome to another Python with OpenGL and PI OpenGL and PI game tutorial video this video we're just gonna be building on the last one and beginning to create our object avoidance games so right now a couple of problems we have is this cube starts in the same place every time also we want to be able to like press and hold our arrow key so this is pretty typical pi game code here as far as that's concerned so let's go ahead and make those necessary changes now so the first thing we want is one we want the cube to start at like a random location so we know we're going to need import random now we're just going to come down here and the first thing that we'll go ahead and do is since we're we're rerunning main every time what we can do to make the cube appear to be you know coming at us randomly really simply could be something like this we could just say a random dot R and range 0 earth actually negative 5 to 5 and then do the exact same thing again so that would be to the creation of the X variable and then again we do the same thing for the Y and that will start us in a random place every time so we can go ahead and visualize that really quick so there's the cube there now it's down there I've got another one there we would have hit that one there another one there but even with this you can see how it's absolutely necessary that we would probably need to have more than one cube but the other thing that we're having problems with is that we have to tapping or key we want to be able to hold the key and old a maneuver so doing that we've covered before but we'll go ahead and throw that into this video as well so first of all we need to specify some simple beginning numbers here so we'll say X move equals 0 Y move equals 0 easy enough and then we'll come down here and instead of you know calling this stuff we're gonna move in I think we'll keep we'll do point threes probably but instead of that we're going to say X move equals 0.3 here so a key left means we're hitting the left key there so 0.3 and then over here we'll just copy this and paste it right here and say negative 0.3 then we'll go ahead and do the exact same thing here so the up arrow we want to go ahead and do Y move equals negative 3 and then we'll do the same thing down here we would say Y move equals 3 then when we're all done well now we actually need to have key ups as well so um there's a couple of ways that we could do this we could just do it really easily we'll just copy that paste and instead of key down we'll do key up and then it would be x equals 0 but mmm I guess we'll just what we'll do is we'll take this move it up here or event equals execute X move equals 0 and then same thing again here or when K Y move equals 0 and that should do it so now if you press and hold the key our our move will hold there and if you release it the move will stop so let us now we'll basically modify this translate down here so you would just say X move wyman right so you could just add an X underscore move and then over here add in Y underscore move save and run that and now we're holding a position now of course there's a there's one major problem with that as if is you're still holding it like because mean is restarting every time while we move really quick there do we do three possibly what do we do there that's too fast yeah we did a whole number okay zero point three and then zero point three that was very fast okay so anyway so obviously we've got a couple of things that we're not too happy with this is really just meant to be a really simple working version of kind of what we had before where there's just one block but obviously this is an ideal so we're gonna have to change a lot of stuff here but anyway what we want is many blocks we want that key to hold we want them to be random the problem with many blocks that are random is the generation of those blocks having them there and then one other final problem that we have down the road is say you put a bunch of blocks on this screen and you just hold the left key or something you're going to avoid every single block okay so you need to have some blocks that are even off the screen entirely or you could have like walls or something like that that keeps the player in this specific area but I kind of like the idea of like infinity you've write down all the sides so that's probably where we're gonna we're going to go with this game but anyway that presents some calculation and really processing issues that we'll have to tackle so anyways that's it for this video if you guys have any questions or comments on this video feel free to leave them below otherwise as always thank you for watching thanks for all the support in the subscriptions and till next time
Original Description
In this OpenGL programming tutorial with Python and PyOpenGL we cover how to start our cube in a random position instead of always having the cube coming from the center.
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
Related Reads
📰
📰
📰
📰
AI Watermark Remover: How EzMaker AI Turns Messy Images into Usable Assets
Dev.to · Lily Chen
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Dev.to · Shaw Sha
Preparo las oposiciones TIC con IA: así lo hago
Dev.to AI
How accurate are AI calorie trackers in 2026, and should nutrition coaches rely on them?
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI