Jasko
Jasko

Reputation: 3025

Space invaders clone issue

i am working on something like Space Invaders since i just started to learn programing i try to keep it simple, what i want is enemy ships coming from top of the screen and then settling in one line.I managed to make them coming from top at some speed but i dont know how to make them stop at a line,for example at y = 40.The code is below:

# Sprites vjezba.py
import pygame

# Define colors
black = (0,0,0)

white = (255,255,255)

red = (255,0,0)

green = (0,0,255)

# Define screen size

SCREEN_WIDTH = 420

SCREEN_HEIGHT = 400

# Classes

class Square(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20,20])

        self.image.fill(red)

        self.rect = self.image.get_rect()

    def update(self):

        pos = pygame.mouse.get_pos()

        self.rect.x = pos[0]

        self.rect.y = pos[1]

class Enemies(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20,20])

        self.image.fill(black)

        self.rect = self.image.get_rect()



    def update(self):

        speed_y = 1

        self.rect.y += speed_y


# Initialize pygame        

pygame.init()

# Initialize screen

screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])

# Set the clock

clock = pygame.time.Clock()

# Create sprites lists

square_list = pygame.sprite.Group()

enemies_list = pygame.sprite.Group()

all_sprites_list = pygame.sprite.Group()

# Create sprites

#--- Enemies sprites

diff_x = 0

diff_y = 0

for i in range(10):

    enemies = Enemies()

    enemies.rect.x = 20 + diff_x

    diff_x += 40

    enemies.rect.y = 20 - diff_y

    diff_y += 20

    enemies_list.add(enemies)

    all_sprites_list.add(enemies)

# --- Square sprite

square = Square()

square.rect.x = 200

square.rect.y = 370

square_list.add(square)

all_sprites_list.add(square)

# -------Main Loop----------

done = False

while not done:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            done = True



    all_sprites_list.update()


    screen.fill(white)

    all_sprites_list.draw(screen)


    pygame.display.flip()

    clock.tick(40)

pygame.quit()

Upvotes: 0

Views: 147

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122091

At the moment, your update() for the Enemies looks like this:

def update(self):
    speed_y = 1
    self.rect.y += speed_y

This has two obvious flaws:

  1. The speed is set locally, then discarded again at the end of the method; and
  2. It doesn't know anything about position.

To fix this, I suggest making speed_y an instance attribute:

def __init__(self):
    ...
    self.speed_y = 1

Allowing the setting of a target position

def set_target(y_pos):
    self.y_target = y_pos

And using this information in update, for example:

def update(self):
    self.rect.y += self.speed_y
    if self.rect.y >= self.y_target:
        self.speed_y = 0

This is a very basic example that just stops at the target y (and only works in one dimension), but hopefully gives you an idea of how to control the movement of your Enemies.

Upvotes: 2

Related Questions