2

I am trying to make a Space Invaders game for University, I have most of the code to get a pass and I had the aliens/invaders move left, down, right, etc, but I made a class and imported it, now when I try to make it work it goes further right than it should then just down. I thought I had followed the university slides correctly but I can't figure out the problem. I think it may have something to do with the xPos numbers.

Here is my code:

import sys, pygame, os, Invader
from pygame.locals import *

class SpaceInvaders:

    # Constructor of the basic game class.
    # This constructor calls initialize and main_loop method.
    def __init__(self):
        self.initialize()
        self.main_loop()

    # Initialization method. Allows the game to initialize different
    # parameters and load assets before the game runs
    def initialize(self):
        pygame.init()
        pygame.key.set_repeat(1, 1) # This means when I hold A or D it repeats it for you so it doesn't move only a little
        os.environ['SDL_VIDEO_CENTERED'] = '1' # centres the game in the window on launch
        self.width = 1260
        self.height = 700
        self.screen = pygame.display.set_mode((self.width, self.height))

        self.caption = "Space Invaders!"    # This makes the top window say "Brick Breaker"
        pygame.display.set_caption(self.caption)

        self.framerate = 60

        self.clock = pygame.time.Clock()

        # Sprites
        self.background = pygame.image.load("background.jpg")
        self.defender = pygame.image.load("defender.png")
        self.alien = pygame.image.load("alien1.png")
        self.rocket = pygame.image.load("rocket.png")

        # Locations
        self.defenderPosX = 570
        self.defenderPosY = 610

        self.rocketPosX = -1
        self.rocketPosY = -1

        self.rocketFired = False

        self.alienDirection = -1
        self.alienSpeed = -1

        self.ticks = 0

        self.invaders = []
        xPos = 300
        for i in range(11):
            invader = Invader.Invader()
            invader.setPosX(xPos)
            invader.setPosY(100)
            self.invaders.append(invader)
            xPos += 65


    # main loop method keeps the game running. This method continuously
    # calls the update and draw methods to keep the game alive.
    def main_loop(self):        
        while True:
            gametime = self.clock.get_time()
            self.update(gametime)
            self.draw(gametime)
            self.clock.tick(self.framerate)

    # Update method contains game update logic, such as updating the game
    # variables, checking for collisions, gathering input, and
    # playing audio.
    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_ESCAPE:    # Makes the game exit when you press escape
                    pygame.quit()
                    sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:       # Makes the Defender move left
                    self.defenderPosX = self.defenderPosX - 15
                elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:    # Makes the Defender move right
                    self.defenderPosX = self.defenderPosX + 15
                elif event.key == pygame.K_SPACE and self.rocketFired == False: # Makes the defender shoot a rocket
                    self.rocketPosX = self.defenderPosX + 47                    # Position of the rocekt on launch
                    self.rocketPosY = 620 - self.rocket.get_height()            # Position of the rocket on launch
                    self.rocketFired = True

        if self.rocketFired == True:
                self.rocketPosY = self.rocketPosY - 10  # If True, the rocket will move up 10px

        if self.rocketPosY < 10:
                self.rocketFired = False                # When it reaches 10px it'll reset the variable to False to start again

        # Stopping the aliens and rocket from going off screen

        if self.defenderPosX < 0:
            self.defenderPosX = 0

        if self.defenderPosX > 1156:
            self.defenderPosX = 1156

        if self.rocketPosX < 48:
            self.rocketPosX = 48

        if self.rocketPosX > 1204:
            self.rocketPosX = 1204

        self.ticks = self.ticks + gametime
        if self.ticks > 500:
            for i in range(11):
                self.invaders[i].moveHorizontal(self.alienSpeed * self.alienDirection)

            if self.invaders[0].getPosX() < 100:
                self.alienDirection = +1

                xPos = 96
                for i in range(11):
                    self.invaders[i].moveVertical(8)
                    self.invaders[i].setPosX(xPos)
                    #xPos = xPos + self.alien.get_width()

            if self.invaders[10].getPosX() > 1160:
                self.invaderDirection = -1

                xPos = 1260 - self.alien.get_width() * 11
                for i in range(11):
                    self.invaders[i].moveVertical(8)
                    self.invaders[i].setPosX(xPos)
                    #xPos = xPos + self.alien.get_width()

            self.ticks = 0          
    # Draw method, draws the current state of the game on the screen
    def draw(self, gametime):
        self.screen.blit(self.background, (0, 0))
        self.screen.blit(self.defender, (self.defenderPosX, self.defenderPosY))
        for i in range(11):
            self.screen.blit(self.alien, self.invaders[i].getPosition())
        if self.rocketFired == True:
            self.screen.blit(self.rocket, (self.rocketPosX, self.rocketPosY))



        #self.sprites.update()
        pygame.display.flip()


if __name__ == "__main__":
    game = SpaceInvaders()

And here is my invader class:

class Invader:
    def __init__(self):
        self.__alienPosX = 0
        self.__alienPosY = 50

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

    def setPosY(self, y):
        self._alienPosY = y

    def getPosX(self):
        return self.__alienPosX

    def getPosY(self):
        return self.__alienPosY

    def getPosition(self):
        return (self.__alienPosX, self.__alienPosY)

    def moveHorizontal(self, amount):
        self.__alienPosX = self.__alienPosX + amount

    def moveVertical(self, amount):
        self.__alienPosY = self.__alienPosY + amount
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • Could you upload your sprites somewhere they can be downloaded by the public? It would be helpful to use the same images for debugging your code. – Noctis Skytower Jan 08 '16 at 19:48

2 Answers2

0
if self.invaders[0].getPosX() < 100:
                self.alienDirection = +1

if self.invaders[10].getPosX() > 1160:
                self.invaderDirection = -1

One of these things is not like the other.

Edit:

Also, arn't you moving them all on top of each other here?

for i in range(11):
                    self.invaders[i].moveVertical(8)
                    self.invaders[i].setPosX(xPos)
                    #xPos = xPos + self.alien.get_width()

Shouldn't there be an offset based on i? Why is that last line commented out?

JBGreen
  • 534
  • 2
  • 6
0

I don't know if you have some limitations in this project as to what functions and classes you can use or not but it would be much simpler if you just use custom events check here!

Community
  • 1
  • 1
Micron
  • 3
  • 6