Форум сайта python.su
Kasta_neda
Почему бы не выкладывать скрипт в начало темы? Столько копий одного и того же в каждом посте.
Зачем там шарик? Его можно в другой теме выложить и назвать тенис. Добавить запуск игры при включении чтобы не терять время на просмотр шарика или просмотр пустого окна.
Офлайн
AZJIOКаждая игра, почти, начинается с начального окна, где, либо кнопки старта, настройки и т.д., либо инфа, либо логотипа, либо шкалы загрузки, у меня это шарик, здесь вся игра, код которой я написал, г..но, так что шарик наверное лучшее из всего
Зачем там шарик? Его можно в другой теме выложить и назвать тенис. Добавить запуск игры при включении чтобы не терять время на просмотр шарика или просмотр пустого окна.
Офлайн
Не знаю удачно или нет, переделал, прошу помощи, не пойму как рисовать линии и круг в случае ошибки, используя компонент QGraphicsView
программа
#!/usr/bin/python # -*- coding: utf-8 -*- # python2.7 #------------------------------------------------ import sys from PyQt4 import QtCore, QtGui, uic from output import * from PyQt4.QtCore import * from PyQt4.QtGui import * import random slova = [u'виселица',u'смартфон',u'маргарин',u'мегагерц',u'страница',u'креветка',u'микрофон',u'квартира',u'скорость',u'художник',u'жидкость',u'карантин',u'интернет',u'документ',u'мембрана',u'радиация',u'мавзолей',u'терминал',u'водитель'] class Proba(QMainWindow, Ui_MainWindow): def __init__(self): QMainWindow.__init__(self) self.setupUi(self) self.win = 0 self.defeat = 0 self.win2 = 0 self.defeat2 = 0 self.wo = None self.wor = [] self.clear.clicked.connect(self.clear_Click) # Обработка нажатия на кнопку очистить self.help.clicked.connect(self.help_Click) # Обработка нажатия на кнопку помощь self.start.clicked.connect(self.start_Click) # Обработка нажатия на кнопку старт self.connect(self.pushButton_b, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_z, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_i, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_j, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_a, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_d, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_e, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_g, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_v, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_e2, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_i2, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_k, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_l, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_m, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_n, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_o, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_p, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_r, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_s, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_t, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_y, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_ph, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_x, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_cs, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_h, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_hsh, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_hc, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_34, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_35, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_36, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_37, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_38, QtCore.SIGNAL("clicked()"), self.on_button_clicked) def on_button_clicked(self): tokens = (self.lineEdit_2, self.lineEdit_3, self.lineEdit_4, self.lineEdit_5, self.lineEdit_6, self.lineEdit_7) button = self.sender() u = button.text() print u'нажали ', unicode(u) print self.wo print self.wor if unicode(u) in self.wor: key_ = self.wor.index(unicode(u)) self.wor[key_] = '1' print key_ out = tokens[int(key_)] out.setProperty('text', unicode(u)) self.win = self.win + 1 else: self.defeat = self.defeat + 1 if unicode(u) in self.wor: try: key_2 = self.wor.index(unicode(u)) self.wor[key_2] = '1' print key_2 out = tokens[int(key_2)] out.setProperty('text', unicode(u)) self.win = self.win + 1 except: pass if unicode(u) in self.wor: try: key_3 = self.wor.index(unicode(u)) self.wor[key_3] = '1' print key_3 out = tokens[int(key_3)] out.setProperty('text', unicode(u)) self.win = self.win + 1 except: pass print self.win, self.defeat if self.win == 6: self.win2 = self.win2 + 1 self.lcdNumber.setProperty("intValue", self.win2) if self.defeat == 6: self.defeat2 = self.defeat2 + 1 self.lcdNumber_2.setProperty("intValue", self.defeat2) view = MyView() view.show() def clear_Click (self): self.lcdNumber.setProperty("intValue", 0) self.lcdNumber_2.setProperty("intValue", 0) print u"Очистить" def help_Click (self): print u"Помощь" def start_Click (self): self.wo = None self.wor = [] self.win = 0 self.defeat = 0 self.lineEdit_2.setProperty('text', '') self.lineEdit_3.setProperty('text', '') self.lineEdit_4.setProperty('text', '') self.lineEdit_5.setProperty('text', '') self.lineEdit_6.setProperty('text', '') self.lineEdit_7.setProperty('text', '') self.word = random.choice(slova) # выбираем рандомно слово self.wo = self.word[1:-1] # отрезаем от слова первую и последнюю букву for i in self.wo: self.wor.append(i) f1 = self.word[0] f2 = self.word[-1] self.lineEdit.setProperty('text', f1) self.lineEdit_8.setProperty('text', f2) app = QtGui.QApplication(sys.argv) w = Proba() w.show() sys.exit(app.exec_())
# -*- coding: utf-8 -*- 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(800, 600) font = QtGui.QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) MainWindow.setFont(font) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.gridLayoutWidget = QtGui.QWidget(self.centralwidget) self.gridLayoutWidget.setGeometry(QtCore.QRect(380, 70, 231, 201)) self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget")) self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget) self.gridLayout.setMargin(0) self.gridLayout.setObjectName(_fromUtf8("gridLayout")) self.pushButton_b = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_b.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_b.setObjectName(_fromUtf8("pushButton_b")) self.gridLayout.addWidget(self.pushButton_b, 5, 1, 1, 1) self.pushButton_z = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_z.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_z.setObjectName(_fromUtf8("pushButton_z")) self.gridLayout.addWidget(self.pushButton_z, 6, 3, 1, 1) self.pushButton_i = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_i.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_i.setObjectName(_fromUtf8("pushButton_i")) self.gridLayout.addWidget(self.pushButton_i, 6, 4, 1, 1) self.pushButton_j = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_j.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_j.setObjectName(_fromUtf8("pushButton_j")) self.gridLayout.addWidget(self.pushButton_j, 6, 2, 1, 1) self.pushButton_a = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_a.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_a.setObjectName(_fromUtf8("pushButton_a")) self.gridLayout.addWidget(self.pushButton_a, 5, 0, 1, 1) self.pushButton_d = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_d.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_d.setObjectName(_fromUtf8("pushButton_d")) self.gridLayout.addWidget(self.pushButton_d, 5, 4, 1, 1) self.pushButton_e = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_e.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_e.setObjectName(_fromUtf8("pushButton_e")) self.gridLayout.addWidget(self.pushButton_e, 6, 0, 1, 1) self.pushButton_g = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_g.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_g.setObjectName(_fromUtf8("pushButton_g")) self.gridLayout.addWidget(self.pushButton_g, 5, 3, 1, 1) self.pushButton_v = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_v.setMaximumSize(QtCore.QSize(20, 16777215)) self.pushButton_v.setObjectName(_fromUtf8("pushButton_v")) self.gridLayout.addWidget(self.pushButton_v, 5, 2, 1, 1) self.pushButton_e2 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_e2.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_e2.setObjectName(_fromUtf8("pushButton_e2")) self.gridLayout.addWidget(self.pushButton_e2, 6, 1, 1, 1) self.pushButton_i2 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_i2.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_i2.setObjectName(_fromUtf8("pushButton_i2")) self.gridLayout.addWidget(self.pushButton_i2, 7, 0, 1, 1) self.pushButton_k = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_k.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_k.setObjectName(_fromUtf8("pushButton_k")) self.gridLayout.addWidget(self.pushButton_k, 7, 1, 1, 1) self.pushButton_l = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_l.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_l.setObjectName(_fromUtf8("pushButton_l")) self.gridLayout.addWidget(self.pushButton_l, 7, 2, 1, 1) self.pushButton_m = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_m.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_m.setObjectName(_fromUtf8("pushButton_m")) self.gridLayout.addWidget(self.pushButton_m, 7, 3, 1, 1) self.pushButton_n = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_n.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_n.setObjectName(_fromUtf8("pushButton_n")) self.gridLayout.addWidget(self.pushButton_n, 7, 4, 1, 1) self.pushButton_o = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_o.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_o.setObjectName(_fromUtf8("pushButton_o")) self.gridLayout.addWidget(self.pushButton_o, 8, 0, 1, 1) self.pushButton_p = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_p.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_p.setObjectName(_fromUtf8("pushButton_p")) self.gridLayout.addWidget(self.pushButton_p, 8, 1, 1, 1) self.pushButton_r = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_r.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_r.setObjectName(_fromUtf8("pushButton_r")) self.gridLayout.addWidget(self.pushButton_r, 8, 2, 1, 1) self.pushButton_s = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_s.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_s.setObjectName(_fromUtf8("pushButton_s")) self.gridLayout.addWidget(self.pushButton_s, 8, 3, 1, 1) self.pushButton_t = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_t.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_t.setObjectName(_fromUtf8("pushButton_t")) self.gridLayout.addWidget(self.pushButton_t, 8, 4, 1, 1) self.pushButton_y = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_y.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_y.setObjectName(_fromUtf8("pushButton_y")) self.gridLayout.addWidget(self.pushButton_y, 9, 0, 1, 1) self.pushButton_ph = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_ph.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_ph.setObjectName(_fromUtf8("pushButton_ph")) self.gridLayout.addWidget(self.pushButton_ph, 9, 1, 1, 1) self.pushButton_x = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_x.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_x.setObjectName(_fromUtf8("pushButton_x")) self.gridLayout.addWidget(self.pushButton_x, 9, 2, 1, 1) self.pushButton_cs = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_cs.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_cs.setObjectName(_fromUtf8("pushButton_cs")) self.gridLayout.addWidget(self.pushButton_cs, 9, 3, 1, 1) self.pushButton_h = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_h.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_h.setObjectName(_fromUtf8("pushButton_h")) self.gridLayout.addWidget(self.pushButton_h, 9, 4, 1, 1) self.pushButton_hsh = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_hsh.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_hsh.setObjectName(_fromUtf8("pushButton_hsh")) self.gridLayout.addWidget(self.pushButton_hsh, 10, 0, 1, 1) self.pushButton_hc = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_hc.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_hc.setObjectName(_fromUtf8("pushButton_hc")) self.gridLayout.addWidget(self.pushButton_hc, 10, 1, 1, 1) self.pushButton_33 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_33.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_33.setObjectName(_fromUtf8("pushButton_33")) self.gridLayout.addWidget(self.pushButton_33, 10, 2, 1, 1) self.pushButton_34 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_34.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_34.setObjectName(_fromUtf8("pushButton_34")) self.gridLayout.addWidget(self.pushButton_34, 10, 3, 1, 1) self.pushButton_35 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_35.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_35.setObjectName(_fromUtf8("pushButton_35")) self.gridLayout.addWidget(self.pushButton_35, 10, 4, 1, 1) self.pushButton_36 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_36.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_36.setObjectName(_fromUtf8("pushButton_36")) self.gridLayout.addWidget(self.pushButton_36, 11, 0, 1, 1) self.pushButton_37 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_37.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_37.setObjectName(_fromUtf8("pushButton_37")) self.gridLayout.addWidget(self.pushButton_37, 11, 1, 1, 1) self.pushButton_38 = QtGui.QPushButton(self.gridLayoutWidget) self.pushButton_38.setMaximumSize(QtCore.QSize(20, 20)) self.pushButton_38.setObjectName(_fromUtf8("pushButton_38")) self.gridLayout.addWidget(self.pushButton_38, 11, 2, 1, 1) self.lcdNumber = QtGui.QLCDNumber(self.centralwidget) self.lcdNumber.setGeometry(QtCore.QRect(200, 460, 64, 23)) self.lcdNumber.setProperty("intValue", 0) self.lcdNumber.setObjectName(_fromUtf8("lcdNumber")) self.lcdNumber_2 = QtGui.QLCDNumber(self.centralwidget) self.lcdNumber_2.setGeometry(QtCore.QRect(200, 490, 64, 23)) self.lcdNumber_2.setProperty("intValue", 0) self.lcdNumber_2.setObjectName(_fromUtf8("lcdNumber_2")) self.start = QtGui.QPushButton(self.centralwidget) self.start.setGeometry(QtCore.QRect(600, 530, 75, 23)) self.start.setObjectName(_fromUtf8("start")) self.help = QtGui.QPushButton(self.centralwidget) self.help.setGeometry(QtCore.QRect(510, 530, 75, 23)) self.help.setObjectName(_fromUtf8("help")) self.clear = QtGui.QPushButton(self.centralwidget) self.clear.setGeometry(QtCore.QRect(190, 530, 75, 23)) self.clear.setObjectName(_fromUtf8("clear")) self.lineEdit = QtGui.QLineEdit(self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(380, 30, 21, 20)) self.lineEdit.setText(_fromUtf8("")) self.lineEdit.setObjectName(_fromUtf8("lineEdit")) self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget) self.lineEdit_2.setGeometry(QtCore.QRect(410, 30, 21, 20)) self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2")) self.lineEdit_3 = QtGui.QLineEdit(self.centralwidget) self.lineEdit_3.setGeometry(QtCore.QRect(440, 30, 21, 20)) self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3")) self.lineEdit_4 = QtGui.QLineEdit(self.centralwidget) self.lineEdit_4.setGeometry(QtCore.QRect(470, 30, 21, 20)) self.lineEdit_4.setObjectName(_fromUtf8("lineEdit_4")) self.lineEdit_5 = QtGui.QLineEdit(self.centralwidget) self.lineEdit_5.setGeometry(QtCore.QRect(500, 30, 21, 20)) self.lineEdit_5.setObjectName(_fromUtf8("lineEdit_5")) self.lineEdit_6 = QtGui.QLineEdit(self.centralwidget) self.lineEdit_6.setGeometry(QtCore.QRect(530, 30, 21, 20)) self.lineEdit_6.setObjectName(_fromUtf8("lineEdit_6")) self.lineEdit_7 = QtGui.QLineEdit(self.centralwidget) self.lineEdit_7.setGeometry(QtCore.QRect(560, 30, 21, 20)) self.lineEdit_7.setObjectName(_fromUtf8("lineEdit_7")) self.lineEdit_8 = QtGui.QLineEdit(self.centralwidget) self.lineEdit_8.setGeometry(QtCore.QRect(590, 30, 21, 20)) self.lineEdit_8.setObjectName(_fromUtf8("lineEdit_8")) self.label = QtGui.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(50, 460, 131, 21)) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(50, 490, 131, 20)) self.label_2.setObjectName(_fromUtf8("label_2")) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) self.menubar.setObjectName(_fromUtf8("menubar")) MainWindow.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) #print dir(self.graphicsView) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(_translate("MainWindow", "Виселица", None)) self.pushButton_b.setText(_translate("MainWindow", u"б", None)) self.pushButton_z.setText(_translate("MainWindow", u"з", None)) self.pushButton_i.setText(_translate("MainWindow", u"и", None)) self.pushButton_j.setText(_translate("MainWindow", u"ж", None)) self.pushButton_a.setText(_translate("MainWindow", u"а", None)) self.pushButton_d.setText(_translate("MainWindow", u"д", None)) self.pushButton_e.setText(_translate("MainWindow", u"е", None)) self.pushButton_g.setText(_translate("MainWindow", u"г", None)) self.pushButton_v.setToolTip(_translate("MainWindow", "<html><head/><body><p align=\"right\"><br/></p></body></html>", None)) self.pushButton_v.setText(_translate("MainWindow", u"в", None)) self.pushButton_e2.setText(_translate("MainWindow", u"ё", None)) self.pushButton_i2.setText(_translate("MainWindow", u"й", None)) self.pushButton_k.setText(_translate("MainWindow", u"к", None)) self.pushButton_l.setText(_translate("MainWindow", u"л", None)) self.pushButton_m.setText(_translate("MainWindow", u"м", None)) self.pushButton_n.setText(_translate("MainWindow", u"н", None)) self.pushButton_o.setText(_translate("MainWindow", u"о", None)) self.pushButton_p.setText(_translate("MainWindow", u"п", None)) self.pushButton_r.setText(_translate("MainWindow", u"р", None)) self.pushButton_s.setText(_translate("MainWindow", u"с", None)) self.pushButton_t.setText(_translate("MainWindow", u"т", None)) self.pushButton_y.setText(_translate("MainWindow", u"у", None)) self.pushButton_ph.setText(_translate("MainWindow", u"ф", None)) self.pushButton_x.setText(_translate("MainWindow", u"х", None)) self.pushButton_cs.setText(_translate("MainWindow", u"ц", None)) self.pushButton_h.setText(_translate("MainWindow", u"ч", None)) self.pushButton_hsh.setText(_translate("MainWindow", u"ш", None)) self.pushButton_hc.setText(_translate("MainWindow", u"щ", None)) self.pushButton_33.setText(_translate("MainWindow", u"ъ", None)) self.pushButton_34.setText(_translate("MainWindow", u"ы", None)) self.pushButton_35.setText(_translate("MainWindow", u"ь", None)) self.pushButton_36.setText(_translate("MainWindow", u"э", None)) self.pushButton_37.setText(_translate("MainWindow", u"ю", None)) self.pushButton_38.setText(_translate("MainWindow", u"я", None)) self.start.setText(_translate("MainWindow", "Старт", None)) self.help.setText(_translate("MainWindow", "Помощь", None)) self.clear.setText(_translate("MainWindow", "Очистить", None)) self.label.setText(_translate("MainWindow", "Побед", None)) self.label_2.setText(_translate("MainWindow", "Поражений", None))
Отредактировано Kasta_neda (Сен. 13, 2014 20:33:36)
Офлайн
Глава 25. Работа с графикой Прохорёнок.
но можно и с 4 главы.
там про циклы.
и
если цель - написать игру “виселица” и больше никогда не программировать то дизайнер форм подойдёт,
но если научиться - то лучше руками пока
внешне стало симпатичнее, но это большей частью заслуга Qt
Офлайн
Менял атрибуты класса робота, не получалось достичь желаемого (изменение поведения), пока пришлось использовать два одинаковых класса робота
робота взял из примеров
#!/usr/bin/python # -*- coding: utf-8 -*- # python2.7 #------------------------------------------------ import sys from PyQt4 import QtCore, QtGui, uic from output import * from PyQt4.QtCore import * from PyQt4.QtGui import * import random slova = [u'виселица',u'смартфон',u'маргарин',u'мегагерц',u'страница',u'креветка',u'микрофон',u'квартира',u'скорость',u'художник',u'жидкость',u'карантин',u'интернет',u'документ',u'мембрана',u'радиация',u'мавзолей',u'терминал',u'водитель'] class Proba(QMainWindow, Ui_MainWindow): def __init__(self): QMainWindow.__init__(self) self.setupUi(self) self.robot = Robot() self.robot.scale(1.2, 1.2) self.robot.setPos(0, 0) self.robotK = RobotK() self.robotK.scale(1.2, 1.2) self.robotK.setPos(0, -70) self.scene = QtGui.QGraphicsScene() self.scene_view = QtGui.QGraphicsView(self.scene, self) self.scene_view.resize(300, 350) self.scene_view.move(40, 50) self.item = QtGui.QGraphicsEllipseItem(-18, -120, 40, 50) self.line = QtGui.QGraphicsLineItem(-150, -200, 100, -200) self.line2 = QtGui.QGraphicsLineItem(-150, -200, -150, 100) self.line3 = QtGui.QGraphicsLineItem(-70, -200, -150, -100) self.line4 = QtGui.QGraphicsLineItem(0, -120, 0, -200) self.line5 = QtGui.QGraphicsLineItem(0, -200, 0, -100) self.win = 0 self.defeat = 0 self.win2 = 0 self.defeat2 = 0 self.wo = None self.wor = [] self.clear.clicked.connect(self.clear_Click) # Обработка нажатия на кнопку очистить self.help.clicked.connect(self.help_Click) # Обработка нажатия на кнопку помощь self.start.clicked.connect(self.start_Click) # Обработка нажатия на кнопку старт self.connect(self.pushButton_b, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_z, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_i, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_j, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_a, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_d, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_e, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_g, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_v, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_e2, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_i2, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_k, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_l, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_m, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_n, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_o, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_p, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_r, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_s, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_t, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_y, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_ph, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_x, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_cs, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_h, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_hsh, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_hc, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_33, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_34, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_35, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_36, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_37, QtCore.SIGNAL("clicked()"), self.on_button_clicked) self.connect(self.pushButton_38, QtCore.SIGNAL("clicked()"), self.on_button_clicked) def on_button_clicked(self): tokens = (self.lineEdit_2, self.lineEdit_3, self.lineEdit_4, self.lineEdit_5, self.lineEdit_6, self.lineEdit_7) button = self.sender() u = button.text() print self.wo if unicode(u) in self.wor: key_ = self.wor.index(unicode(u)) self.wor[key_] = '1' out = tokens[int(key_)] out.setProperty('text', unicode(u)) self.win = self.win + 1 if self.win == 6: try: self.scene.removeItem(self.line) except: pass try: self.scene.removeItem(self.line2) except: pass try: self.scene.removeItem(self.line3) except: pass try: self.scene.removeItem(self.line4) except: pass try: self.scene.removeItem(self.line5) except: pass try: self.scene.removeItem(self.item) except: pass else: self.defeat = self.defeat + 1 print self.defeat men = (self.line,self.line2,self.line3,self.line4) try: sj = men[self.defeat-1] self.scene.addItem(sj) except: pass if self.defeat == 5: self.scene.addItem(self.item) if self.defeat == 6: self.scene.removeItem(self.robot) #self.scene.clear() self.scene.removeItem(self.item) self.scene.addItem(self.line5) self.scene.addItem(self.robotK) if unicode(u) in self.wor: try: key_2 = self.wor.index(unicode(u)) self.wor[key_2] = '1' print key_2 out = tokens[int(key_2)] out.setProperty('text', unicode(u)) self.win = self.win + 1 except: pass if unicode(u) in self.wor: try: key_3 = self.wor.index(unicode(u)) self.wor[key_3] = '1' print key_3 out = tokens[int(key_3)] out.setProperty('text', unicode(u)) self.win = self.win + 1 except: pass if self.win == 6: self.win2 = self.win2 + 1 self.lcdNumber.setProperty("intValue", self.win2) if self.defeat == 6: self.defeat2 = self.defeat2 + 1 self.lcdNumber_2.setProperty("intValue", self.defeat2) return self.defeat def clear_Click (self): self.lcdNumber.setProperty("intValue", 0) self.lcdNumber_2.setProperty("intValue", 0) print u"Очистить" def help_Click (self): print u"Помощь" msgBox = QtGui.QMessageBox() msgBox.setText(u''' Принцип игры Андроид загадывает слово — пишет на бумаге первую и последнюю букву слова и отмечает места для остальных букв, например чертами . Игрок предлагает букву, которая может входить в это слово.Если такая буква есть в слове, то Андроид пишет её над соответствующими этой букве чертами — столько раз, сколько она встречается в слове.Если такой буквы нет, то рисуется часть виселицы. Игрок продолжает отгадывать буквы до тех пор,пока не отгадает всё слово. За каждый неправильный ответ Андроид добавляет одну часть к виселице(их 5, и шестая ошибка фатальная). Если виселица нарисована полностью, то отгадывающий игрок проигрывает, считается повешенным. Если игроку удаётся угадать слово, он выигрывает. ''') ret = msgBox.exec_(); def start_Click (self): self.scene.removeItem(self.robotK) self.scene.removeItem(self.line) self.scene.removeItem(self.line2) self.scene.removeItem(self.line3) self.scene.removeItem(self.line4) self.scene.removeItem(self.line5) self.scene.addItem(self.robot) self.wo = None self.wor = [] self.win = 0 self.defeat = 0 self.lineEdit_2.setProperty('text', '') self.lineEdit_3.setProperty('text', '') self.lineEdit_4.setProperty('text', '') self.lineEdit_5.setProperty('text', '') self.lineEdit_6.setProperty('text', '') self.lineEdit_7.setProperty('text', '') self.word = random.choice(slova) # выбираем рандомно слово self.wo = self.word[1:-1] # отрезаем от слова первую и последнюю букву for i in self.wo: self.wor.append(i) f1 = self.word[0] f2 = self.word[-1] self.lineEdit.setProperty('text', f1) self.lineEdit_8.setProperty('text', f2) ############################################################# # первый робот class RobotPart(QtGui.QGraphicsItem): def __init__(self, parent=None): super(RobotPart, self).__init__(parent) self.color = QtGui.QColor(QtCore.Qt.lightGray) self.pixmap = None self.dragOver = False self.setAcceptDrops(True) def dragEnterEvent(self, event): if event.mimeData().hasColor() or \ (isinstance(self, RobotHead) and event.mimeData().hasImage()): event.setAccepted(True) self.dragOver = True self.update() else: event.setAccepted(False) def dragLeaveEvent(self, event): self.dragOver = False self.update() def dropEvent(self, event): self.dragOver = False if event.mimeData().hasColor(): self.color = QtGui.QColor(event.mimeData().colorData()) elif event.mimeData().hasImage(): self.pixmap = QtGui.QPixmap(event.mimeData().imageData()) self.update() class RobotHead(RobotPart): def boundingRect(self): return QtCore.QRectF(-15, -50, 30, 50) def paint(self, painter, option, widget=None): if not self.pixmap: painter.setBrush(self.dragOver and self.color.light(130) or self.color) painter.drawRoundedRect(-10, -30, 20, 30, 25, 25, QtCore.Qt.RelativeSize) painter.setBrush(QtCore.Qt.white) painter.drawEllipse(-7, -3 - 20, 7, 7) painter.drawEllipse(0, -3 - 20, 7, 7) painter.setBrush(QtCore.Qt.black) painter.drawEllipse(-5, -1 - 20, 2, 2) painter.drawEllipse(2, -1 - 20, 2, 2) painter.setPen(QtGui.QPen(QtCore.Qt.black, 2)) painter.setBrush(QtCore.Qt.NoBrush) painter.drawArc(-6, -2 - 20, 12, 15, 190 * 16, 160 * 16) else: painter.scale(.2272, .2824) painter.drawPixmap(QtCore.QPointF(-15*4.4, -50*3.54), self.pixmap) class RobotTorso(RobotPart): def boundingRect(self): return QtCore.QRectF(-30, -20, 60, 60) def paint(self, painter, option, widget=None): painter.setBrush(self.dragOver and self.color.light(130) or self.color) painter.drawRoundedRect(-20, -20, 40, 60, 25, 25, QtCore.Qt.RelativeSize) painter.drawEllipse(-25, -20, 20, 20) painter.drawEllipse(5, -20, 20, 20) painter.drawEllipse(-20, 22, 20, 20) painter.drawEllipse(0, 22, 20, 20) class RobotLimb(RobotPart): def boundingRect(self): return QtCore.QRectF(-5, -5, 40, 10) def paint(self, painter, option, widget=None): painter.setBrush(self.dragOver and self.color.light(130) or self.color) painter.drawRoundedRect(self.boundingRect(), 50, 50, QtCore.Qt.RelativeSize) painter.drawEllipse(-5, -5, 10, 10) class Robot(RobotPart): def __init__(self): super(Robot, self).__init__() self.torsoItem = RobotTorso(self) self.headItem = RobotHead(self.torsoItem) self.upperLeftArmItem = RobotLimb(self.torsoItem) self.lowerLeftArmItem = RobotLimb(self.upperLeftArmItem) self.upperRightArmItem = RobotLimb(self.torsoItem) self.lowerRightArmItem = RobotLimb(self.upperRightArmItem) self.upperRightLegItem = RobotLimb(self.torsoItem) self.lowerRightLegItem = RobotLimb(self.upperRightLegItem) self.upperLeftLegItem = RobotLimb(self.torsoItem) self.lowerLeftLegItem = RobotLimb(self.upperLeftLegItem) self.timeline = QtCore.QTimeLine() self.settings = [ # item position rotation at # x y time 0 / 1 ( self.headItem, 0, -18, 20, -20 ), ( self.upperLeftArmItem, -15, -10, 190, 180 ), ( self.lowerLeftArmItem, 30, 0, 50, 10 ), ( self.upperRightArmItem, 15, -10, 300, 310 ), ( self.lowerRightArmItem, 30, 0, 0, -70 ), ( self.upperRightLegItem, 10, 32, 40, 120 ), ( self.lowerRightLegItem, 30, 0, 10, 50 ), ( self.upperLeftLegItem, -10, 32, 150, 80 ), ( self.lowerLeftLegItem, 30, 0, 70, 10 ), ( self.torsoItem, 0, 0, 5, -20 ) ] self.animations = [] for item, pos_x, pos_y, rotation1, rotation2 in self.settings: item.setPos(pos_x,pos_y) animation = QtGui.QGraphicsItemAnimation() animation.setItem(item) animation.setTimeLine(self.timeline) animation.setRotationAt(0, rotation1) animation.setRotationAt(1, rotation2) self.animations.append(animation) self.animations[0].setScaleAt(1, 1.1, 1.1) self.timeline.setUpdateInterval(1000 / 25) self.timeline.setCurveShape(QtCore.QTimeLine.SineCurve) self.timeline.setLoopCount(0) self.timeline.setDuration(2000) self.timeline.start() def boundingRect(self): return QtCore.QRectF() def paint(self, painter, option, widget=None): pass ############################################################# #второй робот class RobotPart2(QtGui.QGraphicsItem): def __init__(self, parent=None): super(RobotPart2, self).__init__(parent) self.color = QtGui.QColor(QtCore.Qt.lightGray) self.pixmap = None self.dragOver = False self.setAcceptDrops(True) def dragEnterEvent2(self, event): if event.mimeData().hasColor() or \ (isinstance(self, RobotHead) and event.mimeData().hasImage()): event.setAccepted(True) self.dragOver = True self.update() else: event.setAccepted(False) def dragLeaveEvent2(self, event): self.dragOver = False self.update() def dropEvent2(self, event): self.dragOver = False if event.mimeData().hasColor(): self.color = QtGui.QColor(event.mimeData().colorData()) elif event.mimeData().hasImage(): self.pixmap = QtGui.QPixmap(event.mimeData().imageData()) self.update() class RobotHead2(RobotPart2): def boundingRect(self): return QtCore.QRectF(-15, -50, 30, 50) def paint(self, painter, option, widget=None): if not self.pixmap: painter.setBrush(self.dragOver and self.color.light(130) or self.color) painter.drawRoundedRect(-10, -30, 20, 30, 25, 25, QtCore.Qt.RelativeSize) painter.setBrush(QtCore.Qt.white) painter.drawEllipse(-7, -3 - 20, 7, 7) painter.drawEllipse(0, -3 - 20, 7, 7) painter.setBrush(QtCore.Qt.black) painter.drawEllipse(-5, -1 - 20, 2, 2) painter.drawEllipse(2, -1 - 20, 2, 2) painter.setPen(QtGui.QPen(QtCore.Qt.black, 2)) painter.setBrush(QtCore.Qt.NoBrush) painter.drawArc(-6, -2 - 10, 12, 15, 190 / 16, 160 * 16) else: painter.scale(.2272, .2824) painter.drawPixmap(QtCore.QPointF(-15*4.4, -50*3.54), self.pixmap) class RobotTorso2(RobotPart2): def boundingRect(self): return QtCore.QRectF(-30, -20, 60, 60) def paint(self, painter, option, widget=None): painter.setBrush(self.dragOver and self.color.light(130) or self.color) painter.drawRoundedRect(-20, -20, 40, 60, 25, 25, QtCore.Qt.RelativeSize) painter.drawEllipse(-25, -20, 20, 20) painter.drawEllipse(5, -20, 20, 20) painter.drawEllipse(-20, 22, 20, 20) painter.drawEllipse(0, 22, 20, 20) class RobotLimb2(RobotPart2): def boundingRect(self): return QtCore.QRectF(-5, -5, 40, 10) def paint(self, painter, option, widget=None): painter.setBrush(self.dragOver and self.color.light(130) or self.color) painter.drawRoundedRect(self.boundingRect(), 50, 50, QtCore.Qt.RelativeSize) painter.drawEllipse(-5, -5, 10, 10) class RobotK(RobotPart2): def __init__(self): super(RobotK, self).__init__() self.torsoItem = RobotTorso2(self) self.headItem = RobotHead2(self.torsoItem) self.upperLeftArmItem = RobotLimb2(self.torsoItem) self.lowerLeftArmItem = RobotLimb2(self.upperLeftArmItem) self.upperRightArmItem = RobotLimb2(self.torsoItem) self.lowerRightArmItem = RobotLimb2(self.upperRightArmItem) self.upperRightLegItem = RobotLimb2(self.torsoItem) self.lowerRightLegItem = RobotLimb2(self.upperRightLegItem) self.upperLeftLegItem = RobotLimb2(self.torsoItem) self.lowerLeftLegItem = RobotLimb2(self.upperLeftLegItem) self.timeline = QtCore.QTimeLine() settings = [ # item position rotation at # x y time 0 / 1 ( self.headItem, 0, -18, 20, -20 ), ( self.upperLeftArmItem, -15, -10, 190, 80 ), ( self.lowerLeftArmItem, 30, 0, -50, 10 ), ( self.upperRightArmItem, 15, -10, 500, 410 ), ( self.lowerRightArmItem, 30, 0, -10, 70 ), ( self.upperRightLegItem, 10, 32, 40, 120 ), ( self.lowerRightLegItem, 30, 0, 10, 50 ), ( self.upperLeftLegItem, -10, 32, 100, 80 ), ( self.lowerLeftLegItem, 30, 0, 70, 10 ), ( self.torsoItem, 0, 0, 5, -20 ) ] self.animations = [] for item, pos_x, pos_y, rotation1, rotation2 in settings: item.setPos(pos_x,pos_y) animation = QtGui.QGraphicsItemAnimation() animation.setItem(item) animation.setTimeLine(self.timeline) animation.setRotationAt(0, rotation1) animation.setRotationAt(1, rotation2) self.animations.append(animation) self.animations[0].setScaleAt(1, 1.1, 1.1) self.timeline.setUpdateInterval(1000 / 25) self.timeline.setCurveShape(QtCore.QTimeLine.SineCurve) self.timeline.setLoopCount(0) self.timeline.setDuration(2000) self.timeline.start() def boundingRect(self): return QtCore.QRectF() def paint(self, painter, option, widget=None): pass app = QtGui.QApplication(sys.argv) w = Proba() w.show() sys.exit(app.exec_())
Отредактировано Kasta_neda (Сен. 13, 2014 22:32:25)
Офлайн