Alrighty, so I tried applying they keyboard code I found. My two paddles look like this:
class PongPaddleA(Widget):
score = NumericProperty(0)
def __init__(self, **kwargs):
super(PongPaddleA, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
print 'My keyboard have been closed!'
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'q':
self.center_y += 14
elif keycode[1] == 'a':
self.center_y -= 14
return True
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)
print (bounced.x)
if bounced.x < 35 and bounced.x > -35:
vel = bounced * 1.1
else:
vel = bounced
ball.velocity = vel.x, vel.y + offset
#------------------------------------------------------------------------
class PongPaddleB(Widget):
score = NumericProperty(0)
def __init__(self, **kwargs):
super(PongPaddleB, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
print 'My keyboard have been closed!'
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'up':
self.center_y += 14
elif keycode[1] == 'down':
self.center_y -= 14
return True
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)
print (bounced.x)
if bounced.x < 35 and bounced.x > -35:
vel = bounced * 1.1
else:
vel = bounced
ball.velocity = vel.x, vel.y + offset
Only paddleB works...
Also, when I hold the button, there is a slight pause before the paddle continued to go up without pause. Is there a way to take out that pause?
Regardless, thank you.