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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:06:52+00:00 2026-06-13T13:06:52+00:00

So my game was working fine but the sprite would not handle repeated key

  • 0

So my game was working fine but the sprite would not handle repeated key presses. I thought i fixed it but now i can’t do anything once i get passed the start screen. The code where i think the problem is is under playgame(). If anyone knows that would be great!

import pygame 
from Classes import PlaneClass
pygame.init()
import sys,random,os
from pygame.locals import*
#import pdb

menuscreen=pygame.display.set_mode((640, 480))#FULLSCREEN
def highscores():
    WHITE=(255,255,255)
    global menuscreen
    highscoresFile= open('data/highscores.txt','r')
    filecontentstoread=''
    for line in highscoresFile:
        filecontentstoread+=str(line)+'\n'

    menuscreen.fill(WHITE)
    my_font = pygame.font.SysFont('Courier', 20)
    the_text = my_font.render(filecontentstoread, True, (0,0,0))
    menuscreen.blit(the_text, (20, 40))
    pygame.display.update()

def playgame():

    BLACK=(0,0,0)#Define the color black, later used as backgound
    plane_x=0  #Define the planes x and y coordinates
    plane_y=0


    gamescreen=pygame.display.set_mode((640, 480))#FULLSCREEN
    gamescreen.fill(BLACK) #Fill screen with black, as background
    plane_img=pygame.image.load('data/plane.png')
    plane=PlaneClass(plane_x,plane_y,plane_img)

    plane.move(plane_x,plane_y)


    gamerunning=True

    while gamerunning==True:
        #Move plane according to keyboard input
            keys=pygame.key.get_pressed()
            if keys[K_LEFT]:
                    plane_x -=1
                    plane.move(plane_x,plane_y)
            if keys[K_RIGHT]:
                    plane_x +=1
                    plane.move(plane_x,plane_y)
            if keys[K_UP]:
                    plane_y -=1
                    plane.move(plane_x,plane_y)
            if keys[K_DOWN]:
                    plane_y+=1
                    plane.move(plane_x,plane_y)



            #gamescreen.fill(BLACK)

clock=pygame.time.Clock()
clock.tick(30)
def menu_screen_options():
    menuvalue=main_menu()
    laserstartup=pygame.mixer.Sound('data/laserstartup.wav')
    laserstartup.play()
    if menuvalue==0:
        playgame()
    if menuvalue==1:
        highscores()
    if menuvalue==2:
        credits()
    if menuvalue==3:
        pygame.quit()
        sys.exit()


def main_menu():
    menuclock=pygame.time.Clock()
    clock.tick(30)


    pygame.display.set_caption('Dog Fight')
    #pygame.mouse.set_visible(False)

    WHITE=(255,255,255)
    GREEN=(0,255,0)
    BLUE=(0,0,255)
    background=pygame.image.load('data/background.png')
    lasersound=pygame.mixer.Sound('data/lasershot.wav')


    arrow=pygame.image.load('data/arrow.png')

    arrowpos = { 0 : (140,147) , 1:(140,210) , 2:(140,270) , 3 :(140,330) }



    menu=True
    counter = 0
    menuscreen.blit(arrow,arrowpos[counter])
    menuscreen.blit(background,(0,0))
    pygame.display.update()

    while menu == True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    menu = False






                if event.key == K_UP:
                    if counter > 0:
                        counter -= 1



                if event.key == K_DOWN:
                    if counter < 3:
                        counter += 1





                if event.key == K_RETURN:
                    return counter


            menuscreen.fill(WHITE)
            menuscreen.blit(background,(0,0))

            menuscreen.blit(arrow,arrowpos[counter])
        pygame.display.update()




menu_screen_options()

Also, i dont know if this will help or not but the code for the sprite class is.

import pygame


class PlaneClass(pygame.sprite.Sprite):




    # -- Methods
    # Constructor function
    def __init__(self,x,y,sprite_image):
        self.image=sprite_image

        self.x=x
        self.y=y
        pygame.sprite.Sprite.__init__(self)

    def move(self,new_x,new_y):
        screen=pygame.display.get_surface()
        self.new_x=new_x
        self.new_y=new_y
        screen.blit(self.image,(new_x,new_y))
        pygame.display.update()
  • 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-13T13:06:53+00:00Added an answer on June 13, 2026 at 1:06 pm

    You have that get_pressed call within a while True loop, so you never get back to the main event loop (the bit with for event in pygame.event.get():). This prevents your program from handling more events, which means that your program locks up.

    Instead, you need to wait for a key event in your main event loop and only call get_pressed when you know there’s a keypress to handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a memory matching game. Right now, the game is working fine.
I am working on a iPhone game. Everything is working fine, but what is
I have a game app which is mostly working fine. If a phone call
I am working on game application and i use AnimationDrawable for image change. but
I have this mysql query using PHP and first part is working fine, but
Can you explain me how thread is working. Actually it's working just fine right
I'm creating maps for a game I'm making, and it's working fine on <=Android
I've been trying to get this Sudoku game working, and I am still failing
Working on game where plates will be falling from top to bottom. Some plates
For a game I'm working on, I wanted to throw the audio on another

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.