Search code examples
pythonpython-3.xsimulationpymunk

How do I make this pymunk code work? It shows a black screen


I am new to stackoverflow. My code has a problem which the screen turns completely black. The error. I need the answer quickly so any help will be good. Heres the code:

import pygame, sys
import pymunk
import pymunk.pygame_util
from pymunk.vec2d import Vec2d
size = (800, 800)
FPS = 120

space = pymunk.Space()
space.gravity = (0,250)

pygame.init()
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

class Ball:
    global space
    def __init__(self, pos):
        self.body = pymunk.Body(1,1, body_type = pymunk.Body.DYNAMIC)
        self.body.position = pos
        self.radius = 60
        self.shape = pymunk.Circle(self.body, self.radius)

        space.add(self.body, self.shape)
        def draw(self):
            x = int(self.body.position.x)
            y = int(self.body.position.y)
            pygame.draw.circle(screen, (255,0,0), (x,y), self.radius)

balls = []
balls.append(Ball((400,0)))
balls.append(Ball((100,0)))
balls.append(Ball((600,100)))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

            screeen.fill(217,217,217)

            for ball in balls:
                ball.draw()

            space.step(1/50)
            pygame.display.update()
            clock.tick(FPS)

Any help on what to do??? Thanks.


Solution

    1. screeen.fill(217,217,217) should be screen.fill((217,217,217))

    2. The Indentation is not correct. In particular, the scene must be pulled in the application loop and not in the bleed loop:

    import pygame, sys
    import pymunk
    import pymunk.pygame_util
    from pymunk.vec2d import Vec2d
    size = (800, 800)
    FPS = 120
    
    space = pymunk.Space()
    space.gravity = (0,250)
    
    pygame.init()
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()
    
    class Ball:
        global space
        def __init__(self, pos):
            self.body = pymunk.Body(1,1, body_type = pymunk.Body.DYNAMIC)
            self.body.position = pos
            self.radius = 60
            self.shape = pymunk.Circle(self.body, self.radius)
    
            space.add(self.body, self.shape)
    
        # INDENTATION
        #<--|
    
        def draw(self):
            x = int(self.body.position.x)
            y = int(self.body.position.y)
            pygame.draw.circle(screen, (255,0,0), (x,y), self.radius)
    
    balls = []
    balls.append(Ball((400,0)))
    balls.append(Ball((100,0)))
    balls.append(Ball((600,100)))
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
    
        # INDENTATION
        #<------|    
    
        screen.fill((217,217,217))
    
        for ball in balls:
            ball.draw()
    
        space.step(1/50)
        pygame.display.update()
        clock.tick(FPS)