1

I am trying to create the game, Pong. I have 2 paddles, a left & a right, that should be positioned in the middle of the left and right sides of the window. When I run the game the right paddle does not display the same as the left paddle. I am unsure as to why that is. This is what it looks like currently:

pong screenshot

The right paddle is pushed off screen, while the left paddle seems to be normal. Except that it is pushed off the edge to the right by a few.

Paddle Code:

import pygame


class Paddle:

    def __init__(self, screen, h, w, left):
        self.screen = screen
        self.width = w
        self.y = h / 2
        self.pad_width = 10
        self.pad_height = 100

        if left:
            self.x = self.pad_width / 2
        else:
            self.x = self.width - self.pad_width / 2

    def show(self):
        WHITE = (255, 255, 255)
        rect = pygame.Rect(self.x, self.y, self.pad_width, self.pad_height)
        pygame.draw.rect(self.screen, WHITE, rect)

I create 2 instances of the paddle like this:

# create 2 paddles
left = Paddle(screen, HEIGHT, WIDTH, True)
right = Paddle(screen, HEIGHT, WIDTH, False)

# show the paddles
left.show()
right.show()

Where screen is screen = pygame.display.set_mode((WIDTH, HEIGHT)).

HEIGHT is 400 and WIDTH is 600.

EDIT:

With self.x = self.width - self.pad_width * 2 it shows:

edit

kstullich
  • 601
  • 1
  • 10
  • 27
  • this is about the x coordinate used, try with `self.width - self.pad_width * 2` – PRMoureu Sep 29 '17 at 18:18
  • @PRMoureu that seems to sort of fix the issue. But now the right paddle is pushed off the screen. Even more than the right paddle. I just want them to be flush with the left & right edges of the window. I edited the post showing the changes. – kstullich Sep 29 '17 at 18:29
  • 2
    `self.width - self.pad_width * 1.5` should do it (like it was `self.width - (self.pad_width + self.pad_width/2)` – PRMoureu Sep 29 '17 at 18:33
  • @PRMoureu I should have just tried that out myself. But thanks that did it! – kstullich Sep 29 '17 at 18:34

0 Answers0