This question already has answers here:
Pygame - Mouse clicks not getting detected
(2 answers)
Closed 2 years ago.
While I was learning how to move images in pygame, when I held down a key(any one: up, down, left, right) to move my image(a racecar), the image only moved once, even though holding down the key should've made the image move move in that particular directions continuously, but it only moved one unit. The fix that I found was that the declaration of the variables used to change the original position (x_vary and y_vary, original position: x and y) were not be added in the main while loop of my game.
My question is why? Why does it make a difference where I declare x_vary and y_vary
Here is the unfixed code:
import pygame
pygame.init()
clock = pygame.time.Clock()
game_window = pygame.display.set_mode((800, 600))
game_icon = pygame.image.load('icon2.jpg')
game_caption = pygame.display.set_caption('Racecar Game')
x = 800 * 0.45
y = 600 * 0.08
racecar = pygame.image.load('racecar.png')
def car(x, y):
game_window.blit(racecar,(x,y))
game_run = False
while game_run == False:
x_vary = 0
y_vary = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_run = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_vary = -10
elif event.key == pygame.K_RIGHT:
x_vary = +10
elif event.key == pygame.K_UP:
y_vary = -10
elif event.key == pygame.K_DOWN:
y_vary = +10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
x_vary = 0
y_vary = 0
x += x_vary
y += y_vary
game_window.fill((128, 0, 0))
car(x, y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
The keyboard events (pygame.KEYDOWN, pygame.KEYUP) occur only one time when a key is pressed, respectively released.
If x_vary = 0 and y_vary = 0 are declared in the main application loop, then they are set when a button is pressed, but they are reinitialized (0) immediately in the next frame. You've to presse a button again to set them.
If x_vary = 0 and y_vary = 0 are declared before the main application loop, then they keep the value, which is set when a button is pressed. If a button is released, then both are set 0.
Use pygame.key.get_pressed(), to evaluate if the state of a key is pressed. pygame.key.get_pressed() get the state of all keyboard buttons and a the state of a key is True as long the keyboard is pressed down. e.g.:
game_run = False
while game_run == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_run = True
keys = pygame.key.get_pressed()
x_vary, y_vary = 0, 0
if keys[pygame.K_LEFT]:
x_vary = -10
if keys[pygame.K_RIGHT]:
x_vary = +10
if keys[pygame.K_UP]:
y_vary = -10
if keys[pygame.K_DOWN]:
y_vary = +10
x += x_vary
y += y_vary
game_window.fill((128, 0, 0))
car(x, y)
pygame.display.update()
clock.tick(60)
Related
This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
Closed 1 year ago.
right now it just moves in the direction i tell it to put it just stops. How do i go about making it KEEP going in the direction I inputted by? And make it constantly go in the direction, but be able to control how fast it moves, right now i want it to move every 4 minutes
import pygame
from pygame.locals import *
pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
surface.blit(block, (block_x, block_y))
def draw():
surface.blit(background, (0, 0))
surface.blit(block, (block_x, block_y))
pygame.display.flip()
direction = ''
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_UP:
block_y -= 10
draw()
if event.key == K_DOWN:
block_y += 10
draw()
if event.key == K_LEFT:
block_x -= 10
draw()
if event.key == K_RIGHT:
block_x += 10
draw()
elif event.type == QUIT:
running = False
```
Here's an option with minimal changes to your program. I changed the event handlers to update speed variables, which update the position in an added update() function. The speeds are zeroed when the key is released. I've also removed the duplicated draw() functions. Note this code is untested.
import pygame
from pygame.locals import *
pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
speed_x = 0
speed_y = 0
surface.blit(block, (block_x, block_y))
def draw():
surface.blit(background, (0, 0))
surface.blit(block, (block_x, block_y))
pygame.display.flip()
def update():
"""Update game state"""
block_x += speed_x
block_y += speed_y
direction = ''
pygame.display.flip()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.key == K_UP:
speed_y -= 10
elif event.key == K_DOWN:
speed_y += 10
elif event.key == K_LEFT:
speed_x -= 10
elif event.key == K_RIGHT:
speed_x += 10
elif event.type == KEYUP:
if event.key in (K_DOWN, K_UP):
speed_y = 0
elif event.key in (K_LEFT, K_RIGHT):
speed_x = 0
elif event.type == QUIT:
running = False
update()
draw()
clock.tick(60) # limit frame rate to 60 FPS
EDIT: Added frame rate limiting
Use pygame.key.get_pressed() instead of the KEYDOWN event.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
block_y -= 10
if keys[pygame.K_DOWN]:
block_y += 10
if keys[pygame.K_LEFT]:
block_x -= 10
if keys[pygame.K_RIGHT]:
block_x += 10
draw()
The code can be further simplified:
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
keys = pygame.key.get_pressed()
block_x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 10
block_y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 10
draw()
This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
How can I make a sprite move when key is held down
(6 answers)
Closed 2 years ago.
I asked previously about my code I am doing for my a level coursework and I was able to fix one problem but a solution offered for me not being able to move my sprites I could not fix. I copied out this code but it still didn't work, I have highlighted this section between **
My screen loads the home screen function but when the game loop runs it loads the base but doesn't do any more.
import pygame
import random
import time
#define variables
WIDTH = 1080
HEIGHT =720
FPS = 30
x1 = WIDTH/2.25
y1 = HEIGHT/2.5
x2 = WIDTH/20
y2 = HEIGHT/2.5
xbut = 475
ybut1 = 300
ybut2 = 400
gameTitle = 'Hungry Ghosts'
xChange1 = 0
yChange1 = 0
xChange2 = 0
yChange2 = 0
running = True
#define colours
WHITE = (255,255,255)
BLACK = (0,0,0)
MEGAN = (123,57,202)
MOLLIE = (244,11,12)
KATIE = (164,12,69)
#initialise pygame and window
pygame.init()
pygame.mixer.init()
pygame.font.init()
screen =pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('Hungry Ghosts')
clock = pygame.time.Clock()
#load immages
home_image = pygame.image.load(('homescreen.jpg'))
game_image = pygame.image.load(('gamescreen.jpg'))
player1_image = pygame.image.load(('player 1.png')).convert_alpha()
player2_image = pygame.image.load(('player 2.png')).convert_alpha()
position = (0,0)
screen.blit(home_image, position)
startBut = pygame.image.load('startbutton.jpg').convert()
instructionBut = pygame.image.load('instructionbutton.jpg').convert()
#define functions
def textObjects(gameTitle, font):
textSurface = font.render(gameTitle,True, WHITE)
pygame.display.update()
return textSurface, textSurface.get_rect()
def titleText(gameTitle):
textForTitle = pygame.font.Font('VCR_OSD_MONO_1.001.ttf',115)
TextSurf, TextRect = textObjects(gameTitle, textForTitle)
TextRect.center = ((WIDTH/2),(HEIGHT/6))
screen.blit(TextSurf,TextRect)
pygame.display.update()
def startButton(xbut,ybut1):
screen.blit(startBut,(xbut,ybut1))
pygame.display.update()
def instructionButton(xbut,ybut2):
screen.blit(instructionBut,(xbut,ybut2))
pygame.display.update()
def character1(x1,y1):
screen.blit(player1_image,(x1,y1))
def character2(x,y):
screen.blit(player2_image,(x,y))
def homeScreen():
global running
start = False
home = True
while running and not start:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if startBut.get_rect(topleft = (xbut, ybut1)).collidepoint(event.pos) :
print('clicked on button')
start = True
titleText(gameTitle)
startButton(xbut,ybut1)
instructionButton(xbut,ybut2)
pygame.display.flip()
*******************************************************************
def gameLoop():
global running
global x1, y2, x2, y2, xChange1, yChange1, xChange2, yChange2
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xChange = -5
elif event.key == pygame.K_RIGHT:
xChange = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
xChange = 0
x1 += xChange1
screen.blit(game_image,(0,0))
character1(x1,y1)
character2(x2,y2)
pygame.display.flip()
*********************************************************************
#movement
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xChange1 = -5
elif event.key == pygame.K_RIGHT:
xChange1 = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
xChange1 = 0
x1 += xChange1
#calling functions
homeScreen()
gameLoop()
pygame.quit()
There is typo. The name of the variable is xChange1 rather than xChange. Furthermore read Faster version of pygame.event.get() and just implement 1 event loop:
def gameLoop():
global running
global x1, y1, x2, y2, xChange1, yChange1, xChange2, yChange2
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
xChange1 = -5
elif event.key == pygame.K_RIGHT:
xChange1 = 5
elif event.key == pygame.K_a:
xChange2 = -5
elif event.key == pygame.K_d:
xChange2 = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
xChange1 = 0
if event.key == pygame.K_a or event.key == pygame.K_d:
xChange2 = 0
x1 += xChange1
x2 += xChange2
screen.blit(game_image,(0,0))
character1(x1,y1)
character2(x2,y2)
pygame.display.flip()
#calling functions
homeScreen()
gameLoop()
pygame.quit()
However I recommend to use pygame.key.get_pressed() rather than the keyboard events.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:
def gameLoop():
global running
global x1, y1, x2, y2
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
x1 += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 5
y1 += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 5
x2 += (keys[pygame.K_d] - keys[pygame.K_a]) * 5
y2 += (keys[pygame.K_s] - keys[pygame.K_w]) * 5
screen.blit(game_image,(0,0))
character1(x1,y1)
character2(x2,y2)
pygame.display.flip()
#calling functions
homeScreen()
gameLoop()
pygame.quit()
while True:
pencere = pygame.display.set_mode((800,600))
pygame.display.set_caption("Oyun")
for olay in pygame.event.get():
if olay.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
y -= 3
if keys[pygame.K_DOWN]:
y += 3
if keys[pygame.K_LEFT]:
x -= 3
if keys[pygame.K_RIGHT]:
x += 3
pencere.fill(beyaz)
pygame.draw.rect(pencere, mavi, (x, y, 40, 60))
pygame.display.update()
When i press navigate buttons, the rectangle goes 3 pixel. But how would I make it so when I hold the keys down?
You are reinitialising the pygame window every time round your loop with set_mode. Apparently this resets keyboard input too. (I'm actually surprised this doesn't make the whole window flicker or have other obvious effects.) You should only call set_mode once and you should do it before your main loop.
You can do
import time
key_1 = pygame.key.get_pressed()
time.sleep(0.5)
key_2 = pygame.key.get_pressed()
if key_1 == key_2:
if key_1[pygame.K_UP]:
y -= 3
if key_1[pygame.K_DOWN]:
y += 3
if key_1[pygame.K_LEFT]:
x -= 3
if key_1[pygame.K_RIGHT]:
x += 3
So x and y are updated only if one of the navigation keys is hold down
I use a feature like this a lot in my pygame projects.
The basis of it is to set a variable to True if a key is pressed and false if it is released. When you call a function like move(), you will check if the movement variables are True or False.
If you pressed the right and left arrows at the same time, there would be no movement.
This is just some of the code I use. Since you are using up and down aswell, you should just add more event checks for up and down and the code should be good to go.
Code:
running = True
directions = {"right": False, "left": False}
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
directions['right'] = True
elif event.key == pygame.K_LEFT:
directions['left'] = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
directions['right'] = False
elif event.key == pygame.K_LEFT:
directions['left'] = False
if directions['right']:
x += 3
if directions['left']:
x -= 3
This question already has an answer here:
How do I continuously trigger an action at certain time intervals? Enemy shoots constant beam instead of bullets in pygame [duplicate]
(1 answer)
Closed 2 years ago.
So I wrote my code and everything works how I want it to for now.
The only problem is that my bullet or fireball teleports to the location I set it to.
I am wondering if there is a way to make it look like its actually moving towards the destination using pygame and whatnot.
Thanks a ton!
(width, height) = (1300,800)
screen = pygame.display.set_mode((width, height))
playerImage = pygame.image.load('player.png')
fireballImage = pygame.image.load('fireball.png')
pygame.display.set_caption('Game')
transparent = (0, 0, 0, 0)
#Fireball stats
def player(x ,y):
screen.blit(playerImage, (x, y))
pygame.display.flip()
def fireball(x, y):
screen.blit(fireballImage, (fb_x,fb_y))
pygame.display.flip()
fireball_state = "ready"
print('created')
fb_x = x = width * 0.45
fb_y = y = height * 0.8
x_change = 0
y_change = 0
fb_x_change = 0
fb_y_change = 0
pressed = pygame.key.get_pressed()
#mainloop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
x_change = 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
x_change = -1
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
x_change = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
y_change = -2
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
y_change = 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
y_change = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
fb_y_change = -400
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
fb_y_change = 0
x += x_change
y += y_change
fireball(x,y)
fb_x = x
fb_y = y
screen.fill((255,255,255))
player(x,y)
fb_x += fb_x_change
fb_y += fb_y_change
fireball(fb_x,fb_y)
pygame.display.update()
First of all remove pygame.display.flip() from player and fireball. That casuse flickering. A single pygame.display.update() at the end of the application loop is sufficient.
Note, the fireballs have to be blit at (x, y) rather than (fb_x, fb_y)
def player(x ,y):
screen.blit(playerImage, (x, y))
def fireball(x, y):
screen.blit(fireballImage, (x, y))
fireball_state = "ready"
Further more use pygame.time.Clock() / tick() to control the flops per second (FPS):
clock = pygame.time.Clock()
#mainloop
running = True
while running:
clock.tick(FPS)
Add a list for the fire balls:
fireballs = []
Spawn a new fireball when space is pressed:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
fireballs.append([x, y, 0, -2])
Update and draw the fireballs in a loop:
for fb in fireballs:
fb[0] += fb[2]
fb[1] += fb[3]
for fb in fireballs:
fireball(fb[0],fb[1])
See the example:
fireballs = []
FPS = 60
clock = pygame.time.Clock()
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
fireballs.append([x, y, 0, -2])
pressed = pygame.key.get_pressed()
if pressed[pygame.K_w]:
y -= 2
if pressed[pygame.K_s]:
y += 2
if pressed[pygame.K_a]:
x -= 2
if pressed[pygame.K_d]:
x += 2
for i in range(len(fireballs)-1, -1, -1):
fb = fireballs[i]
fb[0] += fb[2]
fb[1] += fb[3]
if fb[1] < 0:
del fireballs[i]
screen.fill((255,255,255))
player(x,y)
for fb in fireballs:
fireball(fb[0],fb[1])
pygame.display.update()
I have started making something on pygame but I have encountered an issue when moving left or right. if I quickly change from pressing the right arrow key to pressing the left one and also let go of the right one the block just stops moving. this is my code
bg = "sky.jpg"
ms = "ms.png"
import pygame, sys
from pygame.locals import *
x,y = 0,0
movex,movey=0,0
pygame.init()
screen=pygame.display.set_mode((664,385),0,32)
background=pygame.image.load(bg).convert()
mouse_c=pygame.image.load(ms).convert_alpha()
m = 0
pygame.event.pump()
while 1:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_LEFT:
movex =-0.5
m = m + 1
if event.key==K_RIGHT:
movex=+0.5
m = m + 1
elif event.type == KEYUP:
if event.key==K_LEFT and not event.key==K_RIGHT:
movex = 0
if event.key==K_RIGHT and not event.key==K_LEFT:
movex =0
x+=movex
y=200
screen.blit(background, (0,0))
screen.blit(mouse_c,(x,y))
pygame.display.update()
is there a way I can change this so if the right arrow key is pressed and the left arrow key is released that it will go right instead of stopping?
P.S
I am still learning pygame and am very new to the module. I'm sorry if this seems like a stupid question but i couldn't find any answers to it.
Your problem is that when you test the KEYDOWN events with
if event.key==K_LEFT and not event.key==K_RIGHT:
you always get True, because when event.key==K_LEFT is True,
it also always is not event.key==K_RIGHT (because the key of the event is K_LEFT after all).
My approach to this kind of problem is to separate
the intent from the action. So, for the key
events, I would simply keep track of what action
is supposed to happen, like this:
moveLeft = False
moveRight = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_LEFT: moveLeft = True
if event.key == K_RIGHT: moveRight = True
elif event.type == KEYUP:
if event.key == K_LEFT: moveLeft = False
if event.key == K_RIGHT: moveRight = False
Then, in the "main" part of the loop, you can
take action based on the input, such as:
while True:
for event in pygame.event.get():
...
if moveLeft : x -= 0.5
if moveRight : x += 0.5
the problem is that you have overlapping key features; If you hold down first right and then left xmove is first set to 1 and then changes to -1. But then you release one of the keys and it resets xmove to 0 even though you are still holding the other key. What you want to do is create booleans for each key. Here is an example:
demo.py:
import pygame
window = pygame.display.set_mode((800, 600))
rightPressed = False
leftPressed = False
white = 255, 255, 255
black = 0, 0, 0
x = 250
xmove = 0
while True:
window.fill(white)
pygame.draw.rect(window, black, (x, 300, 100, 100))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
rightPressed = True
if event.key == pygame.K_LEFT:
leftPressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
rightPressed = False
if event.key == pygame.K_LEFT:
leftPressed = False
xmove = 0
if rightPressed:
xmove = 1
if leftPressed:
xmove = -1
x += xmove
pygame.display.flip()
One way could be to create a queue that keeps track of the button that was pressed last. If we press the right arrow key we'll put the velocity first in the list, and if we then press the left arrow key we put the new velocity first in the list. So the button that was pressed last will always be first in the list. Then we just remove the button from the list when we release it.
import pygame
pygame.init()
screen = pygame.display.set_mode((720, 480))
clock = pygame.time.Clock()
FPS = 30
rect = pygame.Rect((350, 220), (32, 32)) # Often used to track the position of an object in pygame.
image = pygame.Surface((32, 32)) # Images are Surfaces, so here I create an 'image' from scratch since I don't have your image.
image.fill(pygame.Color('white')) # I fill the image with a white color.
velocity = [0, 0] # This is the current velocity.
speed = 200 # This is the speed the player will move in (pixels per second).
dx = [] # This will be our queue. It'll keep track of the horizontal movement.
while True:
dt = clock.tick(FPS) / 1000.0 # This will give me the time in seconds between each loop.
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dx.insert(0, -speed)
elif event.key == pygame.K_RIGHT:
dx.insert(0, speed)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
dx.remove(-speed)
elif event.key == pygame.K_RIGHT:
dx.remove(speed)
if dx: # If there are elements in the list.
rect.x += dx[0] * dt
screen.fill((0, 0, 0))
screen.blit(image, rect)
pygame.display.update()
# print dx # Uncomment to see what's happening.
You should of course put everything in neat functions and maybe create a Player class.
I know my answer is pretty late but im new to Pygame to and for beginner like me doing code like some previous answer is easy to understand but i have a solution to.I didn`t use the keydown line code, instead i just put the moving event code nested in the main game while loop, im bad at english so i give you guy an example code.
enter code here
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_ESCAPE:
run = False
win.blit(bg, (0, 0))
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
x -= 5
if pressed[pygame.K_RIGHT]:
x += 5
if pressed[pygame.K_UP]:
y -= 5
if pressed[pygame.K_DOWN]:
y += 5
win.blit(image,(x,y))
pygame.display.update()
pygame.quit()
This will make the image move rapidly without repeating pushing the key, at long the code just in the main while loop with out inside any other loop.