我是python的新手,我对一些编码有一些帮助,我使用PyQt作为GUI,但我不确定如何为我的游戏整理我的代码,以便它实际上有效
下面是我的编码,包括PyQt gui。但主要问题是靠近底部,按钮实际连接和反应。
import pygame, sys
from pygame.locals import *
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(565, 516)
font = QtGui.QFont()
font.setBold(True)
font.setUnderline(True)
font.setWeight(75)
MainWindow.setFont(font)
MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("203613_355193901233787_2099162427_q.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout_4 = QtGui.QVBoxLayout()
self.verticalLayout_4.setObjectName(_fromUtf8("verticalLayout_4"))
self.label = QtGui.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Papyrus"))
font.setPointSize(17)
self.label.setFont(font)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName(_fromUtf8("label"))
self.verticalLayout_4.addWidget(self.label)
self.verticalLayout_3 = QtGui.QVBoxLayout()
self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.bWater = QtGui.QPushButton(self.centralwidget)
self.bWater.setMinimumSize(QtCore.QSize(160, 130))
self.bWater.setMaximumSize(QtCore.QSize(160, 130))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Papyrus"))
font.setPointSize(20)
self.bWater.setFont(font)
self.bWater.setObjectName(_fromUtf8("bWater"))
self.verticalLayout.addWidget(self.bWater)
self.bEarth = QtGui.QPushButton(self.centralwidget)
self.bEarth.setMinimumSize(QtCore.QSize(160, 130))
self.bEarth.setMaximumSize(QtCore.QSize(160, 130))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Papyrus"))
font.setPointSize(20)
self.bEarth.setFont(font)
self.bEarth.setObjectName(_fromUtf8("bEarth"))
self.verticalLayout.addWidget(self.bEarth)
self.horizontalLayout.addLayout(self.verticalLayout)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.bAir = QtGui.QPushButton(self.centralwidget)
self.bAir.setMinimumSize(QtCore.QSize(160, 130))
self.bAir.setMaximumSize(QtCore.QSize(160, 130))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Papyrus"))
font.setPointSize(20)
self.bAir.setFont(font)
self.bAir.setObjectName(_fromUtf8("bAir"))
self.verticalLayout_2.addWidget(self.bAir)
self.bFire = QtGui.QPushButton(self.centralwidget)
self.bFire.setMinimumSize(QtCore.QSize(160, 130))
self.bFire.setMaximumSize(QtCore.QSize(160, 130))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Papyrus"))
font.setPointSize(20)
self.bFire.setFont(font)
self.bFire.setObjectName(_fromUtf8("bFire"))
self.verticalLayout_2.addWidget(self.bFire)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.lOutcome = QtGui.QLabel(self.centralwidget)
self.lOutcome.setText(_fromUtf8(""))
self.lOutcome.setObjectName(_fromUtf8("lOutcome"))
self.horizontalLayout_2.addWidget(self.lOutcome)
self.bFinish = QtGui.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Viner Hand ITC"))
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.bFinish.setFont(font)
self.bFinish.setObjectName(_fromUtf8("bFinish"))
self.horizontalLayout_2.addWidget(self.bFinish)
self.verticalLayout_3.addLayout(self.horizontalLayout_2)
self.verticalLayout_4.addLayout(self.verticalLayout_3)
self.gridLayout.addLayout(self.verticalLayout_4, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.bFinish, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
QtCore.QObject.connect(self.bWater, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lOutcome.update)
QtCore.QObject.connect(self.bEarth, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lOutcome.update)
QtCore.QObject.connect(self.bAir, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lOutcome.update)
QtCore.QObject.connect(self.bFire, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lOutcome.update)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Avatar: The Last Airbender", None))
self.label.setText(_translate("MainWindow", "Avatar:The Last Airbender", None))
self.bWater.setText(_translate("MainWindow", "Water", None))
self.bEarth.setText(_translate("MainWindow", "Earth", None))
self.bAir.setText(_translate("MainWindow", "Air", None))
self.bFire.setText(_translate("MainWindow", "Fire", None))
self.bFinish.setText(_translate("MainWindow", "Finish", None))
#to allow random number generation
import random
#variables for the elements
bWater = 1
bEarth = 2
bFire = 3
bAir = 4
def main():
app = QtGui.QApplication( sys.argv )
sys.exit(app.exec_())
bWater.clicked.connect( water_clicked )
bEarth.clicked.connect( earth_clicked )
bFire.clicked.connect( fire_clicked )
bAir.clicked.connect( air_clicked )
def water_clicked( ) :
# Randomly pick one of 'air', 'fire', 'water' or 'earth'
z = [ 'air', 'fire', 'water', 'earth' ][random.randrange( 4 ) ]
if z == 'air' :
print( 'Stalemate' )
elif z == 'water' :
print( 'Air removes Water, you win!' )
def earth_clicked( ) :
# Randomly pick one of 'air', 'fire', 'water' or 'earth'
z = [ 'air', 'fire', 'water', 'earth' ][ random.randrange( 4 ) ]
if z == 'air' :
print( 'Stalemate' )
elif z == 'water' :
print( 'Air removes Water, you win!' )
def fire_clicked( ) :
# Randomly pick one of 'air', 'fire', 'water' or 'earth'
z = [ 'air', 'fire', 'water', 'earth' ][ random.randrange( 4 ) ]
if z == 'air' :
print( 'Stalemate' )
elif z == 'water' :
print( 'Air removes Water, you win!' )
def air_clicked( ) :
# Randomly pick one of 'air', 'fire', 'water' or 'earth'
z = [ 'air', 'fire', 'water', 'earth' ][ random.randrange( 4 ) ]
if z == 'air' :
print( 'Stalemate' )
elif z == 'water' :
print( 'Air removes Water, you win!' )
除此之外,我将如何添加获胜/失败/僵局中的所有结果,以便它们出现在loutcome标签中?
第一个错误:
追踪(最近一次通话): 文件“C:\ Users \ Me \ Desktop \ testy.py”,第174行,中 bWater.clicked.connect(water_clicked) AttributeError:'int'对象没有'clicked'属性