Python:循环和范围麻烦

时间:2013-12-09 01:06:04

标签: python loops time range pygame

我是一个使用pygame编写python语言游戏的新手。这是一款基于游标的游戏,您可以使用鼠标。物体从屏幕顶部掉落,目的是抓住它们。我使用范围函数来确定将落下多少个对象。我的代码有2个问题,我希望得到一些帮助。我将在这里发布代码:

time = 150
level = 1
score = 0

class Bomb(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = load_image("Bomb.png")
    screen = pygame.display.get_surface()
    self.rect = self.image.get_rect()

def bomb_spawn(self):
    self.rect.x = random.randrange(640)
    self.rect.y = random.randrange(-200, -0) # Respawns at the top

def update(self):    
    if self.rect.y > 805: # Respawns if colliding with bottom screen
        self.bomb_spawn()
    if self.rect.x < 105: # I do have a border/margin so if the object spawns outside 
        self.bomb_spawn() # It will respawn
    if level == 1:
        self.rect.y += 3 # The falling speed of the object increases for each level
    elif level == 2:
        self.rect.y += 4
    elif level == 3:
        self.rect.y += 5
    elif level == 4:
        self.rect.y += 6
    elif level == 5:
        self.rect.y += 7
    elif level == 6:
        self.rect.y += 8
    elif level == 7:
        self.rect.y += 9
    else:
        self.rect.y += 10
    if time <= 1: # Game over
        self.kill()

class Clocks(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = load_image("Clock.png")
    screen = pygame.display.get_surface()
    self.rect = self.image.get_rect()

def clock_spawn(self):
    self.rect.x = random.randrange(640)
    self.rect.y = random.randrange(-200, -0)

def update(self):    
    if time < 100 and time >= 0: # Required for my current Clock range function    
        if self.rect.y > 805:
            self.clock_spawn()
    if self.rect.x < 105:
        self.clock_spawn()
    if level == 1:
        self.rect.y += 3
    elif level == 2:
        self.rect.y += 4
    elif level == 3:
        self.rect.y += 5
    elif level == 4:
        self.rect.y += 6
    elif level == 5:
        self.rect.y += 7
    elif level == 6:
        self.rect.y += 8
    elif level == 7:
        self.rect.y += 9
    else:
        self.rect.y += 10
    if time <= 1:
        self.kill()

while done == False:
time -= 0.150 # timer

level = int(score/2000) + 1 # new level when you reach 2000 points.

现在我会谈到这一点,我在范围和循环方面遇到了一些麻烦。 首先是时钟,我希望时钟只在定时器低于100时才会产生。 我设法做到了这一点,但我还有一个问题。目前我使用clock.rect.x = 2000和rect.y相同,这意味着它们会在我的屏幕外生成。我希望精灵消失/删除/杀死,然后当计时器低于100时它重生。我已经尝试了pygame.sprite.Sprite.kill这样的命令,但似乎没有用。

for i in range(1):

clocks = Clocks()

if time > 100:

    clocks.rect.x = 2000 # spawns outside my screen, I want to change this.
    clocks.rect.y = 2000

elif time < 100:
    clocks.rect.x = random.randrange(640)
    clocks.rect.y = random.randrange(-200, -20)

ClockGroup.add(clocks)

接下来我的炸弹范围确实存在问题。我想增加每个新级别的范围数量。例如:在等级1中它将是1个炸弹,在等级2中将有2个炸弹等等。

for i in range(1):
bomb = Bomb()

bomb.rect.x = random.randrange(640)
bomb.rect.y = random.randrange(-200, -20)

BombGroup.add(bomb)

#I have tried some methods but none of them were working.
#Here is what I went with 
if level == 2: # This is where I am at, I don't know how to proceed.

链接到完整代码: http://pastebin.com/cpQwi2fR 任何帮助将不胜感激。真诚的HJ。

1 个答案:

答案 0 :(得分:1)

如果你想在级别n拥有n个炸弹,请试试这个:

for _ in range(level): #level goes here
    bomb = Bomb()
    bomb.rect.x = random.randrange(640)
    bomb.rect.y = random.randrange(-200, -20)
    BombGroup.add(bomb)

此片段:

if level == 1:
    self.rect.y += 3 # The falling speed of the object increases for each level
elif level == 2:
    self.rect.y += 4
elif level == 3:
    self.rect.y += 5
elif level == 4:
    self.rect.y += 6
elif level == 5:
    self.rect.y += 7
elif level == 6:
    self.rect.y += 8
elif level == 7:
    self.rect.y += 9
else:
    self.rect.y += 10

与此代码段相同:

self.rect.y += (level + 2) if level < 8 else 10

现在有了你的精灵问题,我无法帮助你,因为我从未使用过pygame。