Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit823f0aa

Browse files
committed
add planet simulator tutorial
1 parent53690e7 commit823f0aa

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
218218
-[How to Make a File Explorer using Tkinter in Python](https://www.thepythoncode.com/article/create-a-simple-file-explorer-using-tkinter-in-python). ([code](gui-programming/file-explorer))
219219
-[How to Make a Calculator with Tkinter in Python](https://www.thepythoncode.com/article/make-a-calculator-app-using-tkinter-in-python). ([code](gui-programming/calculator-app))
220220
-[How to Make a Typing Speed Tester with Tkinter in Python](https://www.thepythoncode.com/article/how-to-make-typing-speed-tester-in-python-using-tkinter). ([code](gui-programming/type-speed-tester))
221+
-[How to Make a Planet Simulator with PyGame in Python](https://www.thepythoncode.com/article/make-a-planet-simulator-using-pygame-in-python). ([code](gui-programming/planet-simulator))
221222

222223
For any feedback, please consider pulling requests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Make a Planet Simulator with PyGame in Python](https://www.thepythoncode.com/article/make-a-planet-simulator-using-pygame-in-python)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Imports
2+
importsys
3+
importpygame
4+
5+
# We will work with the Vector2 because it has some useful functions.
6+
frompygame.mathimportVector2
7+
8+
fromrandomimportrandrange
9+
10+
importctypes
11+
12+
# Enable High Dots Per Inch so the image displayed on the window is sharper.
13+
ctypes.windll.shcore.SetProcessDpiAwareness(1)
14+
15+
# Configuration
16+
pygame.init()
17+
fps=60
18+
fpsClock=pygame.time.Clock()
19+
20+
# Window Size
21+
windowdim=Vector2(800,800)
22+
screen=pygame.display.set_mode((int(windowdim.x),int(windowdim.y)))
23+
24+
# all the Planets are stored here
25+
# They will append themselves.
26+
planets= []
27+
28+
# The Planet Class which will handle drawing and calculating planets.
29+
classPlanet():
30+
def__init__(self,position,delta=Vector2(0,0),radius=10,imovable=False):
31+
32+
# Where the planet is at the moment
33+
self.position=position
34+
35+
# The Radius determines how much this planet effects others
36+
self.radius=radius
37+
38+
# The Velocity
39+
self.delta=delta
40+
41+
# If this planet is moving
42+
self.imovable=imovable
43+
44+
# If this planet can be eaten by others.
45+
self.eatable=False
46+
47+
48+
# Appending itself to the list so its process
49+
# function will later be called in a loop.
50+
planets.append(self)
51+
52+
53+
defprocess(self):
54+
# This function will be called once every frame
55+
# and it is responsible for calculating where the planet will go.
56+
57+
# No Movement Calculations will happen if the planet doesnt move at all.
58+
# it also wont be eaten.
59+
ifnotself.imovable:
60+
foriinplanets:
61+
ifnotiisself:
62+
try:
63+
ifself.eatable:
64+
ifself.position.distance_to(i.position)<self.radius+i.radius:
65+
print('Eaten')
66+
i.radius+=self.radius
67+
planets.remove(self)
68+
dir_from_obj= (i.position-self.position).normalize()*0.01* (i.radius/10)
69+
self.delta+=dir_from_obj
70+
except:
71+
print('In the same spot')
72+
73+
self.position+=self.delta
74+
75+
# Drawing the planet at the current position.
76+
pygame.draw.circle(
77+
screen,
78+
[255,255,255],
79+
self.position,
80+
self.radius,
81+
)
82+
83+
# Sun and two opposing Planets
84+
""" Planet(Vector2(400, 400), radius=50, imovable=True)
85+
86+
Planet(Vector2(400, 200), delta=Vector2(3, 0), radius=10)
87+
Planet(Vector2(400, 600), delta=Vector2(-3, 0), radius=10) """
88+
89+
# Sun and four opposing Planets
90+
Planet(Vector2(400,400),radius=50,imovable=True)
91+
92+
Planet(Vector2(400,200),delta=Vector2(3,0),radius=10)
93+
Planet(Vector2(400,600),delta=Vector2(-3,0),radius=10)
94+
Planet(Vector2(600,400),delta=Vector2(0,3),radius=10)
95+
Planet(Vector2(200,400),delta=Vector2(0,-3),radius=10)
96+
97+
# Two Suns and two planets
98+
""" Planet(Vector2(600, 400), radius=20, imovable=True)
99+
Planet(Vector2(200, 400), radius=20, imovable=True)
100+
101+
Planet(Vector2(400, 200), delta=Vector2(0, 0), radius=10)
102+
Planet(Vector2(400, 210), delta=Vector2(1, 2), radius=5) """
103+
104+
# Grid
105+
# gridDimension = 10
106+
# gridgap = 80
107+
# for x in range(gridDimension):
108+
# for y in range(gridDimension):
109+
# Planet(Vector2(gridgap * x + 40, gridgap * y + 40), radius=3, imovable=True)
110+
111+
# Planet(Vector2(200, 200), delta=Vector2(randrange(-3, 3), 2), radius=5)
112+
113+
# Game loop.
114+
whileTrue:
115+
screen.fill((0,0,0))
116+
foreventinpygame.event.get():
117+
ifevent.type==pygame.QUIT:
118+
pygame.quit()
119+
sys.exit()
120+
121+
forpinplanets:
122+
p.process()
123+
124+
pygame.display.flip()
125+
fpsClock.tick(fps)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp