簡體   English   中英

使pygame在繼續操作之前等待用戶點擊/輸入(python問答游戲)

[英]Making pygame wait for user click/input before proceeding (python quiz game)

因此,我試圖在pygame上制作一個游戲,該游戲將詞匯顯示為問題,並提供三個答案選擇。 如果用戶按下正確的答案,他們的分數將提高一個,游戲將繼續進行下一個詞匯問題。

我將我的問題存儲在一個名為questions []的二維數組中,在該數組的每個元素中,它以[問題,正確答案選擇,答案選擇,答案選擇]的形式保存每個問題的答案。 因此,正確答案始終在索引位置[i] [1]。 我確實將稍后顯示答案選擇的順序隨機化。

現在我的問題是我的游戲無需等待用戶輸入即可遍歷所有問題。 關鍵是它將等待用戶單擊。 用戶單擊時,它將檢查用戶單擊的位置。 用戶鼠標的位置將確定用戶按下了哪個“答案框”。 假設用戶按下了第一個框。 然后,游戲會比較該框中存儲的文本是否正確(即文本與問題[i] [1]相同)。 它會瞬間顯示每個問題,然后移至下一個問題和下一個問題。

但是它不等待用戶首先單擊。 甚至沒有,它甚至沒有顯示足夠長的問題以使用戶閱讀問題。 有沒有辦法構造循環或添加某些條件,以便程序可以顯示每個問題,直到用戶選擇答案,然后添加分數並移至下一個問題?

這是代碼:

import pygame
from random import randint
from pygame import *

pygame.init()
pygame.font.match_font('Courier New.ttf')


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GREEN = (71, 212, 15)
BLUE = (42, 250, 246)
PINK = (255,102, 196)
YELLOW = (255, 255, 0)
i = 0

size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Spanish Space Quiz") 

done = False

questions = [["Hola", "Hello", "Goodbye", "Cow"],["Amigo", "Friend", "Cat", "Dog"],["Si", "Yes", "No", "Maybe"]]
answerboxes = [[30,300,190,150,BLUE,WHITE,7],[255,300,190,150,YELLOW,WHITE,7],[480,300,190,150,PINK,WHITE,7]]
score = 0
choices = []

def textObject (text, font):
    textWord = font.render(text, True, WHITE)
    return textWord, textWord.get_rect()

def answerbutton(drawbox):
    mouse = pygame.mouse.get_pos()

    if drawbox[0]+drawbox[2] > mouse[0] > drawbox[0] and drawbox[1]+drawbox[3] > mouse[1] > drawbox[1]:
        pygame.draw.rect(screen, drawbox[5],(drawbox[0],drawbox[1],drawbox[2],drawbox[3]),drawbox[6])           
    else:
        pygame.draw.rect(screen, drawbox[4],(drawbox[0],drawbox[1],drawbox[2],drawbox[3]),drawbox[6])

    answerTextFont = pygame.font.SysFont("Courier New",60)
    textWord, textBox = textObject(drawbox[7], answerTextFont) #the text & the "Text box"
    textBox.center = ( (drawbox[0]+(drawbox[2]/2)), (drawbox[1]+(drawbox[3]/2)) )
    screen.blit(textWord, textBox)

def questionbutton(message,x,y,w,h,color):
    mouse = pygame.mouse.get_pos()
    pygame.draw.rect(screen,color,(x,y,w,h))

    answerTextFont = pygame.font.SysFont("Courier New",60)
    textWord, textBox = textObject(message, answerTextFont) #the text & the "Text box"
    textBox.center = ( (x+(w/2)), (y+(h/2)) )
    screen.blit(textWord, textBox)

while not done:
    screen.blit (backgroundImage, [0,0])
    font = pygame.font.SysFont('Courier', 30, True, False)
    text = font.render("SPACE VOCBULARY QUIZ",True,WHITE)
    screen.blit(text, [30, 30])
    font = pygame.font.SysFont('Courier', 30, False, False)
    text = font.render("SCORE: ", True, WHITE)
    screen.blit(text, [500, 30])

    for event in pygame.event.get():
        if i == (len(questions)): #if user clicks close then done becomes true and game quits
            done = True
            event.type == pygame.QUIT
        for c in range (len(questions)):
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
            questionbutton((questions[c][0]),30,150,640,100,GREEN)
            for n in range(3):
                choices.append(questions[c][n+1])
            for r in range(3):
                randomPointer = randint(0, (len(choices)-1))
                answerboxes[r].append(choices[randomPointer])
                choices.remove(choices[randomPointer])
                answerbutton(answerboxes[r][0:8])
            if click[0] == 1: 
                for a in range(3):
                    if answerboxes[a][0]+answerboxes[a][2] > mouse[0] > answerboxes[a][0] and answerboxes[a][1]+answerboxes[a][3] > mouse[1] > answerboxes[a][1]:
                        if answerboxes[a][7] == questions[i][1]:
                            score = score + 1
                            print (score)
            for g in range (3):
                answerboxes[g].pop()           
            i = i+1

    pygame.display.update()

pygame.quit()

您可以在問題循環中放入一個無限循環,其中的中斷條件是在答案框上單擊鼠標時退出無限循環。

范例

for c in len(range(questions)):
    clicked_on_answer = False
    while True:
        # your code

        if click[0] == 1: 
            for a in range(3):
                if answerboxes[a][0]+answerboxes[a][2] > mouse[0] > answerboxes[a][0] and answerboxes[a][1]+answerboxes[a][3] > mouse[1] > answerboxes[a][1]:
                    clicked_on_answer = True
                    if answerboxes[a][7] == questions[i][1]:
                        score = score + 1
                        print (score)
        if clicked_on_answer:
            break

是的,您需要重組程序,更好地將圖形與事件處理和游戲邏輯分開。 如果按下了鼠標鍵,它應該只繼續處理下一個問題,因此請在事件循環中檢查if event.type == pygame.MOUSEBUTTONDOWN:每次單擊僅產生一個MOUSEBUTTONDOWN事件),然后查看是否單擊了rect,增加分數,最后呈現下一個問題和選擇。 在事件循環之外取消問題和選擇文本。

clock = pygame.time.Clock()  # A clock to limit the frame rate.
# Define the fonts outside of the main loop.
font = pygame.font.SysFont('Courier', 30, False, False)

# Render question and choice text surfaces.
# Create the rects for the choices and set their positions.

while not done:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Check if event.pos collides with the correct rect.
            for index, rect in enumerate(rects):
                if rect.collidepoint(event.pos) and choices[index] == correct_answer:
                    score += 1
            # Get next question and choices, render them and update the rects.

    # Draw everything.
    # Blit the question and choice text surfaces at their rects.

    pygame.display.update()
    clock.tick(30)  # Limit frame rate to 30 fps.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM