Categories
Programming

Code, notes, and recording from the “Programmers of Portables” Meetup, February 22, 2021

What happened at the first Programmers of Portables meetup?

The first Programmers of Portables meetup took place last night, and we made our first steps towards making our first videogame. We met over Zoom, where I shared my screen and led the group in a “code along with me” exercise as we started writing a simple videogame from scratch.

This article covers what we did last night, complete with the code that we wrote. If you were there, you can use this for review. If you weren’t, you should still be able to look at what we did and follow along.

This article is primarily a collection of the code we wrote and the recording of the session. In later articles, I’ll go over Pygame programming in more detail. In the meantime, if you’ve like to learn more about Pygame, here are a couple of resources:

Prerequisites

The first part of the session was devoted to downloading and installing the prerequisites for writing videogames with Python.

A code editor (such as Visual Studio Code)

Logo: Visual Studio Code

Any application that calls itself a code editor will do.

I tend to use Visual Studio Code these days, because I’ve already done my time using earlier versions of vim (in the late ’80s, I used a variant called ivi, short for “improved vi”) and Emacs (back when the joke name was “Eight megs and constant swapping”). VS Code is pretty much the same across all the platforms I use — macOS, Windows, Linux, and Raspberry Pi OS — and it feels like a present-day app, and not leftovers from the 1970s.

You can download Visual Studio Code here.

A distribution of Python 3 (such as Anaconda Python)

Logo: AnacondaWe’re programming in Python (preferably Python 3.7 or later), so any reasonably recent Python distribution will do.

I like the Anaconda Python distribution because iy includes a lot of useful libraries and other tools that you’ll need when using Python for things such as data science, and the experience is pretty much the same across macOS, Windows, and Linux.

You can download Anaconda Python here.

Pygame 2

The final prerequisite is Pygame, a cross-platform set of packages that supports game development in Python. It’s been around for 20 years (its was first release in the fall of 2000), and it’s a fun, fantastic 2D game programming platform.

To install it, you’ll need to go to the command line:

  • macOS and Linux users: Open a terminal and enter the command pip install pygame
  • Windows users using Anaconda Python: Open the Start Menu, select the Anaconda Python folder, and run Anaconda Command Prompt, where you’ll enter the command pip install pygame

The first version: A blank black screen

With the prerequisites gathered and installed on our computers, it was time to start working on the game. We worked on it in steps, each time producing an improved version of the game.

The first version of the game wasn’t terribly impressive, as it ended up being a blank black window that did nothing. Still, it was a working program, and the code we wrote would function as a framework on which we’d eventually build the rest of the game:

Here’s its code:

# The first version of the game:
# An 800-by-600 black window
# (Don’t worry; it gets better)

import pygame

# Constants
# =========

# Screen dimensions and refresh rate
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FRAMES_PER_SECOND = 60

# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)


# Initialization
# ==============
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()


# Game loop
# =========
running = True

while running:
    # This method should be called once per frame.
    # It calculates the number of milliseconds since the last
    # call to clock.tick() in order to limit the game’s framerate
    # to a maximum of FRAMES_PER_SECOND.
    clock.tick(FRAMES_PER_SECOND)

    # Handle events
    for event in pygame.event.get():
        # Check to see if the user has closed the window
        # or hit control-c on the command line
        # (i.e. Has the user quit the program?)
        if event.type == pygame.QUIT:
            running = False

    # Draw game objects to the screen surface
    screen.fill(BLACK)

    # Update the screen with the contents of the screen surface
    pygame.display.flip()

# Exit the game
pygame.quit()

The second version: A rightward-moving green square

The second version of the game built upon the code from the first, and was slightly more impressive. It featured an actual image on the screen, complete with animation: a green square, travelling from left to right across the screen, and “wrapping around” back to the left side after it disappears from the right side of the screen.

Here’s its code:

# The second version of the game:
# An 800-by-600 black window,
# now with a rightward-moving green square!

import pygame

# Constants
# =========

# Screen dimensions and refresh rate
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FRAMES_PER_SECOND = 60

# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)


# Sprites
# =======

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)

    def update(self):
        self.rect.x = self.rect.x + 5
        if self.rect.left > SCREEN_WIDTH:
            self.rect.right = 0


# Initialization
# ==============

# Initialize screen and framerate
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

# Create sprites and sprite groups
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)


# Game loop
# =========
running = True

while running:
    # This method should be called once per frame.
    # It calculates the number of milliseconds since the last
    # call to clock.tick() in order to limit the game’s framerate
    # to a maximum of FRAMES_PER_SECOND.
    clock.tick(FRAMES_PER_SECOND)

    # Handle events
    for event in pygame.event.get():
        # Check to see if the user has closed the window
        # or hit control-c on the command line
        # (i.e. Has the user quit the program?)
        if event.type == pygame.QUIT:
            running = False

    # Update the game state
    all_sprites.update()

    # Draw game objects to the screen surface
    screen.fill(BLACK)
    all_sprites.draw(screen)

    # Update the screen with the contents of the screen surface
    pygame.display.flip()

# Exit the game
pygame.quit()

The third version: The green square, now under user control!

The final version of the game was one where we made the green square interactive. Instead of continuously travelling from left to right on the screen, the square stays put until the user presses one of the arrow keys. When that happens, the square moves in the appropriate direction. The square is constrained so that it can’t go offscreen.

Here’s its code:

# The third version of the game:
# The green square, now under user control!

import pygame

# Constants
# =========

# Screen dimensions and refresh rate
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FRAMES_PER_SECOND = 60

# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)


# Sprites
# =======

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)

    def update(self):
        # Get the state of all the keys
        keys = pygame.key.get_pressed()

        # Check the retrieved state to see if any
        # arrow keys have been pressed
        # =======================================

        # Is the user pressing the left-arrow key,
        # and is the sprite’s left edge NOT flush
        # with the screen’s left edge?
        if keys[pygame.K_LEFT] and self.rect.x > 0:
            self.rect.x = self.rect.x - 5

        # Is the user pressing the right-arrow key,
        # and is the sprite’s right edge NOT FLUSH
        # with the screen’s right edge?
        if keys[pygame.K_RIGHT] and self.rect.x < SCREEN_WIDTH - self.rect.width:
            self.rect.x = self.rect.x + 5

        # Is the user pressing the up-arrow key,
        # and is the sprite’s top edge NOT FLUSH
        # with the screen’s top edge?
        if keys[pygame.K_UP] and self.rect.y > 0:
            self.rect.y = self.rect.y - 5

        # Is the user pressing the down-arrow key,
        # and is the sprite’s bottom edge NOT FLUSH
        # with the screen’s bottom edge?
        if keys[pygame.K_DOWN] and self.rect.y < SCREEN_HEIGHT - self.rect.height:
            self.rect.y = self.rect.y + 5


# Initialization
# ==============

# Initialize screen and framerate
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

# Create sprites and sprite groups
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)


# Game loop
# =========
running = True

while running:
    # This method should be called once per frame.
    # It calculates the number of milliseconds since the last
    # call to clock.tick() in order to limit the game’s framerate
    # to a maximum of FRAMES_PER_SECOND.
    clock.tick(FRAMES_PER_SECOND)

    # Handle events
    for event in pygame.event.get():
        # Check to see if the user has closed the window
        # or hit control-c on the command line
        # (i.e. Has the user quit the program?)
        if event.type == pygame.QUIT:
            running = False

    # Update the game state
    all_sprites.update()

    # Draw game objects to the screen surface
    screen.fill(BLACK)
    all_sprites.draw(screen)

    # Update the screen with the contents of the screen surface
    pygame.display.flip()

# Exit the game
pygame.quit()

The recording

Here’s the recording of the session:

Categories
Uncategorized

Windows Phone News: Life of the Party, Petzold’s Book Updated, Glow Air Hockey

The Life of the Party

life of the party

Last night, I attended my friend Alexa Clark’s photo exhibit, which took place at Camaraderie, a downtown Toronto coworking space for indies and entrepreneurs. This sort of event at this sort of location tends to bring out techies with an interest in design and designers with a technical bent, and they all came up to me with the same question: So, do you have one of those Windows phones?

I pulled out my Samsung “Taylor” phone, let people take try it out, and here were the responses:

  • “I like the main screen. Less cluttered than my iPhone’s.”
  • (Flipping through the People hub) “That’s a great way to organize pages!”
  • (Seeing my wife’s tile on the Start screen, pinned to the top) “So people can have their own icon right on the home page? That’s great!”
  • (Seeing the way pages animate as you navigate) “That’s a nice touch.”
  • (On the overall design) “This is a Microsoft phone?”
  • “Love the clean look.”

Here’s what Wayne Lee, one of the people behind Camaraderie, tweeted about the phone:

bunnyhero tweet

Programming Windows Phone 7 Updated

programming windows phone 7

Charles Petzold – the guy who literally wrote the book on Windows programming – is working away on Programming Windows Phone 7 and has released another free preview version of the book. The first preview had 6 chapters; this latest version weighs in at 11 chapters and 265 pages.

Here’s the current table of contents:

  • Part I: The Basics
    • Chapter 1   Hello, Windows Phone 7
    • Chapter 2   Getting Oriented
    • Chapter 3   An Introduction to Touch
    • Chapter 4   Bitmaps, Also Known as Textures
    • Chapter 5   Sensors and Services
    • Chapter 6   Issues in Application Architecture
  • Part II: Silverlight
    • Chapter 7   XAML Power and Limitations
    • Chapter 8   Elements and Properties
  • Part III: XNA
    • Chapter 20   Principles of Movement
    • Chapter 21   Textures and Sprites
    • Chapter 22   Touch and Play

This preview is free-as-in-beer, and the final ebook, which comes out October 28th, will also be free-as-in-beer!

Get your hands on the book and associated downloads:

Windows Phone 7 Game: Glow Air Hockey

Glow Air Hockey is a game Social Indulgence that lets you play air hockey game against the phone or a friend. There’s a little more information about the game at Glow Air Hockey’s page.

(In the video above, the red dots are the players’ finger positions as shown in the emulator; you won’t see them when you’re actually playing the game on the phone.)

This article also appears in Canadian Developer Connection.