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 9214713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:01:53+00:00 2026-06-18T02:01:53+00:00

I’m creating a small game using Python with the module Pygame. I tried implementing

  • 0

I’m creating a small game using Python with the module Pygame.

I tried implementing a pretty basic (and I guess a pretty naive) state manager.

However,
I currently have an issue :
once I switch the the “play” state, I can’t switch back to the “menu”.
Is this a flaw in how I designed my state manager?

I have two methods,

  1. The state_change method takes one argument, the desired state.
  2. The state_check method takes a state as the only argument and returns it as the current state.

Here are the two methods:

def state_check(self, state):
    self.current_state = state
    print self.current_state
    return self.current_state

def state_change(self, state):
    self.state = state

Before the main loop,
The first state is initialized to be “menu”.
Inside the main loop,
state_check is before the if-statements to indicate which state the start with.

def game_loop(self):                                                                       

    running = True
    self.test_car = car()
    self.state = "menu"
    while running:
        pygame.display.set_caption("Project G")
        self.state_check(self.state)
        if self.current_state ==  "menu":
            self.state_check(self.state)
            self.screen.blit(self.background, (0,0))
            #Blits the current state on the screen for testing purposes.
            self.screen.blit(self.menu_text, (700, 580))
            self.event_handler()
            pygame.display.flip()
        if self.current_state == "play":
            self.state_check(self.state)
            self.screen.blit(self.background, (0,0))
            #Blits the current state on the screen.
            self.screen.blit(self.play_text, (700, 580))
            self.test_car.event_handler()
            self.test_car.update(self.test_car.x_speed, self.test_car.y_speed)
            pygame.display.flip()

Within the if-statements,
Are the event handler methods, which I’ve currently made to take keyboard inputs to switch between states (By calling the state_change method) and to quit the program.

Using some print methods, I’ve ruled out the possibility of the keys not registering.

#Event handler for the menu

def event_handler(self):

    for event in pygame.event.get():
        if event.type == QUIT:
           pygame.quit()
           sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_p:
                self.state_change("play")
                print "Key Pressed: p"
            if event.key == K_q:
                print "Key Pressed: q"
                pygame.quit()
                sys.exit()
#Sample of the Event handler for "play"

def event_handler(self):
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_m:
                self.state_change("menu")
                print "Key Pressed: m"
            if event.key == K_q:
                print "Key Pressed: q"
                pygame.quit()
                sys.exit()
  • 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-18T02:01:54+00:00Added an answer on June 18, 2026 at 2:01 am

    I think you’re over-complicating things a little – do you really need check_state and set_state functions, and two variables?

    I would do something like this:

    class State(object):
        menu = "menu"
        play = "play"
    
    class Game(??):
    
        def event_handler(self):
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_m:
                        self.state = State.menu
                        print "Key Pressed: m"
                    elif event.key == K_p:
                        self.state = State.play
                        print "Key Pressed: p"
    
        def game_loop(self):
            self.state = State.menu
    
            while running:
                if self.state is State.menu:
                    # Do menu things
                elif self.state is State.play:
                    # Play game! 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.