NaughtyWasabi
NaughtyWasabi

Reputation: 3

pygame: position not updated with keyboard input

I have been working on a Pong game using pygame. I am trying to get the paddle to move up by pressing 'w' key, but the position for the paddle LeftPaddle().pos seems to not be updated when pressing the key. I suspect LeftPaddle().pos being defined under __init__(self) make it call to the same value every time LeftPaddle().blit(self.background) is being used and therefore the position is not updated? Or is there some other logic problems causing the position to not be updated? Thanks.

import pygame

class DisplayWindow(object):
    def __init__(self, width=800, height=600,):
        """initialize pygame, window, background,
        setup boundaries
        """
        pygame.init()
        pygame.display.set_caption('Pong with Pyton')
        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.background = pygame.Surface(self.screen.get_size()).convert()

        self.bound_width = self.width*0.9
        self.bound_thic = self.height*0.01
        self.leftbound = self.width*0.05
        self.rightbound = self.leftbound + self.bound_width
        self.upperbound = self.height * 0.05
        self.lowerbound = self.height * 0.95
        self.bound_height = self.lowerbound - self.upperbound

    def boundaries(self):
        """paint boundaries and middle line
        """
        pygame.draw.rect(self.background, (255,255,255), (self.leftbound, self.upperbound - self.bound_thic, self.bound_width, self.bound_thic))
        pygame.draw.rect(self.background, (255,255,255), (self.leftbound, self.lowerbound, self.bound_width, self.bound_thic))

    def middleline(self):
        """paint middle line
        """
        mid_thic = self.width*0.01
        mid_height = (self.bound_height) / 81
        mid_left = (self.width - mid_thic) / 2
        mid_top = self.upperbound
        for i in range(0, 81):
        if i % 2 == 1:
            pygame.draw.rect(self.background, (255, 255, 255), (mid_left, mid_top + i * mid_height, mid_thic, mid_height))

    def run(self):
        """main loop
        """
        self.middleline()
        self.boundaries()
        LeftPaddle().blit(self.background)


        running = True
        while running:
            self.screen.blit(self.background, (0,0))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False
                    #keyboard control for moving paddles up and down
                    if event.key == pygame.K_w:
                        if LeftPaddle().pos == 0:
                            continue
                        else:
                            LeftPaddle().pos += 10
                            LeftPaddle().blit(self.background)

            pygame.display.update()




class LeftPaddle(object):

    def __init__(self):
        """initialize left paddle's thickness, height, surface to draw on, and center coordinates
        """
        self.thic = DisplayWindow().width * 0.01
        self.height = (DisplayWindow().bound_height) / 10

        #top position of paddle relative to the left paddle surface
        self.pos = (DisplayWindow().bound_height - self.height) / 2


    def blit(self, background):
        """blit left paddle to the background
        """
        self.surface = pygame.Surface((self.thic, DisplayWindow().bound_height))
        pygame.draw.rect(self.surface, (255,255,255), (0, self.pos, self.thic, self.height))
        background.blit(self.surface, (DisplayWindow().leftbound, DisplayWindow().upperbound))




DisplayWindow(800, 600).run()

Upvotes: 0

Views: 37

Answers (1)

sloth
sloth

Reputation: 101072

Everytime you call LeftPaddle(), you create a new instance of LeftPaddle.

You want to create an instance once and then call methods on that very instance, like:

...
    self.boundaries()
    leftpaddle = LeftPaddle()
    leftpaddle.blit(self.background)


    running = True
...
                if event.key == pygame.K_w:
                    if leftpaddle.pos == 0:
                        continue
                    else:
                        leftpaddle.pos += 10
                        leftpaddle.blit(self.background)
...

Upvotes: 1

Related Questions