0

I am currently making a space invader game, the sprites that I have created are not appearing the only thing that happens when I run the programme is the interpreter says "processed finished with error code 0" in addition I am using the pycharm IDE. Can you help fix this issue?.

import pygame
import sys
from pygame.locals import *


class Launcher(pygame.sprite.Sprite):
def __init__(self):
    super().__init__()
    self.image = pygame.image.load('LaserBase.png')
    self.rect = self.image.get_rect()

    self.rocketXPos = 512

    self.rect.x = self.rocketXPos
    self.rect.y = 650

def update(self, gametime):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        self.rocketXPos += 4
    elif keys[pygame.K_LEFT]:
        self.rocketXPos -= 4

    self.rect.x = self.rocketXPos
    self.rect.y = 650


class Invader(pygame.sprite.Sprite):

def __init__(self):
    super().__init__()
    self.image = pygame.image.load('inv12.png')
    self.rect = self.image.get_rect()
    self.__alienPosX = 0
    self.__alienPosY = 0
    self.__listPos = 0
    self.alienSpeed = 4
    self.alienDirection = -1

def setPosX(self, x):
    self.__alienPosX = x
    self.rect.x = x

def setPosY(self, y):
    self.__alienPosY = y
    self.rect.y = y

def setListPos(self, pos):
    self.__listPos = pos

def update(self, gametime):
    self.__alienPosX += self.alienSpeed * self.alienDirection

    if self.__alienPosX < 100 + (self.__listPos * 32):
        self.alienDirection = +1
        self.__alienPosY += 4
    elif self.__alienPosX > 924 - ((10 - self.__listPos) * 32) :
        self.alienDirection = -1
        self.__alienPosY += 4

    self.rect.x = self.__alienPosX
    self.rect.y = self.__alienPosY


class Missile(pygame.sprite.Sprite):
    def __init__(self, xInitialPos, yInitialPos):
    super().__init__()
    self.image = pygame.image.load('bullet.png')
    self.rect = self.image.get_rect()

    self.__missilePosY = yInitialPos

    self.rect.x = xInitialPos
    self.rect.y = self.__missilePosY

def update(self, gametime):
    self.__missilePosY -= 4
    self.rect.y = self.__missilePosY


class Game:
def __init__(self):
    pygame.init()
    pygame.key.set_repeat(1, 1)

    self.width = 1024
    self.height = 768
    self.screen = pygame.display.set_mode((self.width, self.height))

    self.caption = "Space Invaders!!"
    pygame.display.set_caption(self.caption)

    self.framerate = 60

    self.clock = pygame.time.Clock()
    self.starfieldImg = pygame.image.load('Starfield1024x768.png')

    self.allsprites = pygame.sprite.Group()
    self.launcher = Launcher()
    self.allsprites.add(self.launcher)
    self.aliens = pygame.sprite.Group()
    self.missileFired = None

    xPos = 512
    for i in range(11):
        invader = Invader()
        invader.setPosX(xPos)
        invader.setPosY(100)
        self.allsprites.add(invader)
        self.aliens.add(invader)
        xPos += 32

def update(self, gametime):
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                self.missileFired = Missile(self.launcher.rocketXPos, 650)
                self.allsprites.add(self.missileFired)

    self.allsprites.update(gametime)

def draw(self):
    self.screen.blit(self.starfieldImg, (0, 0))
    self.allsprites.draw(self.screen)
    pygame.display.flip()
  • you need endless loop (called "mainloop") to handle event, move object and draw it on screen. – furas Dec 01 '15 at 22:46

1 Answers1

0

You need endless loop (called "mainloop") to handle events, move/update objects, draw objects on screen.

You could add to Game class

def run(self):

    while True:
        self.update()
        self.draw()
        self.clock.tick(self.framerate)

and then use it

g = Game()
g.run()

or

Game().run()

--

BTW: "error code 0" means "program finished without problem".

furas
  • 134,197
  • 12
  • 106
  • 148
  • At the end of file - without indentation. – furas Dec 01 '15 at 23:14
  • sRGB profile - it is different problem - with images. You could convert images to remove profile - http://stackoverflow.com/questions/22745076/libpng-warning-iccp-known-incorrect-srgb-profile – furas Dec 01 '15 at 23:29
  • `gametime` is in `update()` function - I don't know why you use it - you are author. – furas Dec 01 '15 at 23:31