user14959285
user14959285

Reputation:

Can't get 'function' to move Ship left and right in pygame

New to python and pygame. I was trying to make the ship move left or right continuously, which I did by creating a class "move_ship" and passing self.arguments as false. Then created a function inside which works when self.arguments are True and increase or decreases the x-position value of ship. Then called the class with variable "ship_moving". ...created a event which would set self.argument as true and at last called the function inside the class to move the ship:-

class move_ship():
   def __init__(self):
     self.moving_left=False
     self.moving_right=False
   def moving(self):
     if self.moving_left:
        ship_rect.centerx-=1
     if self.moving_right:
        ship_rect.centerx+=1

ship_moving=move_ship()

def run_game:
    pygame.init()
    while True:
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type ==pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    ship_moving.moving_right=True
                elif event.key == pygame.K_LEFT:
                    ship_moving.moving_left=True

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    ship_moving.moving_right=False
                elif event.key == pygame.K_LEFT:
                    ship_moving.moving_left=False
    
    ship_moving.moving()

which worked perfectly fine but the I wanted to know why it didn't work when I tried the same thing but with function only.

moving_right=False
moving_left= False
def move_ship():
  if moving_left:
     ship_rect.centerx-=1
  if moving_right:
     ship_rect.centerx+=1



def run_game:
    pygame.init()
    while True:
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type ==pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    moving_right=True
                elif event.key == pygame.K_LEFT:
                    moving_left=True

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    moving_right=False
                elif event.key == pygame.K_LEFT:
                    moving_left=False
    
    move_ship()

Upvotes: 1

Views: 54

Answers (1)

Rabbid76
Rabbid76

Reputation: 210998

You must use the global statement if you want to set variables in global namespace within a function:

def run_game():
    global moving_right, moving_left 

    pygame.init()
    while True:
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type ==pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    moving_right=True
                elif event.key == pygame.K_LEFT:
                    moving_left=True

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    moving_right=False
                elif event.key == pygame.K_LEFT:
                    moving_left=False

If you do not specify moveing_right, moveing_left to be interpreted as global, only local variables with the same name are set in the scope of the run_game function, but the global variables are never changed.

Upvotes: 1

Related Questions