-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
99 lines (78 loc) · 3.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from tkinter import *
import random
class App(Frame):
def __init__(self, master):
super().__init__(master)
self.main()
self.mainloop()
def main(self):
self.Wscreen = self.master.winfo_screenwidth()
self.Hscreen = self.master.winfo_screenheight()
self.master.title("Particules")
canvas = Canvas(self.master, width=self.Wscreen, height=self.Hscreen)
canvas.pack()
self.setup(canvas)
def setup(self, canvas):
self.master.update()
canvas.Wscreen = self.Wscreen
canvas.Hscreen = self.Hscreen
self.nbrPart = 250
canvas.particles = []
canvas.particles.append(Particle(canvas))
self.update(canvas)
def update(self, canvas):
self.master.update()
canvas.delete(ALL)
for part in canvas.particles:
part.update()
if len(canvas.particles) < self.nbrPart:
canvas.particles.append(Particle(canvas))
self.after(10, lambda: self.update(canvas))
class Particle():
def __init__(self, canvas):
self.canvas = canvas
self.size = 5
self.screenFace = random.randint(0, 3)
if self.screenFace == 0: # top
self.x = random.randint(0, self.canvas.winfo_width())
self.y = self.canvas.winfo_height() + random.randint(10,100)
self.xspeed = random.uniform(-1,1)
self.yspeed = -random.uniform(0.5,3)
elif self.screenFace == 1: # right
self.x = self.canvas.winfo_width() + random.randint(10,100)
self.y = random.randint(0, self.canvas.winfo_height())
self.xspeed = -random.uniform(0.5,3)
self.yspeed = random.uniform(-1,1)
elif self.screenFace == 2: # bottom
self.x = random.randint(0, self.canvas.winfo_width())
self.y = -random.randint(10,100)
self.xspeed = random.uniform(-1,1)
self.yspeed = random.uniform(0.5,3)
elif self.screenFace == 3: # left
self.x = -random.randint(10,100)
self.y = random.randint(0, self.canvas.winfo_height())
self.xspeed = random.uniform(0.5,3)
self.yspeed = random.uniform(-1,1)
def update(self):
self.canvas.create_oval(self.x-self.size,
self.y-self.size,
self.x+self.size,
self.y+self.size,
fill="green")
self.x = self.x + self.xspeed
self.y = self.y + self.yspeed
if self.screenFace == 0: # top
if self.y < 0:
self.canvas.particles.remove(self)
elif self.screenFace == 1: # right
if self.x < 0:
self.canvas.particles.remove(self)
elif self.screenFace == 2: # bottom
if self.y > self.canvas.Hscreen:
self.canvas.particles.remove(self)
elif self.screenFace == 3: # left
if self.x > self.canvas.Wscreen:
self.canvas.particles.remove(self)
if __name__ == "__main__":
root = Tk()
App(root)