本文主要是介绍使用kivy编写乒乓球小游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
有个师兄公众号有篇文章写了这个东西, 但由于他使用鼠标操作,个人觉得不是很合理,于是加了键盘监听。
说明
该项目由详细教程!教你用Python Kivy写一个乒乓球游戏
更改而来.
项目的代码解释见该公众号的文章
另外安利一下该公众号 有很多我喜欢的文章(不带恰饭成分
改变如下:
增加了键盘事件监听,参考了https://www.soinside.com/question/9KQGDD8VdmUNxY9hDr7Vzn
项目环境准备
拥有Python环境,下好python后默认带有pip, 然后
pip install kivy
运行main.py文件, 即可出现游戏框
python main.py
游戏操作
玩家一: w 球拍向上,s 球拍向下
玩家二: 箭头向上键球拍向上,箭头向下键球拍向下
代码
由两个文件构成。
pong.kv
#:kivy 1.0.9<PongBall>:size: 50, 50 canvas:Ellipse:pos: self.possize: self.size <PongPaddle>:size: 25, 200canvas:Rectangle:pos: self.possize: self.size<PongGame>:ball: pong_ballplayer1: player_leftplayer2: player_rightcanvas:Rectangle:pos: self.center_x - 5, 0size: 10, self.heightLabel:font_size: 70 center_x: root.width / 4top: root.top - 50text: str(root.player1.score)Label:font_size: 70 center_x: root.width * 3 / 4top: root.top - 50text: str(root.player2.score)PongBall:id: pong_ballcenter: self.parent.centerPongPaddle:id: player_leftx: root.xcenter_y: root.center_yPongPaddle:id: player_rightx: root.width - self.widthcenter_y: root.center_y
main.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
from kivy.core.window import Windowclass PongPaddle(Widget):score = NumericProperty(0)def bounce_ball(self, ball):if self.collide_widget(ball):vx, vy = ball.velocityoffset = (ball.center_y - self.center_y) / (self.height / 2)bounced = Vector(-1 * vx, vy)vel = bounced * 1.1ball.velocity = vel.x, vel.y + offsetclass 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.posclass PongGame(Widget):ball = ObjectProperty(None)player1 = ObjectProperty(None)player2 = ObjectProperty(None)def __init__(self, **kwargs):super().__init__(**kwargs)self.keyboard = Window.request_keyboard(None, self)self.keyboard.bind(on_key_down=self.on_keyboard_down)def serve_ball(self, vel=(4, 0)):self.ball.center = self.centerself.ball.velocity = veldef update(self, dt):self.ball.move()# bounce of paddlesself.player1.bounce_ball(self.ball)self.player2.bounce_ball(self.ball)# bounce ball off bottom or topif (self.ball.y < self.y) or (self.ball.top > self.top):self.ball.velocity_y *= -1# went of to a side to score point?if self.ball.x < self.x:self.player2.score += 1self.serve_ball(vel=(4, 0))if self.ball.x > self.width:self.player1.score += 1self.serve_ball(vel=(-4, 0))def on_touch_move(self, touch):if touch.x < self.width / 3:self.player1.center_y = touch.yif touch.x > self.width - self.width / 3:self.player2.center_y = touch.ydef on_keyboard_down(self, keyboard, keycode, text, modifiers):if keycode[0] == 119:'w键' self.player1.center_y += 15elif keycode[0] == 115:'s键' self.player1.center_y -= 15if keycode[0] == 273:'箭头向上键'self.player2.center_y += 15elif keycode[0] == 274:'箭头向下键'self.player2.center_y -= 15# print(keycode[0])class PongApp(App):def build(self):game = PongGame()game.serve_ball()Clock.schedule_interval(game.update, 1.0 / 60.0)return gameif __name__ == '__main__':PongApp().run()
这篇关于使用kivy编写乒乓球小游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!