πŸ–±οΈ Coding the Actual Clicker and Shop Items | myBetabox

πŸ–±οΈ Coding the Actual Clicker and Shop Items

Keyboard Guide

Coding the Actual Clicker and Shop Items

The Code Explained

Classes allow us to give properties, or traits, to objects within those classes. For example, if you had

A class of perfect snacking fruit...
...and those class properties were: red, sweet, and small...
...thenΒ objects that fit could be cherry, strawberry, raspberry (definitely not banana, lime, or watermelon).

This line of code sets the size of the image we want to use. You can change the 300 numbers to anything you want but we find 300 works nicely!

self.image = pygame.transform.scale(clicker_img, (300, 300))

Now let’s center our image on the screen. We use WIDTH / 2 because that gives us the halfway mark, or the center. If you took a string, folded in half and pinched one end, you’d see that is the center of the string!

self.rect.centerx = int(WIDTH / 2)

When using division operators in Python, we need to use integers andΒ int will convert the WIDTH / 2 number into an integer for us.

Let’s do the same for the vertical center.

self.rect.centery = int(HEIGHT / 2)

Make sure to create a rectangle for our image like we did for our text…

self.rect = self.image.get_rect()

…as well as positioning it. This will place our shop icon in the top left of the screen.

self.centerx = x
self.rect.bottom = y

Now it’s time to set up the bonus clicker criteria and the clicker cost to upgrade.

self.cost = cost
self.bonus = bonus
self.name = name

We use self. because if our shop has multiple items available, we can code for each specific item.