Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """An object falls onto another object moving in a circular path and stays on it without falling off"""
- import sys, os, pygame
- from pygame.locals import *
- from standard_object_creator import *
- from math import sin, cos, pi, radians
- SCREENW = 800
- SCREENH = 700
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- FONT1= "data\Cookie-Regular.ttf"
- if sys.platform == 'win32' or sys.platform == 'win64':
- #os.environ['SDL_VIDEO_CENTERED'] = '2'# center of screen
- os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (10,30)#top left corner
- SCREEN = pygame.display.set_mode((SCREENW, SCREENH))
- pygame.display.set_caption('rotating object')
- BLUE = (0, 50, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- PURPLE = (145, 0, 100)
- yellow = (220,220, 0)
- FPS = 160 # frames per second
- platforms = pygame.sprite.Group()
- boxes = pygame.sprite.Group()
- ## this is the center of rotation. objects will be placed on the radius according to the step
- cx = SCREENW / 2 # x pos in relation to screen width
- cy = SCREENH / 2 # y pos in relation to screen height
- ## self, posx, posy, imagelist, speedx = 0, speedy = 0, value = 0
- plat = pygame.image.load("platform.png").convert_alpha()
- box = pygame.image.load("box.png").convert_alpha()
- box = object_factory ([box], cx, cy - 300, 0, 2)
- boxes.add(box)
- RADIUS = 100 # distance from the center
- angle = radians(0) # angular distance between objects
- omega = 1
- for x in xrange(1):
- xpos = (cos(angle * x) * RADIUS) + cx
- ypos = (sin(angle * x) * RADIUS) + cy
- obj = object_factory([plat], xpos, ypos)
- obj.cx = cx
- obj.cy = cy
- obj.angle = radians(60 * x) # position each object around (cx,cy)
- obj.radius = RADIUS # distance from the center
- obj.omega = radians(omega)
- obj.offsetx = cos(obj.angle) * obj.radius
- obj.offsety = -sin(obj.angle) * obj.radius
- platforms.add(obj)
- mouseposlist = []
- okkoma = [platforms, boxes]
- l = []
- x = 0
- rotations = 0
- while True: #x < 360:
- SCREEN.fill(BLACK)
- x += 1
- if x >= 360:
- rotations += 1
- x = 0
- print "rotations=", rotations
- #print box.speedy, obj.speedy
- ##--------------------------------------------------------------
- pygame.event.pump()
- keys = pygame.key.get_pressed()
- for event in pygame.event.get():
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 1:
- pos = pygame.mouse.get_pos()
- val = [pos[0], pos[1], 0, 0]
- print val
- mouseposlist.append(val)
- elif event.button == 3 and mouseposlist != []:
- mouseposlist.pop(-1)
- if event.type == KEYDOWN and event.key == K_ESCAPE:
- print mouseposlist
- pygame.quit()
- sys.exit()
- #elif event.type == KEYDOWN and event.key == K_RIGHT:
- #box.speedx += 10
- #elif event.type == KEYDOWN and event.key == K_LEFT:
- #box.speedx -= 10
- #elif event.type == KEYDOWN and event.key == K_UP:
- #box.speedy -= 5
- #elif event.type == KEYDOWN and event.key == K_DOWN:
- #box.speedy += 5
- #else:
- #box.speedx = 0
- #print box.speedx, box.speedy, obj.speedx, obj.speedy
- for hp in boxes:
- hp.move()
- hp.collide(platforms)
- ## MOVE THE SPRITE IN A CIRCLE center of rotation = cx,cy. Each object is placed by varying the step)
- for obj in platforms:
- obj.angle += obj.omega # larger value increases speed ( angle gets larger)
- #obj.radius += .1 # this will make the object move in an expanding spiral
- obj.offsetx = cos(obj.angle) * obj.radius
- obj.offsety = -sin(obj.angle) * obj.radius
- obj.difx = obj.offsetx - obj.rect.x
- obj.dify = obj.offsety - obj.rect.y
- obj.speedx = round(obj.cx + obj.difx, 2)
- obj.speedy = round(obj.cy + obj.dify, 2)
- obj.move()
- l.append([round(box.speedx - obj.speedx, 2), round(box.speedy - obj.speedy, 2)])
- for ekak in okkoma:
- ekak.update()
- ekak.draw(SCREEN)
- ## --------------------------------------------------------------------
- pygame.draw.line(SCREEN, BLUE, (0, SCREENH / 2), (SCREENW, SCREENH / 2), 2)
- pygame.draw.line(SCREEN, BLUE, (SCREENW / 2, 0), (SCREENW / 2, SCREENH), 2)
- pygame.draw.circle(SCREEN, BLUE, (cx, cy), RADIUS, 2)
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- pygame.time.wait(0)
- print l
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement