Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wxPython
#1
Thought I would try my luck at doing a simple yahtzee in wxpython. Just wanted to share what I have gotten done so far. It is not complete. Only have the scorecard at the moment. Might give someone else a start if they would also like to try.

#! /usr/bin/env python3

import wx
class Card:
    def __init__(self):
        self.section = {
            'ones': None, 'twos': None, 'threes': None,
            'fours': None, 'fives': None, 'sixes': None,
            'upper score': None, 'bonus': None, 'upper total': None,
            '3 of a kind': None, '4 of a kind': None,
            'sm. straight': None, 'lg. straight': None,
            'fullhouse': None, 'yahtzee': None, 'chance': None,
            'lower score': None, 'upper score': None, 'grand total': None
        }


class MyPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)

        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, event):
        self.SetBackgroundColour('white')
        self.SetForegroundColour(wx.Colour(10, 10, 10))

        font = wx.Font(25, wx.ROMAN, wx.ITALIC, wx.BOLD)

        header = wx.PaintDC(self)
        header.SetFont(font)
        header.DrawText('wxPython Yahtzee', 251, 6)

        self.SetForegroundColour('tomato')
        header = wx.PaintDC(self)
        font = wx.Font(25, wx.ROMAN, wx.ITALIC, wx.BOLD)
        header.SetFont(font)
        header.DrawText('wxPython Yahtzee', 250, 5)

        self.SetBackgroundColour('white')
        self.SetForegroundColour('black')
        
        labels = Card()
        colored_rows = ['upper score', 'bonus', 'upper total', 'lower score']
        i = 75
        j = 78

        for label, score in labels.section.items():
            if score == None:
                score = ''
            dc = wx.PaintDC(self)
            dc.SetPen(wx.Pen('gray'))
            if label in colored_rows:
                dc.SetBrush(wx.Brush('light yellow'))
            elif label == 'grand total':
                dc.SetBrush(wx.Brush('light blue'))
            else:
                dc.SetBrush(wx.Brush('gray', wx.TRANSPARENT))
            dc.DrawRectangle(10, i, 250, 25)
            dc.DrawRectangle(260, i, 50, 25)

            dc.DrawText(label.upper(), 15, j)
            dc.DrawText(score, 275, j)
            i += 26
            j += 26


class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(None, -1, 'Yahtzee', size=(800, 600))
        MyPanel(frame, -1)
        frame.Show()
        return True

app = MyApp()
app.MainLoop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
Just wanted to show current progress. Rewrote the code a little. The next step is to figure out how I want to do the die.
#! /usr/bin/env python3

# Do the imports
import wx

class Header(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, size=(800, 60))
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, event):
        font = wx.Font(25, wx.ROMAN, wx.ITALIC, wx.BOLD)
        self.SetBackgroundColour('ivory')

        dc = wx.PaintDC(self)
        dc.SetFont(font)
        dc.DrawText('wxPython Yahtzee', 250, 10)
        self.SetForegroundColour('darkorange')

        dc = wx.PaintDC(self)
        dc.SetFont(font)
        dc.DrawText('wxPython Yahtzee', 251, 11)
        self.SetForegroundColour(wx.Colour(50, 50, 50))

class Footer(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, size=(800, 30))
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, event):
        font = wx.Font(12, wx.ROMAN, wx.ITALIC, wx.BOLD)
        self.SetBackgroundColour('ivory')

        dc = wx.PaintDC(self)
        dc.SetFont(font)
        dc.DrawText('My-Python.org', 350, 5)
        self.SetForegroundColour('darkorange')

        dc = wx.PaintDC(self)
        dc.SetFont(font)
        dc.DrawText('My-Python.org', 351, 6)
        self.SetForegroundColour(wx.Colour(50, 50, 50))


class Card(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, size=(400, 475))
        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.section = {
            'ones': None, 'twos': None, 'threes': None,
            'fours': None, 'fives': None, 'sixes': None,
            'upper score': None, 'bonus': None, 'upper total': None,
            '3 of a kind': None, '4 of a kind': None,
            'sm. straight': None, 'lg. straight': None,
            'fullhouse': None, 'yahtzee': None, 'chance': None,
            'lower score': None, 'upper score': None, 'grand total': None
        }

    def OnPaint(self, event):
        colored_rows = ['upper score', 'bonus', 'upper total', 'lower score']
        self.SetBackgroundColour('white')
        self.SetForegroundColour('black')
        i = 5
        j = 8
        for label, score in self.section.items():
            if score == None:
                score = ''
            dc = wx.PaintDC(self)
            dc.SetPen(wx.Pen('black'))
            if label in colored_rows:
                dc.SetBrush(wx.Brush('light yellow'))
            elif label == 'grand total':
                dc.SetBrush(wx.Brush('light blue'))
            else:
                dc.SetBrush(wx.Brush('gray', wx.TRANSPARENT))
            dc.DrawRectangle(10, i, 250, 25)
            dc.DrawRectangle(261, i, 50, 25)

            dc.DrawText(label.upper(), 15, j)
            dc.DrawText(score, 275, j)
            i += 26
            j += 26

class Die(wx.Panel):
    def __init__(self, parent, id ,number):
        wx.Panel.__init__(self, parent, id)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.number = number

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.SetPen(wx.Pen(wx.BLACK))
        dc.SetBrush(wx.Brush('ivory2'))
        dc.DrawRectangle(10, 10, 50,50)
        dc.SetBrush(wx.Brush('black'))
        dc.DrawCircle(34.5, 34.5, 5)


class MainWindow(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, -1, 'Yahtzee', size=(800, 600))
        sizer = wx.GridBagSizer(vgap=2, hgap=2)

        header = Header(self, wx.ID_ANY)
        card = Card(self, wx.ID_ANY)
        die = Die(self, wx.ID_ANY, number=1)
        footer = Footer(self, wx.ID_ANY)


        sizer.Add(header, pos=(0,0), flag=wx.EXPAND, span=(1, 2))
        sizer.Add(card, pos=(1, 0), flag=wx.EXPAND)
        sizer.Add(die, pos=(1, 1), flag=wx.EXPAND)
        sizer.Add(footer, pos=(2,0), flag=wx.EXPAND, span=(1, 2))
        self.SetSizerAndFit(sizer)



def main():
    app = wx.App(False)
    window = MainWindow(None, wx.ID_ANY)
    window.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()
[Image: yahtzee.png]
snippsat likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020