Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to realize the operation of the solar system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python to realize the operation of the solar system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Python to realize the operation of the solar system.

Realize

For the implementation of the function, we mainly need to go to the pygame library of Python. We first import all the required Python libraries, and the code is as follows:

Import sysimport mathimport pygamefrom pygame.locals import *

Then define some constants (such as color, width, height) and create a window, the code is as follows:

WHITE = (255,255,255) SILVER = (192,192,192) BLACK = (0,0,0) GREEN = (0,255,0) RED = (255,0,0) BLUE = (0,0,255) YELLOW = (255,255,0) SandyBrown = (244,164,96) PaleGodenrod = (238,232,170) PaleVioletRed = (219,1212,147) Thistle = (2161,191,216) size = width, height = 800 600screen = pygame.display.set_mode (size) pygame.display.set_caption ("solar system") # create clock (control game cycle frequency) clock = pygame.time.Clock () # define three empty lists pos_v = pos_e = pos_mm = [] # Earth, The angle of rotation of the moon and other planets roll_v = roll_e = roll_m = 0roll_3 = roll_4 = roll_5 = roll_6 = roll_7 = roll_8 = position of the sun (center) position = size [0] / / 2 Size [1] / / 2

Let's first draw a sun in the window, the code is as follows:

Pygame.draw.circle (screen, YELLOW, position, 60,0)

Take a look at the effect:

Then draw an earth and make it revolve around the sun. The code is as follows:

# draw the earth roll_e + = 0.01suppose the earth rotates 0.01pipos_e_x = int (size [0] / / 2 + size [1] / / 6 * math.sin (roll_e)) pos_e_y = int (size [1] / / 2 + size [1] / / 6 * math.cos (roll_e)) pygame.draw.circle (screen, BLUE, (pos_e_x, pos_e_y), 15 0) # the trajectory of the earth pos_e.append ((pos_e_x, pos_e_y)) if len (pos_e) > 255: pos_e.pop (0) for i in range (len (pos_e)): pygame.draw.circle (screen, SILVER, pos_e [I], 1,0)

Take a look at the effect:

Let's go on to draw the moon, the code is as follows:

# draw roll_m + = 0.1pos_m_x = int (pos_e_x + size [1] / / 20 * math.sin (roll_m)) pos_m_y = int (pos_e_y + size [1] / / 20 * math.cos (roll_m)) pygame.draw.circle (screen, SILVER, (pos_m_x, pos_m_y), 8,0) # the trajectory of the moon pos_mm.append ((pos_m_x) Pos_m_y)) if len (pos_mm) > 255: pos_mm.pop (0) for i in range (len (pos_mm)): pygame.draw.circle (screen, SILVER, pos_mm [I], 1,0)

Take a look at the effect:

The implementation of several other planets is similar, with the following code:

# other planets roll_3 + = 0.03pos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.sin (roll_3)) pos_3_y = int (size [1] / / 2 + size [1] / / 3.5 * math.cos (roll_3)) pygame.draw.circle (screen, GREEN, (pos_3_x, pos_3_y), 20 0) roll_4 + = 0.04pos_4_x = int (size [0] / / 2 + size [1] / / 4 * math.sin (roll_4)) pos_4_y = int (size [1] / / 2 + size [1] / / 4 * math.cos (roll_4)) pygame.draw.circle (screen, SandyBrown, (pos_4_x, pos_4_y), 20 0) roll_5 + = 0.05pos_5_x = int (size [0] / / 2 + size [1] / / 5 * math.sin (roll_5)) pos_5_y = int (size [1] / / 2 + size [1] / / 5 * math.cos (roll_5)) pygame.draw.circle (screen, PaleGodenrod, (pos_5_x, pos_5_y), 20 0) roll_6 + = 0.06pos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.sin (roll_6)) pos_6_y = int (size [1] / / 2 + size [1] / / 2.5 * math.cos (roll_6)) pygame.draw.circle (screen, PaleVioletRed, (pos_6_x, pos_6_y), 20 0) roll_7 + = 0.07pos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.sin (roll_7)) pos_7_y = int (size [1] / / 2 + size [1] / / 4.5 * math.cos (roll_7)) pygame.draw.circle (screen, Thistle, (pos_7_x, pos_7_y), 20 0) roll_8 + = 0.08pos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.sin (roll_8)) pos_8_y = int (size [1] / / 2 + size [1] / / 5.5 * math.cos (roll_8)) pygame.draw.circle (screen, WHITE, (pos_8_x, pos_8_y), 20,0)

Finally, let's take a look at the dynamic effect of the overall implementation:

At this point, I believe you have a deeper understanding of "how to use Python to realize the operation of the solar system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report