Software Developer and the Holy Grail: Part 6 (Attack of the Keydowns)

Andrew Alteri
3 min readMay 11, 2021

Last week on Software Developer and the Holy Grail…I went over the initial setup for Pygame and how to add basic styling to your player window. Today, I will go over adding a playable ship and how to make that ship move with user key presses. Here is my code up to this point in the game if you want to follow along.

There are three main components to creating a playable ship in a Space Invaders clone. You want to draw the ship, then have the ship move on keyboard presses, and then create a simple loop for checking the boundaries of the player screen. Let us start with simply drawing the character. Similar to adding an icon to our player screen, we will use the pygame.image.load(‘name of cool starship picture.png’) method to add our starship image to our Pygame. You can add any picture that you want, but make sure to resize the image to about 64 pixels unless you want to add the Death Star as a playable ship. (Btw, I totally support that decision but it will most likely make for a pretty bad Space Invader clone.) Next, we want to set a starting player position on the x and y axis. The x axis will have to be somewhere between 0 and 800, and the y axis will have to be somewhere between 0 and 600. Set those numbers to constant variables like this, PLAYERY = 500. Also set a variable for the movement of the player and set it to 0. The last step is to define a player method that when called will draw the player’s ship at the parameters given to the method. It will look something like this…

def player(x, y):

screen.blit(playerImg, (x, y))

A video game is not really a video game unless a user or player can interact with the application. In our example, we are going to make the starship move horizontally on the bottom of the player screen. I know, I know, you want have your character move like in Starfox, but one step at a time. All we need to do is create a simple loop that checks what key was pressed and move the ship left or right depending on that key.

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_a:

playerX_move = -.4

if event.key == pygame.K_d:

playerX_move = .4

if event.type == pygame.KEYUP:

if event.key == pygame.K_a or event.key == pygame.K_d:

playerX_move = 0

playerX += playerX_move

In this loop, if the player presses the ‘a’ key then the starship moves -.4 pixels or left, and if the player presses the ‘d’ key then the starship moves .4 pixels or right. As soon as the player takes their finger off the ‘a’ or ‘d’ key, then the starship stops. So basically, the starship will continue to move right or left as long as the player is holding down the ‘a’ or ‘d’ key.

Great now the ship can move and get ready for battle! Oh no! The player never took their finger off the ‘d’ key and the spaceship flew off into infinite space. Since we do not have any boundaries set yet, the player’s ship will never stop at the borders of the screen. Let us create a short loop that checks for boundaries.

if playerX <= 0:

playerX = 0

elif playerX >= 736:

playerX = 736

This will check for the screen edges and stops the player’s starship from going beyond those borders. Why 736 and not 800? If the starship image is 64 pixels, then you want the ship to stop before part of it goes outside the screen.

Call the player(playerX, playerY) function and we are all ready to fly! Come back next time for the exciting conclusion to the Pygame prequel where I add an enemy spaceship. Revenge of the PyInvader!

--

--