Two Projects in Complete Form

I would like to note down these two projects in complete form for future reference. First, Raymond’s virtual startup MVP; second, Tim’s pygame Checker. I would also like to find a complete package for creating a db system for students/employees in an institution.

First, Raymond’s full illustration of creating an MVP startup using Python:

#lean startup MVP deliverable by Raymond again
import math

class Circle(object) :  # new-style class
    'An advanced circle analytic toolkit'

    # flyweight design pattern suppresses
    # the instance dictionary

    __slots__ = ['diameter']
    version = '0.7'

    def __init__(self, radius):
        self.radius = radius  # instance variable

    @property    # convert dotted access to method calls
    def radius(self):
        'Radius of circle'
        return self.diameter / 2.0

    @radius.setter
    def radius(self, radius):
        self.diameter = radius * 2.0

    @staticmethod   # attach function to classes
    def angle_to_grade(self, angle) :
        'Convert angle in degree to a percentage grade'
        return math.tan(math.radians(angle)) * 100.0
    
    
    def area(self) :
        'perform quadrature on a shape of uniform radius'
        p = self.__perimeter()
        r = p / math.pi / 2.0
        return math.pi  * r ** 2.0

    def perimeter(self):
        return 2.0 * math.pi * self.radius
    __perimeter =  perimeter

    @classmethod  #alternative constructor
    def from_bbd(cls, bbd) :
        'Construct a circle from a bounding box diagonal'
        radius = bbd / 2.0 / math.sqrt(2.0)
        return Circle(radius)

class Tire(Circle) :
    'Tires are circles with a corrected perimeter'
  
    def perimeter(self) :
        'Circumference corrected for the rubber'
        return Circle.perimeter(self) * 1.25

from random import random, seed

seed(8675309)
print('Using Circuituous (tm) version', Circle.version)
n = 10
circles = [Circle(random()) for i in range(n)]
print('The average area of', n, 'random circles')
avg = sum([c.area() for c in circles]) / n
print('is %.if') %avg

There are still hiccups for me along the way as to why do so, eventually things will make more sense when applying them. So summarize, the gist is

Now move on to Tim’s full-blown process of make a checker Pygame. The key to learn is how he comes up with the framework or architect?

First, the github repo contains major folder “checkers” and main.py and __init__.py as usual, however, within the “checkers” folder, another __init__.py is created to make it easy to import modules in main.py.

github didn’t paste the init under checkers
however in actual demo he showed above the init within checkers

Meanwhile, within init file, what if I need to import methods/instance in the same subfolder – checkers? Simply from constants import *

In the parallel script – board.py

Likewise, in the parallel piece.py

In the main.py,

Second, dive deep to the main.py script – the Raymond’s MVP demo didn’t include such main.py though, but it’s widely used in almost all Python products.

# Assets: https://techwithtim.net/wp-content/uploads/2020/09/assets.zip
import pygame
from checkers.constants import WIDTH, HEIGHT, SQUARE_SIZE, RED
from checkers.game import Game
from minimax.algorithm import minimax

FPS = 60

WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Checkers')

def get_row_col_from_mouse(pos):
    x, y = pos
    row = y // SQUARE_SIZE
    col = x // SQUARE_SIZE
    return row, col

def main():
    run = True
    clock = pygame.time.Clock()
    game = Game(WIN)

    while run:
        clock.tick(FPS)

        if game.winner() != None:
            print(game.winner())
            run = False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            
            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                row, col = get_row_col_from_mouse(pos)
                game.select(row, col)

        game.update()
    
    pygame.quit()

main()

So the logic of designing this main.py together with constants.py, board.py, piece.py and game.py is essential to learn.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.