Prerequisite: Introduction to PyQt5
PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library.
NOw, we will see how we can make the label jump. Sometimes there is a need to make the label jump while designing 2-D games in PyQt5, Jumping label is a label which when pressed jump key(here space bar) goes up and its speed of going up decreases and when it attains the maximum height, it starts going down with the increase in speed. Speed decreases while going up and decreases while going down it due to the gravity effect.
Concept : We can make the label jump by changing its Y co-ordinate with the time, we can get the Y co-ordinate for each time with the help of basic formula from classical mechanics i.e given below :
Force = 1/2 * mass * velocity^2
By decrementing this force value from the initial Y co-ordinate will give us the new Y co-ordinate for each time and then decrementing the speed.
Implementation Steps :
1. Create a main window.
2. Create a jump flag, and a speed and mass variable.
3. Create a label.
4. Set label geometry and the style sheet.
5. Override the key press event.
6. Inside the key press event check when the space bar key is pressed.
7. If space bar key is pressed make the jump variable true.
8. Create a timer object and add action to the timer which get called after every 50 milliseconds.
9. Inside the timer action get the Y co-ordinate of the label and check if the jump flag is true.
10. If jump flag is true calculate the force and decrement the force value from the Y co-ordinate and set the new position to the label.
11. If the speed becomes zero make the mass negative, and if the label comes back to normal position make the jump flag false and reset the value of mass and speed.
Below is the implementation :
# importing required libraries from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
# Window class class Window(QMainWindow):
def __init__( self ):
super ().__init__()
# setting title
self .setWindowTitle( "Python " )
# width of window
self .w_width = 500
# height of window
self .w_height = 500
# setting geometry
self .setGeometry( 100 , 100 ,
self .w_width, self .w_height)
# calling method
self .UiComponents()
# showing all the widgets
self .show()
# speed variable
self .speed = 5
self .mass = 1
# jump flag
self .jump = False
# method for components
def UiComponents( self ):
# creating a label
self .label = QLabel( self )
# label width
self .l_width = 40
# label height
self .l_height = 40
# setting geometry to the label
self .label.setGeometry( 200 , 200 ,
self .l_width,
self .l_height)
# setting stylesheet to the label
self .label.setStyleSheet( "QLabel"
"{"
"border : 4px solid darkgreen;"
"background : lightgreen;"
"}" )
# creating a timer object
timer = QTimer( self )
# setting timer start time
timer.start( 50 )
# adding action to the timer
timer.timeout.connect( self .show_time)
# method called by the timer
def show_time( self ):
# checking jump flag
if self .jump:
y = self .label.y()
# calculate force (F).
# F = 1 / 2 * mass * velocity ^ 2.
# here we are not using ( 1/2 )
Force = self .mass * ( self .speed * * 2 )
# change in the y co-ordinate
y - = Force
self .label.move( 200 , y)
# decreasing velocity while going up
# and become negative while coming down
self .speed = self .speed - 1
# object reached its maximum height
if self .speed < 0 :
# negative sign is added to
# counter negative velocity
self .mass = - 1
# objected reaches its original state
if self .speed = = - 6 :
# making jump equal to false
self .jump = False
# setting original values to
# speed and mass
self .speed = 5
self .mass = 1
# override the key press event
def keyPressEvent( self , event):
# if space bar is pressed
if event.key() = = Qt.Key_Space:
# make the jump flag true
self .jump = True
# create pyqt5 app App = QApplication(sys.argv)
# create the instance of our Window window = Window()
# start the app sys.exit(App. exec ())
|
Output :