Site icon George的生活點滴

Flet – Rock paper scissors 剪刀石頭布

如何用 Flet 做一個剪刀石頭布的遊戲呢? 請看下面的程式


# Flet - Rock paper scissors - 剪刀石頭布 
# 2024-08-29
# https://github.com/lcc728/flet/blob/main/ex_rock_paper_scissors.py

import flet as ft
import random

def main(page: ft.Page):
    page.title = "剪刀石頭布遊戲"

    result_text = ft.Row([ft.Text("請出拳")], alignment=ft.MainAxisAlignment.CENTER)
    score_text = ft.Text(size=16, text_align=ft.TextAlign.CENTER)
    player_score = 0
    computer_score = 0

    def play_game(player_choice):
        nonlocal player_score, computer_score
        choices = [ft.icons.CONTENT_CUT, ft.icons.CIRCLE, ft.icons.BACK_HAND_SHARP]
        computer_choice = random.choice(choices)
        
        result = ""
        if player_choice == computer_choice:
            result = "平手,再試一次吧!"
        elif (
            (player_choice == ft.icons.CONTENT_CUT and computer_choice ==  ft.icons.BACK_HAND_SHARP) or
            (player_choice == ft.icons.CIRCLE and computer_choice == ft.icons.CONTENT_CUT) or
            (player_choice == ft.icons.BACK_HAND_SHARP and computer_choice == ft.icons.CIRCLE)
        ):
            result = "你贏了!"
            player_score += 1
        else:
            result = "你輸了!"
            computer_score += 1
        
        result_text.controls = [
            ft.Text("你出 "),
            ft.Icon(player_choice, color="red"),
            ft.Text(", 電腦出 "),
            ft.Icon(computer_choice, color="green"),
            ft.Text(f". {result}")
        ]
        score_text.value = f"你贏 {player_score} 次- 輸 {computer_score} 次"
        page.update()

    page.add(
        ft.Row([
            ft.IconButton(icon=ft.icons.CONTENT_CUT, on_click=lambda _: play_game(ft.icons.CONTENT_CUT), icon_size=50),
            ft.IconButton(icon=ft.icons.CIRCLE, on_click=lambda _: play_game(ft.icons.CIRCLE), icon_size=50),
            ft.IconButton(icon=ft.icons.BACK_HAND_SHARP, on_click=lambda _: play_game(ft.icons.BACK_HAND_SHARP), icon_size=50),
        ], alignment=ft.MainAxisAlignment.CENTER),
        result_text,
        score_text,
    )

ft.app(target=main)

程式中利用了三個 icon 分別代表剪刀、石頭和布,只要點一下 Icon 就代表你要出的拳,當點擊 icon 時,會呼叫 play_game 的 function 來判斷輸贏

一個 text 顯示猜拳結果, 另一個 text 顯示分數

執行結果:

Source Code: https://github.com/lcc728/flet/blob/main/ex_rock_paper_scissors.py

Exit mobile version