0

I am creating a pygame project called 'bitcoin clicker', a simple game where you click and, in turn, receive bitcoin. You can then go to an exchange 'tab' to exchange bitcoin for money, then you can buy upgrades to mine bitcoin idle.

I am not having issues with getting it to work, however, I have programmed enough to know that continual 'stairs' of if statements are not very efficient. Here is my code:

import decimal
import math
import pygame

pygame.init()

width = 900
height = 600
frame_rate = 60

black = (0, 0, 0)
white = (255, 255, 255)

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

scene = 0
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Bitcoin Clicker")
clock = pygame.time.Clock()

background = pygame.image.load('trump.png')
trumpImg = pygame.image.load('170907-wilson-trump-tease_h1fks0.png')
bitcoinImg = pygame.image.load('Bitcoin image.png')
dollarBillImg = pygame.image.load('100DollarBill.jpg')
pentiumImg = pygame.image.load('KL Intel Pentium A80501.jpg')

cents = 0
exchange_rate = 18901.83
click_value = 0.000001
pentium_price = .00001 * exchange_rate
passive_value = 0.0
bitcoin = 0.0
coin_x = (width / 2 - 100)
coin_y = (height / 2 - 100)


def coin(x, y):
    gameDisplay.blit(bitcoinImg, (x, y))


def text_objects(string, font):
    textSurface = font.render(string, True, white)
    return textSurface, textSurface.get_rect()


def text(string, x, y, size):
    largeText = pygame.font.Font('freesansbold.ttf', size)
    TextSurf, TextRect = text_objects(string, largeText)
    TextRect.center = (x, y)
    gameDisplay.blit(TextSurf, TextRect)


def clicker():
    global scene
    global bitcoin

    mouse_x = pygame.mouse.get_pos()[0]
    mouse_y = pygame.mouse.get_pos()[1]

    x_squared = (mouse_x - coin_x - 100) ** 2
    y_squared = (mouse_y - coin_y - 100) ** 2

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if math.sqrt(x_squared + y_squared) < 100:
                    bitcoin += click_value
                if mouse_x < 60 and mouse_y < 35:
                    scene = 1
                if 65 < mouse_x < 155 and mouse_y < 30:
                    scene = 2

    text("Store", 30, 15, 20)
    text("Exchange", 115, 16, 17)
    coin(coin_x, coin_y)


def store():
    global cents
    global scene
    global pentium_price
    global passive_value

    pentium_markup = 1.1

    mouse_x = pygame.mouse.get_pos()[0]
    mouse_y = pygame.mouse.get_pos()[1]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if mouse_x < 60 and mouse_y < 35:
                    scene = 0
                if 65 < mouse_x < 155 and mouse_y < 30:
                    scene = 2
                if 75 < mouse_x < 300 and 150 < mouse_y < 375:
                    if cents >= pentium_price:
                        passive_value += .000001
                        cents -= pentium_price
                        pentium_price *= pentium_markup

    gameDisplay.blit(pentiumImg, (75, 150))
    b = decimal.Decimal(str(pentium_price))
    text(str(round(b, 2)), 125, 400, 25)
    text("Shop", width / 2, 25, 25)
    text("Clicker", 35, 15, 18)
    text("Exchange", 115, 16, 17)


def exchange():
    global scene
    global bitcoin
    global cents

    mouse_x = pygame.mouse.get_pos()[0]
    mouse_y = pygame.mouse.get_pos()[1]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if mouse_x < 60 and mouse_y < 35:
                    scene = 1
                if 65 < mouse_x < 155 and mouse_y < 200:
                    scene = 0
                if 200 < mouse_x < 700 and 200 < mouse_y < 409:
                    cents += bitcoin * exchange_rate
                    bitcoin = 0

    gameDisplay.blit(dollarBillImg, (width / 2 - 250, 200))
    text("Clicker", 100, 15, 18)
    text("Store", 30, 15, 20)


def game_loop():
    global bitcoin
    global scene
    global passive_value
    global pentium_price
    global cents

    running = True

    while running:
        bitcoin += passive_value/frame_rate
        gameDisplay.blit(background, (0,0))

        # clicker scene
        if scene == 0:
            clicker()

        # store scene
        if scene == 1:
            store()

        # exchange scene
        if scene == 2:
            exchange()

        a = decimal.Decimal(str(bitcoin))
        c = decimal.Decimal(str(cents))
        text("B " + str(round(a, 6)), width / 2, 100, 25)
        text("$ " + str(round(c, 2)), width / 2, 125, 25)
        pygame.display.update()
        clock.tick(frame_rate)


game_loop()
pygame.quit()
quit()

Is there any way I can simplify the following code? Specifically the many if statements. I deleted some code from the functions to make it easier to see. (each function is a separate 'tab' that renders a different screen based on user input)

def clicker():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if math.sqrt(x_squared + y_squared) < 100:
                    bitcoin += click_value
                if mouse_x < 60 and mouse_y < 35:
                    scene = 1
                if 65 < mouse_x < 155 and mouse_y < 30:
                    scene = 2


def store():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if mouse_x < 60 and mouse_y < 35:
                    scene = 0
                if 65 < mouse_x < 155 and mouse_y < 30:
                    scene = 2
                if 75 < mouse_x < 300 and 150 < mouse_y < 375:
                    if cents >= pentium_price:
                        passive_value += .000001
                        cents -= pentium_price
                        pentium_price *= pentium_markup


def exchange():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if mouse_x < 60 and mouse_y < 35:
                    scene = 1
                if 65 < mouse_x < 155 and mouse_y < 200:
                    scene = 0
                if 200 < mouse_x < 700 and 200 < mouse_y < 409:
                    cents += bitcoin * exchange_rate
                    bitcoin = 0
  • This is not inefficient. One way to improve efficiency is to have `elif` instead of some of the `if`'s, but the difference is negligible. – Ted Klein Bergman May 27 '18 at 08:11
  • For future references; if you got code that works but you just want some feedback, post the question on [codereview](https://codereview.stackexchange.com/) instead. – Ted Klein Bergman May 27 '18 at 08:15

0 Answers0