How do i make continuous player movement in pygame?
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
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
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
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
asked Nov 9 at 12:52
AlfonsL
13
13
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
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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