为什么Pygame不会检测我点击的位置

时间:2018-01-04 12:11:09

标签: python pygame

这是我正在制作的游戏,但我遇到了一些麻烦(这只是我的程序中较大部分的一个功能,我知道我没有在下面的代码中调用它) 。

def qfq():
pygame.event.get()
pygame.mixer.music.pause()
gameDisplay.fill(white)
gameDisplay.blit(plainbg_img, [0, 0])
if year == 7:
    ans = randint(8, 15)
    y = randint(0, 8)
    x1 = ans - y
    x2 = randint(0, 8)
    x3 = randint(0, 8)
    x4 = randint(0, 8)

    eqt = ("x + "+ str(y)+ " = "+ str(ans))
    x1 = ("x = " + str(x1))
    x2 = ("x = " + str(x2))
    x3 = ("x = " + str(x3))
    x4 = ("x = " + str(x4))

    mouse_pos = pygame.mouse.get_pos()
    mathquestion(eqt,black,(screen_width169/2 -250),(screen_height169/2 -400))


    py1 = screen_height169/2
    py2 = screen_height169/2 +100
    py3 = screen_height169/2 +200
    py4 = screen_height169/2 +300

    yArray = [py1,py2,py3,py4]

    yValue1 = random.choice(yArray)
    mathanswer((x1),black,(screen_width169/2 -100),(yValue1))
    yCheck1 = yValue1
    yArray.remove(yValue1)
    yValue2 = random.choice(yArray)
    mathanswer((x2),black,(screen_width169/2 -100),(yValue2))
    yCheck2= yValue2
    yArray.remove(yValue2)
    yValue3 = random.choice(yArray)
    mathanswer((x3),black,(screen_width169/2 -100),(yValue3))
    yCheck3 = yValue3
    yArray.remove(yValue3)
    yValue4 = random.choice(yArray)
    mathanswer((x4),black,(screen_width169/2 -100),(yValue4))
    yCheck4 = yValue4
    yArray.remove(yValue4)

    answer = None

    qfqLoop = True
    while qfqLoop == True:
        pygame.event.get()

        #Checking if answers are correct
        if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #correct answer
            if mouse_pos[1] >=yCheck1 and mouse_pos[1] <=yCheck1+65:
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        answer = True
        if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #wrong answer
            if mouse_pos[1] >=yCheck2 and mouse_pos[1] <=yCheck2+65:
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        answer = False
        if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #wrong answer
            if mouse_pos[1] >=yCheck3 and mouse_pos[1] <=yCheck3+65:
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        answer = False
        if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #wrong answer
            if mouse_pos[1] >=yCheck4 and mouse_pos[1] <=yCheck4+65:
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        answer = False

        print(answer)
        pygame.display.update()    

问题的显示和答案工作正常,但真正的问题在于这一部分。

#Checking if answers are correct
    if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #correct answer
        if mouse_pos[1] >=yCheck1 and mouse_pos[1] <=yCheck1+65:
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    answer = True
    if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #wrong answer
        if mouse_pos[1] >=yCheck2 and mouse_pos[1] <=yCheck2+65:
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    answer = False
    if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #wrong answer
        if mouse_pos[1] >=yCheck3 and mouse_pos[1] <=yCheck3+65:
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    answer = False
    if mouse_pos[0] >= 855 and mouse_pos[0] <= 1065: #wrong answer
        if mouse_pos[1] >=yCheck4 and mouse_pos[1] <=yCheck4+65:
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    answer = False

出于某种原因,每当我尝试点击答案时,他们似乎都无法正常工作。我可以点击它们,但Pygame似乎不让我点击它们并激活 if语句。

这是程序运行时屏幕的样子

这是我点击正确答案后打印出来的内容

1 个答案:

答案 0 :(得分:2)

我认为你太复杂了。你必须只使用一次

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:

因为for获取所有事件,您只需点击第一个区域点击它就不会检查点击其他区域。

实施例

answer = None
correct_answer = 1

while qfqLoop:

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:

            if 855 <= mouse_pos[0] <= 1065:
                if yCheck1 <= mouse_pos[1] <=yCheck1+65:
                    answer = 1
                elif yCheck2 <= mouse_pos[1] <=yCheck2+65:
                    answer = 2
                elif yCheck3 <= mouse_pos[1] <=yCheck3+65:
                    answer = 3
                elif yCheck4 <= mouse_pos[1] <=yCheck4+65:
                    answer = 4

    print('clicked:', answer)
    print('correct:', answer == correct_answer)

    pygame.display.update()   

编辑:我的版本

import pygame
import random

# --- constants ---

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 800

FPS = 5

START_X = SCREEN_WIDTH/2
START_Y = SCREEN_HEIGHT/2
ANSWERS_Y = [START_Y, START_Y+100, START_Y+200, START_Y+300]


# --- functions ---

def display_text(text, color, x, y, font):
    text_image = font.render(text, False, color)
    text_rect = text_image.get_rect(x=x, y=y)
    screen.blit(text_image, text_rect)
    return text_rect

# --- main ---

# - init -

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

font1 = pygame.font.SysFont(None, 100)
font2 = pygame.font.SysFont(None, 75)

# - game -

z = random.randint(8, 15)
all_x = random.sample(range(0, 9), 4) # select 4 unique values (they will not repeat)
x = all_x[0] # correct answer
y = z - x

# shuffle values in random order
random.shuffle(all_x)

# find correct answer after shuffle
correct_answer = all_x.index(x)
answer = None

# - draw -

screen.fill(WHITE)                                                                  

text = "x + {} = {}".format(y, z)
display_text(text, BLACK, START_X-150, 150, font1)

answers_rect = []
for x, pos in zip(all_x, ANSWERS_Y):
    text = "x = " + str(x)
    rect = display_text(text, BLACK, START_X-50, pos, font2)
    answers_rect.append(rect)

pygame.display.update()   

# - loop -

clock = pygame.time.Clock()

while answer != correct_answer:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            for index, rect in enumerate(answers_rect):
                if rect.collidepoint(event.pos):
                    answer = index
                    print('clicked index:', answer)
                    print('clicked value:', all_x[answer])
                    print('correct:', answer == correct_answer)
                    print('---')
                    break


    clock.tick(FPS)

# - end -

pygame.quit()
exit()