Open In App

PyQt5 – Jumble Word Game

Last Updated : 04 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see how we can create a jumble word game using PyQt5. Jumbled word game : Jumbled word is given to player, player has to rearrange the characters of the word to make a correct meaningful word. Below is how game will look like

GUI implementation steps : 
1. Create a heading label that display the game name 
2. Create label that shows the jumbled word 
3. A line edit widget that get the text 
4. A push button that check the input text which is beside the line edit 
5. Create a result label that tells if answer is correct or not and change its color 
6. Two push button to start and reset the game
Back end implementation steps : 
1. Create a list of words 
2. Add action to the check button 
3. Inside the action of check push button get the input text and compare it with the input word 
4. If word matches show result as correct and set color to green else set color to red and say wrong 
5. Add action to the start button 
5. Inside the start button action get the current word from the list using random function 
6. Create a jumble word from the current word and set it to the jumble label 
7. Remove the text of the result label and set color to yellow 
8. Add action to the reset button 
9. Inside the reset button action set the current word to blank 
10. Remove text from all labels and set their color back to original one 

Below is the implementation 

Python3




# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import random
import sys
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Python ")
 
        # setting geometry
        self.setGeometry(100, 100, 320, 350)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
        # words
        self.words = ['red', 'cold', 'hot', 'geeks', 'rain',
                           'black', 'snow', 'hills', 'code']
 
        # current word
        self.current_text = ""
 
    # method for components
    def UiComponents(self):
 
        # creating head label
        head = QLabel("Jumbled Word Game", self)
 
        # setting geometry to the head
        head.setGeometry(20, 10, 280, 60)
 
        # font
        font = QFont('Times', 15)
        font.setBold(True)
        font.setItalic(True)
        font.setUnderline(True)
 
        # setting font to the head
        head.setFont(font)
 
        # setting alignment of the head
        head.setAlignment(Qt.AlignCenter)
 
        # setting color effect to the head
        color = QGraphicsColorizeEffect(self)
        color.setColor(Qt.darkCyan)
        head.setGraphicsEffect(color)
 
        # creating label to show the jumbled word
        self.j_word = QLabel(self)
 
        # setting geometry
        self.j_word.setGeometry(30, 80, 260, 50)
 
        # setting style sheet
        self.j_word.setStyleSheet("border : 2px solid black; background : white;")
 
        # setting font
        self.j_word.setFont(QFont('Times', 12))
 
        # setting alignment
        self.j_word.setAlignment(Qt.AlignCenter)
 
 
        # creating a line edit widget to get the text
        self.input = QLineEdit(self)
 
        # setting geometry
        self.input.setGeometry(20, 150, 200, 40)
 
        # setting alignment
        self.input.setAlignment(Qt.AlignCenter)
 
        # creating push button to check the input
        self.check = QPushButton("Check", self)
 
        # setting geometry
        self.check.setGeometry(230, 155, 80, 30)
 
        # adding action to the check button
        self.check.clicked.connect(self.check_action)
 
        # result label
        self.result = QLabel(self)
 
        # setting geometry
        self.result.setGeometry(40, 210, 240, 50)
 
        # setting font
        self.result.setFont(QFont('Times', 13))
 
        # setting alignment
        self.result.setAlignment(Qt.AlignCenter)
 
        # setting style sheet
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
 
        # creating push buttons to start and reset the game
        start = QPushButton("Start", self)
        reset = QPushButton("Reset", self)
 
        # setting geometry to both the button
        start.setGeometry(15, 290, 140, 40)
        reset.setGeometry(165, 290, 140, 40)
 
        # adding action to both the buttons
        start.clicked.connect(self.start_action)
        reset.clicked.connect(self.reset_action)
 
    def check_action(self):
 
        # getting text from the line edit
        text = self.input.text()
 
        # checking if text is similar to the current text
        if text == self.current_text:
            self.result.setText("Correct Answer")
 
            # making result color green
            self.result.setStyleSheet("background : lightgreen;")
 
        else:
            self.result.setText("Wrong Answer")
 
            # making result color red
            self.result.setStyleSheet("background : red;")
 
 
 
    def start_action(self):
 
        # selecting one word
        self.current_text = random.choice(self.words)
 
        # sample() method shuffling the characters of the word
        random_word = random.sample(self.current_text, len(self.current_text))
 
        # join() method join the elements
        # of the iterator(e.g. list) with particular character .
        jumbled = ''.join(random_word)
 
        # setting text to the jumbled word
        self.j_word.setText(jumbled)
 
        # setting result text to blank
        self.result.setText("")
 
        # making result label color yellow
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
 
        # setting text of input to blank
        self.input.setText("")
 
    def reset_action(self):
 
        # setting current text blank
        self.current_text = ""
 
        # setting text of input to blank
        self.input.setText("")
 
        # clear the text of all the labels
        self.j_word.setText("")
        self.result.setText("")
 
        # making result label color yellow
        self.result.setStyleSheet("border : 2px solid black; background : yellow;")
 
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())


Output : 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads