spawned = False #Global Variable
def timer(clockTime,forwardTime,spawned):
if pygame.time.get_ticks() >= forwardTime:
forwardTime = pygame.time.get_ticks() + 600
clockTime += 1
if clockTime ==21:
bird.rounds += 1
clockTime = 0
spawned = False
print(spawned)
return clockTime,forwardTime,spawned
def Rounds(spawned):
if bird.rounds ==1 and spawned == False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
all_sprites = pygame.sprite.Group([blob, bird])
enemies = pygame.sprite.Group([blob])
spawned ==True
print(spawned)
elif bird.rounds ==2 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(100,200))
blob.target=bird
blob2.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2])
enemies = pygame.sprite.Group([blob,blob2])
spawned == True
elif bird.rounds ==3 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3])
enemies = pygame.sprite.Group([blob,blob2,blob3])
spawned ==True
elif bird.rounds ==4 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob4 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
blob4.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3,blob4])
enemies = pygame.sprite.Group([blob,blob2,blob3,blob4])
spawned ==True
elif bird.rounds ==5 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob4 = Enemy(random.randint(200,1000),random.randint(200,300))
blob5 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
blob4.target=bird
blob5.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3,blob4,blob5])
enemies = pygame.sprite.Group([blob,blob2,blob3,blob4,blob5])
spawned ==True
elif bird.rounds ==6 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob4 = Enemy(random.randint(200,1000),random.randint(200,300))
blob5 = Enemy(random.randint(200,1000),random.randint(200,300))
blob6 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
blob4.target=bird
blob5.target=bird
blob6.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3,blob4,blob5,blob6])
enemies = pygame.sprite.Group([blob,blob2,blob3,blob4,blob5,blob6])
spawned ==True
elif bird.rounds ==7 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob4 = Enemy(random.randint(200,1000),random.randint(200,300))
blob5 = Enemy(random.randint(200,1000),random.randint(200,300))
blob6 = Enemy(random.randint(200,1000),random.randint(200,300))
blob7 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
blob4.target=bird
blob5.target=bird
blob6.target=bird
blob7.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3,blob4,blob5,blob6,blob7])
enemies = pygame.sprite.Group([blob,blob2,blob3,blob4,blob5,blob6,blob7])
spawned ==True
elif bird.rounds ==8 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob4 = Enemy(random.randint(200,1000),random.randint(200,300))
blob5 = Enemy(random.randint(200,1000),random.randint(200,300))
blob6 = Enemy(random.randint(200,1000),random.randint(200,300))
blob7 = Enemy(random.randint(200,1000),random.randint(200,300))
blob8 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
blob4.target=bird
blob5.target=bird
blob6.target=bird
blob7.target=bird
blob8.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3,blob4,blob5,blob6,blob7,blob8])
enemies = pygame.sprite.Group([blob,blob2,blob3,blob4,blob5,blob6,blob7,blob8])
spawned ==True
elif bird.rounds ==9 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob4 = Enemy(random.randint(200,1000),random.randint(200,300))
blob5 = Enemy(random.randint(200,1000),random.randint(200,300))
blob6 = Enemy(random.randint(200,1000),random.randint(200,300))
blob7 = Enemy(random.randint(200,1000),random.randint(200,300))
blob8 = Enemy(random.randint(200,1000),random.randint(200,300))
blob9 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
blob4.target=bird
blob5.target=bird
blob6.target=bird
blob7.target=bird
blob8.target=bird
blob9.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3,blob4,blob5,blob6,blob7,blob8,blob9])
enemies = pygame.sprite.Group([blob,blob2,blob3,blob4,blob5,blob6,blob7,blob8,blob9])
spawned ==True
elif bird.rounds ==10 and spawned ==False:
blob = Enemy(random.randint(200,1000),random.randint(200,300))
blob2 = Enemy(random.randint(200,1000),random.randint(200,300))
blob3 = Enemy(random.randint(200,1000),random.randint(200,300))
blob4 = Enemy(random.randint(200,1000),random.randint(200,300))
blob5 = Enemy(random.randint(200,1000),random.randint(200,300))
blob6 = Enemy(random.randint(200,1000),random.randint(200,300))
blob7 = Enemy(random.randint(200,1000),random.randint(200,300))
blob8 = Enemy(random.randint(200,1000),random.randint(200,300))
blob9 = Enemy(random.randint(200,1000),random.randint(200,300))
blob10 = Enemy(random.randint(200,1000),random.randint(200,300))
blob.target=bird
blob2.target=bird
blob3.target=bird
blob4.target=bird
blob5.target=bird
blob6.target=bird
blob7.target=bird
blob8.target=bird
blob9.target=bird
blob10.target=bird
all_sprites = pygame.sprite.Group([blob, bird,blob2,blob3,blob4,blob5,blob6,blob7,blob8,blob9,blob10])
enemies = pygame.sprite.Group([blob,blob2,blob3,blob4,blob5,blob6,blob7,blob8,blob9,blob10])
spawned ==True
return all_sprites,enemies,spawned
pygame.init() # Pygame is initialised (starts running)
players = pygame.sprite.Group()
screen = pygame.display.set_mode([1250,850]) # Set the width and height of the screen [width,height]
pygame.display.set_caption("My Game") # Name your window
background_image = pygame.image.load("untilted.png")
done = False # Loop until the user clicks the close button.
clock = pygame.time.Clock() # Used to manage how fast the screen updates
black = ( 0, 0, 0) # Define some colors using rgb values. These can be
white = (255, 255, 255) # used throughout the game instead of using rgb values.
font = pygame.font.Font(None,36)
# Define additional Functions and Procedures here
score = 0
bird = Character()
rounds = 0
clockTime = 0
forwardTime = 0
bullet = Bullet(pygame.mouse.get_pos(),[bird.rect.x,bird.rect.y])
# -------- Main Program Loop -----------
while done == False:
for event in pygame.event.get(): # Check for an event (mouse click, key press)
key = pygame.key.get_pressed()
if event.type == pygame.QUIT: # If user clicked close window
done = True # Flag that we are done so we exit this loop
elif key[pygame.K_e]:
bullet = Bullet(pygame.mouse.get_pos(),[bird.rect.x,bird.rect.y])
bullet.rect.x = bird.rect.x
bullet.rect.y = bird.rect.y
all_sprites.add(bullet)
screen.blit(background_image, (0, 0))
clockTime,forwardTime,spawned = timer(clockTime,forwardTime,spawned)
all_sprites,enemies,spawned = Rounds(spawned)
all_sprites.update(enemies)
all_sprites.draw(screen)
textImg = font.render("Score: " + str(bird.score),1,white)
textImg2 = font.render("Lives: " + str(bird.heart),1,white)
textImg3 = font.render("Round: " + str(bird.rounds),1,white)
textImg4 = font.render("clock " + str(clockTime),1,white)
screen.blit(textImg,(10,10))
screen.blit(textImg2,(150,10))
screen.blit(textImg3,(300,10))
screen.blit(textImg4,(700,10))
pygame.display.update() # Go ahead and update the screen with what we've drawn.
clock.tick(20) # Limit to 20 frames per second
if bird.heart == 0:
pygame.quit()
pygame.quit()
當我運行的代碼,它產生的敵人每秒而不只是它產卵回合結束時,所以當我運行它時,敵人的座標不斷變化,但是當它擊中下一輪時,它會產生敵人,但是那個敵人的座標每秒都會像其他的一樣隨機化。我試圖把一個回合制在我的比賽,我不知道爲什麼它不工作
而你的問題是什麼?沒有人會通過這麼多的代碼,把它減少到一個最小的例子! –
我在問我的代碼有什麼問題,以及如何解決這個問題 –
在這個腳本中出現了很多錯誤......比如'spawned == True'是一個比較,在那個地方沒有做任何事情。另一個例子是這樣的:'done == False'。對我來說,這只是不好的風格。你應該把它寫成「未完成」。 'pygame.quit()'也是沒有道理的,因爲你在每一個循環步驟都調用它。 – 2016-03-04 12:25:00