石头剪刀布python代码_Python[Tkinter]石头布剪刀GUI

2024-02-02 14:50

本文主要是介绍石头剪刀布python代码_Python[Tkinter]石头布剪刀GUI,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我目前正在开发Python[Tkinter]石头布剪刀GUI应用程序。到目前为止,我已经能够构建界面,甚至允许用户进行选择。唯一的问题是我不知道如何把电脑的决定打印出来。我想能够更新一个标签或上传一个图像,这取决于计算机选择了什么。例如,用户单击rock,我希望能够在窗口中打印一个岩石的图像,或单词rock,并在下次用户选择新选项(如剪刀或纸张)并重置时更新它。所以简单地说,我希望能够在用户每次单击按钮时创建一个更新的图像或/文本(标签)。

P、 我使用pygame的唯一原因是允许我的应用程序与音乐一起运行。我还提供了下面的应用程序截图。在

代码:#Written by : Pamal Mangat.

#Written on : Monday, July 27th, 2015.

#Rock Paper Scissors : Version 1.2 (Tkinter [GUI] addition)

from tkinter import *

from sys import *

from PIL import Image, ImageTk

import pygame as py

import os

from random import randrange

py.init()

#Function runs the actual game.

def runGame(startWindow):

#Close [startWindow] before advancing:

startWindow.destroy()

startWindow.quit()

master = Tk()

master.title('Lets Play!')

#Function carries on the remainder of the game.

def carryGame(button_id):

result = StringVar()

printResult = Label(master, textvariable = result, font='Bizon 32 bold', bg='PeachPuff2')

printResult.place(x=150, y=300)

#Computer's move:

random_Num = randrange(1,4)

if random_Num == 1:

computer_Move = 'Rock'

elif random_Num == 2:

computer_Move = 'Paper'

else:

computer_Move = 'Scissors'

if button_id == 1:

player_Move = 'Rock'

elif button_id == 2:

player_Move = 'Paper'

else:

player_Move = 'Scissors'

#Rock button

rock_Button = Button(master, width=15, height=7, command=lambda:carryGame(1))

rock_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\rock.png')

rock_Button.config(image=rock_photo,width="120",height="120")

rock_Button.place(x=17, y=70)

#Paper button

paper_Button = Button(master, width=15, height=7, command=lambda:carryGame(2))

paper_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\paper.png')

paper_Button.config(image=paper_photo,width="120",height="120")

paper_Button.place(x=167, y=70)

#Scissors button

scissors_Button = Button(master, width=15, height=7, command=lambda:carryGame(3))

scissors_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\scissors.png')

scissors_Button.config(image=scissors_photo,width="120",height="120")

scissors_Button.place(x=317, y=70)

label_1 = Label(master, text='Please make your selection-', font='Bizon 20 bold', bg='PeachPuff2')

label_1.pack(side=TOP)

label_2 = Label(master, text='The computer picked:', font='Helvetica 22 bold', bg='PeachPuff2')

label_2.place(x=70, y=240)

#Locks window size

master.maxsize(450, 400)

master.minsize(450, 400)

#Sets window background to PeachPuff2

master.config(background='PeachPuff2')

master.mainloop()

def startScreen():

#Plays music for the application

def playMusic(fileName):

py.mixer.music.load(fileName)

py.mixer.music.play()

#Start Window

startWindow = Tk()

startWindow.title('[Rock] [Paper] [Scissors]')

#Imports image as title

load = Image.open(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\title.png')

render = ImageTk.PhotoImage(load)

img = Label(startWindow, image=render, bd=0)

img.image = render

img.place(x=-100, y=-65)

clickToPlay = Button(startWindow, text='Play!', width=8, font='Bizon 20 bold', bg='Black', fg='Yellow', relief=RIDGE, bd=0, command=lambda:runGame(startWindow))

clickToPlay.place(x=75, y=125)

#Credit

authorName = Label(startWindow, text='Written by : Pamal Mangat', font='Times 6 bold', bg='Black', fg='Yellow')

authorName.place(x=2, y=230)

versionNum = Label(startWindow, text='[V 1.2]', font='Times 6 bold', bg='Black', fg='Red')

versionNum.place(x=268, y=230)

#Start Screen Music

playMusic(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Audio\title_Song.mp3')

#Locks window size

startWindow.maxsize(300, 250)

startWindow.minsize(300, 250)

#Sets window background to black

startWindow.config(background='Black')

startWindow.mainloop()

startScreen()

截图:

启动屏幕的图像:

mc0cH.png

在开始屏幕上单击“播放”后打开的窗口的图像:

6P3Gs.png

蓝色圆圈区域是我希望显示计算机决策的地方。在

这篇关于石头剪刀布python代码_Python[Tkinter]石头布剪刀GUI的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/671064

相关文章

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in