Python Physics Library (Pymunk&Pygame Example code)

 Today, we are going to make this physics simulation using "Pymunk" and "Pygame". 



First, download the Pymunk and Pygame, using the "pip install pymunk" and "pip install pygame"

And write some basic code for Pymunk and Pygame:


    import pymunk
    import pygame
    
    pygame.init()
    place = pymunk.Space()
    place.gravity = (0, 500) 
    gameDisplay = pygame.display.set_mode((800, 800))
    time = pygame.time.Clock()

    run = True

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        gameDisplay.fill((217, 217, 217))
        pygame.display.update()
        time.tick(70)
 
First, if you don't know pymunk, the variable called place that I made just means that there is a physics. And place.gravity is just a way of saying that there is gravity. (0, 500) means that we are not going sideways which is X, instead, we are going down. Just like how gravity is! Well, you can change around the numbers and all. But what if you want the ball or whatever object to fall quicker? You can increase the value of 500 to 1000 or even more! But it would crash if you did some numbers like 10000000000. Also, it would go up, if you make the 500 to -500. Now you get the basics of Pymunk. 

Let's start typing or probably copying and pasting for you:

import pymunk
import pygame




def create_image(place, pos):
    body = pymunk.Body(1, 100, body_type=pymunk.Body.DYNAMIC)
    body.position = pos
    shape = pymunk.Circle(body, 30)
    place.add(body, shape)
    return shape


def draw_image(images):
    for image in images:
        pos_x = int(image.body.position.x)
        pos_y = int(image.body.position.y)
        pygame.draw.circle(gameDisplay, (0, 0, 0), (pos_x, pos_y), 52)


def static_floor(place, pos):
    body = pymunk.Body(body_type=pymunk.Body.STATIC)
    body.position = pos
    shape = pymunk.Circle(body, 60)
    place.add(body, shape)
    return shape


def draw_static(floors):
    for floor in floors:
        pos_x = int(floor.body.position.x)
        pos_y = int(floor.body.position.y)
        pygame.draw.circle(gameDisplay, (0, 0, 0), (pos_x, pos_y), 40)
pygame.init()
place = pymunk.Space()
place.gravity = (0, 500)
gameDisplay = pygame.display.set_mode((800, 800))
time = pygame.time.Clock()

run = True
images = []

floors = []
floors.append(static_floor(place, (400, 400)))
images.append(create_image(place, (350, 200)))

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    gameDisplay.fill((217, 217, 217))

    draw_image(images)
    draw_static(floors)
    place.step(1 / 50)
    pygame.display.update()
    time.tick(70)




This would probably show a dot on your screen falling and hitting another ball! So why doesn't the ball on the middle move? There are 3 types of body that you should know Static body, Dynamic body, and Kinematic body. So Static body is the body that doesn't move but can change the course of some balls falling. So it is the ball in the middle! What about the falling one? It is a dynamic body, it has all the same things as the static body. But expect that it reacts with gravity. So static doesn't react to gravity, but dynamic does. But what about kinematic? I didn't show it in the example, but it is the body that can be moved by the player. So this would be a great one for some platformer game. Now that would be all that you need to learn about to make something like this. But you can make it more interesting like the example that I showed you at the first time. 
It is this code:






import pygame
import pymunk




def create_image(space, pos):
    body = pymunk.Body(1, 100, body_type=pymunk.Body.DYNAMIC)
    body.position = pos
    shape = pymunk.Circle(body, 30)
    space.add(body, shape)
    return shape



def draw_image(images):
    for image in images:
        pos_x = int(image.body.position.x)
        pos_y = int(image.body.position.y)
        harry_rect = harry.get_rect(center=(pos_x, pos_y))
        gameDisplay.blit(harry, harry_rect)

def static_floor(space, pos):
    body = pymunk.Body(body_type=pymunk.Body.STATIC)
    body.position = pos
    shape = pymunk.Circle(body, 60)
    space.add(body, shape)
    return shape

def draw_static(floors):
    for floor in floors:
        pos_x = int(floor.body.position.x)
        pos_y = int(floor.body.position.y)
        pygame.draw.circle(gameDisplay, (0, 0, 0), (pos_x, pos_y), 40)
pygame.init()
harry = pygame.image.load("394-3941021_clarify-circle-color-silhouette-harry-potter-clipart-png.png")
harry = pygame.transform.scale(harry, [98, 98])
gameDisplay = pygame.display.set_mode((800, 800))
clock = pygame.time.Clock()
space = pymunk.Space()

space.gravity = (0, 500)
segment_body = pymunk.Body(body_type=pymunk.Body.STATIC)
segment_body1 = pymunk.Body(body_type=pymunk.Body.STATIC)
segment_body2 = pymunk.Body(body_type=pymunk.Body.STATIC)
segment_shape = pymunk.Segment(segment_body, (800, 750), (0, 750), 5)
segment_shape1 = pymunk.Segment(segment_body, (800, 750), (800, 0), 5)
segment_shape2 = pymunk.Segment(segment_body, (0, 750), (0, 0), 5)
space.add(segment_body, segment_shape)
space.add(segment_body1, segment_shape1)
space.add(segment_body2, segment_shape2)
space.collision_type = 1
#handler = space.add_collision_handler()
images = []

floors = []
floors.append(static_floor(space, (400, 400)))
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            images.append(create_image(space, event.pos))


    gameDisplay.fill((217, 217, 217))
    pygame.draw.line(gameDisplay, (0, 0, 0), (800, 770), (0, 770), 10)
    draw_image(images)
    draw_static(floors)
    space.step(1 / 50)
    pygame.display.update()
    clock.tick(120)



So put your image to the harry and see if it works! Just click around. Also, there are 3 types of shapes in pymunk. Which is Circle, Poly, Segment. So the circle is the balls, the segment is the line. And I don't really know about the poly. So that's it for today and I will be back with better lessons next time! 

Comments

Post a Comment