Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame:未使用键盘输入更新位置_Python_Pygame - Fatal编程技术网

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

Python pygame:未使用键盘输入更新位置,python,pygame,Python,Pygame,我一直在用pygame做一个乒乓球游戏。我正试图通过按“w”键使挡板向上移动,但按下该键时,挡板的位置似乎没有更新。我怀疑leftball().pos是在\uuuu init\uuuuu(self)下定义的,每次使用leftball().blit(self.background)时都调用相同的值,因此位置没有更新?或者是否有其他逻辑问题导致位置无法更新?谢谢 import pygame class DisplayWindow(object): def __init__(self, wi

我一直在用pygame做一个乒乓球游戏。我正试图通过按“w”键使挡板向上移动,但按下该键时,挡板的位置似乎没有更新。我怀疑
leftball().pos
是在
\uuuu init\uuuuu(self)
下定义的,每次使用
leftball().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()

每次调用
leftpable()
,都会创建
leftpable
的新实例

您希望创建一个实例,然后对该实例调用方法,如:

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