2016-07-15 2 views
0

저는 작업중인 작은 프로젝트의 제목을 렌더링하려고합니다. 옵션 세트를 생성하는 데 사용 된 것과 동일한 코드를 복제했지만 아직 작동하지 않습니다. 누군가 내가 잘못 가고 있는지 알고 있는지 궁금 해서요? 아마 뭔가 분명 할 것 같지만 파이썬에 대해서는 경험이 없습니다. 난 당신이 어떻게 할 정확히 모르는Python/Pygame 제목 렌더링

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() 

답변

0

:

여기에 코드입니다. 하지만 당신은 단순한 오타가 있다고 생각합니다. title = [Title("NOTPONG", (150,20)) 줄은 닫는 괄호가있는 title = [Title("NOTPONG", (150,20))]이어야합니다. 그리고 title_font = pygame.display.font.Font(None, 42) 줄은 .display 부분이없는 title_font = pygame.font.Font(None, 42)이어야합니다. 오류를 수정 한 후에는 메뉴 화면이 나타나지만 아무것도하지 않습니다. 나는 당신이 단지 메뉴를 보여 주려고했는지 또는 아무 것도 할 수 없거나 보여줄 메뉴를 얻을 수 없다고 확신하지 못합니다.

+0

감사합니다. –