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

  • Home
  • SEARCH
  • 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 7699377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:27:48+00:00 2026-05-31T22:27:48+00:00

So with pygame you have a while loop that loops continuously, then your event

  • 0

So with pygame you have a while loop that loops continuously, then your event handlers in a for loop, then the code you want to execute continuously.

I need to handle an event after i execute a line of code inside the while loop though beacuse they impact each other, but I also need to handle a different event before the line.

How, inside my main while loop, handle a set of events, execute some code, and then handle another set of events?

  • 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-05-31T22:27:49+00:00Added an answer on May 31, 2026 at 10:27 pm

    Solved.

    I set a variable equal to 1 in the user event handler, and then down where i needed to execute the code, i check if the variable was equal to 1, and if it was, i executed my code and set the variable back to 0.

    Good stuff.

    Here is my code if anyone wants to help me find a better solution 😉 (kinda long):

    uif = "images/userGray.png"
    bif = "images/bg.jpg"
    cif = "images/chair3.png"
    i = 0
    playerX = 1
    playerY = 1
    pX = 1
    pY = 1
    bX = 0
    bY = 0
    moveX = 0
    moveY = 0
    windowX = 640
    windowY = 480
    lowerY = 1024
    lowerX = 1024
    bullets = []
    x = 0
    y = 0
    rotate = False
    
    objects = []
    
    objects.append([256,260,410,511])
    
    import pygame, sys
    from pygame.locals import *
    
    pygame.init()
    
    screen = pygame.display.set_mode((640,480),0,32)
    
    background = pygame.image.load(bif).convert()
    user = pygame.image.load(uif).convert_alpha()
    chair = pygame.image.load(cif).convert_alpha()
    chair1 = pygame.image.load(cif).convert_alpha()
    
    pygame.time.set_timer(USEREVENT + 1, 100)
    
    def shoot(inLoc, clLoc, weapon):
        bulletId = len(bullets)
        bullets[bulletId] = [inLoc, clLoc, 200, 3]
    
    moveSpeed = .1
    
    while True:
    
    
    
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == USEREVENT + 1:
                rotate = True;
            if event.type == KEYDOWN:
                if event.key == K_LEFT or event.key == K_a:
                    moveX = -1*moveSpeed
                elif event.key == K_RIGHT or event.key == K_d:
                    moveX = moveSpeed
                if event.key == K_DOWN or event.key == K_s:
                    moveY = moveSpeed
                elif event.key == K_UP or event.key == K_w:
                    moveY = -1*moveSpeed
            if event.type == KEYUP:
                if event.key == K_LEFT or event.key == K_a or event.key == K_RIGHT or event.key == K_d:
                    moveX = 0
                if event.key == K_DOWN or event.key == K_s or event.key == K_UP or event.key == K_w:
                    moveY = 0
    
        dontMoveX = 0
        dontMoveY = 0
    
        for obj in objects:
    
            if playerX + moveX > obj[0]-user.get_width() and playerX + moveX < obj[1] and playerY + moveY > obj[2]-user.get_height() and playerY + moveY < obj[3]:
                if playerY + moveY == obj[2]-user.get_height()-1 or playerY + moveY == obj[3]+1:
                    dontMoveX = 0
                else:
                    dontMoveX = 1
    
            if playerX + moveX > obj[0]-user.get_width() and playerX + moveX < obj[1] and playerY + moveY > obj[2]-user.get_height() and playerY + moveY < obj[3]:
                if playerX + moveX == obj[0]-user.get_width()-1 or playerX + moveX == obj[1]+1:
                    dontMoveY = 0
                else:
                    dontMoveY = 1
    
        if dontMoveX == 0:
            playerX += moveX
    
            if (playerX >= 0 and playerX <= windowX/2) or (playerX >= lowerX-(windowX/2) and playerX <= lowerX-user.get_width()):
                pX+=moveX
            if playerX > windowX/2 and playerX < lowerX-(windowX/2):
                bX+=-1*moveX
    
        if dontMoveY == 0:
            playerY += moveY
    
            if (playerY >= 0 and playerY <= windowY/2) or (playerY >= lowerY-(windowY/2) and playerY <= lowerY-user.get_width()):
                pY+=moveY
            if playerY > windowY/2 and playerY < lowerY-(windowY/2):
                bY+=-1*moveY
    
        screen.blit(background,(bX,bY))
    
    
    
        screen.blit(user,(pX,pY))
    
    
        pygame.mouse.set_visible(False);
    
        if rotate == True:    
    
            if i < 360:
                i = i + 18
            else:
                i = 0
    
            orig_chair_rect = chair.get_rect()
            chair1 = pygame.transform.rotate(chair, i);
            rot_chair_rect = orig_chair_rect.copy()
            rot_chair_rect.center = chair1.get_rect().center
            chair1 = chair1.subsurface(rot_chair_rect).copy()
    
            rotate = False
    
        x,y = pygame.mouse.get_pos()
        x -= chair.get_width()/2
        y -= chair.get_height()/2
    
        screen.blit(chair1,(x,y))
    
        pygame.display.update()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pygame window that I want to know when a file has
I have used PyGame for a while now, and quite enjoyed doing so, but
I have a customized version of SDL 1.2 and pygame 1.9.1 that implement SDL2's
I have the following Python (3.2) code: from pygame import * class Application: def
I have 2 pygame surfaces (technically subsurfaces) and I need to test for their
I'm just beginning a very simple pygame code that draws a green line on
This is my code that I have looked through for a couple of hours.
I have a medium sized array of tiles represented as numbers that pygame blits
I'm working on a pygame project and have the main engine layed out. The
I'm trying to learn pygame, And I found the best way to have the

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.