我们正在尝试用python制作游戏,但是我们只能使用pyqt5库,所以,我们找不到从进程移动标签的方法,它给了我们错误:不可挑选的对象 问题出在widget中,我们试图把它放在管道中,但是得到了同样的错误。 也试图从类Enemy制作qwidget,但问题是一样的。
from PyQt5.QtWidgets import QMainWindow, QLabel, QDesktopWidget,QApplication, QGridLayout
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import multiprocessing as mp
##CONSTANTS##
MAXLIFE = 3
ALIEN_WIDTH = 32
ALIEN_HEIGHT = 32
ALIEN_ROWS = 3
ALIEN_COLS = 10
ALIEN_STEP_CONST = 60
class Enemy:
def __init__(self, xp, yp, picture):
self.x = xp
self.y = yp
self.steps = 0
self.pic = picture
def set_position(self, xp, yp):
self.x = xp
self.y = yp
def update(self):
while(True):
if self.steps < ALIEN_STEP_CONST:
self.steps += 1
self.x += 1
self.pic.setGeometry(self.x, self.y, ALIEN_WIDTH, ALIEN_HEIGHT)
self.pic.updateGeometry()
elif self.steps == ALIEN_STEP_CONST:
self.steps = ALIEN_STEP_CONST*2
elif self.steps > ALIEN_STEP_CONST:
self.steps -= 1
self.x -= 1
self.pic.setGeometry(self.x, self.y, ALIEN_WIDTH, ALIEN_HEIGHT)
self.pic.updateGeometry()
class SpaceInvaders(QMainWindow):
def __init__(self):
super().__init__()enter code here
self.initUI()
def initUI(self):
'''initiates application UI'''
self.setWindowIcon(QIcon('images/launch'))
self.setFixedSize(800, 800)
grid = QGridLayout()
grid.setSpacing(10)
self.setStyleSheet("background-image: url(images/space);")
self.center()
self.setWindowTitle('Space Invaders')
enemies = []
for i in range(0, ALIEN_ROWS):
for j in range(0, ALIEN_COLS):
pic = QLabel(self)
e = Enemy((ALIEN_WIDTH+45)*j, (ALIEN_HEIGHT+30)*i + 50, pic)
enemies.append(e)
pixmap = QPixmap("images/alien.png")
pic.setPixmap(pixmap)
pic.move((ALIEN_WIDTH + 45)*j, (ALIEN_HEIGHT + 30)*i + 50)
pic.show()
'''mp.Process(target=e.update).start()''' #this line
self.show()
def center(self):
'''centers the window on the screen'''
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) / 2,
(screen.height() - size.height()) / 2)
if __name__ == '__main__':
app = QApplication([])
space = SpaceInvaders()
sys.exit(app.exec_())