Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Learning Python
#1
So I am trying to learn/teach myself python and I decided to start with Rock, Paper, Scissors game. I was able to create the game using python but now I am trying to create a GUI for it and so I am working on converting it over using PyQt but I am running into an error in the game_play function. I know it has to do with the tg, cw, and pw variables but I am not sure how to define them differently or why it is failing. Some help would be appreciated.

from random import randint
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 665)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        spacerItem = QtWidgets.QSpacerItem(300, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem)
        self.pushLoadUser = QtWidgets.QPushButton(self.centralwidget)
        self.pushLoadUser.setObjectName("pushLoadUser")
        self.horizontalLayout_2.addWidget(self.pushLoadUser)
        self.currentUser = QtWidgets.QLineEdit(self.centralwidget)
        self.currentUser.setObjectName("currentUser")
        self.horizontalLayout_2.addWidget(self.currentUser)
        self.gridLayout_3.addLayout(self.horizontalLayout_2, 0, 0, 1, 2)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.playerImage = QtWidgets.QFrame(self.centralwidget)
        self.playerImage.setMinimumSize(QtCore.QSize(387, 145))
        self.playerImage.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.playerImage.setFrameShadow(QtWidgets.QFrame.Raised)
        self.playerImage.setObjectName("playerImage")
        self.gridLayout.addWidget(self.playerImage, 0, 0, 1, 1)
        self.computerImage = QtWidgets.QFrame(self.centralwidget)
        self.computerImage.setMinimumSize(QtCore.QSize(387, 145))
        self.computerImage.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.computerImage.setFrameShadow(QtWidgets.QFrame.Raised)
        self.computerImage.setObjectName("computerImage")
        self.gridLayout.addWidget(self.computerImage, 0, 1, 1, 1)
        self.gridLayout_3.addLayout(self.gridLayout, 3, 0, 1, 2)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushRock = QtWidgets.QPushButton(self.centralwidget)
        self.pushRock.setObjectName("pushRock")
        self.horizontalLayout.addWidget(self.pushRock)
        self.pushPaper = QtWidgets.QPushButton(self.centralwidget)
        self.pushPaper.setObjectName("pushPaper")
        self.horizontalLayout.addWidget(self.pushPaper)
        self.pushScissors = QtWidgets.QPushButton(self.centralwidget)
        self.pushScissors.setObjectName("pushScissors")
        self.horizontalLayout.addWidget(self.pushScissors)
        self.gridLayout_3.addLayout(self.horizontalLayout, 2, 0, 1, 2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.status = QtWidgets.QLabel(self.centralwidget)
        self.status.setObjectName("status")
        self.horizontalLayout_3.addWidget(self.status)
        self.gridLayout_2 = QtWidgets.QGridLayout()
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.line_3 = QtWidgets.QFrame(self.centralwidget)
        self.line_3.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_3.setObjectName("line_3")
        self.gridLayout_2.addWidget(self.line_3, 7, 0, 1, 1)
        self.computerWins = QtWidgets.QLabel(self.centralwidget)
        self.computerWins.setObjectName("computerWins")
        self.gridLayout_2.addWidget(self.computerWins, 4, 0, 1, 1)
        self.playerWins = QtWidgets.QLabel(self.centralwidget)
        self.playerWins.setObjectName("playerWins")
        self.gridLayout_2.addWidget(self.playerWins, 2, 0, 1, 1)
        self.ties = QtWidgets.QLabel(self.centralwidget)
        self.ties.setObjectName("ties")
        self.gridLayout_2.addWidget(self.ties, 6, 0, 1, 1)
        self.line_2 = QtWidgets.QFrame(self.centralwidget)
        self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.gridLayout_2.addWidget(self.line_2, 5, 0, 1, 1)
        self.line_4 = QtWidgets.QFrame(self.centralwidget)
        self.line_4.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_4.setObjectName("line_4")
        self.gridLayout_2.addWidget(self.line_4, 1, 0, 1, 1)
        self.line = QtWidgets.QFrame(self.centralwidget)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.gridLayout_2.addWidget(self.line, 3, 0, 1, 1)
        self.horizontalLayout_3.addLayout(self.gridLayout_2)
        self.gridLayout_3.addLayout(self.horizontalLayout_3, 4, 0, 1, 2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushLoadUser.setText(_translate("MainWindow", "Load"))
        self.currentUser.setText(_translate("MainWindow", "Username"))
        

        self.pushRock.setText(_translate("MainWindow", "Rock"))
        self.pushRock.clicked.connect(self.rock_click)
        self.pushRock.clicked.connect(self.game_play)
        
        self.pushPaper.setText(_translate("MainWindow", "Paper"))
        player = self.pushPaper.clicked.connect(self.paper_click)
        self.pushPaper.clicked.connect(self.game_play)
        
        
        self.pushScissors.setText(_translate("MainWindow", "Scissors"))
        player = self.pushScissors.clicked.connect(self.scissors_click)
        self.pushScissors.clicked.connect(self.game_play)
        
        
        self.status.setText(_translate("MainWindow", "Status"))
        self.computerWins.setText(_translate("MainWindow", "Computer Wins"))
        self.playerWins.setText(_translate("MainWindow", "Player Wins"))
        self.ties.setText(_translate("MainWindow", "Ties"))

    def rock_click(self):
        print("RockOn")
        self.player = "Rock"
        self.value = 0
        #return self.player
        
    def paper_click(self):
        print("Paper")
        self.player = "Paper"
        self.value = 1
        #return self.player
        
    def scissors_click(self):
        print("Scissors")
        self.player = "Scissors"
        self.value = 2
        #return self.player
    
    def game_play(self):
        print (self.player)
        if self.player == "Rock":
            print ("Tie!")
            print (self.tg)
            self.tg = self.tg + 1
        elif self.player == "Rock":
            if self.computer == "Paper":
                print("You Lose!", computer, "covers", self.player)
                self.cw = self.cw + 1
            else:
                print("You Win!", self.player, "smashes", computer)
                self.pw = self.pw + 1
        elif self.player == "Paper":
            if computer == "Scissors":
                print("You Lose!", computer, "cut", self.player)
                self.cw = self.cw + 1
            else:
                print("You Win!", self.player, "covers", computer)
                self.pw = self.pw + 1
        elif self.player == "Scissors":
            if computer == "Rock":
                print("You Lose!", computer, "smashes", self.player)
                self.cw = self.cw + 1
            else:
                print("You Win!", self.player, "cut", computer)
                self.pw = self.pw + 1
        else:
            print("That's not a valid play. Check your spelling!")
        
def create_table(db_name,table_name,sql):
    with sqlite3.connect(db_name) as db:
        cursor = db.cursor()
        cursor.execute("select name from sqlite_master where name=?",(table_name,))
        result = cursor.fetchall()
        keep_table = True
        if len(result) == 1:
            response = input("The table {0} already exists, do you wish to recreate it (y/n): ".format(table_name))
            if response == "y":
                keep_table = False
                print(" The {0} table will be recreated - all existing data will be lost".format(table_name))
                cursor.execute("drop table if exists {0}".format(table_name))
                db.commit
            else:
                print("The existing table was kept")
        else:
            keep_table = False
        if not keep_table:
            cursor.execute(sql)
            db.commit()
            
def insert_data(values):
    with sqlite3.connect("RPS.db") as db:
        cursor = db.cursor()
        sql = "insert into " + user + " (RPS, Value) values (?,?)"
        cursor.execute(sql,values)
        db.commit()
        
def select_counter(id):
    with sqlite3.connect("RPS.db") as db:
        cursor = db.cursor()
        cursor.execute("select Value from " + user + " where Counter=?",(id,))
        counter = cursor.fetchone()
        return counter


        
user = input("Input Username: ")
db_name = "RPS.db"
sql = """CREATE TABLE {}(
        Counter integer,
        RPS text,
        Value integer,
        primary key (Counter))""".format(user)
create_table(db_name, user, sql)


# Create a list of play options
t = ["Rock", "Paper", "Scissors"]
x = 1

nr = 0
np = 0
ns = 0

pw = 0
cw = 0
tg = 0

window = [0, 0, 0, 0]

# Assign a random play to the computer
computer = t[randint(0,2)]

# Set player to False
player = False

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I notice that if I comment out these lines

self.tg = self.tg + 1
self.cw = self.cw + 1
self.pw = self.pw + 1
it seems to work or if I change the lines to

self.tg = tg + 1
self.cw = cw + 1
self.pw = pw + 1
but then this does add the value to itself. I am trying to determine the number of tie games, computer wins, and player wins.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)