|
- #!/bin/python3
- #
- # gamepicker.py - pick games for Novemeber 2020 sub crown
- #
- # Two files provided:
- # - subs.txt -- 8 subscribers
- # - teams.txt -- 30 teams
- #
- # Pseudocode:
- # Split screen in 1:3
- # - LHS = subscriber+team space
- # - RHS = teams space
- # Set BG image
- # Init seed
- # Lay out teams in order (3 cols)
- # On keypress, show subscriber name & show team random picker (via highlight)
- # Once picked, add subscriber + team to LHS
-
- import sys
- import random
- import pygame
- import platform
-
- pygame.init()
- random.seed("bertiebdec2021")
-
- HIGHLIGHTEVENT = pygame.USEREVENT + 1 # user specified event
- CLEAREVENT = pygame.USEREVENT + 2 # user specified event
- CHOOSEEVENT = pygame.USEREVENT + 3 # user specified event
-
- SIZE = WIDTH, HEIGHT = 1920, 1080
-
- BGIMG = pygame.image.load("sub_crown_bg_2.png")
-
- FONTHEIGHT = 35
- SUBFONTHEIGHT = 40
-
- if "Windows" not in platform.system():
- SUBFONT = pygame.font.SysFont("Fetamont", SUBFONTHEIGHT)
- FONT = pygame.font.SysFont("Fetamont", FONTHEIGHT)
- else:
- SUBFONT = pygame.font.Font("ffmb10.ttf", SUBFONTHEIGHT)
- FONT = pygame.font.Font("ffmb10.ttf", FONTHEIGHT)
-
- OFFWHITE = (224, 224, 224)
- HIGHLIGHT = (255, 209, 0)
- BLUE = (20, 25, 153)
- RED = (186, 20, 31)
- PURPLE = (121, 35, 158)
-
- screen = pygame.display.set_mode(SIZE)
- pygame.display.set_caption("Sub Crown January 2022 Team Picker")
-
- # Set up team text positioning references
- #
- # 26 teams so 3 cols, 9 rows
-
- TEAMSFILE = "teams.txt"
-
- TEAMNUMCOLS = 3
- TEAMNUMROWS = 15
-
- # COLSTART = int(WIDTH/TEAMNUMCOLS)
- COLSTART = int(WIDTH/(TEAMNUMCOLS+1))
- COLWIDTH = int((WIDTH-COLSTART)/TEAMNUMCOLS)
-
- ROWSTART = int(HEIGHT/(TEAMNUMROWS+2))
- # ROWHEIGHT = int((HEIGHT-ROWSTART)/TEAMNUMROWS)
- ROWHEIGHT = int((HEIGHT-ROWSTART)/(TEAMNUMROWS+0))
-
- TEAMCOLS = []
- TEAMROWS = []
-
- for i in range(TEAMNUMCOLS):
- print(COLSTART + i*COLWIDTH)
- TEAMCOLS.append(COLSTART + i*COLWIDTH)
-
- for i in range(TEAMNUMROWS):
- TEAMROWS.append(ROWSTART + i*ROWHEIGHT)
- print(ROWSTART + i*ROWHEIGHT)
-
- SUBSFILE = "subs.txt"
- SUBS = []
-
- SUBNUMROWS = 9
- SUBROWHEIGHT = int((HEIGHT-ROWSTART)/9)
- SUBROWS = []
-
- for i in range(SUBNUMROWS):
- SUBROWS.append(ROWSTART + i*SUBROWHEIGHT)
-
- TEAMS = [] # List of Team
-
-
- class Team(object):
- """Teams for iteration"""
-
- def __init__(self, team, x, y):
- self.team = team
- self.x = x
- self.y = y
- self.colour = OFFWHITE
-
- def highlight(self):
- self.colour = HIGHLIGHT
-
- def unhighlight(self):
- self.colour = OFFWHITE
-
- def choose(self):
- self.colour = RED
-
- def draw(self):
- draw_text(self.team, FONT, self.colour, self.y, self.x)
-
-
- class Sub(object):
- """Like Team"""
-
- def __init__(self, sub):
- self.sub = sub
- self._sub = ""
- self.team = ""
- self.x = 20
- self.y = 0
-
- def draw(self):
- if self._sub != "":
- draw_text(self.sub, SUBFONT, BLUE, self.x, self.y)
- if self.team != "":
- draw_text(self.team, SUBFONT, PURPLE,
- self.x, int(self.y+SUBFONTHEIGHT))
-
-
- class Highlighter(object):
- """For highlighting teams randomly"""
-
- def __init__(self):
- self.team = None
- self.iterations = 0
- self.max = 70
- self.lastchoice = -1
-
- def highlight(self):
- if self.lastchoice >= 0:
- TEAMS[self.lastchoice].unhighlight()
- self.iterations += 1
- if self.iterations >= self.max:
- # Choosing time
- self.lastchoice = -1
- # # ** Name-specific choices! ** # #
- # for tsub in SUBS:
- # if tsub.team == "":
- # choosing_sub = tsub
- # break
- # for j in range(len(TEAMS)):
- # if "amy" not in choosing_sub.sub:
- # if TEAMS[j].team.lower(
- # ).startswith(choosing_sub.sub[0].lower()):
- # TEAMS[j].choose()
- # break
- # else:
- # if TEAMS[j].team.lower().startswith("a"):
- # TEAMS[j].choose()
- # break
- # # End Name-specific stuff # #
- TEAMS[-1].choose()
- pygame.time.set_timer(HIGHLIGHTEVENT, 0) # Disable highlight
- pygame.time.set_timer(CHOOSEEVENT, 5000, True) # make choice
- self.iterations = 0
- else:
- self.lastchoice = random.randrange(len(TEAMS))
- TEAMS[self.lastchoice].highlight()
-
-
- def draw_text(text, font, text_col, x, y):
- img = font.render(text, True, text_col)
- screen.blit(img, (x, y))
-
-
- def setup_team_board(randomise_teams=False):
- """Grab teams from text file and set their positions"""
- teamlist = []
-
- with open(TEAMSFILE, "r") as fh:
- teams = fh.readlines()
- teamlist = [team.rstrip() for team in teams if team != ""]
-
- if randomise_teams:
- random.shuffle(teamlist)
-
- for row in range(TEAMNUMROWS):
- for col in range(TEAMNUMCOLS):
- if len(teamlist) == 0:
- continue
- name = teamlist.pop(0)
- print("Adding {} at {},{}".format(name,
- TEAMCOLS[col],
- TEAMROWS[row]))
- TEAMS.append(Team(name,
- TEAMROWS[row], TEAMCOLS[col]))
-
-
- def setup_subs():
- """Check positioning, not actually used"""
- for i in range(len(SUBS)):
- # print("drawing {} at {},{}".format(SUBS[i], 20, SUBROWS[i]))
- SUBS[i].y = SUBROWS[i]
-
-
- # Prep
-
- for line in open(SUBSFILE, "r"):
- sub = line.strip()
- SUBS.append(Sub(sub))
- print(SUBS)
-
- random.shuffle(SUBS)
- setup_team_board(randomise_teams=True)
- random.shuffle(TEAMS)
- setup_subs()
-
- HIGHLIGHTER = Highlighter()
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == 113: # Q
- pygame.quit()
- sys.exit()
- elif event.key == 115:
- TEAMS[5].highlight()
- elif event.key == 100: # D for debug
- for sub in SUBS:
- if sub.team == "":
- sub.team = TEAMS.pop().team
- if sub._sub == "":
- sub._sub = sub.sub
- elif event.key == 32:
- # Display who we're choosing for
- for sub in SUBS:
- if sub._sub == "":
- sub._sub = sub.sub
- break
- # Start the "who's it going to pick?" animation
- pygame.time.set_timer(HIGHLIGHTEVENT, 100) # every 100ms
- print(event.key)
- elif event.type == HIGHLIGHTEVENT:
- HIGHLIGHTER.highlight()
- elif event.type == CLEAREVENT:
- for team in TEAMS:
- team.unhighlight()
- elif event.type == CHOOSEEVENT:
- for sub in SUBS:
- if sub.team == "":
- # # Name-specific again # #
- # for k in range(len(TEAMS)):
- # if TEAMS[k].colour == RED:
- # sub.team = TEAMS.pop(k).team
- # break
- # # End name-specific # #
- sub.team = TEAMS.pop().team
- pygame.time.set_timer(CLEAREVENT, 100, True)
- break
-
- screen.blit(BGIMG, (0, 0))
-
- for team in TEAMS:
- team.draw()
-
- for sub in SUBS:
- sub.draw()
-
- pygame.display.update()
|