3

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()
Roketteere
  • 31
  • 6

1 Answers1

0

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

sloth
  • 99,095
  • 21
  • 171
  • 219