繁体   English   中英

Pygame 按钮对点击没有反应

[英]Pygame buttons not reacting to clicks

我一直在尝试为 pygame 多项选择游戏制作一个按钮,在按下随机问题后,它会将您带到一个问题页面,其中有 2 个答案,一个对,一个错。 但是,按下时它们对任何东西都没有反应,我的代码如下所示;

import pygame
import random
pygame.init()

win = pygame.display.set_mode((1200, 600))

pygame.display.set_caption("History Game")

#Background Image Loading
bg = pygame.image.load('bg.jpg')
bgAus = pygame.image.load('bg_Aus.jpg')
bgEN = pygame.image.load('bg_EN.jpg')
bgIR = pygame.image.load('bg_IR.jpg')

WHITE = (255, 255, 255)
ACTIVE_COLOR = pygame.Color('blue')
INACTIVE_COLOR = pygame.Color('red')
FONT = pygame.font.Font(None, 50)

def draw_buttonStart(buttonStart, win):
    pygame.draw.rect(win, buttonStart['color'], buttonStart['rect'])
    win.blit(buttonStart['text'], buttonStart['text rect'])


def draw_buttonAus1(buttonAus1, win):
    pygame.draw.rect(win, buttonAus1['color'], buttonAus1['rect'])
    win.blit(buttonAus1['text'], buttonAus1['text rect'])
    
def draw_buttonAus2(buttonAus2, win):
    pygame.draw.rect(win, buttonAus2['color'], buttonAus2['rect'])
    win.blit(buttonAus2['text'], buttonAus2['text rect'])


def draw_buttonIR1(buttonIR1, win):
    pygame.draw.rect(win, buttonIR1['color'], buttonIR1['rect'])
    win.blit(buttonIR1['text'], buttonIR1['text rect'])

def draw_buttonIR2(buttonIR2, win):
    pygame.draw.rect(win, buttonIR2['color'], buttonIR2['rect'])
    win.blit(buttonIR2['text'], buttonIR2['text rect'])
    

def draw_buttonEN1(buttonEN1, win):
    pygame.draw.rect(win, buttonEN1['color'], buttonEN1['rect'])
    win.blit(buttonEN1['text'], buttonEN1['text rect'])

def draw_buttonEN2(buttonEN2, win):
    pygame.draw.rect(win, buttonEN2['color'], buttonEN2['rect'])
    win.blit(buttonEN2['text'], buttonEN2['text rect'])

def create_button(x, y, w, h, text, callback):
    text_surf = FONT.render(text, True, WHITE)
    button_rect = pygame.Rect(x, y, w, h)
    text_rect = text_surf.get_rect(center=button_rect.center)
    button = {
        'rect': button_rect,
        'text': text_surf,
        'text rect': text_rect,
        'color': INACTIVE_COLOR,
        'callback': callback,
        }
    return button

points = 0
def correct_answer():
    global points
    points += 100
    print(points)

def wrong_answer():
    global points
    points -= 50
    print(points)

moveOn = 0
def move_on():
    global moveOn
    moveOn = 1
                        



win.blit(bg, (0, -200))

#Main Loop
over = False
while not over:
    score = FONT.render('Score: ' + str(points), 1, (255,0,0))
    win.blit(score, (390, 10))
    pygame.display.flip()
    buttonStart = create_button(50, 50, 250, 80, 'Random', move_on)
    button_listStart = [buttonStart]
    draw_buttonStart(buttonStart, win)
    
    #Quits game if X is clicked
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            for button in button_listStart:
                if button['rect'].collidepoint(event.pos):
                    button['callback']()
                    print (moveOn)
                    randomQ = random.randint(1,3)
                    if int(randomQ) == 1:
                        print("1")
                        buttonAus1 = create_button(275, 400, 250, 80, '1606', correct_answer)
                        buttonAus2 = create_button(675, 400, 250, 80, '1723', wrong_answer)
                        button_listAus = [buttonAus1, buttonAus2]
                        win.blit(bgAus, (-400, 0))
                        draw_buttonAus1(buttonAus1, win)
                        draw_buttonAus2(buttonAus2, win)
                        for event in pygame.event.get():
                            if event.type == pygame.MOUSEBUTTONDOWN:
                                for button in button_listAus:
                                    if button['rect'].collidepoint(event.pos):
                                        button['callback']()
                                        print (points)


                            #Hover over button changes colour
                            elif event.type == pygame.MOUSEMOTION:
                                for button in button_listAus:
                                    if button['rect'].collidepoint(event.pos):
                                        button['color'] = ACTIVE_COLOR
                                    else:
                                        button['color'] = INACTIVE_COLOR
                                            

                    elif int(randomQ) == 2:
                        print ("2")
                        buttonEN1 = create_button(675, 400, 250, 80, '1715', correct_answer)
                        buttonEN2 = create_button(275, 400, 250, 80, '1789', wrong_answer)
                        button_listEN = [buttonEN1, buttonEN2]
                        win.blit(bgEN, (0, -150))
                        draw_buttonEN1(buttonEN1, win)
                        draw_buttonEN2(buttonEN2, win)
                        for event in pygame.event.get():
                            if event.type == pygame.MOUSEBUTTONDOWN:
                                for button in button_listEN:
                                    if button['rect'].collidepoint(event.pos):
                                            button['callback']()
                                            
                            #Hover over button changes colour                        
                            elif event.type == pygame.MOUSEMOTION:
                                for button in button_listEN:
                                    if button['rect'].collidepoint(event.pos):
                                        button['color'] = ACTIVE_COLOR
                                    else:
                                        button['color'] = INACTIVE_COLOR
                                                    
                                        
                    else:
                        print ("3")
                        buttonIR1 = create_button(275, 400, 250, 80, '1760', correct_answer)
                        buttonIR2 = create_button(675, 400, 250, 80, '1812', wrong_answer)
                        button_listIR = [buttonIR1, buttonIR2]
                        win.blit(bgIR, (-375, -20))
                        draw_buttonIR1(buttonIR1, win)
                        draw_buttonIR2(buttonIR2, win)
                        for event in pygame.event.get():
                            if event.type == pygame.MOUSEBUTTONDOWN:
                                for button in button_listIR:
                                    if button['rect'].collidepoint(event.pos):
                                        button['callback']()

                            #Hover over button changes colour
                            elif event.type == pygame.MOUSEMOTION:
                                for button in button_listIR:
                                    if button['rect'].collidepoint(event.pos):
                                        button['color'] = ACTIVE_COLOR
                                    else:
                                        button['color'] = INACTIVE_COLOR

我一直在徘徊,如果它可能与 for 循环有关,就像我将 for 循环用于主循环和退出功能之前一样,退出功能根本不起作用,因此感谢您的帮助,谢谢。

尝试将您的按钮绑定到它们各自的鼠标操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM