Start_python’s diary

ふたり暮らし

アラフィフ夫婦のフリーランスプラン

Kivy ボタンでボールの速度を変える(Python Kivyの取説・使い方 第7回)

ボールの速度を変える方法

今回はボタンを使ってみます。スピードアップとスピードダウン、そして逆再生の3つのボタンを作ります。(本当は一時停止を作りたかったのですがグローバル変数とか面倒だったので逆再生にしたのは内緒です)

第6回で使ったプログラムを少し修正します。

 

f:id:Start_python:20191213162647g:plain

プログラムのコード

# フル画面を解除して画面の幅と高さを設定
from kivy.config import Config
Config.set('graphics', 'fullscreen', 0)
Config.set('graphics', 'width', 320)
Config.set('graphics', 'height', 568)
Config.set('graphics', 'resizable', 0)

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty,     ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint


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(BoxLayout):
    ball = ObjectProperty(None)

    def serve_ball(self):
        self.ball.center = self.center
        self.ball.velocity = Vector(4, 0).rotate(randint(0, 360))

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

        # bounce off top and bottom
        if (self.ball.y < self.screen.y) or (self.ball.top > self.screen.top):
            self.ball.velocity_y *= -1

        # bounce off left and right
        if (self.ball.x < 0) or (self.ball.right > self.width):
            self.ball.velocity_x *= -1

    # ボタンをクリック時(追加分)
    def on_down(self):
        self.ball.velocity_x *= 0.8
        self.ball.velocity_y *= 0.8

    def on_reverse(self):
        self.ball.velocity_x *= -1
        self.ball.velocity_y *= -1

    def on_up(self):
        self.ball.velocity_x *= 1.25
        self.ball.velocity_y *= 1.25

class PongApp(App):
    def build(self):
        self.title = 'テスト'
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game

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

kvファイル(pong.kv)

<PongGame>:
    screen: main_screen
    ball: pong_ball

    BoxLayout:
        orientation: "vertical"

        BoxLayout:
            Button:
                size_hint_x: 0.2
                text: "1"
            Label:
                size_hint_x: 0.6
                text: "today"
            Button:
                size_hint_x: 0.2
                text: "2"

        Widget:
            id: main_screen
            size_hint_y: 10
            canvas:
                Rectangle:
                    pos: self.center_x - 5, self.y
                    size: 10, self.height

            Label:
                font_size: 70  
                center_x: root.width / 4
                top: root.top - 50
                text: "0"

            Label:
                font_size: 70  
                center_x: root.width * 3 / 4
                top: root.top - 50
                text: "0"

            PongBall:
                id: pong_ball
                center: self.parent.center

        BoxLayout:
            Button:
                text: "DOWN"
                on_press: root.on_down()

            Button:
                text: "Reverse"
                on_press: root.on_reverse()
            Button:
                text: "UP"
                on_press: root.on_up()

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

 

解説

kvファイルのon_press:をそれぞれ「root.on_down()」「root.on_reverse()」「root.on_up()」と追加しました。それによりclass PongGame(BoxLayout):内の「def on_down(self):」「def on_reverse(self):」「def on_up(self):」が呼び出せるようになりました。そこでボールの速度であるball.velocityの値を「0.8倍」「-1倍」「1.25倍」にしています。

on_press

ボタンが押されたときにイベントが発生します。

on_release

ボタンが離されるとイベントが発生します。(おそらくスマホアプリなどでよく使われるのはこちらだと思います。例えば、押してしまっても別のところで離すと無効(キャンセル)に出来ます)

 

 

次回はスクロールの画面を作りたいと思います。

 

 

保存ファイル

lesson59.py

pong.kv

 

 

文責:Luke