Saya mencoba membuat algojo tetapi saya mendapatkan kesalahan berikut:

enter image description here

Saya mencoba membuatnya mengembalikan koordinat x dari pos_.

Cuplikan kode termasuk kesalahan:

        def DrawLetter(IndexNumber):
        if letterlist[x-1] == "g":
            pos_ = (AdjustMe + (27 * int(IndexNumber), 226))
        else:
            pos_ = (AdjustMe + (27 * int(IndexNumber), 230))
        screen.blit(letter, pos_)
        screen.blit(BottomDashes, (0, 256))
        lettersguessed.append(letterlist[x-1])
        print(x)
        if LTGL == lettersguessed:
            print("you won")
            running = False

    while x < len(letterlist):
        duplicates = [item for item, count in collections.Counter(word).items() if count > 1]
        if letterlist.count(letterlist[x-1]) > 1:
            for y in duplicates:
                DuplicatesList = [i for i, e in enumerate(word) if e == y]
            IndexNumber = list(DuplicatesList)
            print(IndexNumber)
            DrawLetter(IndexNumber)
        else:
            IndexNumber = LTGL.index(letterlist[x - 1])
            DrawLetter(IndexNumber)
        x += 1

Seluruh kode:

    from random_word import RandomWords
r = RandomWords()
import pygame
import sys
import collections

def main():
    pygame.init()
    clock = pygame.time.Clock()
    size = [256, 356]
    white = [255, 255, 255]
    black = [0, 0, 0]

    word = "{0}".format(r.get_random_word(hasDictionaryDef="true", minLength=4, maxLength=8))
    ListToGuess = list(word)
    LTGL = ListToGuess
    print(ListToGuess)

    pygame.display.set_caption('Hang man')
    screen = pygame.display.set_mode(size)
    font = pygame.font.Font('freesansbold.ttf', 28)

    def DisplayLetter(LTGL, letterlist, lettersguessed):
        if len(LTGL) == 4:
            AdjustMe = 78
        if len(LTGL) == 5:
            AdjustMe = 63
        if len(LTGL) == 6:
            AdjustMe = 53
        if len(LTGL) == 7:
            AdjustMe = 37
        if len(LTGL) == 8:
            AdjustMe = 25
        x = 0
        letter = font.render(letterlist[x-1], True, black, white)

        def DrawLetter(IndexNumber):
            if letterlist[x-1] == "g":
                pos_ = (AdjustMe + (27 * int(IndexNumber), 226))
            else:
                pos_ = (AdjustMe + (27 * int(IndexNumber), 230))
            screen.blit(letter, pos_)
            screen.blit(BottomDashes, (0, 256))
            lettersguessed.append(letterlist[x-1])
            print(x)
            if LTGL == lettersguessed:
                print("you won")
                running = False

        while x < len(letterlist):
            duplicates = [item for item, count in collections.Counter(word).items() if count > 1]
            if letterlist.count(letterlist[x-1]) > 1:
                for y in duplicates:
                    DuplicatesList = [i for i, e in enumerate(word) if e == y]
                IndexNumber = list(DuplicatesList)
                print(IndexNumber)
                DrawLetter(IndexNumber)
            else:
                IndexNumber = LTGL.index(letterlist[x - 1])
                DrawLetter(IndexNumber)
            x += 1

    YouLost = pygame.image.load(r'C:\Users\Wit\Desktop\Programming shit\img\Hangman\Youlost.png')
    WhitePng = pygame.image.load(r'C:\Users\Wit\Desktop\Programming shit\img\Hangman\white100px.png')
    BottomDashes = pygame.image.load(r'C:\Users\Wit\Desktop\Programming shit\img\Hangman\Dashes{0}.png'.format(len(ListToGuess)))

    def HangmanDrawImg(Mistakes):
        HangmanImg = pygame.image.load(r'C:\Users\Wit\Desktop\Programming shit\img\Hangman\Hangman{0}.png'.format(Mistakes))
        screen.blit(HangmanImg, (30, -50))

    screen.fill(white)
    button = pygame.Rect(103, 296, 50, 50)  # creates a rect object
    pygame.draw.rect(screen, [255, 0, 0], button)  # draw button
    screen.blit(BottomDashes, (0, 256))
    pygame.display.flip()

    lettersguessed = []
    running = True
    Mistakes = 0

    while running is True:
        try:
            letter = ""
            if Mistakes == 9:
                screen.blit(WhitePng, (0, 259))
                screen.blit(YouLost, (20, 286))
                pygame.display.flip()
                break
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                    return running
                if event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_pos = event.pos
                    if button.collidepoint(mouse_pos):
                        Mistakes += 1
                if event.type == pygame.KEYDOWN:
                    current_text = letter
                    letter += event.unicode
            if current_text == letter:
                pass
            elif letter not in ListToGuess:
                Mistakes += 1
            elif letter in ListToGuess:
                RunMe = True
                letterlist = []
                while RunMe:
                    lettercount = ListToGuess.count(letter)
                    if lettercount > 0:
                        letterlist.append(letter)
                        letterfind = ListToGuess.index(letter)
                        ListToGuess.pop(letterfind)
                    if lettercount == 0:
                        RunMe = False
                        LTGL.insert(letterfind, letter)
                        DisplayLetter(LTGL, letterlist, lettersguessed)
            HangmanDrawImg(Mistakes)
            pygame.display.flip()
            clock.tick(50)
        except UnboundLocalError:
            pass
    pygame.quit()
    sys.exit


if __name__ == '__main__':
    main()

Poin bonus jika seseorang dapat menunjukkan beberapa kode yang tidak mengikuti PEP8. Juga, perhatikan bahwa ini masih jauh dari selesai, jadi jika ada beberapa kode sebagai komentar, saya akan menghapusnya nanti.

0
Selwyn 6 Juli 2020, 18:17

1 menjawab

Jawaban Terbaik

Ini di sini:

pos_ = (AdjustMe + (27 * int(IndexNumber), 226))

Sama dengan:

pos_ = (int + (int * int, int)) ->
       (int + tuple)

Bagaimana python seharusnya menambahkan bilangan bulat itu ke tupel itu?

Inilah cara yang diperbaiki:

if letterlist[x-1] == "g":
    pos_ = (27 * int(IndexNumber) + AdjustMe, 226 + AdjustMe)
else:
    pos_ = (27 * int(IndexNumber) + AdjustMe, 230 + AdjustMe)
2
Ann Zen 6 Juli 2020, 15:20