
アコーディオンボックス内容
# janken_game 6/8/2025
import PySimpleGUI as sg
from PIL import Image
import random
import os
import time
# 画像フォルダ
IMAGE_FOLDER = "janken_image"
# ブランク画像
blank_dealer_path = os.path.join(IMAGE_FOLDER, "blank_dealer.png")
blank_player_path = os.path.join(IMAGE_FOLDER, "blank_player.png")
# 画像リスト
dealer_images = {
"チョキ": "choki_dealer.png",
"グー": "gu_dealer.png",
"パー": "pa_dealer.png"
}
player_images = {
"チョキ": "choki_player.png",
"グー": "gu_player.png",
"パー": "pa_player.png"
}
# 試合数・勝率カウンター
total_games = 0
dealer_wins = 0
player_wins = 0
# 勝敗判定関数
def judge(dealer_hand, player_hand):
global dealer_wins, player_wins, total_games
total_games += 1 # 試合数をカウント
if dealer_hand == player_hand:
return "引き分け"
elif (dealer_hand == "グー" and player_hand == "チョキ") or \
(dealer_hand == "チョキ" and player_hand == "パー") or \
(dealer_hand == "パー" and player_hand == "グー"):
dealer_wins += 1 # Dealer勝利カウント
return "Dealerの勝ち!"
else:
player_wins += 1 # Player勝利カウント
return "Playerの勝ち!"
# GUIレイアウト
sg.theme("DarkAmber") # ゲーム向けのテーマを適用
layout = [
[sg.Text("ジャンケンゲーム", font=("Helvetica", 16), justification="center")],
[sg.Column([
[sg.Image(filename=blank_dealer_path, key="-DEALER-", size=(200, 200))],
[sg.Text("Dealerの勝率: 0/0", key="-DEALER_SCORE-", font=("Helvetica", 12), size=(20, 1), justification="center")]
], justification="center"),
sg.Column([
[sg.Image(filename=blank_player_path, key="-PLAYER-", size=(200, 200))],
[sg.Text("Playerの勝率: 0/0", key="-PLAYER_SCORE-", font=("Helvetica", 12), size=(20, 1), justification="center")]
], justification="center")],
[sg.Text("", key="-RESULT-", font=("Helvetica", 14), size=(30, 1), justification="center")],
[sg.Button("Start/Again"), sg.Button("End")]
]
# ウィンドウ作成
window = sg.Window("Janken Game", layout, finalize=True, size=(600, 400))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "End":
break
elif event == "Start/Again":
# **ウィンドウサイズを強制固定**
window.set_size((600, 400))
# **まずブランク画像を表示**
window["-DEALER-"].update(filename=blank_dealer_path, size=(200, 200))
window["-PLAYER-"].update(filename=blank_player_path, size=(200, 200))
window.refresh() # **画面更新**
time.sleep(0.5) # **0.5秒間ブランク画像を表示**
# ランダムに手を選択
dealer_hand = random.choice(list(dealer_images.keys()))
player_hand = random.choice(list(player_images.keys()))
# 画像読み込み&縮小
dealer_img_path = os.path.join(IMAGE_FOLDER, dealer_images[dealer_hand])
player_img_path = os.path.join(IMAGE_FOLDER, player_images[player_hand])
dealer_img = Image.open(dealer_img_path).resize((200, 200))
player_img = Image.open(player_img_path).resize((200, 200))
dealer_img.save("temp_dealer.png")
player_img.save("temp_player.png")
# **ジャンケン画像を表示**
window["-DEALER-"].update(filename="temp_dealer.png", size=(200, 200))
window["-PLAYER-"].update(filename="temp_player.png", size=(200, 200))
# **ウィンドウサイズを再調整(余計な拡張を防止)**
window.set_size((600, 400))
# 勝敗結果更新
result_text = judge(dealer_hand, player_hand)
window["-RESULT-"].update(result_text)
# 勝率更新
dealer_score_text = f"Dealerの勝率: {dealer_wins}/{total_games}"
player_score_text = f"Playerの勝率: {player_wins}/{total_games}"
window["-DEALER_SCORE-"].update(dealer_score_text)
window["-PLAYER_SCORE-"].update(player_score_text)
window.close()