main.py
main.py — Python Source, 2 KB (2090 bytes)
Dateiinhalt
import kivy kivy.require('1.9.0') from kivy.app import App from kivy.clock import Clock from kivy.uix.gridlayout import GridLayout from kivy.uix.button import Button from random import random from random import randint offen = 0 class MemoryKnopf (Button): def __init__ (self, farbe, *args): self.farbe = farbe self.rueckenFarbe = (0.3, 0.3, 0.3, 1) Button. __init__ (self, text = '', background_color = self.rueckenFarbe) def aufdecken (self, obj): global offen global partner global paare if offen < 2: self.background_color = self.farbe offen += 1 if offen == 1: partner = self else: if self.farbe == partner.farbe: Clock.schedule_once (self.wegnehmen, 2) Clock.schedule_once (partner.wegnehmen, 2) offen -= 2 paare -= 1 if paare == 0: Clock.schedule_once (App.get_running_app().stop, 5) else: Clock.schedule_once (self.zudecken, 2) def zudecken (self, obj): global anzahl global offen self.background_color = self.rueckenFarbe offen -= 1 if offen == 1: partner.zudecken (self) def wegnehmen (self, obj): self.background_color = (0, 0, 0, 1) self.disabled = True class MemoryApp (App): def build(self): global paare self.elter = GridLayout (cols = 4, padding = 5) self.rgbafarben = [(1, 1, 1, 1), (1, 1, 0, 1), (1, 0, 1, 1), (0, 1, 1, 1), (1, 0, 0, 1), (0, 1, 0, 1), (1, 0, 0, 1), (1, 0.7, 0.3, 1)]; paare = len (self.rgbafarben) self.rgbafarben = self.rgbafarben + self.rgbafarben self.knoepfe = [] for i in range (0, 2*paare): index = randint (0, len (self.rgbafarben) - 1) knopf = MemoryKnopf (self.rgbafarben[index]) self.rgbafarben.remove (self.rgbafarben[index]) self.knoepfe.append (knopf) self.elter.add_widget (knopf) knopf.bind (on_release = knopf.aufdecken) return self.elter if __name__ == '__main__': MemoryApp().run()