天天养猪协议脚本
2025.06.02 11:56 6
import random # 游戏状态 class PigFarmGame: def __init__(self): self.pigs = 5 # 初始猪的数量 self.food = 100 # 初始食物数量 self.money = 1000 # 初始金钱 self.day = 1 # 游戏天数 def show_status(self): print(f"Day {self.day}:") print(f"Pigs: {self.pigs}") print(f"Food: {self.food}") print(f"Money: {self.money}") def feed_pigs(self): if self.food >= 10: self.food -= 10 self.pigs += 1 print("Pigs are well-fed and happy!") else: print("Not enough food to feed the pigs!") def sell_pigs(self): if self.pigs > 0: price = random.randint(50, 100) * self.pigs self.money += price self.pigs -= self.pigs print(f"Sold {self.pigs} pigs for {price} money!") else: print("No pigs to sell!") def play_day(self): self.show_status() action = input("Choose an action: feed, sell, rest, exit: ").lower() if action == "feed": self.feed_pigs() elif action == "sell": self.sell_pigs() elif action == "rest": self.food += 5 print("You've rested and gained some food!") elif action == "exit": print("Exiting the game.") return True else: print("Invalid action. Try again.") self.day += 1 return False # 游戏主循环 def main(): game = PigFarmGame() while True: if game.play_day(): break if __name__ == "__main__": main()
这个脚本定义了一个简单的“天天养猪”游戏,玩家可以通过控制台输入来选择不同的操作,如喂养猪、出售猪、休息或退出游戏,游戏状态会根据玩家的选择进行更新,游戏循环将继续,直到玩家选择退出。
本文转载自互联网,如有侵权,联系删除