0

I'm trying to create a Hangman-ish game, and I've noticed something weird going on with the screen.blit.

In the word 'CANADA', there are three A's. I have successfully created a list with all the letters of the alphabet, and when I click on 'C' it actually transfers the 'C' to it's designated place. However, when you click the 'A', only the first 'A' shows up.

Here is my code:

def canada1():
    name = ""
    main_font = "brain flower euro.ttf"
    font1 = pygame.font.Font((main_font), 50)
    font2 = pygame.font.Font((main_font), 40)

    country = list('canada')
    flag = 'canada_flag.jpeg'
    black = 0, 0, 0
    red = 255, 0, 0
    white = 255, 255, 255
    flag1 = pygame.image.load(flag).convert()

    aPosition = 285, 325
    bPosition = 335, 325
    cPosition = 385, 325
    dPosition = 435, 325
    ePosition = 485, 325
    fPosition = 535, 325
    gPosition = 585, 325
    hPosition = 635, 325
    iPosition = 685, 325
    jPosition = 285, 375
    kPosition = 335, 375
    lPosition = 385, 375
    mPosition = 435, 375
    nPosition = 485, 375
    oPosition = 535, 375
    pPosition = 585, 375
    qPosition = 635, 375
    rPosition = 685, 375
    sPosition = 285, 425
    tPosition = 335, 425
    uPosition = 385, 425
    vPosition = 435, 425
    wPosition = 485, 425
    xPosition = 535, 425
    yPosition = 585, 425
    zPosition = 635, 425

    letters = list('abcdefghijklmnopqrstuvwxyza')
    a = font2.render(str(letters[0]), True, (black))
    b = font2.render(str(letters[1]), True, (black))
    c = font2.render(str(letters[2]), True, (black))
    d = font2.render(str(letters[3]), True, (black))
    e = font2.render(str(letters[4]), True, (black))
    f = font2.render(str(letters[5]), True, (black))
    g = font2.render(str(letters[6]), True, (black))
    h = font2.render(str(letters[7]), True, (black))
    i = font2.render(str(letters[8]), True, (black))
    j = font2.render(str(letters[9]), True, (black))
    k = font2.render(str(letters[10]), True, (black))
    l = font2.render(str(letters[11]), True, (black))
    m = font2.render(str(letters[12]), True, (black))
    n = font2.render(str(letters[13]), True, (black))
    o = font2.render(str(letters[14]), True, (black))
    p = font2.render(str(letters[15]), True, (black))
    q = font2.render(str(letters[16]), True, (black))
    r = font2.render(str(letters[17]), True, (black))
    s = font2.render(str(letters[18]), True, (black))
    t = font2.render(str(letters[19]), True, (black))
    u = font2.render(str(letters[20]), True, (black))
    v = font2.render(str(letters[21]), True, (black))
    w = font2.render(str(letters[22]), True, (black))
    x = font2.render(str(letters[23]), True, (black))
    y = font2.render(str(letters[24]), True, (black))
    z = font2.render(str(letters[25]), True, (black))

    a1 = a.get_rect()
    b1 = b.get_rect()
    c1 = c.get_rect()
    d1 = d.get_rect()
    e1 = e.get_rect()
    f1 = f.get_rect()
    g1 = g.get_rect()
    h1 = h.get_rect()
    i1 = i.get_rect()
    j1 = j.get_rect()
    k1 = k.get_rect()
    l1 = l.get_rect()
    m1 = m.get_rect()
    n1 = n.get_rect()
    o1 = o.get_rect()
    p1 = p.get_rect()
    q1 = q.get_rect()
    r1 = r.get_rect()
    s1 = s.get_rect()
    t1 = t.get_rect()
    u1 = u.get_rect()
    v1 = v.get_rect()
    w1 = w.get_rect()
    x1 = x.get_rect()
    y1 = y.get_rect()
    z1 = z.get_rect()

    a1.x, a1.y = 285, 325
    b1.x, b1.y = 335, 325
    c1.x, c1.y = 385, 325
    d1.x, d1.y = 435, 325
    e1.x, e1.y = 485, 325
    f1.x, f1.y = 535, 325
    g1.x, g1.y = 585, 325
    h1.x, h1.y = 635, 325
    i1.x, i1.y = 685, 325
    j1.x, j1.y = 285, 375
    k1.x, k1.y = 335, 375
    l1.x, l1.y = 385, 375
    m1.x, m1.y = 435, 375
    n1.x, n1.y = 485, 375
    o1.x, o1.y = 535, 375
    p1.x, p1.y = 585, 375
    q1.x, q1.y = 635, 375
    r1.x, r1.y = 685, 375
    s1.x, s1.y = 285, 425
    t1.x, t1.y = 335, 425
    u1.x, u1.y = 385, 425
    v1.x, v1.y = 435, 425
    w1.x, w1.y = 485, 425
    x1.x, x1.y = 535, 425
    y1.x, y1.y = 585, 425
    z1.x, z1.y = 635, 425

    while True:
        for evt in pygame.event.get():
            if evt.type == KEYDOWN:
                if evt.unicode.isalpha():
                    name += evt.unicode
                elif evt.key == K_SPACE:
                    name += " "
                elif evt.key == K_BACKSPACE:
                    name = name[:-1]
            elif evt.type == QUIT:
                pygame.quit()
                sys.exit()
            if evt.type == MOUSEBUTTONDOWN:
                if a1.collidepoint(pygame.mouse.get_pos()):
                    letter = letters[0]
                    check = country.count(letter)
                    if check >= 1:
                        if letter == letters[0]:
                            aPosition = 375, 235
                            screen.blit(a, (425, 235))
                            screen.blit(a, (475, 235))
                            a1.x, a1.y = -500, -500
                        elif letter == letters[2]:
                            cPosition = 300, 235
                            c1.x, c1.y = -500, -500
                        elif letter == letters[13]:
                            nPosition = 350, 235
                            n1.x, n1.y = -500, -500
                        elif letter == letters[3]:
                            dPosition = 400, 235
                            d1.x, d2.y = -500, -500
                    else:
                        a2 = font.render(str(letters[0]), True, (red))
                        screen.blit(a2, (cPosition))
                        time.sleep(1)
                        aPosition = -500, -500
                        a1.x, a1.y = -500, -500
                elif b1.collidepoint(pygame.mouse.get_pos()):
                    letter = letters[1]
                    check = country.count(letter)
                    if check >= 1:
                        if letter == letters[0]:
                            aPosition = 325, 235
                            a3 = screen.blit((a), (375, 235))
                            a4 = screen.blit((a), (425, 235))
                            a1.x, a1.y = -500, -500
                        elif letter == letters[2]:
                            cPosition = 300, 235
                            c1.x, c1.y = -500, -500
                        elif letter == letters[13]:
                            nPosition = 350, 235
                            n1.x, n1.y = -500, -500
                        elif letter == letters[3]:
                            dPosition = 400, 235
                            d1.x, d2.y = -500, -500
                    else:
                        b2 = font.render(str(letters[1]), True, (red))
                        screen.blit(b2, (485, 325))
                        time.sleep(1)
                        bPosition = -500, -500
                        b1.x, b1.y = -500, -500
                elif c1.collidepoint(pygame.mouse.get_pos()):
                    letter = letters[2]
                    check = country.count(letter)
                    if check >= 1:
                        if letter == letters[0]:
                            aPosition = 325, 235
                            screen.blit((a), (375, 235))
                            screen.blit((a), (425, 235))
                            a1.x, a1.y = -500, -500
                        elif letter == letters[2]:
                            cPosition = 300, 235
                            c1.x, c1.y = -500, -1
                        elif letter == letters[13]:
                            nPosition = 350, 235
                            n1.x, n1.y = -1, -1
                        elif letter == letters[3]:
                            dPosition = 400, 235
                            d1.x, d2.y = -1, -1
                    else:
                        c2 = font.render(str(letters[2]), True, (red))
                        screen.blit(c2, (cPosition))
                        time.sleep(1)
                        cPosition = -1, -1
                        c1.x, c1.y = -1, -1


        screen.fill((255, 255, 255))

        screen.blit(flag1, (300, 10))
        pygame.draw.line(screen, black, (275, 0), (275, 300), (6))
        pygame.draw.line(screen, black, (715, 0), (715, 300), (6))
        pygame.draw.line(screen, black, (0, 300), (1000, 300), (6))

        block = font1.render(name, True, black)
        rect = block.get_rect(center = (500, 600))
        screen.blit(block, rect)

        pygame.draw.rect(screen, black, (375, 550, 250, 100), (2))
        pygame.draw.line(screen, black, (775, 250), (775, 50), (4))
        pygame.draw.line(screen, black, (750, 250), (800, 250), (4))
        pygame.draw.line(screen, black, (775, 50), (925, 50), (4))
        pygame.draw.line(screen, black, (925, 50), (925, 175), (4))
        pygame.draw.circle(screen, black, (925, 100), 30, (0))
        pygame.draw.line(screen, black, (925, 125), (925, 200), (4))
        pygame.draw.line(screen, black, (885, 160), (965, 160), (4))
        pygame.draw.line(screen, black, (925, 200), (900, 225), (4))
        pygame.draw.line(screen, black, (925, 200), (950, 225), (4))

        pygame.draw.line(screen, black, (295, 275), (320, 275), (5))
        pygame.draw.line(screen, black, (370, 275), (395, 275), (5))
        pygame.draw.line(screen, black, (445, 275), (470, 275), (5))
        pygame.draw.line(screen, black, (520, 275), (545, 275), (5))
        pygame.draw.line(screen, black, (595, 275), (620, 275), (5))
        pygame.draw.line(screen, black, (670, 275), (695, 275), (5))

        a2 = screen.blit((a), (aPosition))
        b2 = screen.blit((b), (bPosition))
        c2 = screen.blit((c), (cPosition))
        d2 = screen.blit((d), (dPosition))
        e2 = screen.blit((e), (ePosition))
        f2 = screen.blit((f), (fPosition))
        g2 = screen.blit((g), (gPosition))
        h2 = screen.blit((h), (hPosition))
        i2 = screen.blit((i), (iPosition))
        j2 = screen.blit((j), (jPosition))
        k2 = screen.blit((k), (kPosition))
        l2 = screen.blit((l), (lPosition))
        m2 = screen.blit((m), (mPosition))
        n2 = screen.blit((n), (nPosition))
        o2 = screen.blit((o), (oPosition))
        p2 = screen.blit((p), (pPosition))
        q2 = screen.blit((q), (qPosition))
        r2 = screen.blit((r), (rPosition))
        s2 = screen.blit((s), (sPosition))
        t2 = screen.blit((t), (tPosition))
        u2 = screen.blit((u), (uPosition))
        v2 = screen.blit((v), (vPosition))
        w2 = screen.blit((w), (wPosition))
        x2 = screen.blit((x), (xPosition))
        y2 = screen.blit((y), (yPosition))
        z2 = screen.blit((z), (zPosition))

        if a1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            a2 = font.render(str(letters[0]), True, (red))
            screen.blit(a2, (285, 325))
        elif b1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            b2 = font.render(str(letters[1]), True, (red))
            screen.blit(b2, (335, 325))
        elif c1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            c2 = font.render(str(letters[2]), True, (red))
            screen.blit(c2, (385, 325))
        elif d1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            d2 = font.render(str(letters[3]), True, (red))
            screen.blit(d2, (435, 325))
        elif e1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            e2 = font.render(str(letters[4]), True, (red))
            screen.blit(e2, (485, 325))
        elif f1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            f2 = font.render(str(letters[5]), True, (red))
            screen.blit(f2, (535, 325))
        elif g1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            g2 = font.render(str(letters[6]), True, (red))
            screen.blit(g2, (585, 325))
        elif h1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            h2 = font.render(str(letters[7]), True, (red))
            screen.blit(h2, (635, 325))
        elif i1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            i2 = font.render(str(letters[8]), True, (red))
            screen.blit(i2, (685, 325))
        elif j1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            j2 = font.render(str(letters[9]), True, (red))
            screen.blit(j2, (285, 375))
        elif k1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            k2 = font.render(str(letters[10]), True, (red))
            screen.blit(k2, (335, 375))
        elif l1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            l2 = font.render(str(letters[11]), True, (red))
            screen.blit(l2, (385, 375))
        elif m1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            m2 = font.render(str(letters[12]), True, (red))
            screen.blit(m2, (435, 375))
        elif n1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            n2 = font.render(str(letters[13]), True, (red))
            screen.blit(n2, (485, 375))
        elif o1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            o2 = font.render(str(letters[14]), True, (red))
            screen.blit(o2, (535, 375))
        elif p1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            p2 = font.render(str(letters[15]), True, (red))
            screen.blit(p2, (585, 375))
        elif q1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            q2 = font.render(str(letters[16]), True, (red))
            screen.blit(q2, (635, 375))
        elif r1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            r2 = font.render(str(letters[17]), True, (red))
            screen.blit(r2, (685, 375))
        elif s1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            s2 = font.render(str(letters[18]), True, (red))
            screen.blit(s2, (285, 425))
        elif t1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            t2 = font.render(str(letters[19]), True, (red))
            screen.blit(t2, (335, 425))
        elif u1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            u2 = font.render(str(letters[20]), True, (red))
            screen.blit(u2, (385, 425))
        elif v1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            v2 = font.render(str(letters[21]), True, (red))
            screen.blit(v2, (435, 425))
        elif w1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            w2 = font.render(str(letters[22]), True, (red))
            screen.blit(w2, (485, 425))
        elif x1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            x2 = font.render(str(letters[23]), True, (red))
            screen.blit(x2, (535, 425))
        elif y1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            y2 = font.render(str(letters[24]), True, (red))
            screen.blit(y2, (585, 425))
        elif z1.collidepoint(pygame.mouse.get_pos()):
            font = pygame.font.Font((main_font), 40)
            z2 = font.render(str(letters[25]), True, (red))
            screen.blit(z2, (635, 425))

        if name == country:
            return
        pygame.display.update()

Absolutely everything else works, but the 'A' problem.

Help!

JaredCubilla
  • 538
  • 1
  • 8
  • 24

2 Answers2

0

I don't use PyGame, but looking at your program, it looks like this is what is happening:

  1. Check for mouse events
  2. Draw any correctly selected letters
  3. Clear the screen (screen.fill((255, 255, 255)))
  4. Redraw everything (only includes original copies of letters)

So you can see that the 'A's will be drawn momentarily but then erased in parts 3 and 4. If you need to clear the screen in every iteration, then you will need to keep track of objects to keep drawing, by adding them to a list, for example.

Radio-
  • 3,151
  • 21
  • 22
0

I screen.blitted something a separate A, with different positions. It's original position was outside the screen, and popped into the proper line of code when the 'A' was pressed.

JaredCubilla
  • 538
  • 1
  • 8
  • 24