new to pygame just wondering how i would go about adding a background image into the game itself? this is my code so far, i've been using the bg as a way to import my image but the py file itself refuses to load up.
import pygame
import sys
from pygame.locals import *
clock = pygame.time.Clock()
screen = pygame.display.set_mode((600,500))
bg = pygame.image.load("images\space.png")
pygame.mouse.set_visible(0)
ship = pygame.image.load("images\ship.png")
ship_top = screen.get_height() - ship.get_height()
ship_left = screen.get_width()/2 - ship.get_width()/2
screen.blit(ship, (ship_left,ship_top))
shot = pygame.image.load("images\shot.png")
shoot_y = 0
pygame.display.set_caption('galaxy invaders')
while True:
clock.tick(60)
screen.fill((r,0,0))
screen.blit(bg.(0,0))
x,y = pygame.mouse.get_pos()
screen.blit(ship, (x-ship.get_width()/2, ship_top))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
shoot_y = 500
shoot_x = x
if shoot_y > 0:
screen.blit(shot, (shoot_x, shoot_y))
shoot_y -= 10
pygame.display.update()
First of all, none of this will work because you did not initialize Pygame after importing it. Also, the pictures won't be loaded because the backslash indicates an escape seqeunce. Lastly, you should fix your indentation.
For background I always make an image the size of my game window or smaller then before all of the images are displayed, I blit that image to 0,0.
Hope this helps.
This problem can be easily solved. You will need an image the size of your screen for your background. Please remember to add
pygame.init()
at the beginning of your game to be able to start it and its abilities. A function for this picture can be used like this:This will allow the program to load your image through this function when you call it like this:
And you will also need these two lines in your
while
loop:This will fill your screen white and put the background image over it but under your other sprites and objects.
Suggestions: You should make another class for your other sprite (maybe the reason why the image is not appearing). An example could be like:
You could then "activate" it like this:
Select the coordinates for
a
andb
. You can then blit the image on to the screen like this but after your background blit statement:I hope this helps you!