Проблема с отображением буквы при нажатии кнопок в Pygame - PullRequest
0 голосов
/ 09 апреля 2020

Я застрял в создании игры "Hang-Man".

Я сделал несколько кнопок на экране. Проблема в том, что когда я нажимаю какой-то алфавит, я хочу показать синюю букву, которую я нажал на экране ... Но это не работает. Я не вижу синих букв ..

Более того, в этом коде

        if (55 <= mouseposx <= 110) and (155 <= mouseposy <= 210):
            answermessage.append('A')
        elif (155 <= mouseposx <= 210) and (155 <= mouseposy <= 210):
            answermessage.append('B')

Должен ли я сделать это грубой силы (?) От А до Я?

Вот мой полный код.

(извините за мой плохой англи sh ... я кореец ..: ()

import sys
import pygame
from pygame.locals import QUIT, Rect,\
    MOUSEBUTTONDOWN, MOUSEBUTTONUP

pygame.init()
SURFACE = pygame.display.set_mode((900, 600))
FPSCLOCK = pygame.time.Clock()

def main():
    mouseposx : int = 0
    mouseposy : int = 0
    sysfont = pygame.font.SysFont(None, 46)
    message = [[0]*4 for i in range(7)]
    message_rect = [[0]*4 for i in range(7)]
    answermessage = []

    for i in range(0, 7):
        for j in range(0, 4):
            if not ((i == 5 and j == 3) or (i == 6 and j == 3)):
                message[i][j] = sysfont.render(chr(65+7*j+i), True, (0, 255, 0))
                message_rect[i][j] = message[i][j].get_rect()
                message_rect[i][j].center = (80 + 100 * i, 180 + 100 * j)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                mouseposx = event.pos[0]
                mouseposy = event.pos[1]
            elif event.type == MOUSEBUTTONUP:
                mouseposx = 0
                mouseposy = 0

        SURFACE.fill((255, 255, 255))
        for i in range(0, 7):
            for j in range(0, 4):
                if not ((i == 5 and j == 3) or (i == 6 and j == 3)):
                    SURFACE.blit(message[i][j], message_rect[i][j])

        for i in range(0, 7):
            for j in range(0, 4):
                if not ((i == 5 and j == 3) or (i == 6 and j == 3)):
                    pygame.draw.rect(SURFACE, (0, 0, 0), (50+100*i, 150+100*j, 60, 60), 5)

        if (55 <= mouseposx <= 110) and (155 <= mouseposy <= 210):
            answermessage.append('A')
        elif (155 <= mouseposx <= 210) and (155 <= mouseposy <= 210):
            answermessage.append('B')

        for i in range(0, len(answermessage)):
            answermessaged = sysfont.render(answermessage[i], True, (0,0,255))
            answermessaged_rect = answermessaged.get_rect()
            answermessaged_rect.center = (40, 60)
            SURFACE.blit(answermessaged, answermessaged_rect)

        pygame.display.update()
        FPSCLOCK.tick(1)

if __name__ == '__main__':
    main()

1 Ответ

0 голосов
/ 09 апреля 2020

Мне нравится делать функцию кнопок и возвращаться, если вы нажмете кнопку

пример:

def button(x,y,w,h,text,mousex,mousey):
    hover = False
    if mousex > x and mousex < x + w:
        if mousey > y and mousey < y + h:
            hover = True
    #usually i would have a if click but since you only getting mouse pos when click it should be fine
    txtobj = sysfont.render(text,True,(0,0,0))
    SURFACE.blit(txtobj, (x,y))
    return hover #return True if mouse clicked on button

, тогда вы можете вызывать это для каждой буквы. Таким образом, у меня был бы список

alphabet = ["A","B","C","D"...]

, теперь вы можете l oop через них

for i,letter in enumerate(alphabet): # enumerate gets the letter and its position in the list
    letter_clicked = button(55 + (100*i),155,55,55,letter,mouseposx,mouseposy)
    if letter_clicked:
        answermessage.append(letter)

Что касается синих букв, не появляющихся, начните с того, чтобы не надевать их

answermessaged_rect.center = (40 + (i*50), 60) # 50 is a guess

, если это не работает, возможно, попробуйте другой шрифт

Вы можете получить шрифты, выполнив

sysfont = pygame.font.Font(pygame.font.match_font("Calibri"), 46)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...