I have the code to move 2 objects across my screen. What I want them to do is reach the other side, and restart back at the beginning, however they just cross the screen and disappear.
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
winW = 500
winH = 300
surface = pygame.display.set_mode ((winW, winH),0,32)
class Enemy():
def __init__(self, char, xMoveAmnt, startY=0, startX=0):
self.char = char
self.x = startX
self.y = startY
self.startX=startX
self.startY=startY
self.xMoveAmnt = xMoveAmnt
self.image = pygame.image.load(self.char)
self.rect = self.image.get_rect()
def moveChar(self):
self.x += self.xMoveAmnt
if self.rect.right >= 500:
self.x=self.startX
enemyList = []
for i in range (0, 2):
leftOrRight1 = random.randint(0,1)
if leftOrRight1 == 0:
leftOrRight = 0
xMoveAmnt = 20
elif leftOrRight1 == 1:
leftOrRight = 500
xMoveAmnt = -20
enemyList.append(Enemy(("orc.png"), xMoveAmnt, random.randint(0, 300), leftOrRight))
while True:
surface.fill ((255,255,255))
for enemy in enemyList:
enemy.moveChar()
surface.blit(enemy.image, (enemy.x, enemy.y))
time.sleep(00.04)
pygame.display.update()
What could be causing this?
Enemy? Is it x:self.x +=...or is it rect:if self.rect.right >=...