How can I move multiple objects at once in PYGAME

I want to create a game in which I have enemies coming from two sides of the screen. Right now I have it so that enemies scroll around the screen one at a time. I would like you to have more than once, gradually increasing how often they collide. 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 enter the loop, it resets the list, which it executes through the class that I made. And one guy will cross the screen to the left or right.

Where would I even start?

+4
source share
2 answers

There are a few things you need to fix in order to have multiple enemies.

What a simple pygame program structure looks like

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

I see that you have already written a draw and moved functions for the enemy, but they do not do what they should.

The drawing method loads the image and calls the move function. Loading is usually done in __init__() .

Your move function draws and moves a character, but has a While loop that makes it stuck until that character appears on the screen.

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 whether it can be removed from the list of objects.

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

+2
source

Get rid of the drawChar function; it's bad practice to let the Enemy class know about the surface , which should exist only in the game logic. Modify the moveChar function so that it simply updates the position of the object. Take the loop from moveChar and handle the movement in your main game.

Enemy Class:

 class Enemy(object): def __init__(self, char, startX=0, startY=0, xMovAmnt=0): self.char = char self.x = startX self.y = startY self.xMovAmnt = xMovAmnt # no reason to load the image every time you want to draw, do it here self.image = pygame.image.load(self.char) self.rect = self.image.get_rect() def moveChar(self): self.x += self.xMovAmnt 

Game loop :

 enemyList = [] while True: ... # you never specified when you want to create a new Enemy, # so you need to figure that out on your own ... # this is a more "Pythonic" way of looping over a list than using a range for enemy in enemyList: enemy.movChar() surface.blit(enemy.image, (enemy.x, enemy.y)) pygame.display.update() 
+2
source

Source: https://habr.com/ru/post/1484684/


All Articles