Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading