0

I am trying to make the game Space Invaders in Pygame and I am trying to display the images that I made in paint. I did manage to get the spaceShipImage on the display and when I want to add a second image, the wallImage, and add DISPLAYSURF.blit(wallImage, (wallx, wally)) the only thing that I see is the wallImage and not the spaceShipImage. So I only can get 1 image on the display. What am I doing wrong and how do I display multiple images at the same time?

import pygame, sys
from pygame.locals import *
pygame.init()

FPS = 60
fpsClock = pygame.time.Clock()

#display specs
display_width = 800
display_height = 600

#colors
black=(0,0,0)
blue=(0,0, 255)
green=(0,128,0)
red=(255,0,0)
white=(255,255,255)
yellow=(255,255,0)

#center of image
shipx = (display_width * 0.45)
shipy = (display_height * 0.8)

#movement ship is 0
shipxchange = 0

#width of spaceship
spaceshipwidth = 78

#location of the wall
wallx = 50
wally = 300

DISPLAYSURF = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Space Invaders')
spaceShipImage = pygame.image.load('spaceship7.png')
spaceshipRect = pygame.Rect(8,6, 71, 79) #rectangle of ship
wallImage = pygame.image.load('wall.png')
wallRect = pygame.Rect(0,0,58,17)

while True: # main game loop
    DISPLAYSURF.fill(white)
    DISPLAYSURF.blit(spaceShipImage, (shipx, shipy))
    DISPLAYSURF.blit(wallImage, (wallx, wally))
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN and event.key == K_LEFT:
            shipxchange += -5
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            shipxchange += +5
        if event.type == KEYUP and event.key == K_LEFT:
            shipxchange += +5
        elif event.type == KEYUP and event.key == K_RIGHT:
            shipxchange += -5

    shipx += shipxchange

    #bounderies for the spaceship
    if shipx >= display_width - spaceshipwidth:
        shipx -= shipxchange
    if shipx < 0:
        shipx -= shipxchange

    pygame.display.update()
    fpsClock.tick(FPS)

enter image description here

enter image description here

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
bhr
  • 477
  • 1
  • 5
  • 14
  • Works on my machine, could you show your wall.png and spaceship7.png? It might be an issue with that. Also, you can use surface.get_rect() to get the rectangle for an image (e.g. wallRect = wallImage.get_rect()) instead of manually creating it. – scrbbL Jan 01 '18 at 17:12
  • Thanks for your reply. I cannot upload an image in yet, because i am new here. I added a link, can you see it? Ok thanks, I will change it to get_rect(). – bhr Jan 01 '18 at 17:51
  • I didn't run code but it seems OK. Or maybe wall image is too big and it hides ship. What is happend if you blit wall before ship ? – furas Jan 01 '18 at 18:01
  • 1
    @bozzy The issue is that your images are not just the sprites, there is also a massive amount of white. The white from the wall is just covering up the sprite for the spaceship. Your images are blitting perfectly fine but the wall just covers it up. If you crop the images down it should work fine. :) – scrbbL Jan 01 '18 at 18:02
  • furas and scrbbl thank you so much for your help! It worked! Thanks! – bhr Jan 01 '18 at 18:11
  • 1
    you can crop image - and probably it will be the best. You can also use [set_colorkey](http://pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkeya) to convert white color to transparent - and this can be useful if object has different shape than rectangle. You can also use [convert_alpha()](http://pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha) if PNG has already transparent background. Rect doesn't crop image. To crop it you could try to use [set_clip](http://pygame.org/docs/ref/surface.html#pygame.Surface.set_clip) – furas Jan 01 '18 at 18:14
  • 1
    Better crop the images and use png files with transparency and then convert them with [`convert_alpha()`](http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha) in pygame. I get much better performance with converted surfaces instead of color keyed ones. BTW, I recommend searching for a better image editor than Paint. – skrx Jan 01 '18 at 18:48
  • Thanks furas and skrx. I will try to use convert_alpha() and check it out. – bhr Jan 01 '18 at 19:22

0 Answers0