CS201: The tsapp library

We've made the decision to move from teaching pygame in the introductory Python course to using a custom-built library called tsapp. Here are the reasons for that change: 

  • The version of pygame used in the previous course was not entirely 1-to-1 with pygame as used outside of TechSmart: we fixed a few bugs and provided some abstractions (such as Sprites) through the tsk library. Because of this, it was decided to defer an in-depth look at how pygame is actually implemented, including more complex/low-level details, to a later point in the TechSmart course series to give students a more accurate look at pygame.
  • Feedback from students and teachers indicated that it would be a great benefit for student engagement to move graphical programs earlier in the course. By using an abstracted library and by introducing library calls earlier, we gain the ability to have students work with graphics before they are explicitly introduced to how to code graphics.

tsapp streamlines many of the processes of pygame that are confusing to students when first being introduced to graphics. By taking away a lot of extraneous, hyper-specific detail from the implementation syntax (to be covered later in the course), students and teachers are able to better focus on the large-scale concepts being taught, rather than esoteric formatting and syntax that they do not yet fully grasp.

Differences between tsapp and pygame

The primary new features gained via tsapp are as follows:

  • Creating a window object is simplified
  • Colors are provided as constants in the tsapp library (tuples and RGB colors are now introduced in Unit 5)
  • Animated sprites are created the same way as normal sprites and animate automatically as long as the sprite uses a spritesheet from the TechSmart built-in asset library
  • There is now a generic format for the main loop in a graphical program: while window.is_running:
  • Manually managing the event loop and the QUIT event has been removed
  • Manually drawing and updating sprites has been removed
  • Manually ticking the clock and flipping the display has been removed
  • Many frame-based actions have been consolidated into a single command, finish_frame, which should be called at the end of each loop iteration
  • Sprites are now created and added to the window at the top of the program, and do not have to be referenced inside the loop in order to be drawn
  • Manually adjusting sprite positions with a c.get_time() speed adjuster has been abstracted to simple speed variables
  • Text labels can be created with automatic text-wrapping and horizontal alignment

Sample program

Take a look at a simple program with the equivalent versions written in both pygame and tsapp.

pygame version

import tsk
import pygame
pygame.init()

window = pygame.display.set_mode([1018, 573])
c = pygame.time.Clock()

sprite_sheet = tsk.ImageSheet(“MyImageSheet.png”, 5, 6)
sprite = tsk.Sprite(sprite_sheet, 250, 250)
sprite_speed = 0.05

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
sprite.visible = False

sprite.x += c.get_time() * sprite_speed

    window.fill((255, 255, 255))
    sprite.draw()
    sprite.update(c.get_time())

c.tick(30)
pygame.display.flip()

tsapp version

import tsapp

window = tsapp.GraphicsWindow(1018, 573, tsapp.WHITE)
sprite = tsapp.Sprite(“MyImageSheet.png”, 250, 250)
window.add_object(sprite)
sprite.x_speed = 0.05

while window.is_running:
if tsapp.was_mouse_pressed():
     sprite.visible = False

window.finish_frame()

What's included in the tsapp library

tsapp provides the following functions, classes, and constants. Further details, including methods and attributes for each class, are available in Code Assist.

  • Color constants
  • Key constants
  • GraphicsWindow class
  • Sprite class
  • TextLabel class
  • Mouse and keyboard interaction functions
  • Timing functions
Was this article helpful?
3 out of 4 found this helpful