Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:54:17+00:00 2026-06-09T12:54:17+00:00

Main program: player = Player(player.png,[10,650]) players = pygame.sprite.Group() players.add(player) while True: for event in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:54:19+00:00Added an answer on June 9, 2026 at 12:54 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I get the main program to recognize that I have an outside
How does a thread knows when to exit? SITUATION: -while the main program must
When I try to compile my program, I get the following error: main.cpp: In
My main program running 8 tasks using Task.Factory.StartNew Each task will request XML format
Our main program is in java but the code that extracts our data from
So the main program is in C#. Inserting new records into a VFP database
when calling PQconnectdb in the main program all run very well, but if i
I am working on a class for an existing main program, and it is
i have console application and web application. i am calling the main program of
I've recently ported my Qt project from qmake to CMake . My main program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.