This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line. This article is referred to -bjbsair.com/2020-03-25/… 1. The solar system

2. Today to simulate the motion of the planets in the solar system, using python3 and pygame.

Step 1:

#-- Step 1 -- Export module --
import pygame  
import sys  
import math  
from pygame.locals import *
Copy the code

4. Step 2:

Step 2: Initialize the game
pygame.init()
Copy the code

5. Step 3:

Step 3: Define the color
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
GREEN = (0, 255, 0)  
RED = (255, 0, 0)  
BLUE = (0, 0, 255)  
YELLOW = (255, 255, 0)
Copy the code

5. 6. Smart refrigerator

Step 4 -- Define window size, title name, font Settings, create clock --
size = width, height = 2200, 1400  
screen = pygame.display.set_mode(size)  
pygame.display.set_caption("Diagram of sun, Earth, Moon, Venus, etc.")  
This is the first definition of the # font
myfont=pygame.font.Font(None,60)  
# If it is Chinese, the font HWFS = Chinese imitation song font, in the root directory
#myfont=pygame.font.Font('hwfs.ttf',60)  
# Create a clock object (which can control the game loop rate)-- mandatory
clock = pygame.time.Clock()
Copy the code

======= The basic format of the above 4 steps is fixed =======

7. Step 5: There are list definitions, multiple list definitions and multiple row assignments, both python features.

Step 5: Initialize the relevant definitions -- specific to each game
Define three empty lists
' '' pos_v=[] pos_e = [] pos_mm = [] '' '  
# same as above
pos_v=pos_e=pos_mm=[]  
# The Angle at which the other planets, such as Venus, Earth and the Moon, have gone around the sun
roll_v = roll_e = roll_m = 0  
roll_3=roll_4=roll_5=roll_6=roll_7=roll_8=0  
  
# The position of the sun -- relatively fixed -- is centered
Size =(width, height)
#size[0]=width,size[1]=height  
position = size[0]//2, size[1]//2
Copy the code

8. Step 6:

#-- Step 6 -- Game loop --
while True:  
    # 6-1 - - - - in the first place
    Pygame's game loop is essential -- personal suggestions and favorites
    for event in pygame.event.get():  
        if event.type == QUIT:  
            sys.exit()  
    The background color is black
    screen.fill(BLACK)  
    # Display text Settings on screen
    textImage=myfont.render("Sun=yellow,Earth=blue,Moon=green,Venas=red",True,GREEN)  
    # display at screen coordinates 100 and 100Screen. Blit (textImage, (100100))#-- 6-2-- Draw the sun size, position, color Settings, the size of 60 is more appropriate --
    pygame.draw.circle(screen, YELLOW, position, 60, 0)  
  
    #-- 6-3-- =the Earth--Roll_e + = 0.01# Assume that the Earth rotates 0.01 PI per frame
    pos_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 earth's trajectory line -- take it or leave it --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN, pos_e[i], 1, 0)  
  
    # 6-4 = The Moon--Roll_m + = 0.1# Assume that the moon orbits 0.1 PI per frame
    pos_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, GREEN, (pos_m_x, pos_m_y), 5, 0)  
  
    #-- The trajectory line of the moon -- yes or no --
    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)):  
        # track line is green= green=0,255,0
        pygame.draw.circle(screen, GREEN ,pos_mm[i], 1, 0)  
  
    #-- 6-5-- Venus = The Venas--Roll_v + = 0.015# Assume that Venus orbits 0.015 PI per frame
    pos_v_x = int(size[0]//2 + size[1]//3*math.sin(roll_v))  
    pos_v_y = int(size[1]//2 + size[1]//3*math.cos(roll_v))  
    pygame.draw.circle(screen, RED, (pos_v_x, pos_v_y), 20, 0)  
      
    #-- Trajectory line of Venus -- take it or leave it --
    pos_v.append((pos_v_x, pos_v_y))  
    if len(pos_v) > 255:  
        pos_v.pop(0)  
    for i in range(len(pos_v)):  
        # track line is green= green=0,255,0Pygame.draw. Circle (screen, (0,255,0), pos_v[I], 1, 0)The other planets have the disadvantage of not having elliptical orbits
    # 3  Roll_3 + = 0.03# Assume that Venus orbits 0.03 PI per framePos_3_x = int (size [0] / / 2 + size [1] / / 3.5 * math.h sin (roll_3) pos_3_y = int (size 2 + [1] / / the size [1] / / 3.5 * math.h cos (roll_3)) pygame.draw.circle(screen, WHITE,(pos_3_x, pos_3_y), 20, 0)# 4  Roll_4 + = 0.04# Assume that Venus orbits 0.04 PI per frame
    pos_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, WHITE,(pos_4_x, pos_4_y), 20, 0)  
  
    # 5  Roll_5 + = 0.05# Assume that Venus orbits 0.05 PI per frame
    pos_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, WHITE, (pos_5_x, pos_5_y), 20, 0)  
    # 6  Roll_6 + = 0.06# Assume that Venus orbits 0.06 PI per framePos_6_x = int (size [0] / / 2 + size [1] / / 2.5 * math.h sin (roll_6) pos_6_y = int (size 2 + [1] / / the size [1] / / 2.5 * math.h cos (roll_6)) pygame.draw.circle(screen, WHITE,(pos_6_x, pos_6_y), 20, 0)# 7  Roll_7 + = 0.07# Assume that Venus orbits 0.07 PI per framePos_7_x = int (size [0] / / 2 + size [1] / / 4.5 * math.h sin (roll_7) pos_7_y = int (size 2 + [1] / / the size [1] / / 4.5 * math.h cos (roll_7)) pygame.draw.circle(screen, WHITE, (pos_7_x, pos_7_y), 20, 0)# 8  Roll_8 + = 0.08# Assume that Venus orbits 0.08 PI per framePos_8_x = int (size [0] / / 2 + size [1] / / 5.5 * math.h sin (roll_8) pos_8_y = int (size 2 + [1] / / the size [1] / / 5.5 * math.h cos (roll_8)) pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20, 0)# refresh
    pygame.display.flip()  
    # The larger the value, the faster the refresh, the faster the ball movement
    clock.tick(40)  

Copy the code

9. Renderings:

10. Bookmarkable, slowly explore PyGame’s code line by line.