Tic Tac Toe Game In Python | Python Project for Beginners
Summary
TLDRIn this Python programming tutorial, Sagar introduces viewers to a project series where he teaches how to create a Tic-Tac-Toe game with a graphical user interface (GUI). The game involves two players, X and O, competing on a 3x3 grid. The first player to complete a line horizontally, vertically, or diagonally wins. Sagar demonstrates the game setup, including importing modules for GUI elements, defining functions to check for a winner, and handling button clicks. The script also covers creating buttons, arranging them in a grid, and updating the game state. The tutorial is interactive, encouraging viewers to try the game and share their feedback.
Takeaways
- 😀 The video is a tutorial on creating a Tic Tac Toe game in Python.
- 🎮 The game features a graphical user interface (GUI) for ease of play.
- 👾 Two players, X and O, take turns placing their symbols on a 3x3 grid.
- 🏆 The first player to complete a line horizontally, vertically, or diagonally wins.
- 🛠️ The 'Tkinter' module is used for creating the GUI elements of the game.
- 🔍 A 'check_winner' function is defined to verify if a player has won after each move.
- 🔑 The script includes a list of possible winning combinations for both horizontal, vertical, and diagonal lines.
- 💡 The 'button_click' function is used to handle player moves and update the game state.
- 🔄 The 'toggle_player' function alternates between players X and O after each turn.
- 🖥️ The 'root' window is created to house the game's GUI components.
- 🔢 A 'Label' widget is used to display whose turn it is and to show game status messages.
Q & A
What is the primary focus of the video series that Sagar is welcoming viewers to?
-The primary focus of the video series is a Python programming project series where Sagar is guiding viewers on how to create a Tic Tac Toe game.
What does 'GI' stand for in the context of the video?
-In the video, 'GI' stands for Graphical User Interface, which is used to make the game visually appealing and easier for players to interact with.
What is the basic gameplay of Tic Tac Toe as described in the script?
-Tic Tac Toe is a game played between two players, X and O, who take turns placing their symbols on a 3x3 grid. The player who first completes a line with their symbol, either horizontally, vertically, or diagonally, wins the game.
What module does the script mention for creating buttons and other graphical elements in Python?
-The script mentions the 'tkinter' module, which is used for creating buttons, labels, and other graphical elements in Python.
What function does the script define to check for a winner after every button click?
-The script defines a function called 'check_and_winner' to check for a winner after every button click by verifying if any of the possible winning combinations match the current state of the game board.
How does the script plan to indicate a win in the game?
-The script plans to indicate a win by changing the background color of the buttons that form a winning line to green and displaying a message to the user through a message box.
What is the purpose of the 'toggle_player' function in the script?
-The 'toggle_player' function is used to switch the current player from X to O or vice versa after each turn, ensuring that the game alternates between players.
How does the script handle the game's main loop to display the window and handle user input?
-The script uses the 'mainloop' method of the root window to handle the game's main loop, which is responsible for displaying the window on the screen and managing user input.
What is the significance of the 'current_player' variable in the script?
-The 'current_player' variable is used to keep track of whose turn it is in the game, alternating between 'X' and 'O' with each turn.
How does the script ensure that the game only progresses if the button is empty and the game is still ongoing?
-The script checks if the button is empty and the 'winner' variable is False (indicating the game is ongoing) before setting the current player's symbol on the button and calling the 'check_winner' function.
What is the role of the 'label' widget in the script?
-The 'label' widget in the script is used to display information to the user, such as whose turn it is, and to provide updates on the game's status.
Outlines
💻 Introduction to Python Programming and Tic Tac Toe Game Project
The speaker, Sagar, welcomes the audience to a Python programming series focused on creating a graphical user interface (GUI) based Tic Tac Toe game. He explains that GUI makes the game easy to play and visually appealing. The game involves two players, X and O, who place their symbols on a 3x3 grid, and the first player to complete a line horizontally, vertically, or diagonally wins. The speaker outlines the steps to create the game in Python, starting with importing the tkinter module for GUI elements and using a message box to display game status messages.
🎲 Developing the Game Logic and Functions
The script describes the process of developing the game logic, including defining a function to check for a winner after each button click. The function, named 'check_winner', verifies if any of the eight possible winning combinations are met. If a win condition is satisfied, the corresponding buttons' background color is changed to green, and a message is displayed to announce the winner. The speaker also discusses creating a 'button_click' function that updates the game state when a player clicks on a button, and a 'toggle_player' function to switch between players X and O. The script also covers creating the main game window and setting up the buttons in a grid layout.
🖥️ Finalizing the Game Setup and Testing
The speaker finalizes the game setup by creating a 'current_player' variable to track the player's turn and a 'winner' variable to determine if the game is still ongoing. A label widget is introduced to display whose turn it is. The speaker then arranges the label in the grid, ensuring it stretches across the full width. The main loop of the root window is initiated to display the window and handle user input. The script concludes with a test run of the game, where the speaker demonstrates the game functionality and encourages the audience to try the game and share their feedback. The source code is promised to be available in the video description for those interested.
Mindmap
Keywords
💡Python
💡Graphical User Interface (GUI)
💡Tic Tac Toe
💡Buttons
💡Variables
💡Functions
💡Loops
💡Conditionals
💡Events
💡Message Box
💡Grid
Highlights
Introduction to a Python programming project series focused on creating a Tic Tac Toe game with a graphical user interface.
Explanation of the game mechanics where two players, X and O, place their symbols on a 3x3 grid.
Discussion on how the game is won by the first player to complete a line horizontally, vertically, or diagonally.
Importing the Tkinter module to create the graphical user interface for the game.
Using the Messagebox for displaying game messages such as win or draw conditions.
Defining a function named 'check_and_winner' to verify if a player has won after each button click.
Creating a list of possible winning combinations for horizontal, vertical, and diagonal lines.
Looping through each combination to check if all three elements in a combination are the same and not empty, indicating a win.
Changing the background color of the buttons in a winning line to green and notifying the user of the winner.
Defining a function 'button_under_click' to handle the button click events and update the player's symbol.
Introducing a 'toggle_player' function to switch between players X and O after each turn.
Creating a root window titled 'Tic Tac Toe' using Tkinter.
Designing nine buttons for the game with specific font, size, and command parameters.
Arranging the buttons in a grid layout to resemble the 3x3 game board.
Initialization of a 'current_player' variable to keep track of whose turn it is.
Introduction of a 'winner' variable to determine if the game is still ongoing or has ended.
Creating a label widget to display whose turn it is using the 'current_player' variable.
Running the root window's main loop method to display the window and handle user input.
Debugging a minor issue where the 'O' player was not defined, which was then fixed.
Encouragement for viewers to try the game and share their feedback in the comments section.
Mention of the availability of the project's code in the video description for those interested.
Anticipation for the next video featuring the next project.
Transcripts
और यहां पे ये बोल रहा है अब ओ की बारी है
तो फॉर एग्जांपल ओ ने यहां पे चला एक्स ने
यहां पे चला ओ ने फिर से यहां पे चला और
मैं एक्स को जिता देता हूं जस्ट फॉर
वेरिफिकेशन पर्पस की कैसा दिख रहा है
दोस्तों मेरा नाम है सागर और मैं आपका
वेलकम करता हूं इस पाइथन प्रोग्रामिंग की
प्रोजेक्ट की सीरीज में और आज हम एक जीआई
बेस्ड टिक टकट गेम बनाने वाले हैं पाइथन
में जीआई का मतलब है कि ग्राफिकल यूजर
इंटरफेस जो कि हमारे गेम को देखने और
खेलने में आसान बनाता है टिक टकट एक बहुत
ही पॉपुलर और सिंपल गेम है जिसमें दो
प्लेयर्स एक्स और ओ का यूज करते हैं और 3
बाथ की ग्रिड में अपने सिंबल्स को लगाते
हैं जिस प्लेयर की पहले सिंबल की लाइन
कंप्लीट होती है चाहे वह हॉरिजॉन्टल हो
वर्टिकल हो या डायगोनल हो वो जीत जाता है
यानी कि उसे विनर करार कर दिया जा आइए
देखते हैं कि हम इस गेम को पाइथन में कैसे
बना सकते हैं सबसे पहले हम टिक इंटर
मॉड्यूल को इंपोर्ट करेंगे जो कि
python's बटंस लेबल्स और और भी बहुत कुछ
बना सकते हैं टेकटर को इंपोर्ट करने के
लिए सिंपल हम
लिखेंगे इससे हम टकटर को टी के नाम से
पुकार सकते हैं जो कि छोटा और आसान है अब
हम ंडर से मैसेज बॉक्स का इस्तेमाल करेंगे
जो कि हमें गेम में किसी भी मैसेज को
दिखाने के लिए काम आएगा जैसे कि अगर कोई
प्लेयर जीत गया हो या फिर गेम ड्रॉ हो गया
हो तो हम मैसेज बॉक्स की मदद से यूजर को
बता सकते हैं कि भाई ये स्टेटस है मैसेज
बॉक्स को इंपोर्ट करने के लिए सिंपल हम
यहां पे
[संगीत]
लिखेंगे अब हम एक फंक्शन डिफाइन करेंगे जो
कि चेक अंडर विनर नाम का होगा इस फंक्शन
का काम यह है कि वो हर बटन क्लिक करने के
बाद चेक करें कि कोई प्लेयर जीता है या
नहीं इसके लिए हम एक लिस्ट बनाएंगे जिसमें
एट पॉसिबल कॉमिनेशन होंगे जीतने के लिए ये
कॉमिनेशन अगर मैच होते हैं इनमें से आठ
में से कोई भी एक मैच होता है तो विनर जीत
जाएगा ये कॉमिनेशन तीन हॉरिजॉन्टल लाइंस
तीन वर्टिकल लाइंस और दो डायगोनल लाइंस
होंगे हर कॉम्बिन में तीन बटंस के इंडेक्स
होंगे जो कि रो से ट तक होंगे हम बटन को
ग्रंड में अरेंज करेंगे जैसे कि 0 1 2 3 4
5 6 7 8 इस लिस्ट को कमो नाम से पुकारेंगे
और इसके हर एलिमेंट को लूप में चेक करेंगे
अगर किसी कमो में तीनों एलिमेंट का
टेक्स्ट सेम है और खाली नहीं है तो मतलब
है कि उस प्लेयर की जीत हुई है उस केस में
हम तीनों बटन का बैकग्राउंड कलर ग्रीन कर
देंगे और मैसेज बॉक्स से यूजर को बताएंगे
कि कौन सा प्लेयर जीता है और फिर हम रूट
विंडो को बंद कर देंगे इस फंक्शन को लिखने
के लिए सिंपल हम लिखेंगे
[संगीत]
डिफाइन
[संगीत]
[संगीत]
तो इस तरीके से हमने हमारी आठ पॉसिबल
कॉमिनेशन कंडीशन रख दी है इनमें से कोई एक
भी मैच होती है तो यूजर को विनर करार दे
दिया
[संगीत]
[संगीत]
[संगीत]
जाएगा
[संगीत]
[संगीत]
[संगीत]
अब यहां पर मैं ये तीन आर्गुमेंट पास करना
चाहता हूं कि भाई जब कोई तीन बॉक्स मैच हो
जाए तो वो उस लाइन को पर्टिकुलर लाइन को
ग्रीन कलर का बैकग्राउंड करते बस यहां
मुझे इंडेक्सिंग चेंज करनी पड़ेगी तो कुछ
लोग क्या करेंगे यहां पे कंट्रोल c
कंट्रोल v करेंगे बट नहीं मैं एक शॉर्टकट
बताता हूं आपको जिस भी लाइन को नीचे सैंपल
रिप्लिकेट करना है उसमें अपना कर्सर लाके
छोड़ दीजिए देन शिफ्ट ऑल्ट और डाउन एरो आप
जैसे ही क्लिक करेंगे तो यह सेम आपको नीचे
कॉपी पेस्ट करके दे देगा शॉर्टकट है और
सिंपल हम यहां पर इसको चेंज कर देंगे वन
और टू
[संगीत]
[संगीत]
से
[संगीत]
यहां तक हम प्रोग्राम को एक बार चला के
देख लेते हैं कोई एरर तो नहीं आ रहा है तो
चलिए कोई एरर नहीं आ रहा है अभी आगे बढ़ते
हैं अब हम एक फंक्शन और डिफाइन करेंगे जो
कि बटन अंडर क्लिक नाम का होगा इस फंक्शन
का काम यह है कि जब यूजर किसी बटन पर
क्लिक करें तो उस बटन पर करंट प्लेयर का
सिंबॉल लग जाए इसके लिए हम फंक्शन में
इंडेक्स पैरामीटर लेंगे जो कि बटन का
इंडेक्स होगा फिर हम चेक करेंगे कि बटन
खाली है या नहीं और विनर वेरिएबल फॉल्स है
या नहीं विनर वेरिएबल यह बताता है कि गेम
अभी चल रहा है या नहीं अगर दोनों कंडीशन
ट्रू है तो हम बटन पर करंट प्लेयर का
टेक्स्ट सेट कर देंगे और चेक विनर फंक्शन
को कॉल कर देंगे फिर हम टॉगल प्लेयर
फंक्शन को कॉल कर देंगे जो कि नेक्स्ट
प्लेयर को सेट करेगा इस फंक्शन को लिखने
के लिए सिंपल हम यहां पर
[संगीत]
[संगीत]
[संगीत]
लिखेंगे
[संगीत]
[संगीत]
अब हम टगल प्लेयर नाम का एक फंक्शन डिफाइन
करेंगे जो कि करंट प्लेयर को चेंज करेगा
इसके लिए हम ग्लोबल कीवर्ड का इस्तेमाल
करेंगे जो कि हमें करंट प्लेयर वेरिएबल को
फंक्शन के अंदर मॉडिफाई करने देगा फिर हम
करंट प्लेयर को x से ओ ओ से एक्स में
बदलते जाएंगे और बदलते रहेगा ये और लेबल
विजेट को अपडेट कर देंगे जो कि यूजर को
बताएगा कि अब कौन से प्लेयर की बारी है या
फिर कौन से प्लेयर को चलने की चांस है इस
फंक्शन को लिखने के लिए सिंपल हम यहां पे
[संगीत]
लिखेंगे
[संगीत]
[संगीत]
[संगीत]
अब हम रूट विंडो बनाएंगे जो कि टटर से आता
है इस विंडो का टाइटल टिक टेक ट रखेंगे और
इसको रूट नाम से पुकारेंगे इस विंडो को
बनाने और टाइटल सेट करने के लिए हम
लिखेंगे
[संगीत]
सिंपल
[संगीत]
अब हम नौ बटन बनाएंगे जो कि गेम के लिए
होंगे इस बटन का टेक्स्ट फंट नॉर्मल और
हल्का सा बड़ा होगा विड्थ और हाइट 6स और
टू होंगे और कमांड पैरामीटर में हम लैडा
फंक्शन देंगे जो कि बटन क्लिक फंक्शन को
कॉल करेगा और उसम बटन का इंडेक्स देगा
लैडा फंक्शन एक एनोनिमस फंक्शन है जो कि
एक लाइन में लिखा जाता है इन बटंस को एक
लिस्ट में स्टोर करेंगे जो कि बटन नाम से
री जाएगी इन बटंस को बनाने के लिए हम एक
लिस्ट कंप्रीहेंशन का इस्तेमाल करेंगे जो
कि पाइथन ट्रिक है जिससे एक लाइन में
लिस्ट बना सकते हैं इसके लिए सिंपल हम
यहां पर
[संगीत]
[संगीत]
लिखेंगे
[संगीत]
[संगीत]
[संगीत]
अब हम इन बटंस को ग्रिड में अरेंज करेंगे
जिससे वह 3 बाती की शेप में देखेंगे इसके
लिए हम एमरेड फंक्शन का इस्तेमाल करेंगे
जो कि हर बटन के साथ हमें उसका इंडेक्स भी
देगा फिर हम ग्रिड मेथड का इस्तेमाल
करेंगे जो कि बटन को रो और कॉलम में सेट
करेगा रो और कॉलम हम बटन के इंडेक्स से
कैलकुलेट करेंगे इस हम सिंपल यहां पे
[संगीत]
[संगीत]
लिखेंगे अब हम करंट प्लेयर वेरिएबल
बनाएंगे जो कि x या ओ होगा हम इसको एक्स
से शुरू करेंगे और इसको करंट प्लेयर नाम
से पुकारेंगे इस वेरिएबल को बनाने के लिए
सिंपल हम
लिखेंगे अब हम विनर वेरिएबल बनाएंगे जो कि
ट्रू या फॉल्स होगा हम इसको फॉल्स से शुरू
करेंगे और इसको विनर नाम से पुकारेंगे यह
वेरिएबल हमें बताएगा कि गेम अभी चल रहा है
या नहीं इस वेरिएबल को बनाने के लिए सिंपल
हम
लिखेंगे
फॉल्स अब हम एक लेबल विजिट बनाएंगे जो कि
यूजर को बताएगा कि अब कौन से प्लेयर की
बारी है इस लेवल का टेक्स्ट करंट प्लेयर
के हिसाब से सेट करेंगे और इसको लेवल नाम
से पुकारेंगे इस लेवल को बनाने के लिए
सिंपल हम यहां पर
[संगीत]
[संगीत]
लिखेंगे
[संगीत]
[संगीत]
अब इस लेवल को ग्रिड में अरेंज करेंगे
जिससे वह बटन के नीचे दिखेंगे इसके लिए हम
ग्रिड मेथड का इस्तेमाल करेंगे और रो तीन
और कॉलम्स पन तीन देंगे इससे लेवल पूरे
विड्थ में स्ट्रेच हो जाएगा इसके लिए हम
सिंपल यहां पर
[संगीत]
लिखेंगे
[संगीत]
अब हम रूट विंडो को मेन लूप मेथड से
चलाएंगे जो कि विंडो को स्क्रीन पर
दिखाएगा और यूजर इनपुट हैंडल करेगा इसके
लिए हम सिंपल लिखेंगे
रूट डॉट मेन
लो तो अब हमारा प्रोग्राम हो चुका है रेट
से रन करके देखते
हैं और यहां पर एक्स तो यहां पर यह बोल
रहा है नेम ओ इज नॉट डिफाइंड देख लेते हैं
क्या गलती हुई है ओके तो यहां पर
मैंने जीरो लगाना था मैंने वो लगा दिया
चलिए तो यह गलती खैर होती रहती है ये हमने
इसे फिक्स भी कर लिया है अब फिर से इसे
क्लियर करते और रन करके देखते
हैं और यहां पर यह बोल रहा है अब ओ की
बारी है तो फॉर एग्जांपल ओ ने यहां पर चला
एक्स ने यहां पर चला ओ ने फिर से यहां पर
चला और मैं एक्स को जिता देता
जस्ट फॉर वेरिफिकेशन पर्पस की कैसा दिख
रहा है अब यहां पर देखिए प्लेयर एक्सस
यहां पर पॉप अप होके आ चुका है हम ओके पर
क्लिक करते हैं एक बार फिर से इसे रन करते
हैं और मैच को टाय करते
हैं मैं य पर किसी को जीतने नहीं दूंगा
वैसे ओ हो व जीत
गया तो लाइक मुझे नहीं पता कि इसमें इसको
बहुत ज्यादा अच्छे से कैसे खेलते हैं बट
आप लोग इस गेम को जरूर ट्राई करिए और
कमेंट सेक्शन में जरूर बताइएगा कि यह
प्रोजेक्ट कैसा लगा आपको और इस कोड का
एक्सेस यदि आपको चाहिए तो वह वीडियो के
डिस्क्रिप्शन में मिल जाएगा मिलते हैं
नेक्स्ट वीडियो में नेक्स्ट प्रोजेक्ट के
साथ थैंक यू सो मच फॉर वाचिंग
Voir Plus de Vidéos Connexes
Python TIC TAC TOE Tutorial | Beginner Friendly Tutorial
Cara Membuat Tetris sederhana untuk pemula di Mit App inventor GRATIS 2024 | programmer pemula
Create Graphical User Interfaces With Python And TKinter
TASK 5.1P - EMBEDDED SYSTEMS DEVELOPMENT
I Played Every SECRET GOOGLE GAME
Axel, Xaviera, Sandy & Jessica COC Nantangin Tim kumparan, Siapa Paling Jago?
5.0 / 5 (0 votes)