Python Forum
Moving an object in a circular path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving an object in a circular path
#2
You can use a timer or you can use some type of ticks in main loop.
example in pygame
import pygame
import math

def move_coords(angle, radius, coords):
	theta = math.radians(angle)
	return coords[0] + radius * math.cos(theta), coords[1] + radius * math.sin(theta)

def main():
	pygame.display.set_caption("Oribit")
	screen = pygame.display.set_mode((800, 600))
	clock = pygame.time.Clock()
	
	coords = 400, 200
	angle = 0
	rect = pygame.Rect(*coords,20,20)
	speed = 50
	next_tick = 500
	
	running = True
	while running:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				running = False
		
		ticks = pygame.time.get_ticks()	
		if ticks > next_tick:
			next_tick += speed
			angle += 1
			coords = move_coords(angle, 2, coords)
			rect.topleft = coords
			
		screen.fill((0,0,30))
		screen.fill((0,150,0), rect)
		pygame.display.flip()
		clock.tick(30)
	
	pygame.quit()

if __name__ == '__main__':
	main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Moving an object in a circular path - by Windspar - Dec-25-2017, 12:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [pygame] Moving an object at angles SheeppOSU 3 8,574 Oct-24-2019, 08:05 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020