# Pymunk and PyGame - Physics Balls - Settings Parser

Jul 5th, 2022
932
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def parseSettings():
2.     settingsFile = open("settings.inf", "r", encoding="utf-8")
3.     (ball_size, ball_elasticity, ball_friction, ball_mass, rectangle_mass, rectangle_width, rectangle_height,
4.      rectangle_elasticity, rectangle_friction) = 0, 0, 0, 0, 0, 0, 0, 0, 0
5.     for i in settingsFile:
6.         if i == "\n":
7.             continue
8.         if "ball_size" in i:
9.             a = i.split()
10.             ball_size = float(a[2])  # 0
11.         elif "ball_elasticity" in i:
12.             a = i.split()
13.             ball_elasticity = float(a[2])  # 1
14.         elif "ball_friction" in i:
15.             a = i.split()
16.             ball_friction = float(a[2])  # 2
17.         elif "ball_mass" in i:
18.             a = i.split()
19.             ball_mass = float(a[2])  # 3
20.
21.         elif "rectangle_mass" in i:
22.             a = i.split()
23.             rectangle_mass = float(a[2])  # 4
24.         elif "rectangle_width" in i:
25.             a = i.split()
26.             rectangle_width = float(a[2])  # 5
27.         elif "rectangle_height" in i:
28.             a = i.split()
29.             rectangle_height = float(a[2])  # 6
30.         elif "rectangle_elasticity" in i:
31.             a = i.split()
32.             rectangle_elasticity = float(a[2])  # 7
33.         elif "rectangle_friction" in i:
34.             a = i.split()
35.             rectangle_friction = float(a[2])  # 8
36.
37.     return (ball_size, ball_elasticity, ball_friction, ball_mass, rectangle_mass, rectangle_width, rectangle_height,
38.             rectangle_elasticity, rectangle_friction)
39.
40.
41. def testParse():
42.     tempSettings = parseSettings()
43.     print(f"Мячики\n"
44.           f"------\n"
45.           f"Радиус[0]: {tempSettings[0]}\n"
46.           f"Упругость[1]: {tempSettings[1]}\n"
47.           f"Трение[2]: {tempSettings[2]}\n"
48.           f"Масса[3]: {tempSettings[3]}\n"
49.           f"------\n")
50.     print(f"Прямоугольники\n"
51.           f"--------------\n"
52.           f"Масса[4]: {tempSettings[4]}\n"
53.           f"Ширина[5]: {tempSettings[5]}\n"
54.           f"Высота[6]: {tempSettings[6]}\n"
55.           f"Упругость[7]: {tempSettings[7]}\n"
56.           f"Трение[8]: {tempSettings[8]}\n"
57.           f"--------------\n")
58.
59.
60. def hmm(fileText, string, value, action=True):
61.     fileText2 = fileText
62.     tempStr0 = ""
63.     tempStr = ""
64.     for i in fileText:
65.         tempStr = i
66.         if string in tempStr:
67.             tempStr0 = tempStr
68.             tempStr2 = tempStr.split()
69.             if action:
70.                 tempStr2[2] = str(round(float(tempStr2[2]) + value, 1))
71.             else:
72.                 tempStr2[2] = str(round(float(tempStr2[2]) - value, 1))
73.             tempStr = " ".join(tempStr2)
74.             break
75.         else:
76.             continue
77.     fileText2 = "\n".join(fileText2)
78.     fileText2 = fileText2.replace(tempStr0, tempStr)
79.     file = open("settings.inf", "w", encoding="utf-8")
80.     file.write(fileText2)
81.     file.close()
82.
83.
84. def changeFileParameters(value, action=True, ball_size=False, ball_elasticity=False, ball_friction=False,
85.                          ball_mass=False, rectangle_mass=False, rectangle_width=False,
86.                          rectangle_height=False, rectangle_elasticity=False, rectangle_friction=False):
87.     tempStr0 = ""
88.     tempStr = ""
89.     file = open("settings.inf", "r", encoding="utf-8")
91.     fileText2 = fileText
92.     fileText = fileText.split("\n")
93.     file.close()
94.     if ball_size:
95.         hmm(fileText, "ball_size", value, action)
96.     elif ball_elasticity:
97.         hmm(fileText, "ball_elasticity", value, action)
98.     elif ball_friction:
99.         hmm(fileText, "ball_friction", value, action)
100.     elif ball_mass:
101.         hmm(fileText, "ball_mass", value, action)
102.     elif rectangle_mass:
103.         hmm(fileText, "rectangle_mass", value, action)
104.     elif rectangle_width:
105.         hmm(fileText, "rectangle_width", value, action)
106.     elif rectangle_height:
107.         hmm(fileText, "rectangle_height", value, action)
108.     elif rectangle_elasticity:
109.         hmm(fileText, "rectangle_elasticity", value, action)
110.     elif rectangle_friction:
111.         hmm(fileText, "rectangle_friction", value, action)
112.     return 0
113.
114.
115. changeFileParameters(12, ball_size=True)
116. testParse()