Make alistfor thesprites(images etc..) in our main game screen calledall_sprites. Think of lists like shopping lists!
Spritesare computer-generated, 2D images used in video games.
all_sprites = pygame.Sprite.Group()
Instancingwill produce that sprite onto the screen at any given time. You can think of instancing like parallel universes where different versions of you are sitting, jumping, swimming, etc.. at the same time but you can only see one version at a time.
Let’s add the clicker object to the game by naming itc…
c = Clicker()
…and then add it to the sprite group. Now our clicker image is in the list!
Now , let’s make a different list for the sprites we’ll use for our store and store items.
store_sprites = pygame.sprite.Group()
Change the names of your powerups by editing thisstringbut keep the quotation marks:
"INSERT NAME HERE"
This Boolean true/false statement will tell the computer if the game is running (on)…
running = True
…then it will run the followingwhile loopto play the game code. Remember to capitalize the T in True!
Let’s set the frame rate of our game.
We’ve set it to 60frames per second (fps). This higher the frame rate, the better the gaming quality. Imagine flipping a flip book slowly vs. quickly – which would be better? If you’ve ever experienced lagging during gameplay, a low fps may be to blame.
If the game is not running we should end this while loop using the following code:
if event.type == pygameQUIT:
running = False
Notice how QUIT is in all upper case. This is because it’s anEnum(enumeration) value, which are always in caps, and they represent things that never change. A QUIT event always means quit, nothing else.
Enumsare Python classes that are associated with unique, constant values.
Now we need to create an event for when the player clicks the mouse!
if event.type == pygame.MOUSEBUTTONDOWN:
What happens when our clicking clicked the clicker?
clicks += click_amount
Incrementingwith += makes coding easier and shorter. It stands forclicks = clicks + click_amount.
Incrementingadds to a variable. In this example we’re adding to the total amount of clicks, which their clicking powers, from the game start.
If the player wants to buy something from the store, the game needs to check if the player has enough points to spend. If yes, then the shop items should increase in cost upon purchase. Let’s code for that.