Docs › game library

game.window()

Open the game window: how wide it is, how tall it is, and what colour it is. It is the first thing every game does, right after import game.

How to write it

game.window(width, height, background="#0b1020")
width
How wide the window is, in pixels. Leave it out and you get 480. Decimals are rounded down to a whole number.
height
How tall the window is, in pixels. Leave it out and you get 360.
background
Optional. The colour behind everything. Use a colour name like "green" or a hex code like "#87ceeb" (sky blue). Leave it out and you get a dark night blue, "#0b1020".

It gives nothing back. Call it once, at the top, before you make any sprites.

A full example

A tall, phone-shaped window with a ball that bounces off all four walls. The width and height are kept in variables, so the walls are always in the right place, even if you change the size:

import game

W = 360                    # window width
H = 560                    # window height
game.window(W, H, background="#14213d")

ball = game.sprite("🏀", W / 2, H / 2, size=50)
info = game.label("Window: " + str(W) + " x " + str(H), W / 2, 24, color="#ffffff")

dx = 5                     # how far the ball moves sideways each frame
dy = 4                     # how far it moves up or down each frame

while game.playing():
    ball.x = ball.x + dx
    ball.y = ball.y + dy

    # Bounce off the left and right walls
    if ball.x < 25 or ball.x > W - 25:
        dx = -dx
    # Bounce off the top and bottom walls
    if ball.y < 25 or ball.y > H - 25:
        dy = -dy

    game.frame()

Try changing W and H to 600 and 300. The ball still bounces off every wall, because the code uses the variables instead of fixed numbers.

How the window works

  • The top-left corner is (0, 0). x grows to the right. y grows downward, so a bigger y is lower on the screen. The bottom-right corner is (width, height).
  • The middle is (width / 2, height / 2). In a 480 by 360 window that is (240, 180). A sprite you make without an x and y starts there.
  • The window stretches to fit the screen, but your numbers never change. A 480 by 360 game is still 480 by 360 inside, whether it is shown small on a phone or big on a whiteboard.
  • It starts the game fresh. It sets game.score() back to 0 and clears anything drawn with game.plot() or game.line().
  • Change the colour later with game.background("black"). Handy for a flash when the player gets hit, or for day and night.

Picking a size

  • 480 by 360 (the default) is a good size for most games on a computer.
  • Wide, like 800 by 400, suits racing and side-scrolling games.
  • Tall, like 400 by 640, fills a phone held upright. Add game.fullscreen() on the next line to fill the whole screen:
game.window(400, 640)
game.fullscreen()          # fills the screen, great on phones

Why isn't my window working?

  • The window is empty. Sprites are only drawn when game.frame() runs. A game with no loop and no game.frame() shows a blank window. See game.frame().
  • My sprite is missing. Check its x and y. In a 480 by 360 window, a sprite at (500, 100) is off the right edge, and one at (100, 400) is below the bottom.
  • Things go up when I wanted down. Remember that y grows downward. To move down, add to y.
  • The colour didn't change. Colours are text, so they need quotes: background="red", not background=red. Spell the word background in full, too.
  • I called it twice. Every call starts the window again. Keep one game.window() at the top and use game.background() to change the colour.