Kivy Tutorial #7 - Ball Bouncing and Boundaries | Pong Game

preview_player
Показать описание
In this video, we will start creating our Kivy game ( Ping Pong Python). We will start by adding a rectangle and some text to our window.

Next video - Adding the Paddles

#python #kivy #pythongui
Рекомендации по теме
Комментарии
Автор

Just to be clear in case people are confused - The information in this vid about the coordinates system for kivy is incorrect. The x- and y- coordinates both start at 0 in the BOTTOM left corner, NOT the top. The bottom left corner is (0, 0). There could be some confusion because with other GUI platforms such as PyQt5, the coordinates start in the top left.

code-e
Автор

Thanks for the tutorials! I only would like to draw your attention, that Kivy's coordinate system start at BOTTOM LEFT corner, so the (0, 0) is not at the top. To prove it, try to change coordinates of the rectangle to 0, 0 and its dimensions to e.g 10, 10 ;)

djdan
Автор

If you don't like half the ball leaving the side of the window when hitting the top or right-hand side of the window, just add +50 to your if-statements:

if (self.ball.y + 50 >= self.height) or (self.ball.y <= 0):
# same as saying: self.ball.velocity_y = -self.ball.velocity_y
self.ball.velocity_y *= -1

# #bounce ball off left & right side of screen
if (self.ball.x + 50 >= self.width) or (self.ball.x <= 0):
self.ball.velocity_x *= -1

Stevesteacher
Автор

Wonderful use of velocity for bounding the ball.

sachinwalunjakar
Автор

A little tip for anyone interested in making the ball actually bounce off of the top and right boundaries instead of partially disappearing before changing direction as in the video. You need to take into account the height and width of the ball itself. Change the code to the following:
# Bounce off top and bottom






code-e
Автор

im looking all your tutorial about Kivy. Really pretty beautiful. Thanks

ErdosainNueve
Автор

i'm trying to understand what the dt do exactly. is it a built-in argument from the clock module that specifically takes the value passed from Clock.schedule_interval ?

Dashingo
Автор

Thank you once again. You are a great teacher. ;)

mxlinuxpl
Автор

Why dont work in
def update(self, dt):
self.ball.move()

if (self.ball.y < 0) or (self.ball.y > self.height -50):
self.ball.velocity_y *= -1

if (self.ball.x < 0) or (self.ball.x > self.width -50):
self.ball.velocity_x *= -1

ismatovismatillo
Автор

y increases from bottom to the top not from top to the bottom thanks !

yacinehefied
Автор

Sir big fan,
After watching your django tutorial I have decided to work on a project.
I have some kinda doubts in it, I want your help to solve it please tell me a way to contact you...

dhruvlohar
Автор

hey, i am getting an error - File "kivy\weakproxy.pyx", line 62, in
TypeError: '<' not supported between instances of 'PongBall' and 'int'
thanks, love the vids btw

danbrown
Автор

Pretty sure 0, 0 would be the bottom left corner

latenmuniz
Автор

Please put the next videos of the series

aryanbhatia
Автор

sir the ball is going out side the window this is my code :

from kivy.app import App
from kivy.uix.widget import Widget
# NumericProperty tells the interpreter that the variable is a int type
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
# Vector is used to get the direction of the ball
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint


# Latest position = current position + current velocity
class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_y, velocity_x)

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


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

# update function will update the position of the ball every time
def serve_ball(self):
self.ball.velocity = Vector(4, 0).rotate(randint(0, 360))

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

if (self.ball.y < 0) or (self.ball.y > self.height ):
self.ball.velocity_y = self.ball.velocity_y*-1

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


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


app = PongApp()
app.run()

mcodes
Автор

i just added the following code instead of that.

out_of_bounds_x = self.ball.x < self.x or self.ball.right > self.right
out_of_bounds_y = self.ball.y < self.y or self.ball.top > self.top
if out_of_bounds_x:
self.ball.velocity[0] *= -1
if out_of_bounds_y:
self.ball.velocity[1] *= -1

Hope it helps.

justmyfavs