graphics

Classes concerned with the screen and drawing things on it.

class pyskool.graphics.Font(image, ink_key, paper_key)

The skool font.

Parameters:
  • image (Image) – The font image.
  • ink_key (RGB triplet) – The ink colour in font.png (used to create transparency).
  • paper_key (RGB triplet) – The paper colour in font.png (used to create transparency).
add_character(char, offset, width)

Define the location and width of a font character bitmap in the font image.

Parameters:
  • char – The font character.
  • offset – The offset (in pixels) of the font character bitmap from the left edge of the font image.
  • width – The width of the font character bitmap.
build_image()

Build the font image. This method is called after rescaling the screen or loading a saved game.

get_width(text)

Return the width (in pixels) of text when rendered at scale 1.

has_char(char)

Return whether the skool font contains a bitmap for a given character.

Parameters:char – The character to look for.
render(words, ink, paper)

Return an image (a pygame.Surface) of a text message written in the skool font.

Parameters:
  • words – The message.
  • ink (RGB triplet) – The desired ink colour.
  • paper (RGB triplet) – The desired paper colour.
class pyskool.graphics.Gallery(images_dir, image_set, scale, images)

A container for all the images used in a game.

Parameters:
  • images_dir – The path to the images directory.
  • image_set – The name of the set of images to use.
  • scale – The desired scale of the images.
  • images (dict) – Key-value pairs (image ID, path) from the Images section.
get_image(image_id)

Return an Image from the gallery, or None if there is no image in the gallery with the given ID. The image will be scaled up as necessary.

Parameters:image_id – The ID of the image.
get_surface(image_id)

Return an image (a pygame.Surface) from the gallery, or None if there is no image in the gallery with the given ID. The image will be scaled up as necessary.

Parameters:image_id – The ID of the image.
reset(scale)

Set the scale and clear the image cache.

Parameters:scale – The scale.
restore(images_dir)

Perform tasks required immediately after loading a saved game.

Parameters:images_dir – The path to the images directory.
scale_coords(coords)

Scale up a pair of coordinates and return them.

Parameters:coords – The coordinates.
scale_down()

Decrease the scale factor by 1 (if it is greater than 1). The image cache will be cleared.

Returns:True if the scale factor was decreased, False otherwise.
scale_length(length)

Scale up a length and return it.

scale_up()

Increase the scale factor by 1. The image cache will be cleared.

class pyskool.graphics.Image(gallery, image_id, surface, top_left=None, size=None, flipped=False, colorkey=None)

A container for a pygame.Surface. This is used to handle the saving and restoring of images, because a pygame.Surface cannot be pickled directly.

Parameters:
  • gallery (Gallery) – The gallery of images.
  • image_id – The ID of the image (the parent image) from which this image is derived.
  • surface (pygame.Surface) – The image itself.
  • top_left – The coordinates of the top-left corner of this image inside the parent image.
  • flipped – If True, the image will be flipped before use.
  • colorkey – The colour key.
blit(*args)

Blit a pygame.Surface onto this image.

Parameters:args – Arguments passed to pygame.Surface.blit().
build()

Build the image. This method is called after rescaling the screen or loading a saved game.

copy()

Return a copy of the image.

flip()

Return a flipped copy of the image.

get_height()

Return the width of the image.

get_size()

Return the size (width, height) of the image.

get_width()

Return the width of the image.

scale_blit(source, dest, area=None)

Draw an image on this image using scaled coordinates.

Parameters:
  • source (pygame.Surface) – The image to draw.
  • dest – The coordinates at which to draw the image.
  • area – A 4-tuple - (left, top, width, height) - defining the subsurface of the image to draw.
scale_length(length)

Scale up a length and return it.

set_colorkey(colorkey)

Set the colour key of the image.

Parameters:colorkey – The colour key.
subsurface(coords, size)

Return a subsurface of the image.

Parameters:
  • coords – The coordinates of the top-left corner of the subsurface.
  • size – The size of the subsurface.
class pyskool.graphics.Screen(config, gallery, title_prefix)

Represents the screen upon which everything is drawn.

Parameters:
  • config (dict) – Configuration parameters from the ini file.
  • gallery (Gallery) – The gallery of images to use for drawing.
  • title_prefix – The window title prefix.
add_font_character(char, offset, width)

Define the location and width of a font character bitmap in the font image.

Parameters:
  • char – The font character.
  • offset – The offset (in pixels) of the font character bitmap from the left edge of the font image.
  • width – The width of the font character bitmap.
contains(character, full=True)

Return whether a character is on-screen.

Parameters:
  • character (Character) – The character to check.
  • full – If True, return whether the character’s entire sprite is on-screen; if False, return whether any part of the character’s sprite is on-screen.
draw(skool_images, cast, speech_bubbles, update)

Draw everything on the screen.

Parameters:
  • skool_images – The play area images.
  • cast – The cast (3-tuples, (x, y, image), where x and y are the coordinates, and image is an image of a cast member).
  • speech_bubbles – Speech bubbles (3-tuples, (x, y, image)).
  • update – Whether to update the screen after drawing.
draw_menu(menu, refresh=False)

Draw the menu and update the screen.

Parameters:
  • menu (Menu) – The menu.
  • refresh – If True, the entire screen will be updated; if False, only the part of the screen occupied by the menu will be updated.
get_bubble(words, lip_pos, shift)

Create a speech bubble displaying a portion of a message.

Parameters:
  • words – The text of the message.
  • lip_pos – The offset (in tiles) from the left edge of the speech bubble at which to place the lip.
  • shift – The offset (in tiles) by which to shift the text image before displaying it in the bubble; if negative, leading spaces will be displayed.
Returns:

A 2-tuple, (bubble, done), where bubble is the speech bubble image (an Image), and done is True if the entire message has been spoken, False otherwise.

get_scroll_increment(x)

Return the direction in which the screen should be scrolled when Eric is at a given x-coordinate.

Parameters:x – Eric’s x-coordinate.
Returns:-1 if the screen should scroll right, 1 if it should scroll left, or 0 if it should not scroll.
get_text(words, ink, paper, transparent=True)

Return a pygame.Surface displaying some text in the skool font.

Parameters:
  • words – The text.
  • ink – The ink colour to use.
  • paper – The paper colour to use.
  • transparent – Whether the paper colour should be transparent.
get_text_width(text)

Return the width (in pixels) of text when rendered at scale 1.

has_font_char(char)

Return whether the skool font contains a bitmap for a given character.

Parameters:char – The character to look for.
initialise_column(skool_width, eric_x)

Set the leftmost column of the play area that will appear on the screen when the game starts.

Parameters:
  • skool_width – The width of the entire play area (in pixels).
  • eric_x – Eric’s initial x-coordinate.
print_inventory(item_images=())

Print the inventory. If no inventory is defined, nothing happens.

Parameters:item_images – A sequence of item images to draw in the inventory box.
print_lesson(*text_lines)

Print some text in the lesson box.

Parameters:text_lines – The lines of text to print.
print_message_box(x, y, message, ink, paper)

Print a message box.

Parameters:
  • x – The x-coordinate of the messenger’s head.
  • y – The y-coordinate of the messenger’s head.
  • message (tuple) – The lines of text to write in the box.
  • ink (RGB triplet) – The ink colour of the box.
  • paper (RGB triplet) – The paper colour of the box.
print_mice(count=0, mouse_image=None)

Print the mouse inventory. If no mouse inventory is defined, nothing happens.

Parameters:
  • count – The number of mice to draw.
  • mouse_image – An image of a captured mouse.
print_score_box(score, lines, hi_score)

Print the score, lines total and hi-score in the score box.

Parameters:
  • score – The score.
  • lines – The lines total.
  • hi_score – The hi-score.
reinitialise()

Reinitialise the screen for a new game.

scale_coords(coords)

Scale up a pair of coordinates and return them.

Parameters:coords – The coordinates.
scale_down()

Decrease the scale factor by 1 (if it is greater than 1), and redraw the screen.

scale_up()

Increase the scale factor by 1, and redraw the screen.

scroll(inc, skool, clock)

Scroll the skool a number of columns across the screen.

Parameters:
  • inc – The scroll increment (-1 to scroll one column at a time rightwards, 1 to scroll one column at a time leftwards).
  • skool (Skool) – The skool.
  • clock (pygame.time.Clock) – The clock to use to time the scrolling.
scroll_skool(skool, clock)

Scroll the skool across the entire width of the screen from right to left.

Parameters:
  • skool (Skool) – The skool.
  • clock (pygame.time.Clock) – The clock to use to time the scrolling.
setup(set_mode=False)

Set up the following things:

  • the window title and icon
  • the screen background
  • the logo image
  • the score box
  • (optionally) the size of the screen
Parameters:set_mode – If True, the size of the screen will be set.
take_screenshot(filename)

Take a screenshot and save it to a file.

Parameters:filename – The name of the file.
pyskool.graphics.FONT = 'FONT'

ID of the font image.

pyskool.graphics.INVENTORY = 'INVENTORY'

ID of the inventory items image.

pyskool.graphics.LESSON_BOX = 'LESSON_BOX'

ID of the lesson box image.

ID of the logo image.

pyskool.graphics.MESSAGE_BOX = 'MESSAGE_BOX'

ID of the message box image.

pyskool.graphics.MUTABLES = 'MUTABLES'

ID of the mutables image.

pyskool.graphics.MUTABLES_INK = 'MUTABLES_INK'

ID of the ink-only mutables image.

pyskool.graphics.MUTABLES_PAPER = 'MUTABLES_PAPER'

ID of the paper-only mutables image.

pyskool.graphics.SCOREBOX = 'SCOREBOX'

ID of the score box image.

pyskool.graphics.SKOOL = 'SKOOL'

ID of the play area image.

pyskool.graphics.SKOOL_INK = 'SKOOL_INK'

ID of the ink-only play area image.

pyskool.graphics.SKOOL_PAPER = 'SKOOL_PAPER'

ID of the paper-only play area image.

pyskool.graphics.SPEECH_BUBBLE = 'SPEECH_BUBBLE'

ID of the speech bubble image.

pyskool.graphics.SPRITES = 'SPRITES'

ID of the sprites image.

Previous topic

game

Next topic

iniparser