Pygame not loading image

Multi tool use
I cannot display the image that is in the same source folder as my project. Also it gives me squiggly lines under (x, y) and it tells me
"Shadows name "x" & "y" from outer scope"
It shows each one for the "x" and "y" so I just put &
Lastly my quit() at the end tells me
"PEP 8: blank line at the end of file"
Completely new to python and pygame
I am not sure what to try
import pygame
pygame.init()
display_Width = 400
display_Height = 400
gameDisplay = pygame.display.set_mode((display_Height, display_Width))
pygame.display.set_caption('Shonen Run Project')
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
heroImg = pygame.image.load('harper.png')
def hero(x, y):
gameDisplay.blit(heroImg, (x, y))
x = (display_Width * 0.45)
y = (display_Height * 0.8)
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white)
hero(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
python-3.x pygame
add a comment |
I cannot display the image that is in the same source folder as my project. Also it gives me squiggly lines under (x, y) and it tells me
"Shadows name "x" & "y" from outer scope"
It shows each one for the "x" and "y" so I just put &
Lastly my quit() at the end tells me
"PEP 8: blank line at the end of file"
Completely new to python and pygame
I am not sure what to try
import pygame
pygame.init()
display_Width = 400
display_Height = 400
gameDisplay = pygame.display.set_mode((display_Height, display_Width))
pygame.display.set_caption('Shonen Run Project')
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
heroImg = pygame.image.load('harper.png')
def hero(x, y):
gameDisplay.blit(heroImg, (x, y))
x = (display_Width * 0.45)
y = (display_Height * 0.8)
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white)
hero(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
python-3.x pygame
add a comment |
I cannot display the image that is in the same source folder as my project. Also it gives me squiggly lines under (x, y) and it tells me
"Shadows name "x" & "y" from outer scope"
It shows each one for the "x" and "y" so I just put &
Lastly my quit() at the end tells me
"PEP 8: blank line at the end of file"
Completely new to python and pygame
I am not sure what to try
import pygame
pygame.init()
display_Width = 400
display_Height = 400
gameDisplay = pygame.display.set_mode((display_Height, display_Width))
pygame.display.set_caption('Shonen Run Project')
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
heroImg = pygame.image.load('harper.png')
def hero(x, y):
gameDisplay.blit(heroImg, (x, y))
x = (display_Width * 0.45)
y = (display_Height * 0.8)
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white)
hero(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
python-3.x pygame
I cannot display the image that is in the same source folder as my project. Also it gives me squiggly lines under (x, y) and it tells me
"Shadows name "x" & "y" from outer scope"
It shows each one for the "x" and "y" so I just put &
Lastly my quit() at the end tells me
"PEP 8: blank line at the end of file"
Completely new to python and pygame
I am not sure what to try
import pygame
pygame.init()
display_Width = 400
display_Height = 400
gameDisplay = pygame.display.set_mode((display_Height, display_Width))
pygame.display.set_caption('Shonen Run Project')
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
heroImg = pygame.image.load('harper.png')
def hero(x, y):
gameDisplay.blit(heroImg, (x, y))
x = (display_Width * 0.45)
y = (display_Height * 0.8)
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white)
hero(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
python-3.x pygame
python-3.x pygame
asked Jan 3 at 2:11
user3924104user3924104
161
161
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I guess you use a IDE. The image loading of your code seems fine, just note that it does only work if the current working directory is actually the directory your python file and your image file is in. A simple way to ensure this is:
import os
import sys
os.chdir(sys.path[0])
The other issues are just warnings.
You have two global variables x
and y
, and a function def hero(x, y):
which has also two arguments x
and y
. The warning tells you that if you access x
or y
inside hero
, you actually access the local x
or y
and you have no way to access the global x
or y
(they are shadowed).
I would suggest renaming the global x
and y
variables to a more meaningful name, and also remove the hero
function, since it is pretty useless.
PEP 8 is the python style guide. You should follow it, as it helps to keep your code readable, especially if other people than yourself are going to read your code. But of course it's not a law, and a missing blank line at the end of file will probably bother no one...
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%2f54015499%2fpygame-not-loading-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I guess you use a IDE. The image loading of your code seems fine, just note that it does only work if the current working directory is actually the directory your python file and your image file is in. A simple way to ensure this is:
import os
import sys
os.chdir(sys.path[0])
The other issues are just warnings.
You have two global variables x
and y
, and a function def hero(x, y):
which has also two arguments x
and y
. The warning tells you that if you access x
or y
inside hero
, you actually access the local x
or y
and you have no way to access the global x
or y
(they are shadowed).
I would suggest renaming the global x
and y
variables to a more meaningful name, and also remove the hero
function, since it is pretty useless.
PEP 8 is the python style guide. You should follow it, as it helps to keep your code readable, especially if other people than yourself are going to read your code. But of course it's not a law, and a missing blank line at the end of file will probably bother no one...
add a comment |
I guess you use a IDE. The image loading of your code seems fine, just note that it does only work if the current working directory is actually the directory your python file and your image file is in. A simple way to ensure this is:
import os
import sys
os.chdir(sys.path[0])
The other issues are just warnings.
You have two global variables x
and y
, and a function def hero(x, y):
which has also two arguments x
and y
. The warning tells you that if you access x
or y
inside hero
, you actually access the local x
or y
and you have no way to access the global x
or y
(they are shadowed).
I would suggest renaming the global x
and y
variables to a more meaningful name, and also remove the hero
function, since it is pretty useless.
PEP 8 is the python style guide. You should follow it, as it helps to keep your code readable, especially if other people than yourself are going to read your code. But of course it's not a law, and a missing blank line at the end of file will probably bother no one...
add a comment |
I guess you use a IDE. The image loading of your code seems fine, just note that it does only work if the current working directory is actually the directory your python file and your image file is in. A simple way to ensure this is:
import os
import sys
os.chdir(sys.path[0])
The other issues are just warnings.
You have two global variables x
and y
, and a function def hero(x, y):
which has also two arguments x
and y
. The warning tells you that if you access x
or y
inside hero
, you actually access the local x
or y
and you have no way to access the global x
or y
(they are shadowed).
I would suggest renaming the global x
and y
variables to a more meaningful name, and also remove the hero
function, since it is pretty useless.
PEP 8 is the python style guide. You should follow it, as it helps to keep your code readable, especially if other people than yourself are going to read your code. But of course it's not a law, and a missing blank line at the end of file will probably bother no one...
I guess you use a IDE. The image loading of your code seems fine, just note that it does only work if the current working directory is actually the directory your python file and your image file is in. A simple way to ensure this is:
import os
import sys
os.chdir(sys.path[0])
The other issues are just warnings.
You have two global variables x
and y
, and a function def hero(x, y):
which has also two arguments x
and y
. The warning tells you that if you access x
or y
inside hero
, you actually access the local x
or y
and you have no way to access the global x
or y
(they are shadowed).
I would suggest renaming the global x
and y
variables to a more meaningful name, and also remove the hero
function, since it is pretty useless.
PEP 8 is the python style guide. You should follow it, as it helps to keep your code readable, especially if other people than yourself are going to read your code. But of course it's not a law, and a missing blank line at the end of file will probably bother no one...
answered Jan 3 at 7:26


slothsloth
74.9k14129172
74.9k14129172
add a comment |
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%2f54015499%2fpygame-not-loading-image%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
2fa0zg TDg4R R 8anb8I fkA,ca0 u4rA 63