【发布时间】:2013-12-09 01:06:04
【问题描述】:
我是使用 pygame 用 Python 语言编写游戏的新手。这是一个基于光标的游戏,您可以在其中使用鼠标。物体从屏幕顶部落下,目标是抓住它们。我使用 range 函数来确定有多少物体会掉落。我的代码确实有 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)
接下来我的 Bomb 范围确实有问题。我想增加每个新级别的范围数量。示例:在第 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。
【问题讨论】:
标签: python loops time range pygame