0

As far as i know this is all i should have to do to display and image on the screen but it doesn't. I know i must be missing something fairly obvious here but i can't see what i have missed out that is not letting my image appear on screen.

main.py

import pygame
from loading import *
from settings import *

class game():
    def __init__(self):
        self.gameRunning = True
        self.screen = pygame.display.set_mode((screen_width, screen_height))
        self.clock = pygame.time.Clock()
        self.Background_images = load_images()

    def gameLoop(self):

        self.clock.tick(fps)
        self.event()
        self.update()

    def event(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.gameRunning = False

    def update(self):
        self.screen.blit(self.Background_images["layer1"], (0,0))
        pygame.display.update()

playGame = game()
while playGame.gameRunning == True:
    playGame.gameLoop()

loading.py

import pygame
from settings import *

def load_images():
    Parallax_images = {}
    Parallax_images["layer1"] = pygame.image.load("PARALLAX\layer1.png")
    Parallax_images["layer2"] = pygame.image.load("PARALLAX\layer2.png")
    Parallax_images["layer3"] = pygame.image.load("PARALLAX\layer3.png")
    Parallax_images["layer4"] = pygame.image.load("PARALLAX\layer4.png")
    Parallax_images["layer5"] = pygame.image.load("PARALLAX\layer5.png")
    Parallax_images["layer6"] = pygame.image.load("PARALLAX\layer6.png")
    Parallax_images["layer7"] = pygame.image.load("PARALLAX\layer7.png")
    Parallax_images["layer8"] = pygame.image.load("PARALLAX\layer8.png")
    return Parallax_images
  • Where are screen_width and screen_height created? If you blit onto a nonexistent screen, nothing will happen. Are you getting any errors, or just a blank screen? – underscoreC Feb 20 '17 at 12:09
  • they are created in settings.py which i imported at the top. Also the pygame window opens fine but it's completely black. The code also works with different images just not the ones i'm using. So i think the issue is with the images not the code. – CustomerSupport Feb 21 '17 at 04:02
  • 1
    Do the pngs have some blank/empty areas? If so, maybe, when you load an image, use : `pygame.image.load("PARALLAX\layer1.png").convert_alpha()` – underscoreC Feb 22 '17 at 09:22

1 Answers1

0

this post was usefull for drawing images

What is a good way to draw images using pygame?

UPDATE

There appears to be something amiss with the png images themselves. When compiled and run with different images, the code works properly.

Community
  • 1
  • 1
The4thIceman
  • 3,709
  • 2
  • 29
  • 36
  • I know that you can blit using an x and y for the top left corner of where you want to image to blit. Also i just tried using a rectangle and still nothing is displayed. – CustomerSupport Feb 20 '17 at 05:09
  • can you confirm that it is actually getting into the update method? – The4thIceman Feb 20 '17 at 05:16
  • Just checked, it's 100% getting to the update method and it's also 100% getting through the load_images method – CustomerSupport Feb 20 '17 at 05:21
  • I just compiled and ran it with my own images and it works, is there any other info that would be helpful? are the screen dimensions set proper as well as the fps? – The4thIceman Feb 20 '17 at 05:22
  • I just went and tried the code with different images and it works fine for them. i'll just have to get different images i think – CustomerSupport Feb 20 '17 at 05:28
  • seems that way, there must be something about the current images that causes issues. If you figure out why those particular pngs won't work, you should edit the question with what you find. It might be helpful to others – The4thIceman Feb 20 '17 at 05:31
  • Blit does not require a rect, just coords, formatted (x, y) – underscoreC Feb 20 '17 at 12:06
  • @C._ I updated the post to eliminate the "requiring a blit" portion, because blits don't require one (but will accept one if given). The original posters code works, but the pngs he was using were bad. When switched for different images, it worked. – The4thIceman Feb 20 '17 at 13:43
  • Ok sounds great! +1 – underscoreC Feb 20 '17 at 15:45