pythonpygame

PyGame not drawing rectangle


pygame not working pygame is not drawing any rectangles or any other shapes all I see is a white and blank screen, and for people wondering yeah I have pygame installed

I am new to oop programming. first) main.py second) setting.py

from setting import *
import pygame


class Game:
    def __init__(self):
        pass
    def game(self):
        pygame.init()
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption(TITLE)
        clock = pygame.time.Clock()
        running = True
        x=0
        y=0
        for row in MAP:
                print(row)
                for pos in row:
                    print(pos)
                    if pos == "x":    
                        pygame.draw.rect(screen, [0, 0, 255], [x, y, 20, 20], 1)
                        x+=20
        while running:
            clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
            
            screen.fill(COLOR)
            pygame.display.flip()
        pygame.quit()
        quit()


game = Game()
game.game()

second) setting.py

FPS = 60
WIDTH = 800
HEIGHT = 600
COLOR = [255,255,255]
TITLE = 'test'


MAP = [['x', 'x', 'x','x','x', 'x', 'x','x','x', 'x', 'x','x']
      ,['x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x']
      ,['x', ' ', ' ', 'P', ' ', ' ', ' ', ' ', ' ', ' ', 'x']
      ,['x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x']
      ,['x', 'x', 'x','x','x', 'x', 'x','x','x', 'x', 'x','x']
]

Solution

  • You need to redraw the scene in each frame. The typical PyGame application loop has to:

    from setting import *
    import pygame
    
    class Game:
        def __init__(self):
            pass
        def game(self):
            pygame.init()
            screen = pygame.display.set_mode((WIDTH, HEIGHT))
            pygame.display.set_caption(TITLE)
            clock = pygame.time.Clock()
            running = True
    
            while running:
                # limit the frames per second
                clock.tick(FPS)
    
                # handle the events
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        running = False
                
                # clear the display
                screen.fill(COLOR)
    
                # draw all objects in the scene
                for y, row in enumerate(MAP):
                    for x, pos in enumerate(row):
                        if pos == "x":    
                            pygame.draw.rect(screen, [0, 0, 255], [x*20, y*20, 20, 20], 1)
    
                # update the display
                pygame.display.flip()
    
            pygame.quit()
            quit()
    
    game = Game()
    game.game()
    

    The definition of the map can be simplified:

    MAP = [
        'xxxxxxxxxxxx',
        'x          x',
        'x  p       x',
        'x          x',
        'xxxxxxxxxxxx'
    ]