如何在kivy中短时间显示标签?

2024-09-29 21:50:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用:

  • 窗口10
  • Python 3.9
  • 基维2.0.0

我的问题是:我只是按照kivy documentation中的教程来做。我想显示一个标签duce = StringProperty()一小段时间,比如1秒后,它会自动消失。我可以让它工作,但想问/寻找更好的解决方案:)thx

代码:

main.py

#import kivy

#kivyVersion = kivy.__version__  # check kivy version

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, StringProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock

class Ball(Widget):
    Vx = NumericProperty(0)
    Vy = NumericProperty(0)

    Velocity = ReferenceListProperty(Vx, Vy)

    def Move(self):
        self.pos = Vector(*self.Velocity) + self.pos


class Paddle(Widget):

    score = StringProperty("0")  # set the score label be a string with text "0" at the beginning

    def Bounce(self, ball):  # to make the ball move faster when touched the paddle --> increase difficulty + more fun
        if self.collide_widget(ball):

            Vx, Vy = ball.Velocity  # unpack the  ball.Velocity
        
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounce = Vector((Vx * -1), Vy)
            bouncedVelocity = bounce * 1.1
            ball.Velocity = bouncedVelocity.x, bouncedVelocity.y + offset

    
class PongGame(Widget):  # ref. mypong.kv
    ball = ObjectProperty(None)
    leftplayer = ObjectProperty(None)
    rightplayer = ObjectProperty(None)

    duce = StringProperty()

    GameOver = False

    def ServeBall(self, v = (4, 0)):
        self.ball.center = self.center  # set the cennter of the ball at the center of the screen
        self.ball.Velocity = v  # start velocity  # Vector(4, 0).rotate(randint(0, 360))  # randomly set a Vx and Vy for the ball

    def Update(self, dt):
        
        self.ball.Move()  # Move() function to make the ball move with constant v

        self.leftplayer.Bounce(self.ball)  # bounce when touched player paddle
        self.rightplayer.Bounce(self.ball)

        if self.ball.y < self.y or self.ball.top > self.top:  # touch bottom or top --> bounce
            self.ball.Vy *= -1  # same velocity by in opposite direction

        if self.ball.x < self.x:  # touch left --> bounce and rightplayer score += 1
            self.rightplayer.score = str(int(self.rightplayer.score) + 1)
            self.ball.Vx *= -1
            self.ServeBall(v = (4, 0))

        if self.ball.right > self.width:  # touch right --> bounce and leftplayer score += 1
            self.leftplayer.score = str(int(self.leftplayer.score) + 1)
            self.ball.Vx *= -1
            self.ServeBall(v = (-4, 0))


        def Duce():  # duce | when one of the player score 10 . two player 's score different == 1
            self.GameOver = False
            Duce()

        ######################
        if int(self.leftplayer.score) == 11 or int(self.rightplayer.score) == 11:  # display the text "Duse !" for a short while
            self.duce = "Duce !"
        else:
            self.duce = ""
        ######################
                    
        if self.leftplayer.score != self.rightplayer.score:
            try:
                if int(self.leftplayer.score) >= 10 or int(self.rightplayer.score) >= 10:

                    if int(self.leftplayer.score) - int(self.rightplayer.score) == 1 or int(self.leftplayer.score) - int(self.rightplayer.score) == -1:
                        Duce()
                    
                    elif int(self.leftplayer.score) - int(self.rightplayer.score) >= 2 or int(self.leftplayer.score) - int(self.rightplayer.score) <= -2:
                        self.GameOver = True
                    
                        self.ServeBall(v = (0, 0))  # set the ball rest on the screen 's center
                    
                        if int(self.leftplayer.score) > int(self.rightplayer.score):
                            self.leftplayer.score = "You Win !"  # change label text
                            self.rightplayer.score = "Ops !"  # change label text
                        
                        elif int(self.rightplayer.score) > int(self.leftplayer.score):
                            self.leftplayer.score = "Ops !"  # change label text
                            self.rightplayer.score = "You Win !"  # change label text

            except RecursionError:
                pass

            except ValueError:
                pass
                        
            except Exception as e:
                print("Error :", e)
            

    def on_touch_move(self, touch):  # | widget | function name must be  on_touch_move
        if touch.x < (self.width / 3):
            self.leftplayer.center_y = touch.y
        
        if touch.x > (self.width - self.width / 3):
            self.rightplayer.center_y = touch.y

        if self.GameOver == True:  # when game is over
            if touch.x < self.width:  # when player toch screen  -->  reset all
                self.GameOver = False
                self.leftplayer.score = "0"
                self.rightplayer.score = "0"
                self.ServeBall()


class MyPongApp(App):  # name of the .kv file should be the text before "App"   # start up 

    def build(self):
        self.title = "My First Kivy Game"
        Game = PongGame()  # creating obj for  class PongGame
        Game.ServeBall()  # run  class PongGame  function  ServeBall()
        Clock.schedule_interval(Game.Update, 1.0/120.0)
        return Game


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

mypong.kv:

#:kivy 2.0.0

<Ball>:
    size: 50, 50
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size


<Paddle>:
    size: 25, 200
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size


<PongGame>:
    ball: myBall
    leftplayer: left_player
    rightplayer: right_player

    canvas:
        Rectangle:
            pos: self.center_x - 5, 0
            size: 10, self.height
    
    Label:
        font_size: 70  
        center_x: root.width / 4
        top: root.top - 50
        text: root.leftplayer.score
        
    Label:
        font_size: 70  
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: root.rightplayer.score

    Label:
        font_size: 70
        center_x: root.width /2
        center_y: root.top / 2
        text: root.duce


    Ball:
        id: myBall
        center: self.parent.center


    Paddle:
        id: left_player
        x: root.x
        center_y: root.center_y

    Paddle:
        id: right_player
        x: root.width - self.width
        center_y: root.center_y

我自己的解决方案:

#####################
if int(self.leftplayer.score) == 11 or int(self.rightplayer.score) == 11:  # display the text "Duse !" for a short while
    self.duce = "Duce !"
else:
    self.duce = ""
######################

我还需要在mypong.kv上添加什么吗


Tags: thetextselfsizeifrootwidthint
1条回答
网友
1楼 · 发布于 2024-09-29 21:50:17

您可以使用Clock对象参考文档here

这就是在1秒后隐藏标签的方式。这将隐藏标签,而不仅仅是删除文本。但是,您应该为标签提供一个id,以便能够修改属性

from kivy.clock import Clock


def hide_label(dt):
    self.ids.[labelId].size_hint_x=0 # to hide the label best practice is setting size to 0

self.ids.[labelId].size_hint_x=1 # Show the label 
Clock.schedule_once(hide_label, 1)

相关问题 更多 >

    热门问题