上一页中的按钮仍然可以点击

时间:2018-10-20 21:31:24

标签: python pygame click mouse tic-tac-toe

我正在使用python中的pygame模块编写一个简单的井字游戏,以获取该语言的基础知识。在编码时,我遇到了一些奇怪的事情:游戏中有四种状态:    1)开始屏幕:此处有两个按钮:“玩家vs玩家”和“玩家vs cpu”    2)“播放器与CPU”屏幕:这里有四个按钮:简单,中等,困难,专家    3)游戏画面:游戏板将在此处    4)最终屏幕:这里有一个按钮可以返回主菜单

问题是当我进入开始屏幕并单击“ player vs cpu”按钮时,它将状态更改为“ Player vs cpu”屏幕。但是,由于此状态下的“专家”按钮和先前状态下的“玩家与CPU”按钮在位置上重叠,因此无需单击“专家”按钮即可将状态更改为游戏屏幕状态。   我不知道。感激不已。

这是我的一些代码:

def start_screen_state(mouse, click):
    start_screen = StartScreen(gameDisplay, screenX, screenY)  # .ven bilmem ne gibi bir dosyada degisiklik yapiyormusum. ondan hata aldim
    global state # her fonk.da global olmasini istiyorsak bildiriyoruz. ilgili fonk.da artik bu degiken globaldir.
    PVPxCoord = screenX - int(screenX * 0.8)
    PVPyCoord = screenY - int(screenY * 0.4)
    PVCxCoord = screenX - int(screenX * 0.3)
    PVCyCoord = screenY - int(screenY * 0.4)
    # for PVP button
    if (PVPxCoord < mouse[0] < PVPxCoord + start_screen.get_PVP_Button().getWidth()) and (PVPyCoord < mouse[1] < PVPyCoord + start_screen.get_PVP_Button().getHeight()): #PVP butonu icin
        start_screen.get_PVP_Button().modifyButton("light_pvp_button.png", "resized_light_pvp_button.png")
        if click[0] == 1:
            state = GAME_SCREEN
    # for PVC button
    if (PVCxCoord < mouse[0] < PVCxCoord + start_screen.get_PVC_Button().getWidth()) and (PVCyCoord < mouse[1] < PVCyCoord + start_screen.get_PVC_Button().getHeight()): #PVC butonu icin
        if click[0] == 1:
            state = PVC_SCREEN



def game_screen_state(mouse, click):
    game_screen = GameScreen(gameDisplay, screenX, screenY)
    global state


def pvc_screen_state(mouse, click):
    pvc_screen = PVCScreen(gameDisplay, screenX, screenY)
    global state
    # for easy button
    easy_x_Coord = pvc_screen.get_easy_button().getX()
    easy_y_Coord = pvc_screen.get_easy_button().getY()
    if (easy_x_Coord < mouse[0] < easy_x_Coord + pvc_screen.get_easy_button().getWidth()) and (easy_y_Coord < mouse[1] < easy_y_Coord + pvc_screen.get_easy_button().getHeight()):
        pvc_screen.get_easy_button().modifyButton("light_easy_button.png", "resized_light_easy_button.png")
        if click[0] == 1:
            state = FINAL_SCREEN # GAME_SCREEN diye update edilecek. FINAL_SCREEN test icin bu sekle getirildi
    # for medium button
    medium_x_Coord = pvc_screen.get_medium_button().getX()
    medium_y_Coord = pvc_screen.get_medium_button().getY()
    if (medium_x_Coord < mouse[0] < medium_x_Coord + pvc_screen.get_medium_button().getWidth()) and (medium_y_Coord < mouse[1] < medium_y_Coord + pvc_screen.get_medium_button().getHeight()):
        pvc_screen.get_medium_button().modifyButton("light_medium_button.png", "resized_light_medium_button.png")
        if click[0] == 1:
            state = GAME_SCREEN
    # for hard button
    hard_x_Coord = pvc_screen.get_hard_button().getX()
    hard_y_Coord = pvc_screen.get_hard_button().getY()
    if (hard_x_Coord < mouse[0] < hard_x_Coord + pvc_screen.get_hard_button().getWidth()) and (hard_y_Coord < mouse[1] < hard_y_Coord + pvc_screen.get_hard_button().getHeight()):
        pvc_screen.get_hard_button().modifyButton("light_hard_button.png", "resized_light_hard_button.png")
        if click[0] == 1:
            state = GAME_SCREEN
    # for extreme button
    extreme_x_Coord = pvc_screen.get_extreme_button().getX()
    extreme_y_Coord = pvc_screen.get_extreme_button().getY()
    if (extreme_x_Coord < mouse[0] < extreme_x_Coord + pvc_screen.get_extreme_button().getWidth()) and (extreme_y_Coord < mouse[1] < extreme_y_Coord + pvc_screen.get_extreme_button().getHeight()):
        pvc_screen.get_extreme_button().modifyButton("light_extreme_button.png", "resized_light_extreme_button.png")
        if click[0] == 1:
            state = GAME_SCREEN




def final_screen_state(mouse, click):
    gameDisplay.fill((0,0,0))
    final_screen = FinalScreen(gameDisplay, screenX, screenY)
    global state
    menu_x_Coord = final_screen.get_main_menu_button().getX()
    menu_y_Coord = final_screen.get_main_menu_button().getY()
    if (menu_x_Coord < mouse[0] < menu_x_Coord + final_screen.get_main_menu_button().getWidth()) and (menu_y_Coord < mouse[1] < menu_y_Coord + final_screen.get_main_menu_button().getHeight()):
        # final_screen.get_menu_button().modifyButton("light_menu_button.png", "resized_light_menu_button.png")
        # LIGHT FOTO GELINCE ACILACAK
        if click[0] == 1:
            state = START_SCREEN

def current_state(mouse, click):
    if state == START_SCREEN:
        start_screen_state(mouse, click)
    elif state == PVC_SCREEN:
        pvc_screen_state(mouse, click)
    elif state == FINAL_SCREEN:
        final_screen_state(mouse, click)
    elif state == GAME_SCREEN:
        game_screen_state(mouse, click)


while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    current_state(mouse, click)
    click = None
    mouse = (0, 0)
    pygame.display.update()  # flip'i de dene
    clock.tick(60)

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的代码,则不会在发出新代码之前擦除屏幕。我建议在“ pygame.display.update()”行之前执行“ gameDisplay.fill((0,0,0))”(我看不到您在其他表面涂抹的地方)以“清洁”重新绘制之前。