0

Hi am new fairly new to programming so I tried to learn by making my hands dirty. I tried making a simple box game program all was working fine until I made my player Class which inherit from the base class of Box_User for some reasons my box(player class object) is no longer moving I tried to see what it prints and it seems like none of the keys work when I press them

Can anyone explain what happened?

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

2 Answers2

0

pygame.key.get_pressed() returns a list of booleans that represent the state of the keyboard when you call the function.

If you continually check that list, no keys will ever change state. It's just a list of Trues and Falses.

You need to reset the values of key_pressed in the loop, updating it with fresh values from pygame.key.get_pressed().

This should do the trick:

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

        if self.key_pressed[pygame.K_a]:
            self.pos_x -= 5
    ...
Starbuck5
  • 1,649
  • 2
  • 7
  • 13
0

The key_pressed class variables is only initialized once. It is never changed. Therefore the pressed keys are not detected.

pygame.key.get_pressed() returns a iterable with the current state of all keyboard buttons. You must get the states of the keys in every frame:

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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174