Kivy Tutorial #9 - Adding Touch to App | Pong Game

preview_player
Показать описание
Alright guys, welcome back. We are going to be covering touch events and how to move our paddles by using our mouse as well as touch screen of our mobile phones.

# on_touch_down() - When our fingers/mouse touches the screen
# on touch_up() - When we lift our finger off the screen after touching it
# on_touch_move() - When we drag our finger on the screen

So There are 3 ways to detect and react to touch on Kivy.
First is the on_touch_down function - Which is automatically called when we touch the screen with our fingers or with our mouse. And when we left our finger off the screen then the on_touch_up function is called.

And third is the on_touch_move function which is called when we keep touching the screen and drag our finger across the screen without lifting it. A great example of this function is swiping right on tinder. You drag your finger across the screen in one clean motion.

To move our paddles up and down we will be using the on_touch move function()

So we will go inside our PongGame class. Create a new method inside the update function because we need to check continuously whether the screen is being touched or not, and we are going to call it on_touch_move function. It will take two arguments, first is itself like every other method inside a class. and second is the touch. This touch argument will contain all kinds of information. For example the x and y coordinate of where we touched the screen.

Now we will need to tell our code two things, firstly what to do if the screen is touched and secondly where is the touching allowed. So in our game we will only be allowing the paddles to be moved if the touch happens before the score text for our player left, which is approximately 1/4th of our game window.

And similarity for the player right, we will only allow touch after the score text. Whose x coordinate will be almost equal to 3/4th of the screen.

So inside our on_touch_move function we create an if condition for our left player, which checks whether the x coordinate of where we touched the screen is less than 1/4th the screen or not. We are basically comparing x co-ordinates.

And if this true then we will move our paddle to the y coordinate of where we touch our screen. We do this by making the center of our paddle, which can be referenced by center_y equal to the y co-ordinate of where we touched the screen

And similarly we for right player we check whether the x coordinate of where we touched the screen is greater than 3/4th the screen or not. We do this by multiplying the width our screen by 3/4. You have to remember that this actually a x-coordinate. I am just called it 3/4th of the screen to make it more understandable. And then if this is true we will again move our paddle to where the screen was touched.

And then just like we did with our ball we are going to hook up our code with our kv file otherwise the paddle is not going to move. Because at the end of the day. kv file is responsible for redrawing the paddle in a different position.

So we will will create two object-properties called player1 and player2 with their default values as None.

And then inside our kv file we will add them inside our PongGame. So player1 will have an id of player_left and player2 will reference the id of player_right. We have already created these id's down below

Next video - Collision Detection

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

You are doing a great job, brother. You are helping a lot of curious minds. Thanks a lot

crazermajor
Автор

Hi, nice videos!!! you explain a lot of things really easy
do you know which function could help me to move the paddle with my keyboard?

sergioa.sosabautista
Автор

Sir thank u very much after watching first I can't understand much but going through the series I am getting better idea hower this function are working how should import these functions and all that I truly appreciate the work and helping the students like me to understand kivy looking forward to your channel

fetkamausam
Автор

Don't why but my left padle is not working on touch, is there any solution on this?

yashdixit
Автор

Thank you for all the tutorials, they really help me learn kivy :)

Stevesteacher
Автор

Awesome videos man!!
At 01:46, I had to do self.width * (1/4) otherwise, my right paddle was moving the left one too 😂😂

TheBhartiyaTrainee
Автор

Thnks for the video. But writing that line (if touch.x<self.width/1/4) is confusing. it should be (if touch.x<self.width/4) .

mariwanj
Автор

Help! my paddles are not moving.I haven't done anything

rmeena
Автор

sir i when i run this file the left portion of screen on touch is moving right paddle and right screeen part is moving left paddle plz help

aadityapant
Автор

'NoneType' object has no attribute 'center_y' is showing

DhrubojyotiBasu
Автор

Error is coming:

PongPaddle:
40: id : player_left
41: x = root.x
42: center_y : root.center
43:

Invalid property name

mriduljindal
Автор

dude what the hell, slow down a bit please :D

cobanumut