1

I've currently been following along with a video over pygame and creating a space invaders type of game that I will modify for my own implementation of the game. However I've gotten to the section where you have to use the blit function in order to display an image for the background of the game and it's not working for me. Once the program is executed, the window is launched but without the image displayed for the background. I believe that I have properly loaded the image and stepped into the function that will use the blit function for displaying the background image as well. Please let me know if I have overlooked anything, any help will be greatly appreciated.

import pygame
import os
import time
import random


#Setting window frame
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Space Battle")

#Image Loading

RED_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
GREEN_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green_small.png"))
BLUE_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_blue_small.png"))

#Ship for player
YELLOW_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_yellow.png"))

#Laser loading
YELLOW_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_yellow.png"))
RED_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_red.png"))
GREEN_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_green.png"))
BLUE_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_blue.png"))

Here is the code for loading in the back ground image

#Loading Backgrnd Img

BG = pygame.image.load(os.path.abspath("assets/background-black.png"))
formatedBG = pygame.transform.scale(BG, (WIDTH,HEIGHT))

Here is the code for the main function and handling background display.

def window_Redraw():
    WINDOW.blit(formatedBG, (0,0))
    pygame.display.update() 


def main():
    run = True
    FPS = 60
    clock = pygame.time.Clock()
    
    while run:
        clock.tick(FPS)
        window_Redraw()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

main()

Here is a screenshot of the window after execution: enter image description here

cody neal
  • 11
  • 2
  • can you please provide us the full code to understand what's happening. how is WINDOW initialised? What do you mean by "it's not working for me"? Is an exception raised? Or is it that you don't see any result? – qouify Jul 17 '20 at 16:40
  • @qouify I've added the beginning of the program to show the initialization of WINDOW. There is no exception being raised. After execution a window is displayed without any image set, just a blank default window. – cody neal Jul 17 '20 at 17:56

2 Answers2

0

Your example is much more complicated that it should be. RED_SPACESHIP, BLUE_SPACESHIP... etc are irrelevant in this case and you should remove them.

You are displaying only a black background - nothing else, so it should be displayed. In order to check if everything is working, start from this example:

import pygame

# initialize the pygame module
pygame.init()

WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))

formatedBG = pygame.Surface((WIDTH, HEIGHT))
formatedBG.fill(pygame.color.Color(255, 0, 0, 0))

# BG = pygame.image.load(os.path.abspath("assets/background-black.png"))
# formatedBG = pygame.transform.scale(BG, (WIDTH, HEIGHT))


run = True
while run:
    WINDOW.blit(formatedBG, (0, 0))
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

And then uncomment the background to check whether the background changed to black color.

Benjamin
  • 3,217
  • 2
  • 27
  • 42
  • Hi Benjamin, redspaceship,..., etc. were loaded in to be used at a later point in the script, however this is where I stopped because I was stuck. Someone previously asked to see how the window was initialized so I provided the beginning of the code, which is why those constants are shown. Secondly, I think the name of the picture im loading in for the background may be misleading you, as it is in fact Not a solid black background. I went ahead and tested your code(just because of your hard work and effort) and still no luck either, starting to think it’s a bigger issue. – cody neal Jul 18 '20 at 05:32
  • 'No luck' means what? Does the red color render properly? I suppose those 'redspaceship etc.' might be useful, but they are not necessary in this example so they might be remove to provide a minimum example. – Benjamin Jul 18 '20 at 08:29
  • ‘No luck’ meaning after running your code it did not give me any different results than from before. The window was not filled with a color at all at during execution. Uncommenting the background did not display the image either. – cody neal Jul 18 '20 at 08:41
  • Is the background of a window white or transparent? Besides, I just realized, there is no `pygame.init()` method at the beginning. Could you try now? – Benjamin Jul 18 '20 at 08:43
  • I went ahead and attached a screenshot of the window to the original post in order to clarify what's happening during execution. – cody neal Jul 18 '20 at 19:52
  • Hi Benjamin I've finally figured out the problem after doing some more research and looking over posts on Stack overflow. For some reason the version of my pygame was creating these issues. After upgrading pygame to 2.0.0.dev4 with 'pip install==2.0.0.dev4' everything seemed to be working fine for me. The background filled with the color, and I was also able to get the original background image to load properly as well. Thank you for your help with this! – cody neal Jul 18 '20 at 20:45
0

My previous version of pygame which was 1.9.4 was causing my initial issues with displaying the background image using blit and the problem with the window not being able to display any color with the fill function. I solved these issues by running the command 'pip install==2.0.0.dev4' to upgrade pygame. Everything seems to be working properly now.

cody neal
  • 11
  • 2