简单的盒子游戏不动 python

Simple Box game not Moving python

大家好,我是编程新手,所以我尝试通过动手来学习。我尝试制作一个简单的盒子游戏程序,一切正常,直到我制作了我的播放器 Class,由于某些原因我的盒子(播放器 class 从 Box_User 的基础 class 继承object) 不再移动 我试图查看它打印的内容,当我按下 none 时,似乎 none 键起作用了

谁能解释一下发生了什么?

进口pygame pygame.init()

# Classes 
class Window():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.window_init() # this functions is to get the window up
        
        
    def window_init(self):
        self.window = pygame.display.set_mode((self.width, self.height)) # this is the main window
        self.background = pygame.Surface((self.window.get_size())) # this one is named background but should be use like the main window
        self.background.fill((255, 255, 255))

    @staticmethod

    def draw_objects_to_Screen():
        win.window.blit(win.background, (0,0))
        win.window.blit(box.box, (box.pos_x - scroll[0], box.pos_y + scroll[1])) # the scroll is used to make it look like its moving
        win.window.blit(box2.box, (box2.pos_x - scroll[0], box2.pos_y + scroll[1]))
        pygame.display.update()



class Box_User():
    jump = 10
    jump_status = False

    def __init__(self, x, y, height, width):
        self.pos_x = x
        self.pos_y = y
        self.box_height = height
        self.box_width = width
        self.box = pygame.Surface((self.box_height, self.box_width))
        self.color = (0, 20, 0)
        
        self.draw_box()

    def draw_box(self):
        pygame.draw.rect(self.box, self.color, pygame.Rect(self.pos_x, self.pos_y, self.box_height, self.box_width))

    @staticmethod
    def _jump():
        if Box_User.jump >= -10:
            box.pos_y -= (Box_User.jump * abs(Box_User.jump)) * 0.3
            scroll[1] += (Box_User.jump * abs(Box_User.jump)) * 0.3
            Box_User.jump -= 1
        else:
            Box_User.jump = 10
            Box_User.jump_status = False
        


class Player(Box_User):
    key_pressed = pygame.key.get_pressed()

    def __init__(self, x, y, height, width):
        super().__init__(x, y, height, width)
       # self.pos_x = x
        #self.pos_y = y

    def movements(self):
        if self.key_pressed[pygame.K_a]:
            self.pos_x -= 5
            
        if self.key_pressed[pygame.K_d]:
            self.pos_x += 5
        
        # place here things that you dont want to move while the box is jumping
        if not self.jump_status:
            if self.key_pressed[pygame.K_w]: 
                self.jump_status = True
        else:
            self._jump() # the box jumps here



class Auto_Box(Box_User):
    def __init__(self, x, y, height, width):
        super().__init__(x, y, height, width)
        pass

# Variables

# window
win = Window(700, 500)
clock = pygame.time.Clock()
FPS = 60

# boxes
box = Player(30, 200, 64, 64)
box2 = Box_User(300, 200, 100, 120)

# The Scroll which controls the things when the box is moving

scroll = [0, 0]

# Functions


# Main Loop
def main():
    run = True 

    while run:
        clock.tick(FPS)
        
        # value of the scroll is updated here
        scroll[0] += (box.pos_x - scroll[0]-250)
        #print("coordinate are")
        #print(scroll)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        box.movements()
        print(box.pos_x, box.pos_y)
        Window.draw_objects_to_Screen()
        


if __name__ == "__main__":
    main()

pygame.key.get_pressed() returns 表示键盘状态的布尔值列表 当您调用函数时

如果您不断检查该列表,则没有任何键会改变状态。这只是一个真假列表。

您需要在循环中重置 key_pressed 的值,用 pygame.key.get_pressed().

的新值更新它

这应该可以解决问题:

    def movements(self):
        self.key_pressed = pygame.key.get_pressed()

        if self.key_pressed[pygame.K_a]:
            self.pos_x -= 5
    ...

key_pressedclass variables只初始化一次。它永远不会改变。因此未检测到按下的键。

pygame.key.get_pressed() returns 具有所有键盘按钮当前状态的可迭代对象。必须获取每一帧按键的状态:

class Player(Box_User):

    def __init__(self, x, y, height, width):
        super().__init__(x, y, height, width)

    def movements(self):

        # get the current states of the keys
        self.key_pressed = pygame.key.get_pressed()

        if self.key_pressed[pygame.K_a]:
            self.pos_x -= 5
            
        if self.key_pressed[pygame.K_d]:
            self.pos_x += 5
        
        # place here things that you dont want to move while the box is jumping
        if not self.jump_status:
            if self.key_pressed[pygame.K_w]: 
                self.jump_status = True
        else:
            self._jump() # the box jumps here