user2387766
user2387766

Reputation:

How to limit the paddle movement?

I want to make it so that the paddle in this pong game won't move past the sides of the game window. (doesn't move too far right or left). I've tried writing a solution and the paddle is limited from moving completely off the screen, but about less than half of it is still able to move off screen.

How do I modify my code so that the paddle will not move off screen at all? The game window is set to be 600 by 600. And I'm using update_paddle to move the paddle.

Here's my paddle class/methods:

import pygame

class Paddle:
   def __init__(self, x, y, c, w, h):
       self.x = x
       self.y = y
       self.color = c
       self.width = w
       self.height = h

   def draw_paddle(self, screen):
      pygame.draw.rect(screen, self.color,
         pygame.Rect(self.x, self.y, self.width, self.height), 0)

   def update_paddle(self, dir, dx):
      if (dir == 'left') and (self.x >= 0):
         self.x = self.x - dx
      elif (dir == 'right') and (self.x + self.width <= 600):
         self.x += dx

   def get_left(self):
      if (self.x < 300):
         return self.x

   def get_right(self):
      if (self.x >= 300):
         return self.x

Upvotes: 1

Views: 1495

Answers (3)

Blckknght
Blckknght

Reputation: 104712

The issue is that you're testing to see if you're already off of the screen, not testing if you're going to be off the screen after your move. If self.x is 1 and you're moving left by 10 pixels, you're going to end up at -9 with the current code.

I suggest making the move unconditionally, then clamping your position to be on the screen.

def update_paddle(self, dir, dx):
   if (dir == 'left'): # always move by dx, even if that moves you off the screen
      self.x -= dx
   else: # (dir == 'right')
      self.x += dx

   self.x = min(max(self.x, 0), 600 - self.width) # clamp to screen afterwards

Unrelated to the screen boundary issue, I'd also suggest letting a negative dx indicate a leftward move, rather than using a dir argument. The first part of the function would just be self.x += dx for both directions.

Upvotes: 1

SingleNegationElimination
SingleNegationElimination

Reputation: 156158

what we want to say is that the paddle position, (its top, left corner, since should be positioned such that the paddle never leaves the screen, or more precisely:

  • the paddle's left side shall not move further left than the left edge of the screen, and
  • the paddle's right side shall not move further right than the right edge of the screen.

and so, lets look at the methods that adjust the paddles position left and right. That's only update_paddle(), and it has separate paths for movement left and right. Lets see what's wrong with yours, starting with the left:

if (dir == 'left') and (self.x >= 0):
    self.x = self.x - dx

When x is exactly zero, all tests pass, and so the paddle is allowed to move even further to the left, it may nudge off screen a bit; we don't want that, we want to keep it from going off screen at all. The better thing to do is to let it get all the way to the left edge, but then keep it on screen:

if dir == 'left':
    self.x = max(self.x - dx, 0)

The same problem happens for right movement:

else:  # LEM: dir == 'right':
   self.x = min(self.x + dx, 600 - self.width)

Upvotes: 2

sabbahillel
sabbahillel

Reputation: 4425

Your end position needs to test self.x+self.width/2 for the right side and self.x-self.width/2 for the left side and similarly for self.y and self.height/2 for the top and bottom. This is based on self.x, self.y being the center of the paddle. Since you prevent the center of the paddle from leaving the screen, you will get that it is at the edge, leaving the remainder of the paddle off the screen.

Upvotes: 1

Related Questions