Jumbled word game : Jumbled word is given to player, player has to rearrange the characters of the word to make a correct meaningful word.
As we know this GUI code required Pyqt5 Module so before getting into code make sure to install pyqt5 in your system using the command,
pip install pyqt5
You can use as many words you want to make the game more interesting or complicated.
Code :
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 # importing libraries from PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *import randomimport sys class Window(QMainWindow): def __init__(self): super().__init__() # setting title self.setWindowTitle("Jumble Word Game") # 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','python','java','apple', 'pink','world'] # current word self.current_text = "" # score .. self.score=0 # start flag self.start_Flag = False self.count_value = 10 # 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 het the text self.input = QLineEdit(self) # setting geometry self.input.setGeometry(20, 150, 200, 40) # setting alignment self.input.setAlignment(Qt.AlignCenter) # bind entry button ... self.input.returnPressed.connect(self.check_action) # creating a timer label self.count = QLabel("10", self) # setting geometry self.count.setGeometry(230, 155, 80, 30) # setting alignment self.count.setAlignment(Qt.AlignCenter) # setting font self.count.setFont(QFont('Times', 14)) # setting style sheet self.count.setStyleSheet("border : 2px solid black;" "background : lightgrey;") # creating a timer object timer = QTimer(self) # adding action to the timer timer.timeout.connect(self.show_time) # start timer timer.start(1000) # 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 : lightgreen;") # 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 show_time(self): if self.start_Flag: # showing count value to label self.count.setText(str(self.count_value)) # checking if count value is zero if self.count_value == 0: # making start flag to false self.start_Flag = False # making line edit widget disable self.input.setDisabled(True) # set result self.result.setText("Game Over | Total Score :" + str(self.score)) # making result color grenn self.result.setStyleSheet("background : red;") # decrementing the count value self.count_value -= 1 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.score += 1 self.count_value = 10 self.result.setText("Score :"+str(self.score)) # making result color grenn self.result.setStyleSheet("background : lightgreen;") self.start_action() else: self.result.setText("Wrong Answer") # making result color red self.result.setStyleSheet("background : red;") def start_action(self): self.start_Flag = True self.input.setFocus() # 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("background : lightgreen;") # 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.start_Flag = False self.score = 0 self.count_value =10 self.count.setText(str(self.count_value)) self.j_word.setText("") self.result.setText("") # making result label color lightgreen self.result.setStyleSheet("background : lightgreen;") # create pyqt5 app App = QApplication(sys.argv)# create the instance of our Window window = Window()# start the app sys.exit(App.exec())
- Get link
- X
- Other Apps
Labels
Game Jumble words PROJECT PYTHON
Labels:
Game
Jumble words
PROJECT
PYTHON
- Get link
- X
- Other Apps
Comments
Post a Comment