如何让两个函数在我的 pygame wordle 副本中互相访问?

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

main
函数中,我在worldle的屏幕底部创建了代表键盘的指示器。在
checkguess
函数中,我尝试创建当猜测正确时变为绿色的指标。

问题在于

checkguess
是一个不同的函数,因此不会读取
main
函数中有关指标的信息。我该如何克服这个问题?

代码如下:

import imghdr
import random, pygame, sys
from pygame.locals import *
pygame.init()

white = (255,255,255)
yellow = (200,182,83)
grey = (120,124,127)
black = (0,0,0)
green= (108,169,101)
lightGreen=(153,255,204)

WIDTH, HEIGHT = 500, 700

ALPHABET = ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"]


SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))

font = pygame.font.SysFont("Helvetica neue", 40)
bigFont = pygame.font.SysFont("Helvetica neue", 80)

youWin = bigFont.render("You Win!",       True, lightGreen)
youLose = bigFont.render("You Lose!",     True, lightGreen)
playAgain = bigFont.render("Play Again?", True, lightGreen)

class Indicator:
    def __init__(self, x, y, letter):
        # Initializes variables such as color, size, position, and letter.
        self.x = x
        self.y = y
        self.text = letter
        self.rect = (self.x, self.y, 57, 75)
        self.bg_color = grey

    def draw(self):
        # Puts the indicator and its text on the screen at the desired position.
        pygame.draw.rect(SCREEN, self.bg_color, self.rect)
        self.text_surface = font.render(self.text, True, "white")
        self.text_rect = self.text_surface.get_rect(center=(self.x+27, self.y+30))
        SCREEN.blit(self.text_surface, self.text_rect)
        pygame.display.update()

# Drawing the indicators on the screen.

def main():
    file = open("wordList.txt","r")
    wordList = file.readlines()
    word = wordList[random.randint(0, len(wordList)-1)].upper()

    width, height = 633, 1000
   

    FPS = 30
    clock = pygame.time.Clock()

    window = pygame.display.set_mode((width, height))
    window.fill(white)

    guess = ""

    indicators = []

    print(word)

    for x in range(0,5):
        for y in range(0,6):
            pygame.draw.rect(window, grey, pygame.Rect(120+(x*80), 50+(y*80), 50, 50),2)

    pygame.display.set_caption("Wordle")

    turns = 0
    win = False

    indicator_x, indicator_y = 20, 520
    
    for i in range(3):
        for letter in ALPHABET[i]:
          new_indicator = Indicator(indicator_x, indicator_y, letter)
          indicators.append(new_indicator)
          new_indicator.draw()
          indicator_x += 60
        indicator_y += 80
        indicator_x = 50

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit ()
                sys.exit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE or len(guess) > 5:
                    guess = guess[:-1]
                    #allow backspace

            if event.type == pygame.KEYDOWN:
                guess+=event.unicode.upper()

                if event.key == K_RETURN and win == True:
                    main()

                if event.key == K_RETURN and turns == 6:
                    main()

                if event.key == pygame.K_BACKSPACE or len(guess) > 5:
                    guess = guess[:-1]

                if event.key == K_RETURN and len(guess) > 4:
                    win = checkGuess(turns, word, guess, window)
                    turns+=  1
                    guess = ""

        if win == True:
            window.blit(youWin,(190,200))
            window.blit(playAgain,(160,300))

        if turns == 6 and win != True:
            window.blit(youLose,(190,200))
            window.blit(playAgain,(160 ,300))
        pygame.display.update()
        clock.tick(FPS)
def checkGuess(turns, word, userGuess, window):
    renderList = ["","","","",""]
    spacing = 0
    guessColourCode = [grey,grey,grey,grey,grey]

    indicators = []
    for x in range(0,5):
        if userGuess[x] in word:
            guessColourCode[x] = yellow

        if word[x] == userGuess[x]:
            guessColourCode[x] = green
            for indicator in indicators:
                if indicator.text == userGuess[x]:
                    indicator.bg_color = green
                    indicator.draw()

    list(userGuess)

    for x in range(0,5):
        renderList[x] = font.render(userGuess[x], True, white)
        pygame.draw.rect(window, guessColourCode[x], pygame.Rect(120 +spacing, 50+ (turns*80), 50, 50))
        window.blit(renderList[x], (130 + spacing, 50 + (turns*80)))
        spacing+=80

    if guessColourCode == [green,green,green,green,green]:
        return True

main()
python function variables pygame indicator
© www.soinside.com 2019 - 2024. All rights reserved.