πŸ‘©πŸ½β€πŸ’» Adding to the Code Template | myBetabox

πŸ‘©πŸ½β€πŸ’» Adding to the Code Template

Keyboard Guide

Adding to the Code Template

The Code Explained

Import the code template into Repl.it (using copy/paste) from the previous section.

Create a new import statements to use the Pygame package and images for our game in Repl.it!

import pygame
from os import path

Next, set up our game screen size dimensions.

WIDTH = 800
HEIGHT = 600

Notice how we use all caps for these variable names. This is because WIDTH and HEIGHT are constant variables.Β Throughout the game, the screen size won’t change so we’ll use variables whose values won’t change.

🌟 Constant variables are variables where the value doesn’t change. Python syntax for constants are to use all caps. Normally, variables store information where the values do change but in this case they do not.

Code an initialization function to be able to run Pygame during gameplay.

pygame.init()

🌟 Initialization functions allow something to run in a program. The syntax for functions is to use () are the end of the name.

If you’d like to change the title of your name, replace the words PUT TITLE HERE in this string between the quotation marks.

pygame.display.set_caption('PUT TITLE HERE')

🌟 Strings are characters, letters, spaces, and symbols in between quotation marks. Think of them like sentences you want your game to show on the screen.

Coding the in-game clock is setting up theΒ frames per second (fps). This is how fast the images in the game refresh to keep up to date with gameplay. Think about it like how fast flip book pages turn to create a moving picture.

clock = pygame.time.Clock()

Clock() is capitalized because it’s anΒ object and Python syntax for object names is to have the first letter be in uppercase and followed by ().

When the game begins, the number of clicks should be set to 0 because we haven’t started playing yet! Create aΒ variable and set the value to 0.

clicks = 0

Next, we have to code how many points each click is worth. Let’s set up another variable and give each click 1 point.

click_amount = 1

We haven’t uploaded any pictures yet for our background so we don’t have to worry about the filename just yet. We can, however,Β scale the size of the image we’d use soon right now so that it’ll fit in the game. Use theΒ tupleΒ (WIDTH, HEIGHT) or (800, 600).

background = pygame.transform.scale(background, (WIDTH, HEIGHT))

🌟 To scale an image means to resize the height and the width in the same proportions when you make it bigger or smaller.

Everything in Pygame, including images and text, needs a rectangle ‘placemat’ to see if two things collide or interact with each other.

bg_rect = background.get_rect()

Finally, we’ll come back to the ‘INSERT FILENAME HERE’ sections later on when we upload our game images!