Game Development in Python 3 With PyGame - 14 - Button Function
Key Takeaways
The video demonstrates how to create a button function in PyGame using Python 3, allowing for interactive buttons with customizable text, colors, and positions.
Full Transcript
hey guys what is going on welcome to another py game tutorial video in this video we're going to be putting on the things together that gave us our button up to this point uh which is putting text on the button and as well as making the button somewhat interactive we'll put all that together and make a button function and then after that we'll add true um functionality I suppose to the button so let's go ahead and get started so our button function so to speak should basically contain all of this code uh and the only difference is is all of the uh hard coding that we've got here besides the size of the font um needs to be contained within our button function and then also that will be just variables basically so let's go ahead and uh we'll take all of this and let's just highlight all everything here and don't cut it yet just hit copy and um we'll come up above game intro and we're going to add a button function so we're just going to Define button and for now let's do empty parameters colon enter paste and since some of this stuff got um misaligned from our paste let's just highlight it all and hold control Open brackets and it should come over so now this is our button fun function so what changes do we have to make well we when we call Button we want to know um first of all what what is the button going to say so we'll have uh message MSG then we want to know um where is that button located so we'll say uh x y and then since it's a rectangle it's width and height so XY width height and um then we need an inactive color and an active color so let's just do I a try to get away with one letter for everything [Music] um and that should contain everything so far in our button functionality so now now what we want to do is the message we'll just copy this replaces go okay that replaces go then we have X which is the starting point so basically anything that should be the x value so that's this one50 this 150 um this 150 and this 150 and then this 150 good um I just noticed that we also copy and pasted this rectangle for our our red um I'm going to go ahead and just delete that from our function we we that wasn't something we wanted so delete that um so we've got X now we're going to define y y is basically anything that says 100 in in this sense so or actually I'm sorry let's see not 100 450 is our y so copy and replace all of the 450s with y the woses of hard coding XY now we've got width width uh is this 100 value so everything 100 basically replace with uh W and then we have height and our height was 50 so everything that says 50 we replace uh with uh H nice and then we have inactive color and active color and actually I'm going to call these IC and AC and the uh active color will replace bright green and the inactive color will replace green and that should be our entire function I don't see any other like true hard code there so that's our button function awesome so now what we want to do is we'll just highlight this for now copy and then we're going to come down to the game intro and let's just put it here actually we'll put it like up here boom so we have button message XY width height height height inactive color active color so what's the message the first one will be go XY width height uh we should have it pasted somewhere like right here so just highlight 150 450 150 copy that um and then highlight X YW H paste inactive color is just green and then the active color was bright green so brightcore green easy enough let's go ahead and delete from game display. blit all the way up to where the button we just defined and now we'll go take this copy this and just paste it again and then instead of go the message is quit the XY width height is 550 450 150 so copy that and we'll put that here paste and then the uh inactive color red and the active color bright white red so now we've got go and quit and we can use our button function and we'll comment this out otherwise it will just right over a red rectangle over the uh red button that we just created delete and let's go ahead and save and run this one and we've got the name a bit racy go is good and quit is good so now we've got these buttons they're hover they're created we can make more buttons add different colors in active all that kind of stuff Active colors um and now we've got a pretty good button function the only thing we're missing at this point is an action for the button with that um stay tuned to the next video and as always thanks for watching thanks for all 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 how we can turn the button creation in PyGame code into a function that we can call to dynamically generate buttons.
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
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 AI Lessons
⚡
⚡
⚡
⚡
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI