Kivy 是用 Python 和 Cython 编写的，基于 OpenGL ES 2，支持各种输入设备并拥有丰富的部件库。使用相同的代码，你可直接实现多平台应用，包括 Windows、macOS、Linux、Android 和 iOS。所有 Kivy 部件都支持多点触控。

## 1.准备

(可选1) 如果你用Python的目的是数据分析，可以直接安装Anaconda：Python数据分析与挖掘好帮手—Anaconda，它内置了Python和pip.

(可选2) 此外，推荐大家用VSCode编辑器来编写小型Python项目：Python 编程的最好搭档—VSCode 详细指南

Windows环境下打开Cmd(开始—运行—CMD)，苹果系统环境下请打开Terminal(command+空格输入Terminal)，输入命令安装依赖：

`pip install kivy[base] kivy_examples`

## 2.简单使用 Kivy

```# main.py
from kivy.app import App
from kivy.uix.widget import Widget

class PongGame(Widget):
pass

class PongApp(App):
def build(self):
return PongGame()

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

## 3.Kivy – 添加简单图形

```#:kivy 1.0.9

<PongGame>:
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: "0"

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

`#:kivy 1.0.9`

## 4. Kivy – 增加乒乓球球体

PongBall 类：

```class PongBall(Widget):

# velocity of the ball on x and y axis
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)

# referencelist property so we can use ball.velocity as
# a shorthand, just like e.g. w.pos for w.x and w.y
velocity = ReferenceListProperty(velocity_x, velocity_y)

# ``move`` function will move the ball one step. This
#  will be called in equal intervals to animate the ball
def move(self):
self.pos = Vector(*self.velocity) + self.pos```

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

```from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty
from kivy.vector import Vector

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):
pass

class PongApp(App):
def build(self):
return PongGame()

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

kv文件如下：

```#:kivy 1.0.9

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

<PongGame>:
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: "0"

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

PongBall:
center: self.parent.center```

## 5. kivy – 增加乒乓球体运动

```Clock.schedule_interval(game.update, 1.0/60.0)
```

```class PongGame(Widget):

def update(self, dt):
# call ball.move and other stuff
pass

class PongApp(App):

def build(self):
game = PongGame()
Clock.schedule_interval(game.update, 1.0/60.0)
return game```

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

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

# bounce off top and bottom
if (self.ball.y < 0) or (self.ball.top > self.height):
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```

```<PongGame>:
ball: pong_ball

# ... (canvas and Labels)

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

## 6. Kivy – 球拍移动事件

```def on_touch_move(self, touch):
if touch.x < self.width/3:
self.player1.center_y = touch.y
if touch.x > self.width - self.width/3:
self.player2.center_y = touch.y```

```class PongPaddle(Widget):

score = NumericProperty(0)

def bounce_ball(self, ball):
if self.collide_widget(ball):
speedup  = 1.1
offset = 0.02 * Vector(0, ball.center_y-self.center_y)
ball.velocity =  speedup * (offset - ball.velocity)```

​Python实用宝典 ( pythondict.com )