Game Development in Python 3 With PyGame - 12 - Button interaction

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

Key Takeaways

This video tutorial demonstrates how to create a hover effect for buttons in PyGame using Python 3, including defining colors, determining mouse position, and using conditional statements to change the button's color when hovered over.

Full Transcript

what's going on everybody welcome to another P game tutorial video in this video we're going to be building on the last video in our sort of like miniseries in P game about how to make buttons in P game so um the way that we're going to give the feel of an interactive button is by having the button change color as we hover it so this is something that happens with like URLs right you have um you hover the mouse over URL and it underlines itself and sometimes it changes color um so this gives the idea to somebody or gives the impression to somebody that hey this is interactive maybe we can click it and so that's how what we're going to do with our buttons as well and actually a lot of buttons um have like hover um attributes to them um as a part of the CSS so uh that's what we're going to do people seem to find this um understandable as a clickable thing you know if it if it lights up so um that's what we're going to do so we'll come down here we've got our buttons oh and and since we want to make it a lighter color we have to define the lighter colors so here we've got red and green um I'm going to make red a little darker and since we're using RGB it's based on light so how would we make make red darker well we would take away some light so red will be 200 now and then we're going to Define brightcore red and then oops uh write underscore green equals equals and a bright red would be 255 and a bright green would be 0 255 easy enough now we'll come down to our game intro and we're going to make these buttons um interactive so how would how will we make them interactive first of all again a computer doesn't know that like oh he's over that green block you know it doesn't know that but what we can do is we can inform of it inform it that we are and as you saw with the events it's it's printing out the position of our Mouse constantly so we can grab that information so first of all how do we grab that so we'll just say mouse equals and this is py game. mouse. getor position and and Just for kicks let's go ahead and print Mouse and see what it says and then also I'm going to go ahead and comment out the print events that way we can see it um so we'll save and run that and you can see here that it is going crazy printing out this tupal about the location of the mouse so no matter where our Mouse is it's giving us a tupal of the X and Y location so how might we bring this button lighter bring it to light well anytime our our Mouse is within the coordinates of this box we we can know that by visiting this number so when it is we since we're drawing in frames per second we can light up the box use a lighter color to draw the box and it'll appear as if the box is interactive in some form or fashion so we'll close out of this and come back down here there's our Mouse we'll just we don't need to print it out every time so I'm just going to comment that out and then we ask the question um if we're going to have an if statement and it just needs to be in between the X and the X plus the width and the Y and the Y plus the width and as long as the two positions are within those values then we're within the box so um let's just write it out because that might sound confusing but if 150 so if the x coordinate plus the um let's see 150 Plus 100 so if the x coordinate plus the width if that is greater than the mouse position zero so the x value so if our x value of the mouse is greater or is less than um 150 plus 100 so that's like the right side of the Box while that while that exp position of our Mouse is simultaneously greater than 150 which is the x coordinate um we're doing good so far now we would just say if that's the case and uh for 50 + 50 right the Y location plus 50 so like the bottom of our box is greater than the mouse location um of the first element or first element so the Y of our Mouse with when it is s simultaneously greater than just straight up the Y so between the top and the bottom of the Y then it just so happens that we are within the boundaries of our uh uh box so we would draw this as um bright green I'm just going to leave that there for now we'll edit that to bright green in just a moment if that's the case um it would be bright green and then L's if it's not within all of those uh parameters then we'll just draw it as green so this need this green becomes a bright green and that should be it so now when we save and run this we have our game here and you can see that now when we hover over our little button here it it lights up as we hover over it and again it's not like the button is really interactive but it it's starting to feel somewhat Interactive because of this illusion that we're giving the user so um now what's left for us to do with this button well we need to put some text on the button and then the button needs to actually do something when we click on the button uh this is both of these things are easy enough but the other major thing that we need to do is we want to actually build a button function because right now our our you know code is already basically this big you know we could ignore some of these empty lines but I mean we've got a decent amount of code piling up here and at the end of the day it would be better to just have a function where we call like button what does the button say where is the button period and then or like what does the button do so we're going to have to add all of that stuff so anyway in the next video uh I think we'll probably throw text on here and then after that we'll make the button function so to speak but um that's what you guys have to look forward to so as always thank you for watching thanks for all of the support and subscriptions and until next time

Original Description

See http://pythonprogramming.net for sample code PyGame with Python 3 Playlist: http://www.youtube.com/playlist?list=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO In this PyGame with Python 3 programming tutorial, we cover the creation of a hover effect for our buttons. The idea of making buttons change color when a mouse is hovering them is useful to alert the user that the thing on the screen might be interactive. http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
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 tutorial teaches how to create interactive buttons in PyGame by adding a hover effect, which changes the button's color when the mouse is over it. This is achieved by using conditional statements to check the mouse position and update the button's color accordingly.

Key Takeaways
  1. Define colors using RGB values
  2. Determine the mouse position using PyGame's mouse.get_pos() function
  3. Use conditional statements to check if the mouse is within the button's boundaries
  4. Update the button's color based on the mouse position
💡 Using conditional statements to check the mouse position and update the button's color can create a hover effect, making the button appear interactive.

Related Reads

📰
Stop Copy-Pasting: How to Use an AI Content Copilot to Rewrite Your Resume Bullet Points
Learn to use AI content copilots to rewrite resume bullet points and tailor your resume for each job application
Medium · AI
📰
5 AI Apps in 2026 That Can Actually Help You Start a Side Hustle
Discover 5 AI-powered apps that can help you start a side hustle in 2026, from automating tasks to generating new business ideas
Medium · Data Science
📰
The AI Tools You’re Paying For Have Free Twins Nobody Mentions
Many AI tools have free alternatives with similar functionality, learn how to identify and utilize them to optimize your budget
Medium · AI
📰
8 Free Web Tools I Built With AI — No Uploads, No Sign-ups, All Browser-Based
Explore 8 free AI-powered web tools that run locally in your browser, requiring no uploads or sign-ups, and learn how to utilize them for various tasks
Dev.to · gan liu
Up next
How AI Is Transforming Analytics in Tableau Cloud & Server
Salesforce Product Center
Watch →