剪刀石頭布

      在〈剪刀石頭布〉中尚無留言

實作

如下是使用wxPython實作出剪刀石頭布的結果. 黑色代碼是UI佈局, 全由wxFormbuilder所完成. 藍色部份則為邏輯代碼, 手動加入.

需注意的地方有 :
1. 更改wxStaticBitmp的圖片時, 最後要加入 self.SendSizeEvent(), 否則圖案會有偏移無法置中的問題
2. wxStaticText只有水平對齊的功能, 沒有垂直對齊的功能

scissors

代碼

# -*- coding: utf-8 -*-

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc
import random


###########################################################################
## Class GamePanel
###########################################################################

class GamePanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(694, 517),
                          style=wx.TAB_TRAVERSAL)

        bSizer1 = wx.BoxSizer(wx.VERTICAL)

        bSizer2 = wx.BoxSizer(wx.HORIZONTAL)

        bSizer6 = wx.BoxSizer(wx.VERTICAL)

        self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"電腦", wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_staticText1.Wrap(-1)
        bSizer6.Add(self.m_staticText1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5)

        self.bmpCmp = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(200, 200),
                                      wx.SIMPLE_BORDER)
        bSizer6.Add(self.bmpCmp, 0, wx.ALIGN_CENTER, 5)

        bSizer2.Add(bSizer6, 1, 0, 5)

        bSizer8 = wx.BoxSizer(wx.VERTICAL)

        self.m_staticText2 = wx.StaticText(self, wx.ID_ANY, u"玩家", wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_staticText2.Wrap(-1)
        bSizer8.Add(self.m_staticText2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5)

        self.bmpUser = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(200, 200),
                                       wx.SIMPLE_BORDER)
        bSizer8.Add(self.bmpUser, 0, wx.ALIGN_CENTER, 5)

        bSizer2.Add(bSizer8, 1, wx.EXPAND, 5)

        bSizer1.Add(bSizer2, 0, wx.ALIGN_CENTER_HORIZONTAL, 5)
        self.m_staticline1 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL)
        bSizer1.Add(self.m_staticline1, 0, wx.EXPAND | wx.ALL, 5)
        self.txtResult = wx.StaticText(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(200, 40),
                                       wx.ALIGN_CENTRE)
        self.txtResult.Wrap(-1)
        self.txtResult.SetFont(wx.Font(18, 70, 90, 90, False, wx.EmptyString))
        self.txtResult.SetBackgroundColour(wx.Colour(0, 128, 255))

        bSizer1.Add(self.txtResult, 0, wx.ALIGN_CENTER, 0)

        bSizer3 = wx.BoxSizer(wx.HORIZONTAL)
        self.btnScissors = wx.BitmapButton(self, wx.ID_ANY, wx.Bitmap(u"icon/scissors.png", wx.BITMAP_TYPE_ANY),
                                           wx.DefaultPosition, wx.Size(200, 200), wx.BU_AUTODRAW, wx.DefaultValidator,
                                           u"scissors")
        bSizer3.Add(self.btnScissors, 0, wx.ALL, 5)

        self.btnRock = wx.BitmapButton(self, wx.ID_ANY, wx.Bitmap(u"icon/rock.png", wx.BITMAP_TYPE_ANY),
                                       wx.DefaultPosition, wx.Size(200, 200), wx.BU_AUTODRAW,wx.DefaultValidator,
                                           u"rock")
        bSizer3.Add(self.btnRock, 0, wx.ALL, 5)

        self.btnPaper = wx.BitmapButton(self, wx.ID_ANY, wx.Bitmap(u"icon/paper.png", wx.BITMAP_TYPE_ANY),
                                        wx.DefaultPosition, wx.Size(200, 200), wx.BU_AUTODRAW,wx.DefaultValidator,
                                           u"paper")
        bSizer3.Add(self.btnPaper, 0, wx.ALL, 5)

        bSizer1.Add(bSizer3, 1, wx.ALIGN_CENTER_HORIZONTAL, 5)

        self.SetSizer(bSizer1)
        self.Layout()

        # Connect Events
        self.btnScissors.Bind(wx.EVT_BUTTON, self.btnClick)
        self.btnRock.Bind(wx.EVT_BUTTON, self.btnClick)
        self.btnPaper.Bind(wx.EVT_BUTTON, self.btnClick)

    def __del__(self):
        pass

    # Virtual event handlers, overide them in your derived class
    def btnClick(self, event):
        cmp=random.randint(0,2)
        if cmp==0:
            self.bmpCmp.SetBitmap(wx.Bitmap("icon/scissors.png"))
        elif cmp==1:
            self.bmpCmp.SetBitmap(wx.Bitmap("icon/rock.png"))
        else:
            self.bmpCmp.SetBitmap(wx.Bitmap("icon/paper.png"))

        btn=event.GetEventObject().GetName()
        player=0
        if btn=="scissors":
            self.bmpUser.SetBitmap(wx.Bitmap("icon/scissors.png"))
            player = 0
        elif btn=="rock":
            self.bmpUser.SetBitmap(wx.Bitmap("icon/rock.png"))
            player=1
        else:
            self.bmpUser.SetBitmap(wx.Bitmap("icon/paper.png"))
            player = 2
        self.SendSizeEvent()
        result=player-cmp
        if result==0:
            self.txtResult.SetLabelText("平手")
        elif result ==-1 or result == 2:
            self.txtResult.SetLabelText("You Loss")
        else:
            self.txtResult.SetLabelText("You Win")



主程式

# -*- coding: utf-8 -*-
import wx
import wx.xrc
from GamePanel import GamePanel
class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                          size=wx.Size(1106, 614), style=wx.DEFAULT_FRAME_STYLE | wx.MAXIMIZE | wx.TAB_TRAVERSAL)

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        self.statusBar = self.CreateStatusBar(1, wx.STB_SIZEGRIP, wx.ID_ANY)
        self.menubar = wx.MenuBar(0)
        self.menu_file = wx.Menu()
        self.it_open_video = wx.MenuItem(self.menu_file, wx.ID_ANY, u"開啟影像(&V)", wx.EmptyString, wx.ITEM_NORMAL)
        self.menu_file.Append(self.it_open_video)

        self.menu_file.AppendSeparator()

        self.it_close = wx.MenuItem(self.menu_file, wx.ID_ANY, u"結束(&C)", wx.EmptyString, wx.ITEM_NORMAL)
        self.menu_file.Append(self.it_close)

        self.menubar.Append(self.menu_file, u"檔案(&F)")

        self.menu_view = wx.Menu()
        self.menubar.Append(self.menu_view, u"檢視(&V)")

        self.menu_game = wx.Menu()
        self.it_rock = wx.MenuItem(self.menu_game, wx.ID_ANY, u"猜拳遊戲", wx.EmptyString, wx.ITEM_NORMAL)
        self.menu_game.Append(self.it_rock)

        self.menubar.Append(self.menu_game, u"遊戲(&G)")

        self.menu_help = wx.Menu()
        self.it_version = wx.MenuItem(self.menu_help, wx.ID_ANY, u"版本", wx.EmptyString, wx.ITEM_NORMAL)
        self.menu_help.Append(self.it_version)

        self.it_license = wx.MenuItem(self.menu_help, wx.ID_ANY, u"版權", wx.EmptyString, wx.ITEM_NORMAL)
        self.menu_help.Append(self.it_license)

        self.menubar.Append(self.menu_help, u"說明(&H)")

        self.SetMenuBar(self.menubar)

        self.Centre(wx.BOTH)
        self.frameBoxSizer=wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.frameBoxSizer)
        self.panel=wx.Panel(self, -1)
        self.panel.SetBackgroundColour("cyan")
        self.frameBoxSizer.Add(self.panel, 1, wx.EXPAND |wx.ALL, 5 )
        # Connect Events
        self.Bind(wx.EVT_MENU, self.it_open_video_Click, id=self.it_open_video.GetId())
        self.Bind(wx.EVT_MENU, self.it_rock_Click, id=self.it_rock.GetId())
        self.Bind(wx.EVT_MENU, self.it_version_Click, id=self.it_version.GetId())
        self.Bind(wx.EVT_MENU, self.it_license_Click, id=self.it_license.GetId())
    def __del__(self):
        pass
    # Virtual event handlers, overide them in your derived class
    def it_rock_Click(self, event):
        self.RemoveChild(self.panel)
        self.panel.Destroy()
        self.panel = GamePanel(self)
        self.frameBoxSizer.Add(self.panel, 1, wx.EXPAND | wx.ALL, 5)
        self.Layout()
        #self.SendSizeEvent()
    def it_version_Click(self, event):
        wx.MessageBox("Version\n\n1.0.0","版本說明")
    def it_license_Click(self, event):
        wx.MessageBox("PowerBy\n\nThomas Wu(mahaljsp@gmail.com)","版權說明")
    def it_open_video_Click(self, event):
        openFileDialog = wx.FileDialog(self, "Open", "", "", "Python files (*.py)|*.py", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)

        openFileDialog.ShowModal()
        print(openFileDialog.GetPath())
        openFileDialog.Destroy()
app=wx.App()
MainFrame(None).Show()
app.MainLoop()


發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *