在pygame和Python3.6中,如果没有一堆if语句,我怎么能有不同的“菜单”?

2024-09-29 02:28:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个名为“比特币点击器”的pygame项目,这是一个简单的游戏,在这里你点击,然后接收比特币。然后你可以去一个交换“标签”把比特币换成钱,然后你就可以买升级版的比特币闲置

我在让它工作方面没有问题,但是,我已经编写了足够的程序,知道if语句的连续“阶梯”不是很有效。这是我的密码:

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()

有什么方法可以简化下面的代码吗?特别是许多if语句。我从函数中删除了一些代码,使其更易于查看(每个函数都是一个单独的“选项卡”,根据用户输入呈现不同的屏幕)

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

Tags: andtexteventgetiftypesceneglobal