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










share|improve this question






















  • 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










  • @skrx oh ok thank you so much
    – AlfonsL
    Nov 9 at 14:10















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










share|improve this question






















  • 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










  • @skrx oh ok thank you so much
    – AlfonsL
    Nov 9 at 14:10













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • 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










  • @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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff