【问题标题】:Buttons From Previous Pages Are Still Clickable上一页的按钮仍然可以点击
【发布时间】:2018-10-20 21:31:24
【问题描述】:

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

问题是当我在开始屏幕并单击“播放器与 CPU”按钮时,它会将状态更改为“播放器与 CPU”屏幕。但是,由于此状态的“专家”按钮和先前状态的“玩家 vs 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)

【问题讨论】:

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


【解决方案1】:

如果我正确理解了您的代码,那么您不会在删除新屏幕之前擦除屏幕。我建议在 'pygame.display.update()' 行之前做一个 'gameDisplay.fill((0,0,0))' (不过我看不到你在其他表面上的位置)以“清理”在重绘之前。

【讨论】:

  • 感谢您的回复!但是当我这样做时,整个屏幕变黑了。在 current_state 函数中,每当调用 current_state 函数的任何内部函数,即 Pvc_screen_state 函数时,都会首先调用它的 init。在 init 中,我调用了 gamedisplay.fill((0,0,0))。这是我为所有州所做的。我在他们的 init 开头调用填充函数
  • 你应该在重绘之前清理屏幕。也就是在update方法中,因为init方法只调用了一次。代码将是“填充、绘制、更新屏幕”)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多