Kivy 乒乓游戏教程 基于Minconda或Anconda 运行

文章目录

Kivy 乒乓游戏教程 基于Minconda或Anconda 运行

为什么写这篇笔记

Python部署Android软件apk或者aak早有耳闻,而Kivy就是其中最臭名昭著的(开玩笑),所以有了这篇用于展示 乒乓游戏教程 的文章

不过这篇文章 不会讲解具体的代码 逻辑以及意义,只是单纯的测试与演示 ,有望在其他文章中编写

扩展阅读

Kivy应用程序

笔者环境

  • 系统环境: Fedora Linux 42 (Workstation Edition)
  • Python环境: Python 3.13.X
  • Conda/Minconda环境:conda 25.7.0

安装Kivy

因为笔者的环境是 Linux上的Minconda所以使用如下命令

py 复制代码
conda install kivy -c conda-forge
参考

Anconda安装Kivy

其他各种平台安装

运行演示

乒乓球游戏教程

下面的俩个文件要在同一文件夹下

main.py
py 复制代码
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import (
    NumericProperty, ReferenceListProperty, ObjectProperty
)
from kivy.vector import Vector
from kivy.clock import Clock


class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.1
            ball.velocity = vel.x, vel.y + offset


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)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

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

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

        # bounce off paddles
        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)

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

        # went off to a side to score point?
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(4, 0))
        if self.ball.right > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-4, 0))

    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 PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game


if __name__ == '__main__':
    PongApp().run()
pong.kv
xml 复制代码
#:kivy 1.0.9

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

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

<PongGame>:
    ball: pong_ball
    player1: player_left
    player2: player_right
    
    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: str(root.player1.score)
        
    Label:
        font_size: 70  
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)
    
    PongBall:
        id: pong_ball
        center: self.parent.center
        
    PongPaddle:
        id: player_left
        x: root.x
        center_y: root.center_y
        
    PongPaddle:
        id: player_right
        x: root.width - self.width
        center_y: root.center_y
相关推荐
MediaTea1 天前
Python 第三方库:TensorFlow(深度学习框架)
开发语言·人工智能·python·深度学习·tensorflow
Joker-Tong1 天前
大模型数据洞察能力方法调研
人工智能·python·agent
B站计算机毕业设计之家1 天前
基于Python+Django+双协同过滤豆瓣电影推荐系统 协同过滤推荐算法 爬虫 大数据毕业设计(源码+文档)✅
大数据·爬虫·python·机器学习·数据分析·django·推荐算法
逻极1 天前
Webhook 全解析:事件驱动时代的实时集成核心技术
python·web
程序员三藏1 天前
一文了解UI自动化测试
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
极客代码1 天前
第七篇:深度学习SLAM——端到端的革命--从深度特征到神经辐射场的建图新范式
人工智能·python·深度学习·计算机视觉·slam·回环检测·地图构建
larance1 天前
python中的鸭子类型
开发语言·python
陈辛chenxin1 天前
【大数据技术04】数据可视化
大数据·python·信息可视化
大邳草民1 天前
深入理解 Python 的属性化方法
开发语言·笔记·python
洗紫1 天前
Python中的条件语句怎么使用?
python