PyGame paddle board not moving
The key events are being registered fine on the console. Also, my paddle is showing up which suggests that my paddle class called from another file works. However, I would like my paddle to move smoothly when the left arrow key or the right arrow key is pressed down so the user does not have to repeatedly click. However, with my current code, the paddle does not move at all. I've tried changing around the position of the if/else statement that changes position to no avail. What am I doing wrong here?
The main game:
import pygame
from pygame.locals import *
from Config import Config
from Paddle import Paddle
pygame.init()
#--Display settings
pygame.display.set_caption(Config['game']['caption'])
game_display = pygame.display.set_mode((Config['game']['display_width'],
Config['game']['display_height']))
clock = pygame.time.Clock()
paddle= Paddle(game_display)
x_change = 0
pressed_left = False
pressed_right = False
def event_handler():
for event in pygame.event.get():
print (event)
if (event.type == QUIT) or (event.type == KEYDOWN and event.key ==
K_ESCAPE):
pygame.quit()
quit()
#To move the paddle to the left and right
if event.type == KEYDOWN:
if event.key == K_LEFT:
pressed_left = True
elif event.key== K_RIGHT:
pressed_right = True
#If the key is up
if event.type == KEYUP:
if event.key == K_LEFT:
pressed_left = False
x_change = 0
elif event.key == K_RIGHT:
pressed_right = False
x_change = 0
#--This changes the position
if pressed_left:
x_change = -5
if pressed_right:
x_change = 5
while True:
event_handler()
game_display.fill(Config['colors']['white'])
paddle.draw()
paddle.movement(x_change)
pygame.display.update()
clock.tick(Config['game']['fps'])
And from my paddle.py that contains the Paddle class:
import pygame
from pygame.locals import *
from Config import Config
class Paddle:
def __init__(self, game_display):
self.x = (Config['game']['display_width'] * 0.45)
self.y = (Config['game']['display_height'] * 0.92)
self.game_display=game_display
def draw(self):
pygame.draw.rect(self.game_display, Config['colors']['red'],
[self.x, self.y, 40, 25])
def movement(self, x_change):
self.x += x_change
python-3.x pygame
add a comment |
The key events are being registered fine on the console. Also, my paddle is showing up which suggests that my paddle class called from another file works. However, I would like my paddle to move smoothly when the left arrow key or the right arrow key is pressed down so the user does not have to repeatedly click. However, with my current code, the paddle does not move at all. I've tried changing around the position of the if/else statement that changes position to no avail. What am I doing wrong here?
The main game:
import pygame
from pygame.locals import *
from Config import Config
from Paddle import Paddle
pygame.init()
#--Display settings
pygame.display.set_caption(Config['game']['caption'])
game_display = pygame.display.set_mode((Config['game']['display_width'],
Config['game']['display_height']))
clock = pygame.time.Clock()
paddle= Paddle(game_display)
x_change = 0
pressed_left = False
pressed_right = False
def event_handler():
for event in pygame.event.get():
print (event)
if (event.type == QUIT) or (event.type == KEYDOWN and event.key ==
K_ESCAPE):
pygame.quit()
quit()
#To move the paddle to the left and right
if event.type == KEYDOWN:
if event.key == K_LEFT:
pressed_left = True
elif event.key== K_RIGHT:
pressed_right = True
#If the key is up
if event.type == KEYUP:
if event.key == K_LEFT:
pressed_left = False
x_change = 0
elif event.key == K_RIGHT:
pressed_right = False
x_change = 0
#--This changes the position
if pressed_left:
x_change = -5
if pressed_right:
x_change = 5
while True:
event_handler()
game_display.fill(Config['colors']['white'])
paddle.draw()
paddle.movement(x_change)
pygame.display.update()
clock.tick(Config['game']['fps'])
And from my paddle.py that contains the Paddle class:
import pygame
from pygame.locals import *
from Config import Config
class Paddle:
def __init__(self, game_display):
self.x = (Config['game']['display_width'] * 0.45)
self.y = (Config['game']['display_height'] * 0.92)
self.game_display=game_display
def draw(self):
pygame.draw.rect(self.game_display, Config['colors']['red'],
[self.x, self.y, 40, 25])
def movement(self, x_change):
self.x += x_change
python-3.x pygame
add a comment |
The key events are being registered fine on the console. Also, my paddle is showing up which suggests that my paddle class called from another file works. However, I would like my paddle to move smoothly when the left arrow key or the right arrow key is pressed down so the user does not have to repeatedly click. However, with my current code, the paddle does not move at all. I've tried changing around the position of the if/else statement that changes position to no avail. What am I doing wrong here?
The main game:
import pygame
from pygame.locals import *
from Config import Config
from Paddle import Paddle
pygame.init()
#--Display settings
pygame.display.set_caption(Config['game']['caption'])
game_display = pygame.display.set_mode((Config['game']['display_width'],
Config['game']['display_height']))
clock = pygame.time.Clock()
paddle= Paddle(game_display)
x_change = 0
pressed_left = False
pressed_right = False
def event_handler():
for event in pygame.event.get():
print (event)
if (event.type == QUIT) or (event.type == KEYDOWN and event.key ==
K_ESCAPE):
pygame.quit()
quit()
#To move the paddle to the left and right
if event.type == KEYDOWN:
if event.key == K_LEFT:
pressed_left = True
elif event.key== K_RIGHT:
pressed_right = True
#If the key is up
if event.type == KEYUP:
if event.key == K_LEFT:
pressed_left = False
x_change = 0
elif event.key == K_RIGHT:
pressed_right = False
x_change = 0
#--This changes the position
if pressed_left:
x_change = -5
if pressed_right:
x_change = 5
while True:
event_handler()
game_display.fill(Config['colors']['white'])
paddle.draw()
paddle.movement(x_change)
pygame.display.update()
clock.tick(Config['game']['fps'])
And from my paddle.py that contains the Paddle class:
import pygame
from pygame.locals import *
from Config import Config
class Paddle:
def __init__(self, game_display):
self.x = (Config['game']['display_width'] * 0.45)
self.y = (Config['game']['display_height'] * 0.92)
self.game_display=game_display
def draw(self):
pygame.draw.rect(self.game_display, Config['colors']['red'],
[self.x, self.y, 40, 25])
def movement(self, x_change):
self.x += x_change
python-3.x pygame
The key events are being registered fine on the console. Also, my paddle is showing up which suggests that my paddle class called from another file works. However, I would like my paddle to move smoothly when the left arrow key or the right arrow key is pressed down so the user does not have to repeatedly click. However, with my current code, the paddle does not move at all. I've tried changing around the position of the if/else statement that changes position to no avail. What am I doing wrong here?
The main game:
import pygame
from pygame.locals import *
from Config import Config
from Paddle import Paddle
pygame.init()
#--Display settings
pygame.display.set_caption(Config['game']['caption'])
game_display = pygame.display.set_mode((Config['game']['display_width'],
Config['game']['display_height']))
clock = pygame.time.Clock()
paddle= Paddle(game_display)
x_change = 0
pressed_left = False
pressed_right = False
def event_handler():
for event in pygame.event.get():
print (event)
if (event.type == QUIT) or (event.type == KEYDOWN and event.key ==
K_ESCAPE):
pygame.quit()
quit()
#To move the paddle to the left and right
if event.type == KEYDOWN:
if event.key == K_LEFT:
pressed_left = True
elif event.key== K_RIGHT:
pressed_right = True
#If the key is up
if event.type == KEYUP:
if event.key == K_LEFT:
pressed_left = False
x_change = 0
elif event.key == K_RIGHT:
pressed_right = False
x_change = 0
#--This changes the position
if pressed_left:
x_change = -5
if pressed_right:
x_change = 5
while True:
event_handler()
game_display.fill(Config['colors']['white'])
paddle.draw()
paddle.movement(x_change)
pygame.display.update()
clock.tick(Config['game']['fps'])
And from my paddle.py that contains the Paddle class:
import pygame
from pygame.locals import *
from Config import Config
class Paddle:
def __init__(self, game_display):
self.x = (Config['game']['display_width'] * 0.45)
self.y = (Config['game']['display_height'] * 0.92)
self.game_display=game_display
def draw(self):
pygame.draw.rect(self.game_display, Config['colors']['red'],
[self.x, self.y, 40, 25])
def movement(self, x_change):
self.x += x_change
python-3.x pygame
python-3.x pygame
asked Nov 15 '18 at 2:42
Hasanat JahanHasanat Jahan
2314
2314
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Let's look at a simpler example. Try running this program.
pressed_left = False
def event_handler():
pressed_left = True
event_handler()
print(pressed_left)
While you expected the program to print True
, it prints False
. This happens due to the way variable scoping works in python. When you try to modify a variable in a function that isn't defined in that function itself, python defines a new variable instead of looking for a global one.
So, the pressed_left = True
line in event_handler()
just creates a new pressed_left
variable that exists solely in the event_handler
function without modifying the global one.
To solve this, you need to declare the variable as global in the function. Try running this program.
pressed_left = False
def event_handler():
global pressed_left
pressed_left = True
event_handler()
print(pressed_left)
I added this because this solution makes sense. However, pressed_left and pressed_right are still being registered as False and the key presses, while registered, don't change the value of the varibles to from False to True. Thank you for this much though!
– Hasanat Jahan
Nov 16 '18 at 4:53
@HasanatJahan What does your code look like now?
– merlyn
Nov 16 '18 at 4:54
I changed the top of the event_handler function to include the global declarations.Sorry, I'm trying to get the formatting right:
– Hasanat Jahan
Nov 16 '18 at 4:56
1
@HasanatJahan Why is the x_change update not part of event handler function. It's not run in the loop either.
– merlyn
Nov 16 '18 at 5:04
1
I just changed it the x_change update inside the event handler function and it worked. The paddle is moving. Thank you kind wizard.
– Hasanat Jahan
Nov 16 '18 at 5:07
|
show 3 more comments
change your keys to something like this
key = pygame.key.pressed()
if key[pygame.K_left]:
pygame movement
Why is this a better alternative? Why would this solve the problem of pressed_left and pressed_right as being registered as false?
– Hasanat Jahan
Nov 16 '18 at 4:51
because the player will constantly move until the ey as stopped being pressed
– Dextron
Nov 16 '18 at 6:48
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53311671%2fpygame-paddle-board-not-moving%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's look at a simpler example. Try running this program.
pressed_left = False
def event_handler():
pressed_left = True
event_handler()
print(pressed_left)
While you expected the program to print True
, it prints False
. This happens due to the way variable scoping works in python. When you try to modify a variable in a function that isn't defined in that function itself, python defines a new variable instead of looking for a global one.
So, the pressed_left = True
line in event_handler()
just creates a new pressed_left
variable that exists solely in the event_handler
function without modifying the global one.
To solve this, you need to declare the variable as global in the function. Try running this program.
pressed_left = False
def event_handler():
global pressed_left
pressed_left = True
event_handler()
print(pressed_left)
I added this because this solution makes sense. However, pressed_left and pressed_right are still being registered as False and the key presses, while registered, don't change the value of the varibles to from False to True. Thank you for this much though!
– Hasanat Jahan
Nov 16 '18 at 4:53
@HasanatJahan What does your code look like now?
– merlyn
Nov 16 '18 at 4:54
I changed the top of the event_handler function to include the global declarations.Sorry, I'm trying to get the formatting right:
– Hasanat Jahan
Nov 16 '18 at 4:56
1
@HasanatJahan Why is the x_change update not part of event handler function. It's not run in the loop either.
– merlyn
Nov 16 '18 at 5:04
1
I just changed it the x_change update inside the event handler function and it worked. The paddle is moving. Thank you kind wizard.
– Hasanat Jahan
Nov 16 '18 at 5:07
|
show 3 more comments
Let's look at a simpler example. Try running this program.
pressed_left = False
def event_handler():
pressed_left = True
event_handler()
print(pressed_left)
While you expected the program to print True
, it prints False
. This happens due to the way variable scoping works in python. When you try to modify a variable in a function that isn't defined in that function itself, python defines a new variable instead of looking for a global one.
So, the pressed_left = True
line in event_handler()
just creates a new pressed_left
variable that exists solely in the event_handler
function without modifying the global one.
To solve this, you need to declare the variable as global in the function. Try running this program.
pressed_left = False
def event_handler():
global pressed_left
pressed_left = True
event_handler()
print(pressed_left)
I added this because this solution makes sense. However, pressed_left and pressed_right are still being registered as False and the key presses, while registered, don't change the value of the varibles to from False to True. Thank you for this much though!
– Hasanat Jahan
Nov 16 '18 at 4:53
@HasanatJahan What does your code look like now?
– merlyn
Nov 16 '18 at 4:54
I changed the top of the event_handler function to include the global declarations.Sorry, I'm trying to get the formatting right:
– Hasanat Jahan
Nov 16 '18 at 4:56
1
@HasanatJahan Why is the x_change update not part of event handler function. It's not run in the loop either.
– merlyn
Nov 16 '18 at 5:04
1
I just changed it the x_change update inside the event handler function and it worked. The paddle is moving. Thank you kind wizard.
– Hasanat Jahan
Nov 16 '18 at 5:07
|
show 3 more comments
Let's look at a simpler example. Try running this program.
pressed_left = False
def event_handler():
pressed_left = True
event_handler()
print(pressed_left)
While you expected the program to print True
, it prints False
. This happens due to the way variable scoping works in python. When you try to modify a variable in a function that isn't defined in that function itself, python defines a new variable instead of looking for a global one.
So, the pressed_left = True
line in event_handler()
just creates a new pressed_left
variable that exists solely in the event_handler
function without modifying the global one.
To solve this, you need to declare the variable as global in the function. Try running this program.
pressed_left = False
def event_handler():
global pressed_left
pressed_left = True
event_handler()
print(pressed_left)
Let's look at a simpler example. Try running this program.
pressed_left = False
def event_handler():
pressed_left = True
event_handler()
print(pressed_left)
While you expected the program to print True
, it prints False
. This happens due to the way variable scoping works in python. When you try to modify a variable in a function that isn't defined in that function itself, python defines a new variable instead of looking for a global one.
So, the pressed_left = True
line in event_handler()
just creates a new pressed_left
variable that exists solely in the event_handler
function without modifying the global one.
To solve this, you need to declare the variable as global in the function. Try running this program.
pressed_left = False
def event_handler():
global pressed_left
pressed_left = True
event_handler()
print(pressed_left)
answered Nov 15 '18 at 4:08
merlynmerlyn
1,77211323
1,77211323
I added this because this solution makes sense. However, pressed_left and pressed_right are still being registered as False and the key presses, while registered, don't change the value of the varibles to from False to True. Thank you for this much though!
– Hasanat Jahan
Nov 16 '18 at 4:53
@HasanatJahan What does your code look like now?
– merlyn
Nov 16 '18 at 4:54
I changed the top of the event_handler function to include the global declarations.Sorry, I'm trying to get the formatting right:
– Hasanat Jahan
Nov 16 '18 at 4:56
1
@HasanatJahan Why is the x_change update not part of event handler function. It's not run in the loop either.
– merlyn
Nov 16 '18 at 5:04
1
I just changed it the x_change update inside the event handler function and it worked. The paddle is moving. Thank you kind wizard.
– Hasanat Jahan
Nov 16 '18 at 5:07
|
show 3 more comments
I added this because this solution makes sense. However, pressed_left and pressed_right are still being registered as False and the key presses, while registered, don't change the value of the varibles to from False to True. Thank you for this much though!
– Hasanat Jahan
Nov 16 '18 at 4:53
@HasanatJahan What does your code look like now?
– merlyn
Nov 16 '18 at 4:54
I changed the top of the event_handler function to include the global declarations.Sorry, I'm trying to get the formatting right:
– Hasanat Jahan
Nov 16 '18 at 4:56
1
@HasanatJahan Why is the x_change update not part of event handler function. It's not run in the loop either.
– merlyn
Nov 16 '18 at 5:04
1
I just changed it the x_change update inside the event handler function and it worked. The paddle is moving. Thank you kind wizard.
– Hasanat Jahan
Nov 16 '18 at 5:07
I added this because this solution makes sense. However, pressed_left and pressed_right are still being registered as False and the key presses, while registered, don't change the value of the varibles to from False to True. Thank you for this much though!
– Hasanat Jahan
Nov 16 '18 at 4:53
I added this because this solution makes sense. However, pressed_left and pressed_right are still being registered as False and the key presses, while registered, don't change the value of the varibles to from False to True. Thank you for this much though!
– Hasanat Jahan
Nov 16 '18 at 4:53
@HasanatJahan What does your code look like now?
– merlyn
Nov 16 '18 at 4:54
@HasanatJahan What does your code look like now?
– merlyn
Nov 16 '18 at 4:54
I changed the top of the event_handler function to include the global declarations.Sorry, I'm trying to get the formatting right:
– Hasanat Jahan
Nov 16 '18 at 4:56
I changed the top of the event_handler function to include the global declarations.Sorry, I'm trying to get the formatting right:
– Hasanat Jahan
Nov 16 '18 at 4:56
1
1
@HasanatJahan Why is the x_change update not part of event handler function. It's not run in the loop either.
– merlyn
Nov 16 '18 at 5:04
@HasanatJahan Why is the x_change update not part of event handler function. It's not run in the loop either.
– merlyn
Nov 16 '18 at 5:04
1
1
I just changed it the x_change update inside the event handler function and it worked. The paddle is moving. Thank you kind wizard.
– Hasanat Jahan
Nov 16 '18 at 5:07
I just changed it the x_change update inside the event handler function and it worked. The paddle is moving. Thank you kind wizard.
– Hasanat Jahan
Nov 16 '18 at 5:07
|
show 3 more comments
change your keys to something like this
key = pygame.key.pressed()
if key[pygame.K_left]:
pygame movement
Why is this a better alternative? Why would this solve the problem of pressed_left and pressed_right as being registered as false?
– Hasanat Jahan
Nov 16 '18 at 4:51
because the player will constantly move until the ey as stopped being pressed
– Dextron
Nov 16 '18 at 6:48
add a comment |
change your keys to something like this
key = pygame.key.pressed()
if key[pygame.K_left]:
pygame movement
Why is this a better alternative? Why would this solve the problem of pressed_left and pressed_right as being registered as false?
– Hasanat Jahan
Nov 16 '18 at 4:51
because the player will constantly move until the ey as stopped being pressed
– Dextron
Nov 16 '18 at 6:48
add a comment |
change your keys to something like this
key = pygame.key.pressed()
if key[pygame.K_left]:
pygame movement
change your keys to something like this
key = pygame.key.pressed()
if key[pygame.K_left]:
pygame movement
answered Nov 15 '18 at 6:22
DextronDextron
789
789
Why is this a better alternative? Why would this solve the problem of pressed_left and pressed_right as being registered as false?
– Hasanat Jahan
Nov 16 '18 at 4:51
because the player will constantly move until the ey as stopped being pressed
– Dextron
Nov 16 '18 at 6:48
add a comment |
Why is this a better alternative? Why would this solve the problem of pressed_left and pressed_right as being registered as false?
– Hasanat Jahan
Nov 16 '18 at 4:51
because the player will constantly move until the ey as stopped being pressed
– Dextron
Nov 16 '18 at 6:48
Why is this a better alternative? Why would this solve the problem of pressed_left and pressed_right as being registered as false?
– Hasanat Jahan
Nov 16 '18 at 4:51
Why is this a better alternative? Why would this solve the problem of pressed_left and pressed_right as being registered as false?
– Hasanat Jahan
Nov 16 '18 at 4:51
because the player will constantly move until the ey as stopped being pressed
– Dextron
Nov 16 '18 at 6:48
because the player will constantly move until the ey as stopped being pressed
– Dextron
Nov 16 '18 at 6:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53311671%2fpygame-paddle-board-not-moving%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