user1588560
user1588560

Reputation: 13

My pygame sprite can me moved left and right, but I can't get it to stop at the end of the screen

Main program:

player = Player("player.png",[10,650])
players = pygame.sprite.Group()
players.add(player)

while True:
    for event in pygame.event.get():
      if event.type == KEYDOWN:
        if event.key == K_RIGHT:
          player.goright()
        if event.key == K_LEFT:
          player.goleft()           
      if event.type == KEYUP:
        if event.key == K_RIGHT:
          player.cangoright = False
        if event.key == K_LEFT:
          player.cangoleft = False

players.update()
players.draw(SCREEN)
pygame.display.update()
clock.tick(FPS)

The relevant sprite's functions:

def update(self):
  if self.cangoright:
    self.rect.left += self.speed
  if self.cangoleft:
    self.rect.left -= self.speed

def goright(self):               
  if self.rect.right <= 1024:
    self.cangoright = True
  else:
    self.cangoright = False

def goleft(self):             
  if self.rect.left >= 0:
    self.cangoleft = True
  else:
    self.cangoleft = False

The problem is that the "cangoright" and "cangoleft" flags don't seem to be working properly. When the sprite has surpassed the edges of the screen (0 on the left and 1024 on the right), the flags should be set to false, and thus the ifs in the update function should return false, but this doesn't happen.

Upvotes: 1

Views: 657

Answers (3)

Noctis Skytower
Noctis Skytower

Reputation: 22041

This answer has some sample move methods. Maybe looking at how they were written would help?

Paddle.move()

def move(self, *, up=False, down=False):
    if up or (not down and self.keys.up and
              self.position.y - self.size.y > 0):
        self.position -= Point(0, self.move_by)
    if down or (not up and self.keys.down and
                self.position.y + self.size.y < self.height):
        self.position += Point(0, self.move_by)

Ball.move()

def move(self):
    self.position += self.velocity
    self.bounce()

Upvotes: 2

Ben Burns
Ben Burns

Reputation: 15226

I believe part of the problem is that you're only updating left in self.rect. Try updating both left and right instead, and see if that changes things.

More importantly, this code is a bit hard to read, and too complex what you're trying to do. You might be better served by checking the bounds of the screen in your update function instead of in your key event handler. In reality this is part of the physics of your game, so it should probably be hung off of there.

In other words, use the event loop to set flags which express the intention of the player to the update function. Use the update function to apply the rules of the game (physics, etc) to the game state based on the flags set by the event loop.

Maybe something like this?

while True:
  ...
    for event in pygame.event.get():
      if event.type == KEYDOWN:
        if event.key == K_RIGHT:
          player.goright = True
        if event.key == K_LEFT:
          player.goleft = True
      if event.type == KEYUP:
        if event.key == K_RIGHT:
          player.goright = False
        if event.key == K_LEFT:
          player.goleft = False

  players.update()
  players.draw(SCREEN)
  pygame.display.update()
  clock.tick(FPS)

def update(self):
  if self.goright and self.speed < SPEED_MAX:
    self.speed += ACCEL_INCREMENT
  if self.goright and self.speed > -SPEED_MAX:
    self.speed -= ACCEL_INCREMENT

  new_left_pos = self.rect.left + self.speed
  new_right_pos = self.rect.right + self.speed

  if new_left_pos > 0 and new_right_pos < 1024:
        self.rect.left += self.speed
        self.rect.right += self.speed

Upvotes: 0

Brandon
Brandon

Reputation: 191

The if statments are slightly off.

if self.rect.right <= 1024:  #Shouldn't this be < 1024 and not <=
    self.cangoright = True 

Same for:

if self.rect.left >= 0:  #Same thing here
    self.cangoleft = True 

Looks like your going one to far, thus placing your sprite just off the edge of the screen.

Hope this helps.

Upvotes: 0

Related Questions