0

I've created a button that I've imported from another class Button. However, after I create the button object, I still don't see it. How can I fix this? Below is my function that runs the game.

def run_game(width, height, fps, starting_scene):
    pygame.init()
    screen = pygame.display.set_mode((width, height))
    clock = pygame.time.Clock()

    active_scene = starting_scene


    while active_scene != None:
        pressed_keys = pygame.key.get_pressed()

        # Event filtering 
        filtered_events = []
        for event in pygame.event.get():
            quit_attempt = False
            if event.type == pygame.QUIT:
                quit_attempt = True
            elif event.type == pygame.KEYDOWN:
                alt_pressed = pressed_keys[pygame.K_LALT] or \
                              pressed_keys[pygame.K_RALT]
                if event.key == pygame.K_ESCAPE:
                    quit_attempt = True
                elif event.key == pygame.K_F4 and alt_pressed:
                    quit_attempt = True

            if quit_attempt:
                active_scene.Terminate()
            else:
                filtered_events.append(event)

        active_scene.ProcessInput(filtered_events, pressed_keys)
        active_scene.Update()
        active_scene.Render(screen)

        active_scene = active_scene.next

        pygame.display.flip()
        clock.tick(fps)

# The rest is code where you implement your game using the Scenes model 

class TitleScene(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)

    def ProcessInput(self, events, pressed_keys):
        for event in events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                # Move to the next scene when the user pressed Enter 
                self.SwitchToScene(GameScene())

            if event.type == pygame.KEYUP:
                print("You are hitting up!") 
                print(self.next)

    def Update(self):
        pass

    def Render(self, screen):
        # For the sake of brevity, the title scene is a blank red screen 
        print("where's the button")
        screen.fill((255, 0, 0))

        #Title Creation
        myfont = pygame.font.SysFont(("Moyko"), 50)
        textImage = myfont.render("Anime Pong", True, (0, 255, 0))
        screen.blit(textImage, (100,100))

        #Button Creation 
        #play_button = TextButton((50,50), (50,50), ((180, 20, 20)), "Play")
        button_01 = Button("Great!", (60, 30), my_great_function)
        #button_02 = Button("Boo!", (60, 70), my_fantastic_function, bg=(50, 200, 20))


def my_great_function():
    print("Great! " * 5)


def my_fantastic_function():
    print("Fantastic! " * 4)


class GameScene(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)

    def ProcessInput(self, events, pressed_keys):
        pass

    def Update(self):
        pass

    def Render(self, screen):
        # The game scene is just a blank blue screen 
        screen.fill((0, 0, 255))

run_game(400, 300, 60, TitleScene())
furas
  • 134,197
  • 12
  • 106
  • 148
turtlefish12
  • 241
  • 3
  • 12
  • use button `{}` to correctly format code in question. – furas Nov 25 '17 at 10:34
  • I see you create button, but I don't see that you draw it on screen . Where do you use `blit()` or `draw()` to put button on screen ? BTW: why do you create button in `Render` ? This way you create it many time but you could create it only once at start. – furas Nov 25 '17 at 10:37
  • BTW: you can use more `print()` to see which part of code is executed - maybe you do some `fill()` after you display button and it remove button from screen. You can also remove `fill()` for a while - to see if it draws button without `fill()` – furas Nov 25 '17 at 10:39
  • BTW: always add tag `python` - it highlights code. And more people will see your question. – furas Nov 25 '17 at 10:41
  • 1
    Please add the `Button` class to your code sample. – skrx Nov 25 '17 at 11:05

0 Answers0