Scheduling events with the clock

We saw that Animation has a duration parameter that establishes the time in which an animation should take place. A different time-related topic is the scheduling of a particular task at a certain time or during intervals of n seconds. In these cases, we use the Clock class. Let's analyze the following code, fragment 2 (of 2), of fleet.py:

189. # File name: fleet.py (Fragment 2) 190. def schedule_events(self): 191. Clock.schedule_interval(self.solo_attack, 2) 192. Clock.schedule_once(self.shoot,random()) 193. 194. def solo_attack(self, dt): 195. if len(self.survivors): 196. rint = randint(0, len(self.survivors) - 1) 197. child = self.survivors[rint] 198. child.invader.solo_attack() 199. 200. def shoot(self, dt): ...

Get Kivy – Interactive Applications and Games in Python - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.