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 ifrandom.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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • DIY Scoreboard

    We look at a broadcast video system network that uses Python code to control a video router and check out another program that creates a scoreboard.

  • Panda3D

    Several free game engines are available for Linux users, but programming with them is often less than intuitive. Panda3D is an easy-to-use engine that is accessible enough for newcomers but still powerful enough for the pros at Disney Studios.

  • Top Coder

    Springtime is application time! Mike Schilli, who has experience with job application procedures at companies in Silicon Valley, digs up a question asked at the Google Engineering interview and explains a possible solution in Go.

  • Nerf Target Game

    A cool Nerf gun game for a neighborhood party provides a lesson in Python coding with multiple processors.

  • Tutorials – WM Tiling

    Regular window managers are so 2016 – install a tiling WM and work faster, smarter, and cooler.

comments powered by Disqus
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.

Learn More