GoldCooldown = time.process_time() + 1 for row in board: for tile in row: gold += 2**tile.rank - 1 #Shop Buttons for button in Buttons: button.draw() #Automatically Saving the game every 10 seconds if SaveTime - time.process_time() <= 0: SaveTime = time.process_time() + 10 DataList = [] DataList.append(str(gold)) for row in board: for tile in row: DataList.append(str(tile.rank)) for upgrade in upgrades: DataList.append(str(upgrade[0])) DataList.append(str(upgrade[1])) SaveFile = open("Save File/SaveFile.txt", "w") SaveFile.write(" ".join(DataList)) pygame.display.flip() clock.tick(60) if __name__ == "__main__": MainMenu.HomeScreen()
def game_loop(Colors=[(0, 255, 0), (0, 150, 0)]): game_run = True Images = load_images("Images") Choices = ["Grapes", "Orange", "Apple", "Lemon", "Strawberry"] player = Player() Fruits = [] Lives = 3 score = 0 for i in range(random.randint(2, 5)): choice = random.choice(Choices) if choice == "Strawberry": Fruits.append( Fruit(Images[choice], 500, 800, random.randint(-20, 20), random.randint(-22, -20), 125, 125)) else: Fruits.append(Fruit(Images[choice])) if random.randint(1, 4) <= 3: Bombs = [ Fruit(Images["Bomb"], 500, 1000, random.randint(-30, 30), -25, 100, 100) ] else: Bombs = [] SplitFruit = [] Explosions = [] while game_run == True: #gameDisplay.fill((210,140,42)) gameDisplay.blit( pygame.transform.scale(Images["Bg"], (DisplayWidth, DisplayHeight)), (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: player.drag = True if event.type == pygame.MOUSEBUTTONUP: player.Past = [] player.drag = False #Drawing the lives if Lives > 0: for i in range(Lives): pygame.draw.rect(gameDisplay, (250, 0, 0), (25 + (i * 55), 10, 50, 50), 0) pygame.draw.rect(gameDisplay, (150, 0, 0), (25 + (i * 55), 10, 50, 50), 5) else: MainMenu.HomeScreen(score) #Drawing all the fruit and its sliced counterparts stop = False for fruit in Fruits: fruit.update() if fruit.y <= 800: stop = True if pygame.sprite.collide_rect( player, fruit) == True and player.drag and not fruit.split: fruit.split = True if fruit.Image == Images["Grapes"]: fruit.Image = Images["GrapeTop"] Fruits.append( Fruit(Images["GrapeBottom"], fruit.x, fruit.y, fruit.Vx * -2, fruit.gravity * 1.5)) elif fruit.Image == Images["Orange"]: fruit.Image = Images["OrangeTop"] Fruits.append( Fruit(Images["OrangeBottom"], fruit.x, fruit.y, fruit.Vx * -2, fruit.gravity * 1.5)) elif fruit.Image == Images["Apple"]: fruit.Image = Images["AppleTop"] Fruits.append( Fruit(Images["AppleBottom"], fruit.x, fruit.y, fruit.Vx * -2, fruit.gravity * 1.5)) elif fruit.Image == Images["Lemon"]: fruit.Image = Images["LemonTop"] Fruits.append( Fruit(Images["LemonBottom"], fruit.x, fruit.y, fruit.Vx * -2, fruit.gravity * 1.5)) elif fruit.Image == Images["Strawberry"]: fruit.Image = Images["StrawberryTop"] Fruits.append( Fruit(Images["StrawberryBottom"], fruit.x, fruit.y, fruit.Vx * -2, fruit.gravity * 1.5, 125, 125)) Fruits[-1].split = True score += 10 for fruit in Bombs: fruit.update() if fruit.y <= 800: stop = True if pygame.sprite.collide_rect(player, fruit) == True and player.drag: Explosions.append(Explosion(fruit.x, fruit.y)) Lives -= 1 fruit.x = -100 fruit.y = 900 if stop == False: for fruit in Fruits: if fruit.split == False: Lives -= 1 Fruits = [] for i in range(random.randint(2, 5)): choice = random.choice(Choices) if choice == "Strawberry": Fruits.append( Fruit(Images[choice], 500, 800, random.randint(-20, 20), random.randint(-22, -20), 125, 125)) else: Fruits.append(Fruit(Images[choice])) if random.randint(1, 4) <= 3: Bombs = [ Fruit(Images["Bomb"], 500, 800, random.randint(-40, 40), -20, 100, 100) ] else: Bombs = [] for explosion in Explosions: explosion.update(Images) if explosion.Life <= 0: Explosions.pop(Explosions.index(explosion)) #Drawing the slashy thingy if player.drag == True: player.update(Colors) pygame.display.flip() clock.tick(60)