adding bingo module
55
bingo.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
import glob
|
||||
from PIL import Image
|
||||
from random import randrange
|
||||
from uuid import uuid4
|
||||
|
||||
class Bingo:
|
||||
"""Class which allows creation and cleanup of F1 bingo card images."""
|
||||
def __init__(self):
|
||||
self.LAYOUT_WIDTH = 88
|
||||
self.X_OFFSET = 10
|
||||
self.Y_OFFSET = 110
|
||||
self.SQUARES_PATH = "bingo_images/squares/*.png"
|
||||
self.FREE_SQUARE_PATH = "bingo_images/free_space.png"
|
||||
self.BLANK_CARD_PATH = "bingo_images/card_blank.png"
|
||||
self.TEMP_FOLDER = "bingo_images/temp/"
|
||||
|
||||
self.square_files = glob.glob(self.SQUARES_PATH)
|
||||
|
||||
def get_card(self):
|
||||
used_files = set()
|
||||
|
||||
with Image.open(self.BLANK_CARD_PATH) as card_img:
|
||||
card_img.load()
|
||||
card_img = card_img.convert('RGBA')
|
||||
|
||||
# Fill the grid
|
||||
for y in range(5):
|
||||
for x in range(5):
|
||||
square_file = ""
|
||||
# If this is the center square, use the free square
|
||||
if x == 2 and y == 2:
|
||||
square_file = self.FREE_SQUARE_PATH
|
||||
|
||||
# otherwise, find a random file that hasn't been used yet
|
||||
else:
|
||||
rand_file_idx = randrange(len(self.square_files))
|
||||
while rand_file_idx in used_files:
|
||||
rand_file_idx = randrange(len(self.square_files))
|
||||
|
||||
square_file = self.square_files[rand_file_idx]
|
||||
used_files.add(rand_file_idx)
|
||||
|
||||
with Image.open(square_file) as square:
|
||||
|
||||
position = (self.X_OFFSET + (x * self.LAYOUT_WIDTH),
|
||||
self.Y_OFFSET + (y * self.LAYOUT_WIDTH))
|
||||
|
||||
card_img.paste(square, position, square)
|
||||
|
||||
# Write image to temp file
|
||||
outfile = "".join((self.TEMP_FOLDER, str(uuid4()), ".png"))
|
||||
print(f"{outfile=}")
|
||||
card_img.save(outfile)
|
||||
return outfile
|
||||
|
BIN
bingo_images/card_blank.png
Normal file
After Width: | Height: | Size: 6.1 KiB |
BIN
bingo_images/card_blank.xcf
Normal file
BIN
bingo_images/free_space.png
Normal file
After Width: | Height: | Size: 3.7 KiB |
BIN
bingo_images/squares/a_driver_does_no_laps.png
Normal file
After Width: | Height: | Size: 3.1 KiB |
BIN
bingo_images/squares/aerorake.png
Normal file
After Width: | Height: | Size: 1.7 KiB |
BIN
bingo_images/squares/anyone_but_max_p1.png
Normal file
After Width: | Height: | Size: 3.0 KiB |
BIN
bingo_images/squares/big_changes.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
bingo_images/squares/big_spin_t12.png
Normal file
After Width: | Height: | Size: 2.3 KiB |
BIN
bingo_images/squares/car_debris_on_track.png
Normal file
After Width: | Height: | Size: 2.6 KiB |
BIN
bingo_images/squares/car_different.png
Normal file
After Width: | Height: | Size: 3.2 KiB |
BIN
bingo_images/squares/carbon_fiber.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
bingo_images/squares/chadlonso_time_life.png
Normal file
After Width: | Height: | Size: 2.9 KiB |
BIN
bingo_images/squares/commentator_car_confused.png
Normal file
After Width: | Height: | Size: 2.6 KiB |
BIN
bingo_images/squares/driver_complains_impeding.png
Normal file
After Width: | Height: | Size: 3.6 KiB |
BIN
bingo_images/squares/flowviz.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
bingo_images/squares/haas_stops.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
bingo_images/squares/honda_engine_dies.png
Normal file
After Width: | Height: | Size: 2.5 KiB |
BIN
bingo_images/squares/its_only_testing.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
bingo_images/squares/lockup_t1.png
Normal file
After Width: | Height: | Size: 1.9 KiB |
BIN
bingo_images/squares/mercedes_downplays.png
Normal file
After Width: | Height: | Size: 3.2 KiB |
BIN
bingo_images/squares/no_power.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
bingo_images/squares/radical_concept_fails.png
Normal file
After Width: | Height: | Size: 3.5 KiB |
BIN
bingo_images/squares/radical_concept_works.png
Normal file
After Width: | Height: | Size: 3.4 KiB |
BIN
bingo_images/squares/red_flag.png
Normal file
After Width: | Height: | Size: 2.1 KiB |
BIN
bingo_images/squares/redbull_run_most.png
Normal file
After Width: | Height: | Size: 3.1 KiB |
BIN
bingo_images/squares/sandbagging.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
bingo_images/squares/silly_season.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
bingo_images/squares/square_template.xcf
Normal file
BIN
bingo_images/squares/team_hides_work.png
Normal file
After Width: | Height: | Size: 3.1 KiB |
BIN
bingo_images/squares/team_misnamed.png
Normal file
After Width: | Height: | Size: 2.2 KiB |
BIN
bingo_images/squares/torro_rosso_or_vcarb.png
Normal file
After Width: | Height: | Size: 2.8 KiB |
BIN
bingo_images/squares/williams_first_out.png
Normal file
After Width: | Height: | Size: 3.1 KiB |
BIN
bingo_images/squares/williams_top_speed_trap.png
Normal file
After Width: | Height: | Size: 3.5 KiB |