Pygame 说视频系统未初始化

问题描述 投票:0回答:1

我是 pygame 新手,我一直在尝试制作 Wordle 游戏。这个函数特别有缺陷,我认为这是因为我使用 while true 循环,它说视频系统未初始化 (pygame.display.update() 和 pygame.event.get()) 但我不知道如何解决这个问题。

def playgame():
    global t_score 
    screen.fill("White")
    row = []
    newgame = Word()
    screen.blit(header,header_rec)
    newgame = getgame()    
    row.append(Box(100,100,120,space_)) 
    row.append(Box(100,100,260,space_)) 
    row.append(Box(100,100,400,space_)) 

    row[0].makerow()
    row[1].makerow()
    row[2].makerow()

    meaning_text = font_smaller.render(f'Meaning: {newgame.meaning}',True,"Black")
    meaning_rec = meaning_text.get_rect(center=(325,576))
    screen.blit(meaning_text,meaning_rec)
    screen.blit(keyboard,keyboard_rec)
    score_text = font_score.render(f'Total score: {t_score}',True,"Black")
    score_text_rec = score_text.get_rect(center=(35,19))
    screen.blit(score_text,score_text_rec)

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

    canenter = False
    win = 0
    score = 8 
    for attempts in range (0,3):
        word_guess = [" "]*5
        for letter_count in range (0,5):
            letter = ' '
            run = True
            while run:  
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        run = False 
                        pygame.quit()
                    if event.type == pygame.MOUSEBUTTONDOWN: 
                        if(a_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'A'
                        if(b_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'B'
                        if(c_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'C'
                        if(d_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'D'
                        if(e_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'E'
                        if(f_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'F'     
                        if(g_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'G'
                        if(h_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'H'  
                        if(i_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'I'
                        if(j_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'J'
                        if(k_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'K'   
                        if(l_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'L' 
                        if(m_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'M'
                        if(n_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'N'
                        if(o_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'O'   
                        if(p_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'P'
                        if(q_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'Q'
                        if(r_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'R'
                        if(s_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'S'
                        if(t_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'T'   
                        if(u_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'U'   
                        if(v_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'V'
                        if(w_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'W'
                        if(x_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'X'
                        if(y_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'Y' 
                        if(z_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = 'Z'

                        if(letter != " "):
                            letter_print = font_letter.render(f'{letter}',True,"Black")
                            letter_rec = letter_print.get_rect(center=(row[attempts].box_surface_rect[letter_count].center))
                            screen.blit(screen,letter_rec)
                        
                        if(letter != " " and delete_kb_rec.collidepoint(pygame.mouse.get_pos())):
                            letter = " "
                            row[attempts].surface_.fill("White")

                        if(letter != " " and enter_kb_rec.collidepoint(pygame.mouse.get_pos())):
                            if(letter == newgame.word[letter_count]):
                                row[attempts].surface_.fill("Green")
                                screen.blit(screen,letter_rec)
                                run = False
                            elif(letter in newgame.word):
                                row[attempts].surface_.fill("Amber")
                                screen.blit(screen,letter_rec)
                                run = False
                            else:
                                row[attempts].surface_.fill("Green")
                                screen.blit(screen,letter_rec)
                                run = False
            pygame.display.update()
            word_guess[letter_count] = letter
        guess = ''.join(word_guess)
        if(guess == newgame.word):
            win  = 1
            t_score += score
            start = True
            while start:
                congrats = font_smaller.render(f'Congrats!!! Word was: {newgame.word} Press Enter to start new game',True,"Black")
                congrats_rect = pygame.Rect(110,670,586,24)
                screen.blit(congrats,congrats_rect)
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_RETURN:
                            playgame()
                            start = False
            pygame.display.update()
        else:
            score -= 1

    if(win == 0):
        start = True
        while start:
            congrats = font_smaller.render(f'Sorry you could not guess!!! Word was: {newgame.word} Press Enter to start new game',True,"Black")
            congrats_rect = pygame.Rect(110,670,586,24)
            screen.blit(congrats,congrats_rect)
            for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_RETURN:
                            playgame()
                            start = False
        pygame.display.update()

这个函数应该显示标题和键盘,在添加 if 语句之前它工作得很好,现在它卡在主页上。该函数还应该循环,直到输入一个字母为止,这对于其他 4 个字母也是如此。每个单词还有五次尝试。尝试完成后,或者如果猜测正确,它应该故意停留在同一个窗口上,显示祝贺消息或抱歉消息,并询问用户是否想通过按回车键玩另一个游戏。

错误 enter image description here

python pygame syntax-error game-development
1个回答
0
投票

pygame 必须先初始化才能使用。

确保您正在使用:

pygame.init()

在代码的开头。

© www.soinside.com 2019 - 2024. All rights reserved.