Random Cube Position - OpenGL with PyOpenGL Python and PyGame - 5

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

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 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 a random cube position and enable press-and-hold functionality for arrow keys in an OpenGL project using PyOpenGL and PyGame. The tutorial covers the necessary code changes to achieve these features.

Key Takeaways
  1. Import the random module
  2. Generate random X and Y coordinates for the cube
  3. Specify beginning numbers for X and Y movement
  4. Implement press-and-hold functionality for arrow keys
  5. Modify the translate function to include X and Y movement
💡 To create a random cube position, you can use the random module to generate random X and Y coordinates. To enable press-and-hold functionality, you need to specify beginning numbers for X and Y movement and implement key up and key down events.

Related Reads

📰
AI Watermark Remover: How EzMaker AI Turns Messy Images into Usable Assets
Learn how EzMaker AI removes watermarks from images, enabling businesses to reuse and repurpose visual assets efficiently
Dev.to · Lily Chen
📰
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Learn how to choose the right AI API for your project by considering tradeoffs, not just picking the 'best' model
Dev.to · Shaw Sha
📰
Preparo las oposiciones TIC con IA: así lo hago
Learn how to prepare for TIC oppositions with AI, focusing on efficiency and changing your mindset
Dev.to AI
📰
How accurate are AI calorie trackers in 2026, and should nutrition coaches rely on them?
AI calorie trackers have improved in accuracy, but their true value for nutrition coaches lies in promoting adherence, not just accuracy
Dev.to AI
Up next
15 NotebookLM Hacks That Change How You Work
SCALER
Watch →