我有几个对象在屏幕上滚动。我唯一的问题是它们同时开始。我希望他们在不同的时间开始,这样他们就不会总是在同一个 X 坐标上。我该怎么做?
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
winW = 800
winH = 500
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.x + self.rect.right >= 800:
self.x=self.startX
self.y=random.randint(0,300)
enemyList = []
for i in range (0, 3):
xMoveAmnt=20
enemyList.append(Enemy(("orc.png"), xMoveAmnt, random.randint(0, 500), 0))
while True:
surface.fill ((255,255,255))
for enemy in enemyList:
enemy.moveChar()
surface.blit(enemy.image, (enemy.x, enemy.y))
time.sleep(00.01)
pygame.display.update()