Pergunta

I've tried multiple different solutions for this and I'm out of idea's. I'm brand new to Kivy and still learning. I'm building a very basic memorization game. I need the buttons inside the grid to blink in a certain order. I'm confident that I can make that work, however I can't figure out how to get one of the buttons to blink. Can anyone point me in the right direction? I know I just need need it to change colors for a second and then change back.

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from random import random
import time


class MemorizeGame(GridLayout):
    def __init__(self, **kwargs):
        super(MemorizeLayout, self).__init__(**kwargs)
        self.cols = 2

        #create buttons
        self.button1 = Button(text='', background_color=(0,0,1,1))
        self.button2 = Button(text='', background_color=(0,1,1,1))
        self.button3 = Button(text='', background_color=(1,0,1,1))
        self.button4 = Button(text='', background_color=(0,1,0,1))
        self.buttonList = [self.button1, self.button2, self.button3, self.button4]

        #add buttons to the screen
        for button in self.buttonList:
            self.add_widget(button)

    def blinkSquare(self):
        #logic to make a square blink

class MemorizeApp(App):

    def build(self):
        game= MemorizeGame()
        return game

if __name__ == "__main__":
    MemorizeApp().run()
Foi útil?

Solução

Here's an example of how to do it, using kivy's clock.

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from random import random
import time

from kivy.clock import Clock
from functools import partial

def set_color(button, color, *args):
    button.color = color

class MemorizeGame(GridLayout):
    def __init__(self, **kwargs):
        super(MemorizeGame, self).__init__(**kwargs)
        self.cols = 2

        #create buttons                                                                                                                                                                                                            
        self.button1 = Button(text='', background_color=(0,0,1,1))
        self.button2 = Button(text='', background_color=(0,1,1,1))
        self.button3 = Button(text='', background_color=(1,0,1,1))
        self.button4 = Button(text='', background_color=(0,1,0,1))
        self.buttonList = [self.button1, self.button2, self.button3, self.button4]

        #add buttons to the screen                                                                                                                                                                                                 
        for button in self.buttonList:
            self.add_widget(button)

    def blinkSquare(self):
        self.button1.background_color = (1, 1, 1, 1)
        def reset_color(*args):
            self.button1.background_color = (0, 0, 1, 1)
        Clock.schedule_once(reset_color, 1)

        # alternative:
        # Clock.schedule_once(partial(set_color, self.button1, (0, 0, 1, 1)))

    def on_touch_down(self, touch):
        self.blinkSquare()


class MemorizeApp(App):

    def build(self):
        game= MemorizeGame()
        return game

if __name__ == "__main__":
    MemorizeApp().run()

This just blinks the first button, but I'm sure you can extend it to do exactly what you want.

You might find it neatest to make your own button subclass with the color reset function, rather than making a new function each time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top