How do i make continuous player movement in pygame?

Multi tool use
up vote
0
down vote
favorite
I've been watching a pygame tutorial on youtube on player movement, and by using this code below, the guy making the video was able to hold down a key and the character would keep moving, but when i hold down a key the character will move once and then stop. Any ideas on how to fix this?
Code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("huge honkabonkaros")
x = 50
y = 440
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()
//Alfons
python-3.x pygame
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I've been watching a pygame tutorial on youtube on player movement, and by using this code below, the guy making the video was able to hold down a key and the character would keep moving, but when i hold down a key the character will move once and then stop. Any ideas on how to fix this?
Code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("huge honkabonkaros")
x = 50
y = 440
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()
//Alfons
python-3.x pygame
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Possible duplicate of Pygame moving objects while pressing key
– skrx
Nov 9 at 13:37
keys = pygame.key.get_pressed()
and the following lines should not be in your event loop but in the outerwhile
loop. -- Also, please make sure that your code is indented correctly when you post it here (select the code and press Ctrl+K to indent it once).
– skrx
Nov 9 at 13:39
@skrx oh ok thank you so much
– AlfonsL
Nov 9 at 14:10
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've been watching a pygame tutorial on youtube on player movement, and by using this code below, the guy making the video was able to hold down a key and the character would keep moving, but when i hold down a key the character will move once and then stop. Any ideas on how to fix this?
Code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("huge honkabonkaros")
x = 50
y = 440
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()
//Alfons
python-3.x pygame
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I've been watching a pygame tutorial on youtube on player movement, and by using this code below, the guy making the video was able to hold down a key and the character would keep moving, but when i hold down a key the character will move once and then stop. Any ideas on how to fix this?
Code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("huge honkabonkaros")
x = 50
y = 440
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()
//Alfons
python-3.x pygame
python-3.x pygame
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 9 at 12:52


AlfonsL
13
13
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
AlfonsL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Possible duplicate of Pygame moving objects while pressing key
– skrx
Nov 9 at 13:37
keys = pygame.key.get_pressed()
and the following lines should not be in your event loop but in the outerwhile
loop. -- Also, please make sure that your code is indented correctly when you post it here (select the code and press Ctrl+K to indent it once).
– skrx
Nov 9 at 13:39
@skrx oh ok thank you so much
– AlfonsL
Nov 9 at 14:10
add a comment |
Possible duplicate of Pygame moving objects while pressing key
– skrx
Nov 9 at 13:37
keys = pygame.key.get_pressed()
and the following lines should not be in your event loop but in the outerwhile
loop. -- Also, please make sure that your code is indented correctly when you post it here (select the code and press Ctrl+K to indent it once).
– skrx
Nov 9 at 13:39
@skrx oh ok thank you so much
– AlfonsL
Nov 9 at 14:10
Possible duplicate of Pygame moving objects while pressing key
– skrx
Nov 9 at 13:37
Possible duplicate of Pygame moving objects while pressing key
– skrx
Nov 9 at 13:37
keys = pygame.key.get_pressed()
and the following lines should not be in your event loop but in the outer while
loop. -- Also, please make sure that your code is indented correctly when you post it here (select the code and press Ctrl+K to indent it once).– skrx
Nov 9 at 13:39
keys = pygame.key.get_pressed()
and the following lines should not be in your event loop but in the outer while
loop. -- Also, please make sure that your code is indented correctly when you post it here (select the code and press Ctrl+K to indent it once).– skrx
Nov 9 at 13:39
@skrx oh ok thank you so much
– AlfonsL
Nov 9 at 14:10
@skrx oh ok thank you so much
– AlfonsL
Nov 9 at 14:10
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
AlfonsL is a new contributor. Be nice, and check out our Code of Conduct.
AlfonsL is a new contributor. Be nice, and check out our Code of Conduct.
AlfonsL is a new contributor. Be nice, and check out our Code of Conduct.
AlfonsL is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226061%2fhow-do-i-make-continuous-player-movement-in-pygame%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
3,SCxOHVDsKKNJdsJiSkxobtc,Bzw1HGhOz,n
Possible duplicate of Pygame moving objects while pressing key
– skrx
Nov 9 at 13:37
keys = pygame.key.get_pressed()
and the following lines should not be in your event loop but in the outerwhile
loop. -- Also, please make sure that your code is indented correctly when you post it here (select the code and press Ctrl+K to indent it once).– skrx
Nov 9 at 13:39
@skrx oh ok thank you so much
– AlfonsL
Nov 9 at 14:10