Search code examples
pippygamepycharm

Unavailable response in Pygame


I'm using a pip version of Pygame, and I've written all my code. There's absolutely no errors in the code (for all I know). I mean, there could be some type of hidden error, but all I'm seeing is my everyday on the top right of the screen. I run it, and it's barely displaying the wanted output. It's like a huge chunk of code is acting... null.

Anyways, here's my code.

import pygame as pg
import pygame.display

pg.init()

x = 1600
y = 900

scrn = pg.display.set_mode((x, y))

red = (255, 0, 0)
white = (255, 255, 255)
grey = (128, 128, 128)

scrn.fill(white)

pg.draw.rect(scrn, grey, pg.Rect(30, 30, 60, 60))
pg.display.flip()

def message(msg, colour):
    mesg = font_style.render(msg, True, colour)

pg.display.set_caption("Toronto Raptors - Stats Viewer by Krish")

new_icon = pg.image.load("Raptors-Logo.png")

pg.display.set_icon(new_icon)

players = ["Pascal Siakam", "pascal siakam", "PASCAL SIAKAM", "Pascal siakam", "pascal Siakam", "Scottie Barnes", "scottie barnes", "SCOTTIE BARNES", "Scottie barnes", "scottie Barnes", "Jakob Poeltl", "jakob poeltl" "JAKOB POELTL", "Jakob poeltl", "jakob Poeltl"]

pg.draw.rect(scrn, grey, pg.Rect(30, 30, 60, 60))
pygame.display.flip()

def main():
    screen = pg.display.set_mode((900, 600))
    font = pg.font.Font(None, 32)
    clock = pg.time.Clock()
    input_box = pg.Rect(25, 540, 140, 32)
    color_inactive = pg.Color('red2')
    color_active = pg.Color((30, 30, 30))
    color = color_inactive
    active = False
    text = ''
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            if event.type == pg.MOUSEBUTTONDOWN:
                if input_box.collidepoint(event.pos):
                    active = not active
                else:
                    active = False
                color = color_active if active else color_inactive
            if event.type == pg.KEYDOWN:
                if active:
                    if event.key == pg.K_RETURN:
                        print(text)
                        text = ''
                    elif event.key == pg.K_BACKSPACE:
                        text = text[:-1]
                    else:
                        text += event.unicode

        screen.fill((white))
        txt_surface = font.render(text, True, color)
        width = max(200, txt_surface.get_width()+10)
        input_box.w = width
        screen.blit(txt_surface, (input_box.x+5, input_box.y+5))
        pg.draw.rect(screen, color, input_box, 2)
        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

Now, take a look at the output: Click for output

Either something's going on with Pygame, or my code's just wrong.

The part that's not showing is my rectangle, even thought the code is perfectly fine.

pg.draw.rect(scrn, grey, pg.Rect(30, 30, 60, 60))
pg.display.flip()

Solution

  • Your code has some issues that need to be addressed.

    Multiple Display Instances: You have initialized two different display instances. One is with scrn = pg.display.set_mode((x, y)) and another is inside the main() function with screen = pg.display.set_mode((900, 600)). You should ideally have only one display instance.

    Multiple Initializations: You've also initialized Pygame twice, once at the beginning and once at the end in the if name == 'main': block.

    Unused Function: The function message(msg, colour) is defined but never used.

    Here is a refactored version of your code that should display the rectangle as you intended:

    import pygame as pg
    
    pg.init()
    
    x = 1600
    y = 900
    screen = pg.display.set_mode((x, y))
    
    pg.display.set_caption("Toronto Raptors - Stats Viewer by Krish")
    
    new_icon = pg.image.load("Raptors-Logo.png")
    pg.display.set_icon(new_icon)
    
    grey = (128, 128, 128)
    white = (255, 255, 255)
    
    def main():
        clock = pg.time.Clock()
        input_box = pg.Rect(25, 540, 140, 32)
        color_inactive = pg.Color('red2')
        color_active = pg.Color((30, 30, 30))
        color = color_inactive
        active = False
        text = ''
        done = False
    
        while not done:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    done = True
                if event.type == pg.MOUSEBUTTONDOWN:
                    if input_box.collidepoint(event.pos):
                        active = not active
                    else:
                        active = False
                    color = color_active if active else color_inactive
                if event.type == pg.KEYDOWN:
                    if active:
                        if event.key == pg.K_RETURN:
                            print(text)
                            text = ''
                        elif event.key == pg.K_BACKSPACE:
                            text = text[:-1]
                        else:
                            text += event.unicode
    
            font = pg.font.Font(None, 32)
            screen.fill(white)
            pg.draw.rect(screen, grey, pg.Rect(30, 30, 60, 60))
            txt_surface = font.render(text, True, color)
            screen.blit(txt_surface, (input_box.x+5, input_box.y+5))
            pg.draw.rect(screen, color, input_box, 2)
            pg.display.flip()
            clock.tick(30)
    
    
    if __name__ == '__main__':
        main()
        pg.quit()