Python Forum
Program to move a dot towards a circle center
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program to move a dot towards a circle center
#1
Got an idea but dont know where to start.

I want a program that shows a dot moving from the circumference towards the circle center. The size, color, and speed of the dot can be changed when required. On reaching the center, the dot will start from the circumference, x cm from the last starting point.

How can I get started on this?

Thanks.
Reply
#2
Is this a game? Does a user somehow control the dot, or is it a status display? If a game, how does the user control the dot?
Reply
#3
Here is a very basic script using pygame:

from pygame import display, QUIT, event, draw, time 
SCREEN = display.set_mode ((500, 500))

dot_x_position = 30
while True :
	for user_input_event in event.get () :
		if user_input_event.type ==  QUIT :
			exit ()
	SCREEN.fill ((0, 0, 0))
	draw.circle (SCREEN, (200, 0, 0), (249, 249), 220, 4)
	draw.circle (SCREEN, (0, 0, 200), (dot_x_position, 249), 20)
	if dot_x_position < 249 :
		dot_x_position += 1
	time.Clock ().tick (100)
	display.update ()
Reply
#4
(Nov-21-2021, 03:02 PM)deanhystad Wrote: Is this a game? Does a user somehow control the dot, or is it a status display? If a game, how does the user control the dot?
Not as a game. User does not control the dot. User presses a key at a certain phase.
Reply
#5
(Nov-21-2021, 03:06 PM)BashBedlam Wrote: Here is a very basic script using pygame:

from pygame import display, QUIT, event, draw, time 
SCREEN = display.set_mode ((500, 500))

dot_x_position = 30
while True :
	for user_input_event in event.get () :
		if user_input_event.type ==  QUIT :
			exit ()
	SCREEN.fill ((0, 0, 0))
	draw.circle (SCREEN, (200, 0, 0), (249, 249), 220, 4)
	draw.circle (SCREEN, (0, 0, 200), (dot_x_position, 249), 20)
	if dot_x_position < 249 :
		dot_x_position += 1
	time.Clock ().tick (100)
	display.update ()

Many thanks! I have tried it and it is what I want to start with. I will add to it to do what I have in mind.

Much appreciated.
Reply
#6
(Nov-21-2021, 03:06 PM)BashBedlam Wrote: Here is a very basic script using pygame:

from pygame import display, QUIT, event, draw, time 
SCREEN = display.set_mode ((500, 500))

dot_x_position = 30
while True :
	for user_input_event in event.get () :
		if user_input_event.type ==  QUIT :
			exit ()
	SCREEN.fill ((0, 0, 0))
	draw.circle (SCREEN, (200, 0, 0), (249, 249), 220, 4)
	draw.circle (SCREEN, (0, 0, 200), (dot_x_position, 249), 20)
	if dot_x_position < 249 :
		dot_x_position += 1
	time.Clock ().tick (100)
	display.update ()
BashBedlam,

Need your help again.

At the end of your program, I want to add some other codes. But for some reason, it does not proceed after display.update

time.Clock ().tick (100)
display.update ()

Print("I am here")

It does not print I am here

Appreciate your help. Thanks.
Reply
#7
Is your code like this?
    time.Clock ().tick (100)
    display.update ()
    print("I am here")
Or like this?
    time.Clock ().tick (100)
    display.update ()
print("I am here")
If the latter you aren't reaching the print statement because you are still in the while True: loop.
Reply
#8
(Dec-01-2021, 05:38 AM)deanhystad Wrote: Is your code like this?
    time.Clock ().tick (100)
    display.update ()
    print("I am here")
Or like this?
    time.Clock ().tick (100)
    display.update ()
print("I am here")
If the latter you aren't reaching the print statement because you are still in the while True: loop.
Thanks.

I have tried both. Former one will print as many times as it cycles through the loop. I dont want that.

I want to exit from the moving dot program and do something else. But using the latter format, it just does not print the "I am here".

Somehow I need to exit from pygame, I think, to continue to do the rest.
Reply
#9
Okay... In this version, when the dot reaches the middle, we usebreakto fall through the firstwhileloop. After that we print to the screen and wait for the user to close the window. That is where you would put any extra code. Does that help?
from pygame import display, QUIT, event, draw, time, font, init
SCREEN = display.set_mode ((500, 500))
init () 
dot_x_position = 30
while True :
	for user_input_event in event.get () :
		if user_input_event.type ==  QUIT :
			exit ()
	SCREEN.fill ((0, 0, 0))
	draw.circle (SCREEN, (200, 0, 0), (249, 249), 220, 4)
	draw.circle (SCREEN, (0, 0, 200), (dot_x_position, 249), 20)
	if dot_x_position < 249 :
		dot_x_position += 1
	else : break
	time.Clock ().tick (100)
	display.update ()

text = font.SysFont ('ariel', 42).render ('Close window to contunue.',
		1, (255, 255, 255), (1, 1, 1))
SCREEN.blit (text, (70, 50))
display.update ()
while True :
	for user_input_event in event.get () :
		if user_input_event.type ==  QUIT :
			exit ()
Reply
#10
(Dec-01-2021, 05:45 PM)BashBedlam Wrote: Okay... In this version, when the dot reaches the middle, we usebreakto fall through the firstwhileloop. After that we print to the screen and wait for the user to close the window. That is where you would put any extra code. Does that help?
from pygame import display, QUIT, event, draw, time, font, init
SCREEN = display.set_mode ((500, 500))
init () 
dot_x_position = 30
while True :
	for user_input_event in event.get () :
		if user_input_event.type ==  QUIT :
			exit ()
	SCREEN.fill ((0, 0, 0))
	draw.circle (SCREEN, (200, 0, 0), (249, 249), 220, 4)
	draw.circle (SCREEN, (0, 0, 200), (dot_x_position, 249), 20)
	if dot_x_position < 249 :
		dot_x_position += 1
	else : break
	time.Clock ().tick (100)
	display.update ()

text = font.SysFont ('ariel', 42).render ('Close window to contunue.',
		1, (255, 255, 255), (1, 1, 1))
SCREEN.blit (text, (70, 50))
display.update ()
while True :
	for user_input_event in event.get () :
		if user_input_event.type ==  QUIT :
			exit ()

Many thanks! Now I got it running. Your help is very much appreciated!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to plot seaborn heatmap on top of a background circle SriRajesh 0 1,370 Jul-09-2022, 04:00 AM
Last Post: SriRajesh
  (OpenCV) Help to improve code for object detection and finding center of it saoko 0 1,159 May-14-2022, 05:34 PM
Last Post: saoko
  [S0LVED] [Tkinter] Center dialog? Winfried 8 4,080 Mar-01-2022, 07:17 PM
Last Post: Winfried
  drawing moving circle on line with python barrai 1 3,038 Feb-25-2022, 03:33 PM
Last Post: BashBedlam
  Draw circle from GPS coords, and get GPX file? Winfried 0 2,145 Mar-29-2021, 07:19 PM
Last Post: Winfried
  Center align Kristenl2784 1 1,920 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  Pyplot and circle Reldaing 0 1,446 Apr-01-2020, 10:44 AM
Last Post: Reldaing
  calculate circle sector Bloody_geek 2 2,245 Sep-11-2019, 08:54 PM
Last Post: Larz60+
  How can I move in an external program via Python? Pythoner 0 2,242 Dec-29-2018, 12:43 PM
Last Post: Pythoner
  Help with circle and motion wigigx 4 3,991 Aug-29-2017, 09:49 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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