Last active
March 24, 2025 12:35
-
-
Save znxkznxk1030/20df99744b26c6f90fc7784f147c09c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Showcase of flying arrows that can stick to objects in a somewhat | |
realistic looking way. | |
""" | |
import sys | |
from typing import List | |
import pygame | |
import pymunk | |
import pymunk.pygame_util | |
from pymunk.vec2d import Vec2d | |
import math | |
width, height = 690, 600 | |
def main(): | |
### PyGame init | |
pygame.init() | |
screen = pygame.display.set_mode((width, height)) | |
clock = pygame.time.Clock() | |
running = True | |
font = pygame.font.SysFont("Arial", 16) | |
### Physics stuff | |
space = pymunk.Space() | |
space.gravity = 0, 900 | |
draw_options = pymunk.pygame_util.DrawOptions(screen) | |
# walls - the left-top-right walls | |
static: List[pymunk.Shape] = [ | |
pymunk.Segment(space.static_body, (50, 550), (50, 50), 15), | |
pymunk.Segment(space.static_body, (50, 50), (650, 50), 15), | |
pymunk.Segment(space.static_body, (650, 50), (650, 550), 15), | |
pymunk.Segment(space.static_body, (50, 550), (650, 550), 15), | |
] | |
for s in static: | |
s.friction = 1.0 | |
s.group = 1 | |
space.add(*static) | |
# 카트 생성 | |
cart_mass = 5 | |
cart_size = (50, 30) | |
cart_moment = pymunk.moment_for_box(cart_mass, cart_size) | |
cart_body = pymunk.Body(cart_mass, cart_moment) | |
cart_body.position = (300, 200) | |
cart_shape = pymunk.Poly.create_box(cart_body, cart_size) | |
cart_shape.friction = 0.5 | |
# 폴 생성 | |
pole_mass = 1 | |
pole_length = 100 | |
pole_thickness = 5 | |
pole_moment = pymunk.moment_for_box(pole_mass, (pole_thickness, pole_length)) | |
pole_body = pymunk.Body(pole_mass, pole_moment) | |
pole_body.position = (300, 150) | |
pole_shape = pymunk.Segment(pole_body, (0, 0), (0, pole_length), pole_thickness / 2) | |
pole_shape.friction = 0.5 | |
# 카트와 폴 연결 | |
pivot = pymunk.PivotJoint(cart_body, pole_body, cart_body.position + (0, cart_size[1] / 2)) | |
# 공간에 객체 추가 | |
space.add(cart_body, cart_shape, pole_body, pole_shape, pivot) | |
# mass = 1 | |
# moment = pymunk.moment_for_poly(mass, vs) | |
# space.add(arrow_body, arrow_shape) | |
while running: | |
for event in pygame.event.get(): | |
if ( | |
event.type == pygame.QUIT | |
or event.type == pygame.KEYDOWN | |
and (event.key in [pygame.K_ESCAPE, pygame.K_q]) | |
): | |
running = False | |
keys = pygame.key.get_pressed() | |
speed = 2.5 | |
if keys[pygame.K_LEFT]: | |
cart_body.position += Vec2d(-1, 0) * speed | |
if keys[pygame.K_RIGHT]: | |
cart_body.position += Vec2d(1, 0) * speed | |
### Clear screen | |
screen.fill(pygame.Color("black")) | |
### Draw stuff | |
space.debug_draw(draw_options) | |
pygame.display.flip() | |
### Update physics | |
fps = 60 | |
dt = 1.0 / fps | |
space.step(dt) | |
clock.tick(fps) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment