Цвет текста не меняется - PullRequest
0 голосов
/ 29 апреля 2020

Я делаю подводную игру в Pygame, но текст в меню не хочет менять цвет, когда я закрываю его мышью. Я попытался обновить отображение sh в этой части и изменить местоположение предложений, но ничего не помогло. Там не было никаких синтаксических ошибок. Вот код:

import pygame
pygame.init()
screen = pygame.display.set_mode((600, 500), pygame.FULLSCREEN)
pygame.display.set_caption('Podmornca')
run = True
black = (0,0,0)
red = (255,0,0)
colour1 = black
colour2 = black
colour3 = black
location = pygame.mouse.get_pos()
background = pygame.image.load('meni.png')
clock = pygame.time.Clock()
font = pygame.font.SysFont('bauhaus93', 20)
text1 = font.render('SINGLE PLAYER', 1, barva1)
text2 = font.render('MULTIPLAYER', 1, barva2)
text3 = font.render('QUIT', 1, barva3)
pygame.display.update()
def grafika():
    clock.tick(60)
    screen.blit(background, (0,0))
    screen.blit(text1, (100, 200))
    screen.blit(text2, (100, 250))
    screen.blit(text3, (100, 300))
    pygame.display.update()     
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    if location == (100, 200):
        colour1 = red
    if polozaj == (100, 250):
        colour2 = red
    if location == (100, 300):
        colour3 = red
    grafika()
pygame.quit()

Может кто-нибудь сказать мне, где я запутался и как это исправить?

1 Ответ

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

Вы должны получить положение мыши в главном приложении. L oop.

Визуализировать каждый текст дважды, один раз черным и один раз красным:

text1 = font.render('SINGLE PLAYER', 1, black)
text2 = font.render('MULTIPLAYER', 1, black)
text3 = font.render('QUIT', 1, black)
text1red = font.render('SINGLE PLAYER', 1, red)
text2red = font.render('MULTIPLAYER', 1, red)
text3red = font.render('QUIT', 1, red)

Использование pygame.Rect и collidepoint(), чтобы определить, находится ли мышь на тексте. Нарисуйте красный текст, если мышь находится над текстом, и нарисуйте текст blcok еще. Например:

def drawText(text, textRed, pos):
    location = pygame.mouse.get_pos()
    if text.get_rect(topleft = pos).collidepoint(location):
        screen.blit(textRed, pos)
    else:
        screen.blit(text, pos)

См. Пример:

import pygame

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption('Podmornca')
run = True
black = (0,0,0)
red = (255,0,0)
background = pygame.image.load('meni.png')
clock = pygame.time.Clock()
font = pygame.font.SysFont('bauhaus93', 20)

text1 = font.render('SINGLE PLAYER', 1, black)
text2 = font.render('MULTIPLAYER', 1, black)
text3 = font.render('QUIT', 1, black)
text1red = font.render('SINGLE PLAYER', 1, red)
text2red = font.render('MULTIPLAYER', 1, red)
text3red = font.render('QUIT', 1, red)

def drawText(text, textRed, pos):
    location = pygame.mouse.get_pos()
    if text.get_rect(topleft = pos).collidepoint(location):
        screen.blit(textRed, pos)
    else:
        screen.blit(text, pos)

def grafika():
    clock.tick(60)
    screen.blit(background, (0,0))
    drawText(text1, text1red, (100, 200))
    drawText(text2, text2red, (100, 250))
    drawText(text3, text3red, (100, 300))
    pygame.display.update()     

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    grafika()
pygame.quit()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...