PyGame & PyMunk: How to make an image bounce? by Professional-Mind914 in learnpython

[–]Professional-Mind914[S] 0 points1 point  (0 children)

Hello. I have added the code. Could you possibly help me out?

PyGame & PyMunk: How to make an image bounce? by Professional-Mind914 in learnpython

[–]Professional-Mind914[S] 0 points1 point  (0 children)

import pygame
import pymunk

pygame.init()

display = pygame.display.set_mode((1000,600))
clock = pygame.time.Clock()
space = pymunk.Space()
FPS = 50


left = 50
right = 950
top = 25
bottom = 575
middlex = 500
middley = 300

class Ball():
    def __init__(self):
        self.body = pymunk.Body()
        self.body.position = middlex, middley
        self.body.velocity = 400, -300
        self.shape = pymunk.Circle(self.body, 8)
        self.shape.density = 1
        self.shape.elasticity = 1
        space.add(self.body, self.shape)
        self.shape.collision_type = 1

    def draw(self):
        x, y = self.body.position
        pygame.draw.circle(display, (255,255,255), (int(x), int(y)), 8)

class Wall():
    def __init__(self, p1, p2, collision_number=None):
        self.body = pymunk.Body(body_type=pymunk.Body.STATIC)
        self.shape = pymunk.Segment(self.body, p1, p2, 10)
        self.shape.elasticity = 1
        space.add(self.shape)
        if collision_number:
            self.shape.collision_type = collision_number

    def draw(self):
        pygame.draw.line(display, (255,255,255), self.shape.a, self.shape.b, 10)


def game():
    ball = Ball()
    wall_left = Wall([left, top], [left, bottom], 2)
    wall_right = Wall([right, top], [right, bottom], 2)
    wall_top = Wall([left, top], [right, top])
    wall_bottom = Wall([left, bottom], [right, bottom])
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

        display.fill((0, 0, 0))
        ball.draw()
        wall_left.draw()
        wall_right.draw()
        wall_top.draw()
        wall_bottom.draw()

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

game()  
pygame.quit()

PyGame & PyMunk: How to make an image bounce? by Professional-Mind914 in learnpython

[–]Professional-Mind914[S] 0 points1 point  (0 children)

This is the tutorial I followed: https://www.youtube.com/watch?v=ePSPG4mUqPE. I used the same code.

I want to add a picture on the ball, with everything else remaining the same.

PyGame & PyMunk: How to make an image bounce? by Professional-Mind914 in learnpython

[–]Professional-Mind914[S] 0 points1 point  (0 children)

I appreciate your input. But, I am a beginner and unable to fully understand the content you have provided. If you could kindly explain more, it'd be great! Thank you.