Pygame - 保持Sprites不重叠

时间:2013-11-15 02:39:40

标签: python pygame

我正在使用python和pygame制作太空入侵者类型的游戏。我目前在哪里可以用我的箭头键在底部来回移动我的用户船,我的敌人产生并在顶部来回移动。我的问题在于敌人。如果它们从游戏画面边缘达到100像素,我就会设置它们,它们会开始向另一个方向移动。这导致重叠,而已经达到极限的那些已经回头而未达到极限的那些仍然是原始方向。我想要它,以便当边缘上的敌人达到极限时,所有敌人立即回到另一个方向,就像太空入侵者一样。我的代码如下。我有KeepFormation在敌人面前试图做我想要的,但它不起作用。谢谢!

from pygame import *

size_x = 900
size_y = 650

class Object:
    def disp(self, screen):
        screen.blit(self.sprite, self.rect)

class User_Ship(Object):
    def __init__(self):
        self.sprite = image.load("mouse.bmp")
        self.rect = self.sprite.get_rect()
        self.rect.centerx = size_x/2
        self.rect.centery = size_y - 40
        self.count = 0
        self.move_x = 0
        self.move_y = 0

    def checkwith(self, otherrect):
        if self.rect.colliderect(otherrect):
            exit()

    def cycle(self):
        self.rect.centerx += self.move_x
        if self.rect.centerx < 100:
            self.rect.centerx = 100
        if self.rect.centerx > size_x - 100:
            self.rect.centerx = size_x - 100

        self.rect.centery += self.move_y
        if self.rect.centery < 0:
            self.rect.centery = 800

    def right(self):
        self.move_x += 10

    def left(self):
        self.move_x -= 10

    def stop_x(self):
        self.move_x = 0

    def stop_y(self):
        self.move_y = 0

    def shoot(self):
        if keys[pygame.K_SPACE]:
            self.Bullet.x = self.rect.centerx
            self.Bullet.y = self.rect.centery
            self.Bullet.speed = 10

class Enemys(Object):
    def __init__(self):
        self.sprite = image.load("ball.bmp")
        self.rect = self.sprite.get_rect()
        self.rect.centerx = size_x/3
        self.rect.centery = 200
        self.mval = -2

    def KeepFormation(self, otherrect):
        if self.rect.colliderect(otherrect):
            self.rect.centerx += 5

    def cycle(self):
        self.rect.centerx += self.mval
        if self.rect.centerx < 100:
            self.mval = 2
        elif self.rect.centerx > (size_x - 100):
            self.mval = -2

class Bullet(Object):
    def __init__(self):
        self.sprite = image.load("missile.png")
        self.rect = self.sprite.get_rect()
        self.rect.centerx = -100
        self.rect.centery = 100
        self.speed = 0

EnemyList = []
for i in range(14):
    EnemyList.append(Enemys())
    EnemyList[i].rect.centerx = (i) * 50
#   EnemyList[i].count = i * 20 

init()
screen = display.set_mode((size_x, size_y))
m = User_Ship()
en = Enemys()
b = Bullet()
clock = time.Clock()

while True:
    for e in event.get():
        if e.type == QUIT:
            quit()
        if e.type == KEYDOWN:
            if e.key == K_RIGHT:
                m.right()
            elif e.key == K_LEFT:
                m.left()
        if e.type == KEYUP:
            if e.key == K_RIGHT or e.key == K_LEFT:
                m.stop_x()

    m.cycle()
    screen.fill((255,255,255))
    for enemy in EnemyList:
        enemy.cycle()
        enemy.disp(screen)
        enemy.KeepFormation(enemy.rect)
    m.disp(screen)
    b.disp(screen)
    display.flip()
    clock.tick(60)  

1 个答案:

答案 0 :(得分:2)

中添加标记
def cycle(self):
    self.rect.centerx += self.mval
    if self.rect.centerx < 100:
        self.mval = 2
        return True # Direction change!
    elif self.rect.centerx > (size_x - 100):
        self.mval = -2
        return True
    return False # no change

但是,你需要让这种变化只发生一次。因此...

done_once = False
for enemy in EnemyList:
    if not done_once:
        change = enemy.cycle()
    if change and not done_once:
        # call the new enemy function that changes every enemy's direction
        done_once = True
    enemy.disp(screen)
    enemy.KeepFormation(enemy.rect)

处理此标志,以便每个人都应该改变方向。您可能需要其他功能&amp; Enemy课程中的di​​rection_tracking变量。祝你好运干得好!