Making programs work together.

Peter Otten __peter__ at web.de
Wed Aug 17 02:39:51 EDT 2005


ChuckDubya at gmail.com wrote:

> Example: I'm driving a car in a game and I hit an oil slick so instead
> of me having to lift off the throttle button on the keyboard, I want to
> make a program to disengage the throttle as long as I'm on that oil
> slick.  Does that clear anything up?

Yes. Here's how to do it:

import random
import time

class Car:
    def throttle(self, on):
        if on:
            print "full throttle"
        else:
            print "on that oil slick again"


class ThatOilSlick:
    def __contains__(self, item):
        return random.random() < .7

class Game:
    def __init__(self):
        self.car = Car()
        self.thatOilSlick = ThatOilSlick()
    def play(self):
        while True:
            self.car.throttle(self.car in self.thatOilSlick)
            time.sleep(.2)

if __name__ == "__main__":
    Game().play()

:-)

Look here for further information:
www.catb.org/~esr/faqs/smart-questions.html

Peter





More information about the Python-list mailing list