Advertisement
diliupg

an object falls onto a body moving in a circular path

May 4th, 2015
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. """An object falls onto another object moving in a circular path and stays on it without falling off"""
  2. import sys, os, pygame
  3. from pygame.locals import *
  4. from standard_object_creator import *
  5. from math import sin, cos, pi, radians
  6.  
  7. SCREENW = 800
  8. SCREENH = 700
  9.  
  10. pygame.init()
  11. FPSCLOCK = pygame.time.Clock()
  12.  
  13. FONT1= "data\Cookie-Regular.ttf"
  14.  
  15. if sys.platform == 'win32' or sys.platform == 'win64':
  16.     #os.environ['SDL_VIDEO_CENTERED'] = '2'# center of screen
  17.     os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (10,30)#top left corner
  18.  
  19. SCREEN = pygame.display.set_mode((SCREENW, SCREENH))
  20. pygame.display.set_caption('rotating object')
  21.  
  22. BLUE = (0, 50, 255)
  23. BLACK = (0, 0, 0)
  24. RED = (255, 0, 0)
  25. PURPLE = (145, 0, 100)
  26. yellow = (220,220,  0)
  27.  
  28. FPS = 160  # frames per second
  29.  
  30.  
  31. platforms = pygame.sprite.Group()
  32. boxes = pygame.sprite.Group()
  33.  
  34. ## this is the center of rotation. objects will be placed on the radius according to the step
  35. cx = SCREENW / 2 # x pos in relation to screen width
  36. cy = SCREENH / 2 # y pos in relation to screen height
  37.  
  38. ## self, posx, posy, imagelist, speedx = 0, speedy = 0, value = 0
  39. plat = pygame.image.load("platform.png").convert_alpha()
  40. box = pygame.image.load("box.png").convert_alpha()
  41.  
  42. box = object_factory ([box], cx, cy - 300, 0, 2)
  43.  
  44. boxes.add(box)
  45.  
  46. RADIUS = 100 # distance from the center
  47. angle = radians(0)  # angular distance between objects
  48. omega = 1
  49. for x in xrange(1):
  50.     xpos = (cos(angle * x) * RADIUS) + cx
  51.     ypos = (sin(angle * x) * RADIUS) + cy
  52.     obj = object_factory([plat], xpos, ypos)
  53.     obj.cx = cx
  54.     obj.cy = cy
  55.     obj.angle = radians(60 * x) # position each object around (cx,cy)
  56.     obj.radius = RADIUS  # distance from the center
  57.     obj.omega = radians(omega)
  58.     obj.offsetx = cos(obj.angle) * obj.radius
  59.     obj.offsety = -sin(obj.angle) * obj.radius
  60.     platforms.add(obj)
  61.  
  62. mouseposlist = []
  63. okkoma = [platforms, boxes]
  64.  
  65. l = []
  66. x = 0
  67. rotations = 0
  68. while True:  #x < 360:
  69.  
  70.     SCREEN.fill(BLACK)
  71.     x += 1
  72.     if x >= 360:
  73.         rotations += 1
  74.         x = 0
  75.         print "rotations=", rotations
  76.     #print box.speedy, obj.speedy
  77.     ##--------------------------------------------------------------
  78.     pygame.event.pump()
  79.     keys = pygame.key.get_pressed()
  80.     for event in pygame.event.get():
  81.         if event.type == MOUSEBUTTONDOWN:
  82.             if event.button == 1:
  83.                 pos = pygame.mouse.get_pos()
  84.                 val = [pos[0], pos[1], 0, 0]
  85.                 print val
  86.                 mouseposlist.append(val)
  87.             elif event.button == 3 and mouseposlist != []:
  88.                 mouseposlist.pop(-1)
  89.  
  90.         if event.type == KEYDOWN and event.key == K_ESCAPE:
  91.             print mouseposlist
  92.             pygame.quit()
  93.             sys.exit()
  94.        
  95.         #elif event.type == KEYDOWN and event.key == K_RIGHT:
  96.             #box.speedx += 10
  97.  
  98.  
  99.         #elif event.type == KEYDOWN and event.key == K_LEFT:
  100.             #box.speedx -= 10
  101.        
  102.         #elif event.type == KEYDOWN and event.key == K_UP:
  103.             #box.speedy -= 5
  104.  
  105.  
  106.         #elif event.type == KEYDOWN and event.key == K_DOWN:
  107.             #box.speedy += 5
  108.            
  109.         #else:
  110.             #box.speedx = 0
  111.     #print box.speedx, box.speedy, obj.speedx, obj.speedy
  112.  
  113.     for hp in boxes:
  114.         hp.move()
  115.         hp.collide(platforms)
  116.  
  117.     ## MOVE THE SPRITE IN A CIRCLE center of rotation = cx,cy. Each object is placed by varying the step)
  118.  
  119.     for obj in platforms:
  120.        
  121.         obj.angle += obj.omega  # larger value increases speed ( angle gets larger)
  122.         #obj.radius += .1  # this will make the object move in an expanding spiral
  123.         obj.offsetx = cos(obj.angle) * obj.radius
  124.         obj.offsety = -sin(obj.angle) * obj.radius
  125.         obj.difx = obj.offsetx - obj.rect.x
  126.         obj.dify = obj.offsety - obj.rect.y
  127.         obj.speedx = round(obj.cx + obj.difx, 2)
  128.         obj.speedy = round(obj.cy + obj.dify, 2)
  129.  
  130.         obj.move()
  131.    
  132.     l.append([round(box.speedx - obj.speedx, 2), round(box.speedy - obj.speedy, 2)])
  133.  
  134.     for ekak in okkoma:
  135.         ekak.update()
  136.         ekak.draw(SCREEN)
  137.  
  138.     ## --------------------------------------------------------------------
  139.     pygame.draw.line(SCREEN, BLUE, (0, SCREENH / 2), (SCREENW, SCREENH / 2), 2)
  140.     pygame.draw.line(SCREEN, BLUE, (SCREENW / 2, 0), (SCREENW / 2, SCREENH), 2)
  141.     pygame.draw.circle(SCREEN, BLUE, (cx, cy), RADIUS, 2)
  142.    
  143.  
  144.     pygame.display.update()
  145.     FPSCLOCK.tick(FPS)
  146.    
  147.    
  148.     pygame.time.wait(0)
  149.  
  150. print l
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement