-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbouncing_ball.py
65 lines (48 loc) · 1.71 KB
/
bouncing_ball.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
# -*- coding: utf-8 -*-
import sfml as sf
WIDTH = 640
HEIGHT = 480
TITLE = "Python SFML Bouncing Ball"
SPEED_FACTOR = 60
settings = sf.ContextSettings()
settings.antialiasing_level = 8
class Game:
def __init__(self):
self.window = sf.RenderWindow(sf.VideoMode(WIDTH, HEIGHT),
TITLE,
sf.Style.DEFAULT,
settings)
self.window.framerate_limit = 60
self.circle = sf.CircleShape(30)
self.circle.origin = self.circle.radius, self.circle.radius
self.circle.position = self.window.size / 2
self.c_vel = sf.Vector2(5, -5)
self.clock = sf.Clock()
def run(self):
while self.window.is_open:
for event in self.window.events:
self.event_handler(event)
elapsed_time = self.clock.restart().seconds
self.update(elapsed_time)
self.render()
def event_handler(self, event):
if type(event) is sf.CloseEvent:
self.window.close()
def update(self, delta):
if not (self.circle.radius < self.circle.position.y < HEIGHT - self.circle.radius):
x, y = self.c_vel
y *= -1.0
self.c_vel = sf.Vector2(x, y)
if not self.circle.radius < self.circle.position.x < WIDTH - self.circle.radius:
x, y = self.c_vel
x *= -1.0
self.c_vel = sf.Vector2(x, y)
self.circle.move(self.c_vel * delta * SPEED_FACTOR)
def render(self):
self.window.clear()
self.window.draw(self.circle)
self.window.display()
if __name__ == '__main__':
oyun = Game()
oyun.run()
# Game().run()