在Python中使用 PyQt5 制作标签跳转
先决条件:PyQt5简介
PyQt5是跨平台的 GUI 工具包,一组用于 Qt v5 的Python绑定。由于该库提供的工具和简单性,人们可以非常轻松地开发交互式桌面应用程序。
现在,我们将看到如何使标签跳转。在 PyQt5 中设计 2-D 游戏时有时需要使标签跳跃,跳跃标签是当按下跳跃键(这里是空格键)时上升,上升速度降低,达到最大高度时的标签,它随着速度的增加开始下降。由于重力效应,上升时速度会降低,下降时速度会降低。
概念:我们可以通过随时间改变标签的Y坐标来使标签跳跃,我们可以借助经典力学的基本公式得到每次的Y坐标,即如下所示:
Force = 1/2 * mass * velocity^2
通过从初始 Y 坐标递减这个力值将为我们提供每次新的 Y 坐标,然后递减速度。
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.
下面是实现:
Python3
# 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())
输出 :