world war II 0.1_try
2025-06-02 11:53:58
发布于:天津
今天,由本人编写的小游戏world war II
Python版在今天,测试版正式发布给大家
·游戏公告:
1.更新欧洲小部分国家
2.更新保存、加载存档
3.可以选择自己喜欢的国家进行游玩(把第83行”XXX“改成游戏里你喜欢的国家名)
游玩方式可以大家自行探索一下,当然,这是一个测试版本,所以肯定有很多BUG,但是我也已经尽力去Debug了,所以如果大家发现了游戏BUG可以私聊或者在评论区里告诉我,并附带一下图片和情况,不然我这个蒟蒻不好找。
·编辑器基本信息和Python版本、游戏版本:
建议使用Python版本:3.12.9-64bit
建议使用编辑器:VSCode最新windows版
最新游戏版本号:0.1_try
好了,最后就是游戏代码分享给大家。
当然,这个游戏开源的代码仅限测试版。
大家下好Python开始游玩吧,可能有些简陋,但是希望你们玩的开心
最后,不喜勿喷
代码:
# 版本号:0.1 属性:测试版
# 导入Python标准库
import random
import json
# 定义国家类
class Country:
def __init__(self, name, is_player=False):
self.name = name
self.is_player = is_player
self.resources = 100
self.manpower = 100
self.armies = []
self.controlled_territories = []
def recruit_army(self, size):
cost = size * 10
if self.resources >= cost and self.manpower >= size:
self.resources -= cost
self.manpower -= size
self.armies.append(Army(size, self.name))
return True
return False
def produce_resources(self):
self.resources += len(self.controlled_territories) * 5
self.manpower += len(self.controlled_territories) * 6
# 定义军队类
class Army:
def __init__(self, size, country):
self.size = size
self.country = country
self.location = None
self.experience = 0
def train(self):
self.experience += 1
def merge(self, other_army):
if self.country == other_army.country:
self.size += other_army.size
self.experience = (self.experience + other_army.experience) / 2
# 定义领土类
class Territory:
def __init__(self, name, resources, strategic_value):
self.name = name
self.resources = resources
self.strategic_value = strategic_value
self.controlling_country = None
self.armies = []
def add_army(self, army):
army.location = self
self.armies.append(army)
def remove_army(self, army):
if army in self.armies:
self.armies.remove(army)
army.location = None
# 定义游戏主函数
class MainGame:
def __init__(self):
self.countries = {}
self.territories = {}
self.current_turn = 0
self.max_turns = 20
self.player_country = None
self.territory_connections = {
"柏林": ["慕尼黑", "维也纳", "华沙", "汉堡"],
"汉堡": ["柏林", "伦敦"],
"慕尼黑": ["柏林", "维也纳", "威尼斯"],
"维也纳": ["慕尼黑", "威尼斯"],
"华沙": ["柏林", "莫斯科"],
"莫斯科": ["华沙"],
"罗马": ["威尼斯", "巴黎"],
"威尼斯": ["罗马", "巴黎"],
"巴黎": ["罗马", "威尼斯", "伦敦"],
"伦敦": ["巴黎", "汉堡"]
}
def initialize_game(self):
self.countries = {
"德国": Country("德国"),
"英国": Country("英国"),
"法国": Country("法国"),
"苏联": Country("苏联"),
"意大利": Country("意大利"),
"波兰": Country("波兰"),
"奥地利": Country("奥地利")
}
self.player_country = "XXX"
self.countries[self.player_country].is_player = True
european_territories = [
("柏林", 60, 16), ("巴黎", 40, 18), ("伦敦", 45, 10),
("莫斯科", 80, 20), ("罗马", 35, 7), ("华沙", 30, 6),
("汉堡", 25, 5), ("维也纳", 30, 6), ("威尼斯", 35, 7), ("慕尼黑", 45, 9)
]
for name, res, strat in european_territories:
self.territories[name] = Territory(name, res, strat)
self.countries["德国"].controlled_territories = ["柏林", "汉堡", "慕尼黑"]
self.countries["英国"].controlled_territories = ["伦敦"]
self.countries["法国"].controlled_territories = ["巴黎"]
self.countries["苏联"].controlled_territories = ["莫斯科"]
self.countries["意大利"].controlled_territories = ["罗马", "威尼斯"]
self.countries["波兰"].controlled_territories = ["华沙"]
self.countries["奥地利"].controlled_territories = ["维也纳"]
for country in self.countries.values():
for territory in country.controlled_territories:
country.recruit_army(10)
self.territories[territory].add_army(country.armies[-1])
self.territories[territory].controlling_country = country.name
def move_army(self, from_territory_name, to_territory_name, army_index):
if from_territory_name not in self.territories or to_territory_name not in self.territories:
return "错误:领土不存在"
from_territory = self.territories[from_territory_name]
to_territory = self.territories[to_territory_name]
if from_territory.controlling_country != self.player_country:
return "错误:你只能移动自己领土的军队"
if to_territory_name not in self.territory_connections.get(from_territory_name, []):
return "错误:只能移动到相邻领土"
if army_index < 0 or army_index >= len(from_territory.armies):
return "错误:无效的军队索引"
army = from_territory.armies[army_index]
if to_territory.controlling_country == self.player_country:
from_territory.remove_army(army)
to_territory.add_army(army)
return f"成功将军队从 {from_territory_name} 移动到 {to_territory_name}"
else:
return self.attack_territory(from_territory_name, to_territory_name, army_index)
def attack_territory(self, from_territory_name, to_territory_name, army_index):
from_territory = self.territories[from_territory_name]
to_territory = self.territories[to_territory_name]
attacking_army = from_territory.armies[army_index]
if not to_territory.armies:
from_territory.remove_army(attacking_army)
to_territory.add_army(attacking_army)
to_territory.controlling_country = self.player_country
self.countries[self.player_country].controlled_territories.append(to_territory_name)
old_owner = to_territory.controlling_country
if old_owner:
self.countries[old_owner].controlled_territories.remove(to_territory_name)
return f"成功占领 {to_territory_name}!"
defending_army = to_territory.armies[0]
attack_power = attacking_army.size * (1 + attacking_army.experience * 0.1) * random.uniform(0.8, 1.2)
defense_power = defending_army.size * (1 + defending_army.experience * 0.1) * random.uniform(0.8, 1.2)
if attack_power > defense_power:
from_territory.remove_army(attacking_army)
to_territory.add_army(attacking_army)
old_owner = to_territory.controlling_country
to_territory.controlling_country = self.player_country
self.countries[self.player_country].controlled_territories.append(to_territory_name)
if old_owner:
self.countries[old_owner].controlled_territories.remove(to_territory_name)
defending_army.size = max(1, int(defending_army.size * 0.5))
return (f"战斗胜利!占领 {to_territory_name}\n"
f"你的军队剩余: {attacking_army.size}\n"
f"敌方军队剩余: {defending_army.size}")
else:
attacking_army.size = max(1, int(attacking_army.size * 0.6))
defending_army.size = max(1, int(defending_army.size * 0.8))
return (f"战斗失败!\n"
f"你的军队剩余: {attacking_army.size}\n"
f"敌方军队剩余: {defending_army.size}")
def ai_turn(self):
for country_name, country in self.countries.items():
if country_name != self.player_country and country.armies:
for army in country.armies:
if army.location and random.random() > 0.5:
possible_targets = [t for t in self.territories.values()
if t != army.location and t.controlling_country != country_name]
if possible_targets:
target = random.choice(possible_targets)
if target.controlling_country:
defending_army = random.choice(target.armies)
attack_power = army.size * (1 + army.experience * 0.1) * random.uniform(0.8, 1.2)
defense_power = defending_army.size * (1 + defending_army.experience * 0.1) * random.uniform(0.8, 1.2)
if attack_power > defense_power:
old_controller = self.countries[target.controlling_country]
old_controller.controlled_territories.remove(target.name)
country.controlled_territories.append(target.name)
target.controlling_country = country.name
defending_army.size = max(1, defending_army.size // 2)
else:
army.location.remove_army(army)
target.add_army(army)
def resolve_battle(self, attacking_army, defending_army):
attack_power = attacking_army.size * (1 + attacking_army.experience * 0.1)
defense_power = defending_army.size * (1 + defending_army.experience * 0.1)
attack_power *= random.uniform(0.8, 1.2)
defense_power *= random.uniform(0.8, 1.2)
return attack_power > defense_power
def next_turn(self):
self.current_turn += 1
for country in self.countries.values():
country.produce_resources()
if random.random() > 0.7:
country.recruit_army(random.randint(5, 15))
self.ai_turn()
def check_victory(self):
territory_count = {name: 0 for name in self.countries}
for territory in self.territories.values():
if territory.controlling_country:
territory_count[territory.controlling_country] += 1
total_territories = len(self.territories)
for country, count in territory_count.items():
if count >= (total_territories // 2) + (1 if total_territories % 2 != 0 else 0):
return country
return None
def save_game(self, filename):
game_state = {
"countries": {name: {
"resources": c.resources,
"manpower": c.manpower,
"controlled_territories": c.controlled_territories,
"is_player": c.is_player
} for name, c in self.countries.items()},
"territories": {name: {
"controlling_country": t.controlling_country,
"armies": [{
"size": a.size,
"country": a.country,
"experience": a.experience
} for a in t.armies]
} for name, t in self.territories.items()},
"current_turn": self.current_turn,
"player_country": self.player_country
}
with open(filename, 'w') as f:
json.dump(game_state, f)
def load_game(self, filename):
with open(filename, 'r') as f:
game_state = json.load(f)
self.countries = {}
for name, data in game_state["countries"].items():
country = Country(name, data["is_player"])
country.resources = data["resources"]
country.manpower = data["manpower"]
country.controlled_territories = data["controlled_territories"]
self.countries[name] = country
self.territories = {}
for name, data in game_state["territories"].items():
territory = Territory(name, 0, 0)
territory.controlling_country = data["controlling_country"]
for army_data in data["armies"]:
army = Army(army_data["size"], army_data["country"])
army.experience = army_data["experience"]
territory.add_army(army)
self.countries[army.country].armies.append(army)
self.territories[name] = territory
self.current_turn = game_state["current_turn"]
self.player_country = game_state["player_country"]
def main():
game = MainGame()
game.initialize_game()
print("world war II")
print("著作权: Louye")
print("禁止盗取、篡改代码")
print(f"\n你的国家: {game.player_country}")
while game.current_turn < game.max_turns:
print(f"\n=== 回合 {game.current_turn + 1} ===")
player = game.countries[game.player_country]
print(f"\n资源: {player.resources}, 人力: {player.manpower}")
print("领土:", ", ".join(player.controlled_territories))
for terr_name in player.controlled_territories:
terr = game.territories[terr_name]
print(f"{terr_name}: {len(terr.armies)}支军队")
for i, army in enumerate(terr.armies):
print(f" 军队{i}: 规模{army.size}, 经验{army.experience}")
print("\n可选操作:")
print("1. 招募军队")
print("2. 移动/攻击")
print("3. 结束回合")
print("4. 保存游戏")
print("5. 加载游戏")
choice = input("选择操作(1-5): ")
if choice == "1":
size = int(input("增加军队规模(1-20): "))
if player.recruit_army(size):
territory = input("新军队将部署到哪个领土? ")
if territory in player.controlled_territories:
game.territories[territory].add_army(player.armies[-1])
print(f"已增加并部署{size}人的军队到{territory}")
else:
print("错误: 只能部署到自己控制的领土")
player.armies.pop()
else:
print("错误: 资源或人力不足")
elif choice == "2":
from_terr = input("从哪个领土移动军队? ")
if from_terr not in player.controlled_territories:
print("错误: 这不是你的领土")
continue
to_terr = input("移动到哪个领土? ")
if from_terr == to_terr:
print("错误: 不能移动到同一领土")
continue
terr = game.territories[from_terr]
if not terr.armies:
print("错误: 该领土没有军队")
continue
print(f"{from_terr}的军队:")
for i, army in enumerate(terr.armies):
print(f"{i}: 规模{army.size}, 经验{army.experience}")
army_idx = int(input("选择哪支军队(输入编号)? "))
result = game.move_army(from_terr, to_terr, army_idx)
print(result)
elif choice == "3":
game.next_turn()
winner = game.check_victory()
if winner:
print(f"\n游戏结束! {winner}获胜!")
break
elif choice == "4":
filename = input("输入保存文件名: ")
game.save_game(filename)
print("游戏已保存!")
elif choice == "5":
filename = input("输入加载文件名: ")
game.load_game(filename)
print("游戏已加载!")
else:
print("无效选择!")
if game.current_turn >= game.max_turns:
print("\n游戏结束! 达到最大回合数!")
if __name__ == "__main__":
main()
这里空空如也
有帮助,赞一个