如何让图片移动而不停留在pygame中?

时间:2013-11-17 03:42:03

标签: python pygame move

我正在制作游戏,其中我有“Orange”“Witch”“MyGame”类。橙子只是在屏幕上绘制,并且母狗从屏幕上的某个位置移动到橙色的位置。用户通过点击它们杀死女巫,例如,当活着的女巫数量== 2时,一些新的女巫应该出现在旧(第一)位置并转到橙子。现在我不能让新的女巫出现。

class Witch(object):
def __init__(self, position, image):

    self.image = image
    self.speed = 5

    self.position = [random.randint(0, 760), random.randint(0, 200)]       
    self.destination = (random.randint(350, 500), random.randint(350, 550))  
    self.play = True 


def draw(self, surface):
    if self.destination != self.position:   
        v = (self.destination[0] - self.position[0], self.destination[1]-self.position[1])
        n = math.sqrt(v[0]**2 + v[1]**2)
        uv = v[0]/n, v[1]/n

        self.position[0] += uv[0]*self.speed
        self.position[1] += uv[1]*self.speed

    if self.destination == self.position:
        self.position = self.position

    surface.blit(self.image, self.position)

class MyGame(object):
def __init__(self):
    """Initialize a new game"""
    pygame.mixer.init()
    pygame.mixer.pre_init(44100, -16, 2, 2048)
    pygame.init()        

self.oranges = []  
    for x in xrange(25):
        position = self.width//2, self.height//2
        self.oranges.append(Orange(position, self.orange))

    self.witches = []  
    for x in xrange(4):
        position = self.width//2, self.height//2
        self.witches.append(Witch(position, self.witch))

    self.pos = 0, 0
        if self.timer > 30:
        for i in self.oranges:
            i.draw(self.screen)

        if len(self.witches) == 2:
            for witch in self.new_witches:
                self.new_witches.append(witch)
                witch.draw(self.screen)

        for witch in self.witches:
            witch.draw(self.screen)
            witch_x = witch.position[0]
            witch_y = witch.position[1]                



            if int(witch_y) in range(350,550):
                for o in self.oranges:
                    self.oranges.remove(o)
                    if len(self.oranges) == 0:
                        self.lives -= 1    

            player_click_x = witch_x-35 <= self.pos[0] <= witch_x + 35
            player_click_y = witch_y-40 <= self.pos[1] <= witch_x + 40
            if player_click_x == True and player_click_y == True:
                self.witches.remove(witch)

1 个答案:

答案 0 :(得分:0)

女巫必须始终记住开始位置。

 self.start_position = self.position = [random.randint(...), random.randint(...)]

当巫婆去世时,您将其从witches列表移至unused_witches列表并将其position更改为start_position

self.witches.remove(witch)
witch.position = witch.start_position
self.unused_witches.append(witch)

比以后再次使用

if len(self.witches) <= 2:
    for witch in self.unused_witches:
        self.witches.append(witch)
        self.unused_witches.remove(witch)

顺便说一下:

你可以这个

if int(witch_y) in range(350,550):

写为

if 350 <= int(witch_y) < 550 :

range(350,550)给出了列表350 ... 549

你可以这个

witch_x-35 <= self.pos[0] <= witch_x + 35

写为

-35 <= self.pos[0]-witch_x <= 35

abs(self.pos[0]-witch_x) <= 35