화면에 두 개의 물체를 움직이는 코드가 있습니다. 내가 원하는 것은 상대방에 도달하고 처음부터 다시 시작하는 것입니다. 그러나 그들은 화면을 가로 지르며 사라집니다.PYGAME의 움직이는 물체
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()
무엇이 원인 일 수 있습니까?
무엇이 '적'의 위치를 결정합니까? x :'self.x + = ...'또는 rect :'if self.rect.right> = ...' – Patashu
self.x는 객체의 x입니다. 개체를 증가 시키면 개체는 멀리있는 곳에 도달 할 때 개체를 시작 위치로 다시 이동시킵니다. 그것을 한 –