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
Current Events Tampa Bay

What’s happening in the Tampa Bay tech/entrepreneur/nerd scene (Week of Monday, February 22, 2021)

Believe it or not, the end of this week is also the end of the month. Here’s your list of tech, entrepreneur, and nerd events for Tampa Bay and surrounding areas for the week of Monday, February 22 through Sunday, February 28, 2020.

This is a weekly service from Tampa Bay’s tech blog, Global Nerdy! For the past four years, I’ve been compiling a list of tech, entrepreneur, and nerd events happening in Tampa Bat and surrounding areas. There’s a lot going on in our scene here in “The Other Bay Area, on the Other West Coast”!

By “Tampa Bay and surrounding areas”, this list covers events that originate or are aimed at the area within 100 miles of the Port of Tampa. At the very least, that includes the cities of Tampa, St. Petersburg, and Clearwater, but as far north as Ocala, as far south as Fort Myers, and includes Orlando and its surrounding cities.

For the time being, I’m restricting this list to online events. We’re still deep in a pandemic, and the way out is to stop the spread, however we can. In the age of broadband internet, smartphones, and social media, it’s not that hard. Stay home, stay safe, stay connected, and #MakeItTampaBay!

Monday, February 22

Tuesday, February 23

Wednesday, February 24

Thursday, February 25

Friday, February 26

Saturday, February 27

Sunday, February 28

Do you have any events or announcements that you’d like to see on this list?

Let me know at joey@joeydevilla.com!

Join the mailing list!

If you’d like to get this list in your email inbox every week, enter your email address below. You’ll only be emailed once a week, and the email will contain this list, plus links to any interesting news, upcoming events, and tech articles.

Join the Tampa Bay Tech Events list and always be informed of what’s coming up in Tampa Bay!


Categories
Programming

On the Auth0 blog: How to read, edit, and erase location and other EXIF metadata from your photos

Auth0 logoMy latest article on the Auth0 blog, How to Read and Remove Metadata from Your Photos With Python, shows you how to use Python and the exif module to examine, alter, and even remove the metadata that your phone adds to pictures you take.

In addition to picture data, photos taken with smartphones and modern digital cameras contain metadata, which is additional information about the photo. This metadata is stored in a format called EXIF, which is short for EXchangeable Image File format, which is a continually evolving standard for information added to digital image and sound recordings.

In photos, EXIF can include information such as:

  • The dimensions and pixel density of the photo
  • The make and model of the device used to take the photo
  • Zoom, aperture, flash, and other camera settings when the photo was taken
  • The orientation of the device when the photo was taken
  • When the photo was taken
  • Where the photo was taken
  • Which direction the camera was facing
  • The altitude at which the photo was taken

My article will show you how to use Python’s exif module to access this information, as well as how to alter it (I show you how to tag your photos so it seems as if they were taken at Area 51) or erase it.

EXIF data was recently in the spotlight as a result of the January 6th riots in Washington, DC. Many of the rioters posted photos to Parler, which did not strip EXIF data from photos uploaded to it.

When Parler started to shut down as a result of Amazon and other providers kicking them off their services, it opened some security holes that a hacktivist who goes by the handle @donk_enby was able to exploit. They were able to scrape the posts and uploaded photos and videos and upload them to the Internet Archive. Soon after, it was discovered that Parler never removed the EXIF data from the photos and videos, which made it possible to easily identify participants in the riot, see who broke into the Capitol, and for authorities to make arrests. The New York Times used this data to make a timeline of events, which they published in their article, How a Presidential Rally Turned Into a Capitol Rampage.

Graphic from the New York Times. Tap to view at full size.

While Parler’s sloppy security was by and large good news, there’s still good reason to follow good security practices, and part of that is managing EXIF data in photographs. That’s what my article covers, and in a fun way as well!

Read the article on the Auth0 blog: How to Read and Remove Metadata from Your Photos With Python.

 

Categories
Current Events Programming

Global Nerdy’s mega-list of online tech events for Tampa and beyond (Tuesday, February 16, 2021)

This is an experiment!

Creative Commons image from Alpha Stock Images and taken by Nick Youngson. Tap to view the source.

Each week, I publish a listing of tech, entrepreneur, and nerd online events by and for people in Tampa Bay and surrounding areas. But what if I tried expanding the scope and listing online events beyond Tampa Bay?

That’s the subject of this week’s experiment. Each weekday this week, I’m going to provide a list of:

  • the day’s tech, entrepreneur, and nerd online events for Tampa Bay and surrounding areas, and
  • a selection of the day’s tech events for the rest of the world.

Let’s face it — with COVID-19 still raging away, new variants of the disease popping up, and vaccines being slow in coming, we need to step up our online connection game. The way to do that is to look at tech events from beyond our backyard, see what other people are talking about, learn from them, and take those lessons back here to “The Other Bay Area!”

I’ve listed the events below. Check them out, and let me know if this sort of list is interesting or useful to you.

Tuesday’s ONLINE tech, entrepreneur, and nerd events in the Tampa Bay area

Here’s the list of Tuesday’s online events for the Tampa Bay area, taken straight from my larger list of this week’s Tampa Bay tech, entrepreneur, and nerd events.

Tuesday’s ONLINE tech events worldwide

…and here’s a selection of online tech events taking place across the country and around the world. I have no idea if any are limiting attendance to locals, but I suggest you drop in and find out!

Categories
Current Events

Even more online tech events happening this afternoon and evening (Monday, February 15, 2021)

If you can’t find the kind of event you’re looking for in my weekly list, perhaps you’ll find something in this list of events taking place tonight!

Categories
Current Events Tampa Bay

What’s happening in the Tampa Bay tech/entrepreneur/nerd scene (Week of Monday, February 15, 2021)

It’s hard to believe that’s it’s the middle of February already! Here’s your list of tech, entrepreneur, and nerd events for Tampa Bay and surrounding areas for the week of Monday, February 15 through Sunday, February 21, 2020.

By “Tampa Bay and surrounding areas”, this list covers events that originate or are aimed at the area within 100 miles of the Port of Tampa. At the very least, that includes the cities of Tampa, St. Petersburg, and Clearwater, but as far north as Ocala, as far south as Fort Myers, and includes Orlando and its surrounding cities.

For the time being, I’m restricting this list to online events. We’re still deep in a pandemic, and the way out is to stop the spread, however we can. In the age of broadband internet, smartphones, and social media, it’s not that hard. Stay home, stay safe, stay connected, and #MakeItTampaBay!

Monday, February 15

Tuesday, February 16

Wednesday, February 17

Thursday, February 18

Friday, February 19

Saturday, February 20

Sunday, February 21

I’ve running an event on Monday, February 22!

My Programmers of Portables Meetup will have its first session on Monday, February 22nd at 7:00 p.m.. We’ll be using Python and Pygame to write a simple game about the “Super Bowl Streaker”. Want to know more? Check out the Meetup page!

Do you have any events or announcements that you’d like to see on this list?

Let me know at joey@joeydevilla.com!

Join the mailing list!

If you’d like to get this list in your email inbox every week, enter your email address below. You’ll only be emailed once a week, and the email will contain this list, plus links to any interesting news, upcoming events, and tech articles.

Join the Tampa Bay Tech Events list and always be informed of what’s coming up in Tampa Bay!


Categories
Current Events Programming Tampa Bay

“Programmers of Portables” Meetup on Monday, Feb. 22: Let’s write a game!

It’s high time for the Programmers of Portables Meetup group’s first meeting. It’s called “Let’s write a game (with Python and Pygame)!”, and it’s happening online on Monday, February 22nd at 7:00 p.m.!

In this meetup, we’ll use the Python programming language and the Pygame collection of game development libraries to write a game similar to this “Super Bowl Streaker”-themed parody of the old videogame Tecmo Bowl:

That’s right, we’re going to build a game where the player controls the guy who ran onto the field in a pink leotard during the Super Bowl, and the goal of the game is to dodge security for as long as you can.

While writing the game, you’ll learn about key game programming concepts including the main event loop and collision detection, as well as everyday programming topics including object-oriented programming and data structures.

This is going to be a “code along with the instructor” exercise. I’ll explain a key concept for the game, and then we’ll code it together. By the end of the session, you’ll have a working game that you can play but even modify and improve on!

Register here to join us on Monday, February 22nd at 7:00 p.m., and let’s write a game!

What will you need for the Programmers of Portables Meetup?

Compaq 610 laptop
Even this decade-plus-old hardware (which I own) will do.

Aside from a computer (which could run macOS, Windows, Linux, or Raspberry Pi OS) made sometime in the past ten or so years and an internet connection, you’ll need:

  • Zoom (I’ll provide a link to the Meetup as the day gets closer)
  • Python 3 (version 3.6 or later — I recommend Anaconda’s distribution, although the Python.org distribution is also good)
  • Pygame (version 2.0; once you’ve installed Python 3, go to the command line and execute the command pip install pygame==2.0.0)
  • You favorite code editor. I’ll be using Visual Studio Code.

All you need for this session is just some programming experience, and it doesn’t have to be in Python. If you can code a “Magic 8-Ball” in JavaScript, you’ll understand most of what we’ll cover when writing our game.