xxxecute
xxxecute

Reputation: 190

How can i add new position to my ball - Kivy

As it can be understood from the code, I want to assign a new position to the ball on the screen when a certain pixel range is clicked, that is, I want to change its current position. I tried this with self.ids.widget.pos and things like that but didn't get any results.

There is also one more thing I want to ask regardless of the subject. While I define this ball on the screen, the ball does not appear in the center of the screen, I adjusted it myself. Normally I use a 300 width and 500 height screen for the application, but I was able to achieve this by giving 175 width and 275 height values to get this ball right in the middle. How can I better adjust this?

I have removed the parts I don't need to shorten the code, but I can add them later if you want.

from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivymd.app import MDApp
from time import sleep, time
from random import randint
from kivy.properties import NumericProperty
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.graphics import Color


Window.size = (300, 500)

helper = """
ScreenManager:

    MenuScreen:

    ReflexScreen:
    
    MainScreen:
    
    AimScreen:
    
<Ball>:
    canvas:
        Ellipse:
            pos: self.pos
            size: 50,50
            
<AimWidget>
    ball: pong_ball
    Ball:
        id: pong_ball
        center_x: 175
        center_y: 275
<AimScreen>
    id: aim
    name: 'aim'
    AimWidget:
        id: widget
       
           
"""
class Ball(Widget):
    pos_x = NumericProperty()
    pos_y = NumericProperty()


class AimWidget(Widget):

    def update(self, dt):
        pass

class AimScreen(Screen):
    aim = AimWidget()
    Clock.schedule_interval(aim.update, 1.0/60.0)
    def on_pre_enter(self, *args):
        pass
    def on_touch_down(self, touch):


        if (touch.x >= 125 and touch.x <= 175) and (touch.y >= 225 and touch.y <= 275):
            pass

Upvotes: 0

Views: 78

Answers (1)

John Anderson
John Anderson

Reputation: 38962

Here is a modified version of your code:

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen
from kivy.uix.widget import Widget
from kivy.clock import Clock

Window.size = (300, 500)

helper = """
ScreenManager:

    # MenuScreen:

    # ReflexScreen:

    # MainScreen:

    AimScreen:

<Ball>:
    size_hint: None, None
    size: 50, 50
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size

<AimWidget>
    ball: pong_ball
    Ball:
        id: pong_ball
        center: root.center
<AimScreen>
    id: aim
    name: 'aim'
    AimWidget:
        id: widget


"""


class Ball(Widget):
    pass


class AimWidget(Widget):

    def update(self, dt):
        pass


class AimScreen(Screen):
    aim = AimWidget()
    Clock.schedule_interval(aim.update, 1.0 / 60.0)

    def on_pre_enter(self, *args):
        pass

    def on_touch_down(self, touch):
        if (touch.x >= 125 and touch.x <= 175) and (touch.y >= 225 and touch.y <= 275):
            ball = self.ids.widget.ball
            ball.center = touch.pos
  • The Ball widget size was the default (100, 100), so the canvas instructions were drawing the circle in the lower left corner of the Ball widget. Added size to make it (50,50), the desired size of the circle.
  • The canvas instructions for the Ball, now just reference the size and pos of the Ball widget.
  • The Ball in the AimWidget is initially positioned using center. This works because the Ball is now the same size as the Ellipse.
  • The on_touch_down() method can now just set the center of the Ball to the touch.pos.

Upvotes: 1

Related Questions