Program a game of bingo with ReportLab and Panda3D for Python
titles and freeSpace Functions
The header row of each card is the first place a font is needed, so the titles
section starts with self.doc.setFont
in line 33, asking ReportLab to use a font previously loaded – on line 9 in this case. The first parameter is the internal name of the font to be used (as provided when it was loaded), and the second parameter is its size.
For each self.doc.drawString
, you provide the x and y coordinates and the text to be drawn. In this case, BINGO, with extra spaces between the letters so the font will line up inside the grid. This command is repeated four times, once for each card.
The freeSpace
function (lines 54-59) works exactly the same as titles
. Only the parameters are different, in order to label each center square "FREE"
.
The makeCard Function
The makeCard
function doesn't actually draw the card; rather, it creates the list of numbers in the appropriate ranges for each column. Line 40 initializes card
as a list
, then line 41 sets up a loop that runs 25 times, once for each square in the grid. The next line starts an infinite loop until lines 43-47 find a number not currently on the card.
Each if
… random.randint
line picks numbers in the right range for each column. Line 49 checks that the number is not on the card already, adds it to the list (line 50), and then exits the infinite loop (line 51). Finally, line 52 returns card
, which now has a unique set of numbers for the bingo card.
The numbers Function
The numbers
function draws the cards to the document. To begin, it calls self.makeCard
four times to get the numbers for each card. Then, self.doc.setFont
(line 66) assigns the font to be used to draw the numbers.
Line 68 sets y = 9.05
. The origin of the document is the lower left of the page, so this location is near the top.
On line 69, the for
loops over a range of 25
, one for each space on the grid. If i == 12
(line 70), then this is the free space. All you do is move down to the next square (line 71) then continue
with the next iteration of the loop (line 72).
Lines 74-78 determine the x
offset for the column. This value will be added to a hard-coded x value for each of the four cards on the page.
Lines 80 and 81 handle character spacing. If the index i
is less than 10
, the number on the card will be a single digit. In this case, spacing
is set to .15
so the number is centered; otherwise, the number is two digits, so spacing
is
.
Line 82 uses drawString
with the calculated x and y positions to fill in the card with the number. Lines 80-82 are reproduced for each of the four cards in lines 84-94.
Line 96 moves the y
value down for the next row, and line 97 checks to see if the bottom of a column has been reached. If so, then y
is reset to the top of the page.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.