Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have multiple objects moving at once in PYGAME

I want to make a game where I have enemies coming from two sides of the screen. Right now I have it so that enemies scroll across the screen one at a time. I would like to have more then one come at a time slowly increasing how often they come across. This is my code

import pygame, sys, time, random
from pygame.locals import *
pygame.init()
winW = 1000
winH = 600
surface = pygame.display.set_mode ((winW, winH),0,32)


pygame.display.set_caption ('Moving Orc')

class Enemy:
    def __init__(self, char, startY, startX):
        self.char=char
        self.startY=startY
        self.startX=startX
        self.drawChar()

    def drawChar (self):
        self.space = pygame.image.load (self.char)
        self.spaceRect = self.space.get_rect ()
        self.spaceRect.topleft = (self.startX,self.startY)
        self.moveChar()

    def moveChar (self):
        if self.startX == 0:
            self.xMoveAmt = 5
        elif self.startX == 800:
            self.xMoveAmt = -5

        while True:
            surface.fill ((255,255,255))
            self.spaceRect.left += self.xMoveAmt

            surface.blit (self.space, self.spaceRect)

            pygame.display.update()

            time.sleep (0.02)

            if self.spaceRect.right >= winW:
                surface.fill ((255,255,255))
                break

            elif self.spaceRect.left <= 0:
                surface.fill ((255,255,255))
                break


#MAINLINE
while True:
    enemyList=[]
    leftOrRight = random.randint(0,1)
    if leftOrRight == 0:
        leftOrRight = 0
    elif leftOrRight == 1:
        leftOrRight = 800
    enemyList.append(Enemy(("orc.png"), random.randint(50, 500), leftOrRight))

    for i in range (0,len(enemyList)):
        enemyList[i].drawChar()
        break

I have it so that every time you go into the loop, it resets the list which it runs through the class I made. And one guy will go across the screen from the left or right.

Where would i even start?

like image 465
Stephen Sparks Avatar asked Jun 05 '13 16:06

Stephen Sparks


1 Answers

There are few things that you should fix in order to have multiple enemies.

How a simple pygame program structure look like

init() 
While(True):
    draw()
    update()
    checkInput()

I see you already wrote a draw and move functions for the enemy, but they don't do what they should.

Your draw method loads the image, and calls a move function. Loading should be usually done in the __init__().

Your move function draws and moves the character, but it has a While loop, which makes it stuck until that character is out of the screen.

A example solution:

def draw(self,surface):
    surface.blit (self.space, self.spaceRect)

def move(self):
    self.spaceRect.left += self.xMoveAmt
    if self.spaceRect.right >= winW:
        self.kill()
    elif self.spaceRect.left <= 0:
        self.kill()

a possible way to kill an object is to set a flag, and in the While method, check if it can be removed from the list of objects.

Now you can create a list of enemies, and call draw, and update for each of them. In a for loop.

like image 83
Bartlomiej Lewandowski Avatar answered Nov 08 '22 16:11

Bartlomiej Lewandowski