简体   繁体   中英

how to make kivy widgets make a pause

I wanted to make a small program with two widgets to learn some things about game development with kivy. One Widget is supposed to run around the screen randomly, the otherone stands still. Now I wanted to find a way to implement a "pause button" wich is supposed to create a "pause modus", where all widgets of the game stop doing anything, if the player wants to make a break or something. I tried to google it, but I did not find anything. So I wanted to ask if someone alredy knows such a funktion? Here is my code:

from kivy.base import runTouchApp
from kivy.lang import Builder

from random import random

from kivy.uix.widget import Widget

from kivy.clock import Clock
from kivy.animation import Animation

from kivy.properties import ListProperty
from kivy.core.window import Window
Builder.load_string('''
<Root>:
    ClockRect:
        pos: 300, 300
    AnimRect:
        pos: 500, 300

<ClockRect>:
    canvas:
        Color:
            rgba: 10, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
<AnimRect>:
    canvas:
        Color:
            rgba:0, 20, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
''')

class Root(Widget):
    pass

class ClockRect(Widget):

    velocity = ListProperty([10, 15])
    def __init__(self, **kwargs):
        super(ClockRect, self).__init__(**kwargs)
        Clock.schedule_interval(self.Update, 1/60.)

    def Update(self, *args):
        self.x += self.velocity[0]
        self.y += self.velocity[1]

        if self.x < 0 or (self.x + self.width) > Window.width:
            self.velocity[0] *= -1
        if self.y < 0 or (self.y + self.height) > Window.height:
            self.velocity[1] *= -1
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print 'es geht'

class AnimRect(Widget):


    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            pass#if this funktion is executed, I want to pause the game

runTouchApp(Root())

I'm very new to Kivy and graphics programming in general, but I kind of managed to get it to stop. Hopefully you can take this further:

I replaced your line:

Clock.schedule_interval(self.Update, 1/60.)

With these two lines:

self.My_Clock = Clock
self.My_Clock.schedule_interval(self.Update, 1/60.)

Then I added the following line to the top of your 'def on_touch_down' method:

self.My_Clock.unschedule(self.Update)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM