trigonometric function
If we use OP as the radius r of the circle and point o as the center of the circle, the x-coordinate of the point on the circle is r * cos a , and the y-coordinate is r * sin a .
Python provides () and (), which require the arguments to be radians.
Relationship between radians and angles
PI stands for 180 degrees, and PI is pi: 3.1415926 535 897392 23846. python provides conversions for angles and radians
() Radian to angle
() Angle to radian
a = ((90))
The horizontal coordinate of 90 degrees is 0, but because the PI is not a floating-point decimal, it results in inaccurate arithmetic and is a value close to 0.
Basic Packages and Event Capture
Initialize the window, configure the center and radius of the circle, and add a timer to control the speed of the drawing.
import sys, random, math, pygame from import * () screen = .set_mode((600, 500)) .set_caption("Dream Circle.") ((0, 0, 100)) pos_x = 300 pos_y = 250 radius = 200 angle = 360 # Timers mainClock = ()
while True: for event in (): if == QUIT: () () keys = .get_pressed() if keys[K_ESCAPE]: () ()
main program
The angle is constantly added, and if it exceeds 360 degrees it restarts at 1, randomizes a color, calculates the point on the great circle at that angle, and draws a circle with a radius of 10 with that point.
angle += 1 if angle >= 360: angle = 0 r = (0, 255) g = (0, 255) b = (0, 255) color = r, g, b x = ((angle)) * radius y = ((angle)) * radius pos = (int(pos_x + x), int(pos_y + y)) (screen, color, pos, 10, 0) () (20)
All Codes
import sys, random, math, pygame from import * () screen = .set_mode((600, 500)) .set_caption("Dream Circle.") ((0, 0, 100)) pos_x = 300 pos_y = 250 radius = 200 angle = 360 # Timers mainClock = () while True: for event in (): if == QUIT: () () keys = .get_pressed() if keys[K_ESCAPE]: () () angle += 1 if angle >= 360: angle = 0 r = (0, 255) g = (0, 255) b = (0, 255) color = r, g, b x = ((angle)) * radius y = ((angle)) * radius pos = (int(pos_x + x), int(pos_y + y)) (screen, color, pos, 10, 0) () (10)
to this article on the use of Pygame to draw a circle of sample code is introduced to this article, more related to Pygame to draw a circle content please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!