I'm making submarine game in pygame, but text in menu doesn't want to change color when I cover it with mouse. I tried to refresh display at that part, and changing where if sentences are, but nothing seemed to help. There wasn't any syntax errors. Here's the code:
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()
Can anyone tell me, where I messed up and how to fix it?
You have to get the mouse position in the main application loop.
Render the each text twice, once in black and once in red:
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)
Use pygame.Rect
and collidepoint()
to detect the if the mouse is on the text. Draw the red text if the mouse is on the text and draw the blcok text else. For instance:
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)
See the example:
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()