質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

533閲覧

じゃんけん  ボタンを押したとき、画像のYou win!を正しい結果を表示させたいのですが方法が分かりません。教えてください。

suzu

総合スコア3

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/01/26 10:04

編集2022/01/26 11:26

import

1import random 2 3def hand(rpc): 4 if(rpc==0): 5 return "Rock" 6 if(rpc==1): 7 return "Paper" 8 if(rpc==2): 9 return "Scissors" 10 11def checkwinner(chand,yhand): 12 if yhand==chand: 13 result="Draw!" 14 result=tk.Label(window,text="You win!",width=10) 15 else: 16 if(yhand==0) and (chand==2): 17 result="You win!" 18 result=tk.Label(window,text="You win!",width=10) 19 20 else: 21 if(yhand==0) and (chand==1): 22 result="You lose!" 23 result=tk.Label(window,text="You lose!",width=10) 24 25 if(yhand==1) and (chand==0): 26 result="You win!" 27 result=tk.Label(window,text="You win!",width=10) 28 29 else: 30 if(yhand==1) and (chand==2): 31 result="You lose!" 32 result=tk.Label(window,text="You lose!",width=10) 33 34 if(yhand==2) and (chand==1): 35 result="You win!" 36 result=tk.Label(window,text="You win!",width=10) 37 38 else: 39 if(yhand==2) and (chand==0): 40 result="You lose!" 41 result=tk.Label(window,text="You lose!",width=10) 42 return result 43 44 45import random 46comphand=random.randint(0,2) 47 48def rock(): 49 result=tk.Label(window,text="You win!",width=10) 50 result=tk.Label(window,text="Draw!",width=10) 51 result=tk.Label(window,text="You lose!",width=10) 52 checkwinner(comphand,0) 53 54def paper(): 55 result=tk.Label(window,text="You win!",width=10) 56 result=tk.Label(window,text="Draw!",width=10) 57 result=tk.Label(window,text="You lose!",width=10) 58 checkwinner(comphand,1) 59 60def scissors(): 61 result=tk.Label(window,text="You win!",width=10) 62 result=tk.Label(window,text="Draw!",width=10) 63 result=tk.Label(window,text="You lose!",width=10) 64 checkwinner(comphand,2) 65 66import tkinter as tk 67 68window=tk.Tk() 69 70window.geometry("640x300") 71 72window.title("Rock-Paper-Scissors Game") 73 74window.configure(bg="#FFFFFF") 75 76canvas=tk.Canvas(window,bg="#FFFFFF",width=640,height=220) 77canvas.place(x=0,y=0) 78 79rockImage=tk.PhotoImage(file="rock.gif") 80paperImage=tk.PhotoImage(file="paper.gif") 81scissorsImage=tk.PhotoImage(file="scissors.gif") 82 83canvas.create_image(10,30,image=rockImage,anchor="nw") 84canvas.create_image(210,30,image=paperImage,anchor="nw") 85canvas.create_image(410,30,image=scissorsImage,anchor="nw") 86 87rockbutton=tk.Button(window,text="Rock",width=6,command=rock) 88paperbutton=tk.Button(window,text="Paper",width=6,command=paper) 89scissorsbutton=tk.Button(window,text="Scissors",width=6,command=scissors) 90 91rockbutton.place(x=80,y=230) 92paperbutton.place(x=280,y=230) 93scissorsbutton.place(x=480,y=230) 94 95result=tk.Label(window,text="You win!",width=10) 96result.place(x=270,y=270) 97 98コード

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shiracamus

2022/01/26 10:59

プログラムコードを コードブロック にしてインデントを正しくしていただけませんか? コードブロックは、先頭行がバッククオート3個、最終行もバッククオート3個でプログラムコード全体を囲みます。
y_waiwai

2022/01/26 11:17

このままではコードが読めないので、質問を編集し、</>(コードの挿入)ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
suzu

2022/01/26 11:27

出来ていますでしょうか??
ROGPURS

2022/01/26 11:48

できていますよ
ROGPURS

2022/01/26 11:49

ですが最後のコードの文字は削除したほうが
guest

回答1

0

ベストアンサー

単純な実装にしてみました。

python

1import random 2import tkinter as tk 3 4comphand = random.randint(0, 2) 5 6 7def rock(): 8 if comphand == 2: result['text'] = "You win!" 9 if comphand == 0: result["text"] = "Draw!" 10 if comphand == 1: result["text"] = "You lose!" 11 12def paper(): 13 if comphand == 0: result["text"] = "You win!" 14 if comphand == 1: result["text"] = "Draw!" 15 if comphand == 2: result["text"] = "You lose!" 16 17def scissors(): 18 if comphand == 1: result["text"] = "You win!" 19 if comphand == 2: result["text"] = "Draw!" 20 if comphand == 0: result["text"] = "You lose!" 21 22 23window= tk.Tk() 24window.geometry("640x300") 25window.title("Rock-Paper-Scissors Game") 26window.configure(bg="#FFFFFF") 27 28canvas = tk.Canvas(window, bg="#FFFFFF", width=640, height=220) 29canvas.place(x=0, y=0) 30 31rockImage= tk.PhotoImage(file="rock.gif") 32paperImage= tk.PhotoImage(file="paper.gif") 33scissorsImage= tk.PhotoImage(file="scissors.gif") 34 35canvas.create_image(10, 30, image=rockImage, anchor="nw") 36canvas.create_image(210, 30, image=paperImage, anchor="nw") 37canvas.create_image(410, 30, image=scissorsImage, anchor="nw") 38 39rockbutton = tk.Button(window, text="Rock", width=6, command=rock) 40paperbutton = tk.Button(window, text="Paper", width=6, command=paper) 41scissorsbutton = tk.Button(window, text="Scissors", width=6, command=scissors) 42 43rockbutton.place(x=80, y=230) 44paperbutton.place(x=280, y=230) 45scissorsbutton.place(x=480, y=230) 46 47result= tk.Label(window, text="Janken!", width=10) 48result.place(x=270, y=270) 49 50window.mainloop()

投稿2022/01/27 02:09

shiracamus

総合スコア5406

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

suzu

2022/01/27 04:36

ありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問