CS201: The tsapp library

Follow

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

Comments

9 comments
  • Out of curiosity it mentions in the differences between tsapp and pygame that, "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" does this refer to being able to implement sprites from external sources?

    -1
    Comment actions Permalink
  • Is it possible that this module will be available on GitHub?
    I'm planning on making CsHelper, a python VSCode plugin that has techsmart modules.
    If it is available on GitHub, could you please post a link?

    -1
    Comment actions Permalink
  • im a furry

    -3
    Comment actions Permalink
  • @Lily Schaal
    Which school are you in?
    I hope it's not chinook because.. that will be.. not nice.

    0
    Comment actions Permalink
  • I'm in chinook
    what's wrong with chinook

    1
    Comment actions Permalink
  • also i was looking on how to make a sprite and i came i copied the work but idk on how to make it work all im getting is errors

    1
    Comment actions Permalink
  • the string file name for the fonts keeps giving me an error

    0
    Comment actions Permalink
  • That is great

    0
    Comment actions Permalink
  • One thing is though that pygame provides a significant performance boost over tsapp, and is essential to consider when coding in Techsmart. Listed below I have 2 simple programs, using the same assets and code, to test performance. It is a simple test on 2D collision

    Pygame
    #Importing our libraries. Without this, none of the code would work
    import tsk
    import pygame
    pygame.init()

    #Variables
    speed = 10
    playerx = 200
    playery = 100
    update_pre_pos = 1
    grounded = 0
    screen_width = 1000
    screen_height = 1000
    playing = 1
    pre_x = 0
    pre_y =100
    can_update = 1

    #Making a graphics window
    box = pygame.Surface((500,500))
    window = pygame.display.set_mode([800, 600])
    #Drawing a sprite
    t = tsk.Sprite("CarBlueSport2.png", 400, 300)
    t.scale = 0.5

    #Drawing a Box
    enviorment = tsk.Sprite("PlatformWood.png", 200, 300)
    enviorment.scale = 0.5

    #Infinite Loop
    while playing:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    playing = False

    can_update = 1

    if pygame.sprite.collide_rect(t, enviorment):
    can_update = 0
    t.x = pre_x
    t.y = pre_y

    if (can_update == 1):
    pre_x = t.x
    pre_y = t.y

    t.draw()
    enviorment.draw()
    pygame.display.flip()
    window.fill((255, 255, 255))


    if tsk.is_key_down(pygame.K_UP):
    #print("Pressing the up arrow key!")
    t.y = t.y -speed


    if tsk.is_key_down(pygame.K_DOWN):
    #print("Pressing the up arrow key!")
    t.y = t.y +speed


    if tsk.is_key_down(pygame.K_RIGHT):
    #print("Pressing the up arrow key!")
    t.x = t.x +speed


    if tsk.is_key_down(pygame.K_LEFT):
    #print("Pressing the up arrow key!")
    t.x = t.x -speed

    Tsapp

    import tsapp

    #Variables
    prex = 0
    prey = 0
    speed = 200
    update_pre_pos = 1
    grounded = 0

    #Making a graphics window
    window = tsapp.GraphicsWindow(800, 600, tsapp.WHITE)

    #Adding the player
    player = tsapp.Sprite("CarBlueSport2.png", 400, 300, 0.5)
    window.add_object(player)

    #Adding the platforms
    platforms = tsapp.Sprite("PlatformWood.png", 200, 300, 0.5)
    window.add_object(platforms)

    #Infinite loop so the program dosent end.
    while window.is_running:
    window.finish_frame()

    #Sets preposition to be updated
    update_pre_pos = 1


    #Checks if the player is touching any platforms
    if player.is_colliding_rect(platforms):
    #Disables updating pre positions
    update_pre_pos = 0
    player.x = prex
    player.y = prey

    #Updates pre positions if conditions are met
    if (update_pre_pos == 1):
    prex = player.x
    prey = player.y
    grounded = 0

    #player.y_speed = speed

    #Moving Up
    if tsapp.is_key_down(tsapp.K_UP):
    player.y_speed = -speed



    if tsapp.was_key_released(tsapp.K_UP):
    player.y_speed = 0

    #Moving Dpwm
    if tsapp.is_key_down(tsapp.K_DOWN):
    player.y_speed = speed


    if tsapp.was_key_released(tsapp.K_DOWN):
    player.y_speed = 0


    #Moving Left
    if tsapp.is_key_down(tsapp.K_LEFT):
    player.x_speed = -speed


    if tsapp.was_key_released(tsapp.K_LEFT):
    player.x_speed = 0


    #Moving Right
    if tsapp.is_key_down(tsapp.K_RIGHT):
    player.x_speed = speed

    if tsapp.was_key_released(tsapp.K_RIGHT):
    player.x_speed = 0

    The result? the pygame version using TST provides a higher performance then the TSAPP version. But both are great tools and are awesome learning tools. In the end, I would recommend Pygame.

    1
    Comment actions Permalink

Please sign in to leave a comment.