pygame:位置未使用键盘输入更新

时间:2018-08-21 07:24:22

标签: python pygame

我一直在使用pygame开发Pong游戏。我试图通过按'w'键使拨片向上移动,但是按键时拨片LeftPaddle().pos的位置似乎没有更新。我怀疑LeftPaddle().pos下定义的__init__(self)会在每次使用LeftPaddle().blit(self.background)时调用相同的值,因此位置不会更新吗?还是存在其他一些逻辑问题导致职位无法更新?谢谢。

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()

1 个答案:

答案 0 :(得分:1)

每次调用LeftPaddle()时,都会创建一个LeftPaddle的新实例。

您要创建一个实例一次,然后在该实例上调用方法,例如:

...
    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)
...