我的第一个Python小程序
程序员文章站
2024-03-23 19:29:40
...
大家好,我一个月前刚开始学Python,前几天刚完成一个小游戏,这游戏使用了4个模块。
1- PyQt5 (需要下载)
2- random (Python自带)
3- sys (Python 自带)
4- texttable (需要下载)
首先,玩家一开始时会有10积分,他们可以用这个10积分来玩这个游戏。
玩法:
1.玩家输入要赌的积分,然后选择‘0’或 ‘1’
2.如果赢了,玩家会得到他赌的 积分 x 2
3.如果输了,玩家会输掉他赌的积分
4.玩的时候,系统会生成3个txt文件 , 一个是计入赢的时候赢了多少积分(从大到小,前100个),还有一个就是输的时候输了多少积分。最后一个就是他赢的次数,和输的次数 加上当局的积分。
#上面的两个空白格本来是想放图片的,可是不知道为什么,没办法把qrc文件改成py文件,如果大神知道怎么改请教教我。
#代码有哪里写的不够好的或可以改善的也请大家多多指教。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'exedxe.ui'
#
# Created by: PyQt5 UI code generator 5.10
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import random
#win
cat = []
#lose
dog = []
pointz = [10]
#wincount
WC = [0]
#losecount
LC = [0]
class Ui_Dialog(object):
#UI in Console
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(597, 461)
self.AmountNum = QtWidgets.QLineEdit(Dialog)
self.AmountNum.setGeometry(QtCore.QRect(90, 240, 81, 21))
self.AmountNum.setObjectName("AmountNum")
self.Points = QtWidgets.QLabel(Dialog)
self.Points.setGeometry(QtCore.QRect(0, 120, 47, 13))
self.Points.setObjectName("Points")
self.Amount = QtWidgets.QLabel(Dialog)
self.Amount.setGeometry(QtCore.QRect(0, 240, 81, 16))
self.Amount.setObjectName("Amount")
self.PB0 = QtWidgets.QPushButton(Dialog)
self.PB0.setGeometry(QtCore.QRect(0, 280, 75, 23))
self.PB0.setObjectName("PB0")
self.PB1 = QtWidgets.QPushButton(Dialog)
self.PB1.setGeometry(QtCore.QRect(70, 280, 75, 23))
self.PB1.setObjectName("PB1")
self.Exit = QtWidgets.QPushButton(Dialog)
self.Exit.setGeometry(QtCore.QRect(510, 430, 75, 23))
self.Exit.setObjectName("Exit")
self.PointsNum = QtWidgets.QLabel(Dialog)
self.PointsNum.setGeometry(QtCore.QRect(100, 110, 61, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.PointsNum.setFont(font)
self.PointsNum.setObjectName("PointsNum")
self.pic1 = QtWidgets.QTextBrowser(Dialog)
self.pic1.setGeometry(QtCore.QRect(0, 0, 111, 101))
self.pic1.setObjectName("pic1")
self.pic2 = QtWidgets.QTextBrowser(Dialog)
self.pic2.setGeometry(QtCore.QRect(110, 0, 111, 101))
self.pic2.setObjectName("pic2")
self.WinCount = QtWidgets.QLabel(Dialog)
self.WinCount.setGeometry(QtCore.QRect(0, 160, 61, 16))
self.WinCount.setObjectName("WinCount")
self.WinCountNum = QtWidgets.QLabel(Dialog)
self.WinCountNum.setGeometry(QtCore.QRect(100, 160, 71, 16))
font = QtGui.QFont()
font.setPointSize(14)
self.WinCountNum.setFont(font)
self.WinCountNum.setObjectName("WinCountNum")
self.LoseCount = QtWidgets.QLabel(Dialog)
self.LoseCount.setGeometry(QtCore.QRect(0, 200, 61, 16))
self.LoseCount.setObjectName("LoseCount")
self.LoseCountNum = QtWidgets.QLabel(Dialog)
self.LoseCountNum.setGeometry(QtCore.QRect(100, 200, 47, 16))
font = QtGui.QFont()
font.setPointSize(14)
self.LoseCountNum.setFont(font)
self.LoseCountNum.setObjectName("LoseCountNum")
self.Outcome = QtWidgets.QLabel(Dialog)
self.Outcome.setGeometry(QtCore.QRect(20, 320, 391, 91))
font = QtGui.QFont()
font.setPointSize(14)
self.Outcome.setFont(font)
self.Outcome.setText("")
self.Outcome.setObjectName("Outcome")
Dialog.setWindowTitle("Dialog")
self.Points.setText("Points:")
self.Amount.setText("Amount to Bet:")
self.PB0.setText("0")
self.PB1.setText("1")
self.Exit.setText("Exit")
self.PointsNum.setText("10")
self.WinCount.setText("Win Count:")
self.WinCountNum.setText("0")
self.LoseCount.setText("Lose Count:")
self.LoseCountNum.setText("0")
#Buttons
self.Exit.clicked.connect(Dialog.close)
self.PB0.clicked.connect(self.btn_clk)
self.PB1.clicked.connect(self.btn_clk)
QtCore.QMetaObject.connectSlotsByName(Dialog)
#Button's Function
def btn_clk(self):
win_count = WC[0]
lose_count = LC[0]
points = pointz[0]
#generating a number , using it as a percentage
chance = random.randint(1, 100)
x = int(self.AmountNum.text())
if x > points or x == 0:
self.Outcome.setText("Please Enter A Valid Value")
elif points == 0:
self.Outcome.setText("You Have Ran Out of Points")
else:
if x == points or x == points - 5:
points += x
win_count += 1
cat.append(x)
pointz.append(points)
WC.append(win_count)
del pointz[0]
del WC[0]
self.PointsNum.setNum(points)
self.WinCountNum.setNum(win_count)
self.LoseCountNum.setNum(lose_count)
self.Outcome.setText("Congratulations , You Won!")
elif x >= 10000:
points -= x
lose_count += 1
dog.append(x)
pointz.append(points)
LC.append(win_count)
del pointz[0]
del LC[0]
self.PointsNum.setNum(points)
self.WinCountNum.setNum(win_count)
self.LoseCountNum.setNum(lose_count)
self.Outcome.setText("Sorry, You Lost")
else:
if chance <= 75:
points += x
win_count += 1
cat.append(x)
pointz.append(points)
WC.append(win_count)
del pointz[0]
del WC[0]
self.PointsNum.setNum(points)
self.WinCountNum.setNum(win_count)
self.LoseCountNum.setNum(lose_count)
self.Outcome.setText("Congratulations , You Won!")
elif chance > 75:
points -= x
lose_count += 1
dog.append(x)
pointz.append(points)
LC.append(win_count)
del pointz[0]
del LC[0]
self.PointsNum.setNum(points)
self.WinCountNum.setNum(win_count)
self.LoseCountNum.setNum(lose_count)
self.Outcome.setText("Congratulations , You Won!")
cat.sort(reverse=True)
dog.sort(reverse=True)
#Making a texttable for win/lose
import texttable as tt
tab = tt.Texttable()
headings = ['cat']
tab.header(headings)
for row in zip(cat[:100]):
tab.add_row(row)
s = tab.draw()
with open('7777.txt', 'a') as f:
print(s, file=f)
tab1 = tt.Texttable()
headings = ['dog']
tab1.header(headings)
for row in zip(dog[:100]):
tab1.add_row(row)
s1 = tab1.draw()
with open('7778.txt', 'a') as f:
print(s1, file=f)
#7779 includes wincount,losecount and points.
with open('7779.txt', 'a') as f:
print("win count: ", win_count, "lose count: ", lose_count, "Points: ", points, file=f)
#Letting the script function
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
看了这个代码后,你们应该知道怎么拿到很多很多积分了吧。嘻嘻嘻
#还有一个就是我用了cx_freeze打包后,启动exe时,cmd会跑出来。。。。怎么拿掉。。。帮帮忙