Harry Eastell Harry Eastell - 29 days ago 16
Python Question

Python/Pygame Title Rendering

I'm trying to render a title for a little project I'm working on. I've replicated the same code which was used to create the options set (merely for ease) yet it isn't working. I was wondering if anyone knows where I'm going wrong? it's probably something really obvious but I'm not experienced when it comes to python.

Here's the code:

import pygame

class Option:

hovered = False

def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()

def draw(self):
self.set_rend()
screen.blit(self.rend, self.rect)

def set_rend(self):
self.rend = menu_font.render(self.text, True, self.get_color())

def get_color(self):
if self.hovered:
return (255, 255, 255)
else:
return (100, 100, 100)

def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos

class Title:

hovered = False

def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()

def draw(self):
self.set_rend()
screen.blit(self.rend, self.rect)

def set_rend(self):
self.rend = title_font.render(self.text, True, self.get_color())

def get_color(self):
if self.hovered:
return (255, 255, 255)
else:
return (255, 255, 255)

def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos

pygame.init()

screen = pygame.display.set_mode((480, 320))

menu_font = pygame.font.Font(None, 40)
options = [Option("PLAY GAME", (140, 105)), Option("OPTIONS", (155, 155)),
Option("QUIT", (180, 205)), Option("NOTPONG", (150,20))]

title_font = pygame.display.font.Font(None, 42)
title = [Title("NOTPONG", (150,20))

while True:
pygame.event.pump()
screen.fill((0, 0, 0))
for option in options:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
else:
option.hovered = False
option.draw()
pygame.display.update()

while True:
pygame.event.pump()
screen.fill((0, 0, 0))
for Title in title:
if title.rect.collidepoint(pygame.mouse.get_pos()):
title.hovered = True
else:
title.hovered = False
title.draw()
pygame.display.update()

Answer

I do not know what exactly you want to happen. But I think you simple have some typos. The line title = [Title("NOTPONG", (150,20)) needs to be title = [Title("NOTPONG", (150,20))] with the closing bracket. And the line title_font = pygame.display.font.Font(None, 42) needs to be title_font = pygame.font.Font(None, 42) without the .display part. After fixing those errors your menu screen appears but does not do anything. I'm not sure if you were just trying to get the menu to show or you can't get the menu to do anything or show.