Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. import pymunk as pmk
  2. import arcade as ade
  3. from time import sleep
  4.  
  5. SCREEN_WIDTH = 640
  6. SCREEN_HEIGHT = 400
  7. SCREEN_TITLE = "Learning Arcs"
  8.  
  9. class GameWindow(ade.Window):
  10.     def __init__(self, width, height, title):
  11.         super().__init__(width, height, title)
  12.  
  13.         ade.set_background_color(ade.color.WHITE)
  14.  
  15.  
  16.     def setup(self):
  17.         self.play = True
  18.        
  19.         self.g_space = pmk.Space()
  20.         self.g_space.gravity = (0,-500)
  21.        
  22.         mass = 1
  23.         radius = 30
  24.        
  25.         moment_cir = pmk.moment_for_circle(mass,0,radius)
  26.         circle_body = pmk.Body(mass,moment_cir)
  27.         circle_shape = pmk.Circle(circle_body,radius)
  28.         circle_body.position = (560, 300)
  29.         circle_shape.elasticity = 0.98
  30.         circle_shape.friction = 1.0
  31.        
  32.         circle_shape.type = "circle" #specifics for displaying
  33.         circle_shape.color = ade.color.CYAN
  34.  
  35.  
  36.         #self.segment_body = pmk.Body(body_type=pmk.Body.STATIC) - replace with "space.static_body"
  37.         segment_shape = pmk.Segment(self.g_space.static_body, (50,50), (590,0), 2)
  38.         segment_shape.body.position = (0,0)
  39.         segment_shape.elasticity = 0.98
  40.         segment_shape.friction = 1.0
  41.        
  42.         segment_shape.type = "segment"
  43.         segment_shape.color = ade.color.GRAY
  44.        
  45.        
  46.         segment_shape2 = pmk.Segment(self.g_space.static_body, (300,120), (590,130), 2)
  47.         segment_shape2.body.position = (0,0)
  48.         segment_shape2.elasticity = 0.5
  49.         segment_shape2.friction = 1.0
  50.        
  51.         segment_shape2.type = "segment"
  52.         segment_shape2.color = ade.color.GRAY
  53.  
  54.         self.g_space.add(circle_body, circle_shape)
  55.         self.g_space.add(segment_shape, segment_shape2)
  56.        
  57.     def on_draw(self):
  58.         ade.start_render()
  59.        
  60.         for shape in self.g_space.shapes:
  61.             (x, y) = shape.body.position
  62.             type = shape.type
  63.            
  64.             if (type == "circle"):
  65.                 ade.draw_circle_filled(x,y,shape.radius,shape.color)
  66.                
  67.                 rx,ry = ade.rotate_point(x+shape.radius,y,x,y,shape.body.angle * (180/3.1415))
  68.                
  69.                 ade.draw_line(x,y,rx,ry,ade.color.GRAY,2)
  70.  
  71.             if (type == "segment"):
  72.                 s_x,s_y = shape.a
  73.                 e_x,e_y = shape.b
  74.                
  75.                 ade.draw_line(s_x,s_y,e_x,e_y,shape.color,shape.radius)
  76.  
  77.  
  78.     def update(self, delta_time):
  79.         if (self.play):
  80.             self.g_space.step(delta_time)
  81.            
  82.         for shape in self.g_space.shapes:
  83.             if (shape.body.position.y < -50):
  84.                 self.g_space.remove(shape, shape.body)
  85.  
  86.     def on_key_press(self, key, key_modifiers):
  87.         if (key == ade.key.Q):
  88.             quit()
  89.        
  90.         if (key == ade.key.SPACE):
  91.             self.play = not self.play
  92.            
  93.     def on_mouse_press(self, x, y, button, modifiers):
  94.         mass = 1
  95.         radius = 10
  96.        
  97.         moment_cir = pmk.moment_for_circle(mass,0,radius)
  98.         circle_body = pmk.Body(mass,moment_cir)
  99.         circle_shape = pmk.Circle(circle_body,radius)
  100.         circle_body.position = (x, y)
  101.         circle_shape.elasticity = 0.98
  102.         circle_shape.friction = 1.0
  103.        
  104.         circle_shape.type = "circle"
  105.         circle_shape.color = ade.color.CYAN
  106.        
  107.         self.g_space.add(circle_body, circle_shape)
  108.         print("done")
  109.    
  110.    
  111. def main():
  112.     game = GameWindow(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
  113.     game.setup()
  114.     ade.run()
  115.  
  116.  
  117. if __name__ == "__main__":
  118.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement