Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

python - 单击球时如何在屏幕上制作动画?(How to do animation on screen when click on ball?)

This code includes 3 screens first is play button screen, second is stage screen and third is mainscreen on which whole animation is displaying.

(该代码包括3个屏幕,第一个是播放按钮屏幕,第二个是舞台屏幕,第三个是主屏幕,在整个屏幕上显示动画。)

As soon as I reach the animation screen, animation gets started automatically that I want after I click the ball.

(到达动画屏幕后,单击该球后,动画将自动开始播放。)

I have tried many things but nothing worked so please help!

(我已经尝试了很多东西,但是没有任何效果,所以请帮忙!)

Here is the code

(这是代码)

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, CardTransition
from kivy.properties import ObjectProperty, NumericProperty, ReferenceListProperty
from kivy.uix.widget import Widget
from kivy.vector import Vector
from kivy.graphics import Rotate, Rectangle, Ellipse, Color
from kivy.uix.gridlayout import GridLayout
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.progressbar import ProgressBar

from kivy.uix.popup import Popup
from kivy.uix.button import Button

Builder.load_string('''
<PongBall>:
    size: 50, 50
    canvas:
        Color:
            rgba: 0,0,1,1
        Ellipse:
            pos: self.pos
            size: self.size


<PongGame>:
    ball: pong_ball
    object: Object

    PongBall:
        id: pong_ball
        center: self.center

    Object:
        id: Object
        center: self.rotate_origin


<Manager>:
    id: screen_manager

    Screen:
        name:"P"
        FloatLayout:


            Button:
                pos_hint:{"x":0.2,"y":0.05}
                size_hint: 0.6, 0.2
                font_size: (root.width**2 + root.height**2) / 13**4
                text: "Play"
                on_release:
                    root.transition.direction = "up"        
                    root.current = "stage"

    Screen:
        name: 'Loading'
        MyWidget:                

    Screen:
        name: "stage"
        FloatLayout:
            Label:
                pos_hint:{"x": 0.04, "y":0.3}
                size_hint:1.0, 1.0
                text: "Stage>>"

            Button:
                pos_hint:{"x":0,"y":0.05}
                size_hint: 0.2, 0.2
                text:"1"
                on_release:
                    root.transition.direction = "right"
                    root.current= "Game";game.serve_ball()

    Screen:
        name: 'Game'
        PongGame:
            id: game

''')

class Object(Widget):
    def __init__(self, *args, **kwargs):
        Widget.__init__(self, *args, **kwargs)
        self.rect_pos_x = 500
        self.rect_pos_y = 300
        self.rect_pos = self.rect_pos_x, self.rect_pos_y
        self.rect_width = 200
        self.rect_height = 50
        self.rect_size = self.rect_width, self.rect_height
        self.rotate_origin_x = self.rect_pos_x + self.rect_width / 2
        self.rotate_origin_y = self.rect_pos_y + self.rect_height / 2
        self.rotate_origin = self.rotate_origin_x, self.rotate_origin_y
        self.angle = 135
        with self.canvas:
            Rotate(origin=self.rotate_origin, angle=self.angle)
            Color(rgb=(0,197,68))
            Rectangle(pos=self.rect_pos, size=self.rect_size)


    def rotate(self):
        self.canvas.clear()
        with self.canvas:
            Rotate(origin=self.rotate_origin, angle=self.angle)
            Color(rgb=(0, 255, 100))
            Rectangle(pos=self.rect_pos, size=self.rect_size)
        self.angle += 90

    def deflect_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            if self.angle == 135 :
                ball.velocity = Vector(-vx, vy).rotate(90)
            else:
                ball.velocity = Vector(-vx, vy).rotate(270)
            print('collided')

    def on_touch_up(self, touch):
        if self.collide_point(*touch.pos):
            self.rotate()

class PongBall(Widget):
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    def move(self):
        self.pos = Vector(*self.velocity) + self.pos


class PongGame(Widget):
    ball = ObjectProperty(None)
    object = ObjectProperty(None)

    def __init__(self, *args, **kwargs):
        super(PongGame, self).__init__(*args, **kwargs)
        Clock.schedule_interval(self.update, 1.0 / 60.0)


    def serve_ball(self, vel=(4, 0)):
        self.ball.center = 40, 310
        self.ball.velocity = vel

    def update(self, dt):
        self.ball.move()

        self.object.deflect_ball(self.ball)

        if (self.ball.y < self.y):
            self.ball.velocity_y *= -1
            self.ball.velocity_y = 0
        if (self.ball.top > self.top):
            print('Done')



class Manager(ScreenManager):
    pass

sm = Manager()

class ScreensApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    ScreensApp().run()

Thanks in advance...

(提前致谢...)

  ask by Tanishq chandra translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...