我怎样才能拥有不同的';菜单';在pygame中使用Python3.6而不使用一堆if语句?

我怎样才能拥有不同的';菜单';在pygame中使用Python3.6而不使用一堆if语句?,python,pygame,Python,Pygame,我正在创建一个名为“比特币点击器”的pygame项目,这是一个简单的游戏,您可以点击,然后接收比特币。然后,您可以转到交换“标签”将比特币兑换成货币,然后您可以购买升级版以挖掘比特币闲置 我在让它工作方面没有问题,但是,我已经编写了足够的程序,知道if语句的连续“阶梯”不是很有效。这是我的密码: import decimal import math import pygame pygame.init() width = 900 height = 600 frame_rate = 60 bl

我正在创建一个名为“比特币点击器”的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()
导入十进制
输入数学
导入pygame
pygame.init()
宽度=900
高度=600
帧速率=60
黑色=(0,0,0)
白色=(255,255,255)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
场景=0
gameDisplay=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“比特币点击器”)
clock=pygame.time.clock()
background=pygame.image.load('trump.png')
trumpImg=pygame.image.load('170907-wilson-trump-strise_h1fks0.png'))
比特币img=pygame.image.load('Bitcoin image.png')
dollarBillImg=pygame.image.load('100DollarBill.jpg')
pentiumImg=pygame.image.load('KL英特尔奔腾A80501.jpg')
美分=0
汇率=18901.83
单击_值=0.000001
奔腾_价格=.00001*汇率
被动_值=0.0
比特币=0.0
硬币x=(宽度/2-100)
硬币y=(高度/2-100)
def硬币(x,y):
blit(比特币img,(x,y))
def text_对象(字符串、字体):
textSurface=font.render(字符串、真值、白色)
返回textSurface,textSurface.get_rect()
def文本(字符串、x、y、大小):
largeText=pygame.font.font('freesansbold.ttf',size)
TextSurf,TextRect=text\u对象(字符串,largeText)
TextRect.center=(x,y)
blit(TextSurf,TextRect)
def clicker():
全球场景
全球比特币
mouse_x=pygame.mouse.get_pos()[0]
mouse_y=pygame.mouse.get_pos()[1]
x_平方=(鼠标x-硬币x-100)**2
y_平方=(鼠标y-硬币y-100)**2
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
如果math.sqrt(x_平方+y_平方)<100:
比特币+=点击价值
如果鼠标x<60且鼠标y<35:
场景=1
如果65=奔腾_价格:
被动_值+=.000001
美分-=奔腾价格
奔腾_价格*=奔腾_加价
游戏显示.blit(奔腾(75150))
b=十进制。十进制(str(奔腾价格))
文本(str(圆形(b,2)),125400,25)
文字(“车间”,宽度/2,25,25)
文本(“点击器”,35、15、18)
案文(“交换”,115、16、17)
def交换():
全球场景
全球比特币
全球分
mouse_x=pygame.mouse.get_pos()[0]
mouse_y=pygame.mouse.get_pos()[1]
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
如果鼠标x<60且鼠标y<35:
场景=1
如果65
有没有办法简化以下代码?特别是许多if语句。我从函数中删除了一些代码,以便于查看。(每个功能都是一个单独的“选项卡”,根据用户输入呈现不同的屏幕)

def clicker():
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
如果math.sqrt(x_平方+y_平方)<100:
比特币+=点击价值
如果鼠标x<60且鼠标y<35:
场景=1
如果65def 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