编写一个简单的python游戏

今天,我们将创建一个著名的石头,剪刀,纸游戏。 python python库和tkinter库将为我们提供帮助,但是如果您不知道它是什么,我建议您阅读本文。




我们需要的第一件事是一个初始结构,一个窗口,对我来说,它将像这样:


from tkinter import * import random as rdm class Main(Frame): def __init__(self, root): super(Main, self).__init__(root) self.startUI() def startUI(self): pass if __name__ == '__main__': root = Tk() root.geometry("500x500+200+200") root.title(", , ") root.resizable(False, False) root["bg"] = "#FFF" app = Main(root) app.pack() root.mainloop() 

在这里,我们创建了一个500 x 500不变的窗口,标题为“石头,剪刀,纸”和白色背景。 我们将在此窗口中添加按钮,计数器等。



现在在我们的startUI方法中添加以下行:


 btn = Button(root, text="", font=("Times New Roman", 15)) btn = Button(root, text="", font=("Times New Roman", 15)) btn3 = Button(root, text="", font=("Times New Roman", 15)) btn.place(x=10, y=100, width=120, height=50) btn2.place(x=155, y=100, width=120, height=50) btn3.place(x=300, y=100, width=120, height=50) 

这7行将在我们的窗口中添加3个不起作用的按钮。 我们稍后将解决此问题。



用户通过单击三个按钮之一来做出选择,这很酷,但是我们需要一个对手,这就是random模块的用途。



现在,我们将添加一个函数来处理选择,并给出在本轮中获胜者的答案。 让我们这样做:


 btn = Button(root, text="", font=("Times New Roman", 15), command=lambda x=1: self.btn_click(x)) btn2 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=2: self.btn_click(x)) btn3 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=3: self.btn_click(x)) 

这是怎么回事


一切都非常简单。 粗略地说,如果玩家按一块石头,则走1,如果有剪刀,则走2,如果是纸,则走3,不仅会走,而且还会显示在控制台中。
以计算机为代价。 他做出了选择,但是他的选择没有任何进展。



在执行逻辑之前,我们需要将结果传递给播放器,为此,我们将使用Label将以下行添加到startUI


 self.lbl = Label(root, text=" !", bg="#FFF", font=("Times New Roman", 21, "bold")) self.lbl.place(x=120, y=25) self.lbl2 = Label(root, justify="left", font=("Times New Roman", 13), text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}", bg="#FFF") self.lbl2.place(x=5, y=5) 

太好了 现在,我们有一个题字,其中将输出回合结果和带有统计信息的题字。



让我们做3个柜台:


1.损失
2.胜利
3.什么都没有



为此, 将以下行添加到同一startUI



 self.win = self.drow = self.lose = 0 

现在,在类中,创建btn_click方法,并将以下行写入其中:



 def btn_click(self, choise): comp_choise = rdm.randint(1, 3) print(choise) 

音乐没有播放很长时间。 在同一位置btn_click中 ,删除

打印(选择)
并写这些行:



 if choise == comp_choise: self.drow += 1 self.lbl.configure(text="") elif choise == 1 and comp_choise == 2 \ or choise == 2 and comp_choise == 3 \ or choise == 3 and comp_choise == 1: self.win += 1 self.lbl.configure(text="") else: self.lose += 1 self.lbl.configure(text="") self.lbl2.configure(text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}") del comp_choise 

实际上,一切都到此为止。 一切正常,您可以玩。



完整代码:


 from tkinter import * import random as rdm class Main(Frame): def __init__(self, root): super(Main, self).__init__(root) self.startUI() def startUI(self): btn = Button(root, text="", font=("Times New Roman", 15), command=lambda x=1: self.btn_click(x)) btn2 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=2: self.btn_click(x)) btn3 = Button(root, text="", font=("Times New Roman", 15), command=lambda x=3: self.btn_click(x)) btn.place(x=10, y=100, width=120, height=50) btn2.place(x=155, y=100, width=120, height=50) btn3.place(x=300, y=100, width=120, height=50) self.lbl = Label(root, text=" !", bg="#FFF", font=("Times New Roman", 21, "bold")) self.lbl.place(x=150, y=25) self.win = self.drow = self.lose = 0 self.lbl2 = Label(root, justify="left", font=("Times New Roman", 13), text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}", bg="#FFF") self.lbl2.place(x=5, y=5) def btn_click(self, choise): comp_choise = rdm.randint(1, 3) if choise == comp_choise: self.drow += 1 self.lbl.configure(text="") elif choise == 1 and comp_choise == 2 \ or choise == 2 and comp_choise == 3 \ or choise == 3 and comp_choise == 1: self.win += 1 self.lbl.configure(text="") else: self.lose += 1 self.lbl.configure(text="") self.lbl2.configure(text=f": {self.win}\n:" f" {self.lose}\n: {self.drow}") del comp_choise if __name__ == '__main__': root = Tk() root.geometry("430x160+200+200") root.title(", , ") root.resizable(False, False) root["bg"] = "#FFF" app = Main(root) app.pack() root.mainloop() 

Source: https://habr.com/ru/post/zh-CN480906/


All Articles