├── README.md └── tutorial.py /README.md: -------------------------------------------------------------------------------- 1 | # PyMunk-Physics-Simulation 2 | 3 | # 💻 Launch Your Software Development Career Today! 4 | 5 | 🎓 **No degree? No problem!** My program equips you with everything you need to break into tech and land an entry-level software development role. 6 | 7 | 🚀 **Why Join?** 8 | - 💼 **$70k+ starting salary potential** 9 | - 🕐 **Self-paced:** Complete on your own time 10 | - 🤑 **Affordable:** Low risk compared to expensive bootcamps or degrees 11 | - 🎯 **45,000+ job openings** in the market 12 | 13 | 👉 **[Start your journey today!](https://techwithtim.net/dev)** 14 | No experience needed—just your determination. Future-proof your career and unlock six-figure potential like many of our students have! 15 | -------------------------------------------------------------------------------- /tutorial.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import pymunk 3 | import pymunk.pygame_util 4 | import math 5 | 6 | pygame.init() 7 | 8 | WIDTH, HEIGHT = 1000, 800 9 | window = pygame.display.set_mode((WIDTH, HEIGHT)) 10 | 11 | def calculate_distance(p1, p2): 12 | return math.sqrt((p2[1] - p1[1])**2 + (p2[0] - p1[0])**2) 13 | 14 | def calculate_angle(p1, p2): 15 | return math.atan2(p2[1] - p1[1], p2[0] - p1[0]) 16 | 17 | def draw(space, window, draw_options, line): 18 | window.fill("white") 19 | 20 | if line: 21 | pygame.draw.line(window, "black", line[0], line[1], 3) 22 | 23 | space.debug_draw(draw_options) 24 | 25 | pygame.display.update() 26 | 27 | def create_boundaries(space, width, height): 28 | rects = [ 29 | [(width/2, height - 10), (width, 20)], 30 | [(width/2, 10), (width, 20)], 31 | [(10, height/2), (20, height)], 32 | [(width - 10, height/2), (20, height)] 33 | ] 34 | 35 | for pos, size in rects: 36 | body = pymunk.Body(body_type=pymunk.Body.STATIC) 37 | body.position = pos 38 | shape = pymunk.Poly.create_box(body, size) 39 | shape.elasticity = 0.4 40 | shape.friction = 0.5 41 | space.add(body, shape) 42 | 43 | def create_structure(space, width, height): 44 | BROWN = (139, 69, 19, 100) 45 | rects = [ 46 | [(600, height - 120), (40, 200), BROWN, 100], 47 | [(900, height - 120), (40, 200), BROWN, 100], 48 | [(750, height - 240), (340, 40), BROWN, 150] 49 | ] 50 | 51 | for pos, size, color, mass in rects: 52 | body = pymunk.Body() 53 | body.position = pos 54 | shape = pymunk.Poly.create_box(body, size, radius=2) 55 | shape.color = color 56 | shape.mass = mass 57 | shape.elasticity = 0.4 58 | shape.friction = 0.4 59 | space.add(body, shape) 60 | 61 | def create_swinging_ball(space): 62 | rotation_center_body = pymunk.Body(body_type=pymunk.Body.STATIC) 63 | rotation_center_body.position = (300, 300) 64 | 65 | body = pymunk.Body() 66 | body.position = (300, 300) 67 | line = pymunk.Segment(body, (0, 0), (255, 0), 5) 68 | circle = pymunk.Circle(body, 40, (255, 0)) 69 | line.friction = 1 70 | circle.friction = 1 71 | line.mass = 8 72 | circle.mass = 30 73 | circle.elasticity = 0.95 74 | rotation_center_joint = pymunk.PinJoint(body, rotation_center_body, (0, 0), (0, 0)) 75 | space.add(circle, line, body, rotation_center_joint) 76 | 77 | 78 | def create_ball(space, radius, mass, pos): 79 | body = pymunk.Body(body_type=pymunk.Body.STATIC) 80 | body.position = pos 81 | shape = pymunk.Circle(body, radius) 82 | shape.mass = mass 83 | shape.elasticity = 0.9 84 | shape.friction = 0.4 85 | shape.color = (255, 0, 0, 100) 86 | space.add(body, shape) 87 | return shape 88 | 89 | def run(window, width, height): 90 | run = True 91 | clock = pygame.time.Clock() 92 | fps = 60 93 | dt = 1 / fps 94 | 95 | space = pymunk.Space() 96 | space.gravity = (0, 981) 97 | 98 | create_boundaries(space, width, height) 99 | create_structure(space, width, height) 100 | create_swinging_ball(space) 101 | 102 | draw_options = pymunk.pygame_util.DrawOptions(window) 103 | 104 | pressed_pos = None 105 | ball = None 106 | 107 | while run: 108 | line = None 109 | if ball and pressed_pos: 110 | line = [pressed_pos, pygame.mouse.get_pos()] 111 | 112 | for event in pygame.event.get(): 113 | if event.type == pygame.QUIT: 114 | run = False 115 | break 116 | 117 | if event.type == pygame.MOUSEBUTTONDOWN: 118 | if not ball: 119 | pressed_pos = pygame.mouse.get_pos() 120 | ball = create_ball(space, 30, 10, pressed_pos) 121 | elif pressed_pos: 122 | ball.body.body_type = pymunk.Body.DYNAMIC 123 | angle = calculate_angle(*line) 124 | force = calculate_distance(*line) * 50 125 | fx = math.cos(angle) * force 126 | fy = math.sin(angle) * force 127 | ball.body.apply_impulse_at_local_point((fx, fy), (0, 0)) 128 | pressed_pos = None 129 | else: 130 | space.remove(ball, ball.body) 131 | ball = None 132 | 133 | draw(space, window, draw_options, line) 134 | space.step(dt) 135 | clock.tick(fps) 136 | 137 | pygame.quit() 138 | 139 | if __name__ == "__main__": 140 | run(window, WIDTH, HEIGHT) --------------------------------------------------------------------------------