我有在我的屏幕上移动 2 个对象的代码。我想让他们做的是到达另一边,然后从头开始重新开始,但是他们只是穿过屏幕消失了。
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()
这可能是什么原因造成的?