3

I have 50 alien sprites that I want to follow the player, but first I want a camera to follow the player wherever he goes.

I had this working in a old game, however in my old game I did not use classes or sprite groups so it was inefficient.

So in this game I have the player set to the center of the screen, and everything else moves, so to do this I have a variable say CameraX and CameraY and when the player moves the two camera variables go up and down. However, in my script the aliens don't get updated. Anyway here are my scripts:

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

black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)


screen_width = 1080
screen_height = 720
screen = pygame.display.set_mode([screen_width,screen_height])

alien_list = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()

Alien = "graphics\sprites\Alien.png"
Player = "graphics\sprites\Player.png"

CameraX = 0
CameraY = 0


def main():
    class Enemy(pygame.sprite.Sprite):

        def __init__(self, image):
            pygame.sprite.Sprite.__init__(self) 

            self.image = pygame.image.load(image).convert_alpha()
            self.rect = self.image.get_rect()

        def Create():
            for i in range(50):
                alien = Enemy(Alien)

                alien.rect.x = random.randrange(screen_width - 50 - CameraX)
                alien.rect.y = random.randrange(screen_height - 50 - CameraY)

                alien_list.add(alien)
                all_sprites.add(alien)


    player = Enemy(Player)
    all_sprites.add(player)

    done = False

    clock = pygame.time.Clock()

    score = 0

    moveCameraX = 0
    moveCameraY = 0

    player.rect.x = 476
    player.rect.y = 296

    Enemy.Create()

    while done == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        if event.type == KEYDOWN:
            if event.key == K_w:
                moveCameraY = -10
            if event.key == K_s:
                moveCameraY = 10
            if event.key == K_a:
                moveCameraX = -10
            if event.key == K_d:
                moveCameraX = 10

        if event.type == KEYUP:
            if event.key == K_w:
                moveCameraY = 0 
            if event.key == K_s:
                moveCameraY = 0
            if event.key == K_a:
                moveCameraX = 0
            if event.key == K_d:
                moveCameraX = 0

        screen.fill(white)

        enemys_hit = pygame.sprite.spritecollide(player, alien_list, True)

        for enemy in enemys_hit:
            score += 1
            print(score)

        all_sprites.draw(screen)

        clock.tick(40)

        pygame.display.flip()

    pygame.quit()

and then the script that runs the whole thing:

import pygame
import GameEg
from pygame.locals import *
pygame.init()

game = GameEg.main()

game.main()

thanks for your time and help

munk
  • 12,340
  • 8
  • 51
  • 71
user3342999
  • 35
  • 1
  • 5

2 Answers2

1

You don't need to use a CameraX and CameraY variable. Instead, when you get the keyboard input, simply make the aliens all move that direction. You also don't have any code that is actually moving the player and aliens. You need to add an update function to your class that changes the location of the source rects.

Remolten
  • 2,614
  • 2
  • 25
  • 29
0

Concerning the camera: The simplest implementation of a camera in my opinion would be an a camera xoffset and a camera yoffset. If a sprite is at position x,y it should be drawn at position x+xoffset,y+yoffset

Now if we want the player at position player_x,player_y to be at the center of the screen, in every iteration:

We update the player normally

We set the xoffset and yoffset to: xoffset = screen_width/2 - player_x yoffset = screen_height/2 - player_y

We draw all our sprites (including the player) at positions sprite_x + xoffset , sprite_y + yoffset

Rinse and repeat. I hope that helps:)

Alex Koukoulas
  • 998
  • 1
  • 7
  • 21