Python一个回合制兵棋小游戏(1)

1.步兵能力

        按照输入移动部队

    def move(self):
        if self.v > 0 and self.free:
            print(self.name+"##########")
            self.speed = max(eval(input("input speed")),self.speed)
            a,b = [eval(i) for i in input("input direction"+self.name).split()]
            d = (a**2+b**2)**0.5
            a = a/d * self.speed if d !=0 else 0
            b = b/d * self.speed if d !=0 else 0
            c = [a,b]
            for i in range(2):
                self.location[i] += c[i]

        if self.v > 0 and not self.free:
            self.speed = random.uniform(0,2) * self.speed
            a,b = [1,1]
            d = (a**2+b**2)**0.5
            a = a/d * self.speed if d !=0 else 0
            b = b/d * self.speed if d !=0 else 0
            c = [a,b]
            for i in range(2):
                self.location[i] += c[i]
 

        按照指令开火射击

    def fire(self,another):
        if self.v > 0 and another.flag != self.flag and self.a > 0:
            d = 0
            for i in range(2):
                d += (self.location[i]-another.location[i])**2
            d = d**0.5
            if d < 1:
                print("fire"+str(self.location))
                another.v -= self.k
            self.a -= 1

完整代码

main部分

import matplotlib.pyplot as plt
from troops import *
from do import *


if __name__ == "__main__":
    for ii in range(10):
        for i in I:
            try:
                aa,bb = i.report()
            except:
                continue
            plt.scatter(aa,bb,label=i.name,c="blue" if i.flag == "US" else "red")
            plt.text(aa,bb,i.name,fontsize=10)
        plt.legend()
        plt.show()
        plt.savefig(str(ii)+".jpg")
        plt.clf()
        for i in US:
            i.move()
        for i in JPA:
            i.move()
        for i in US:
            print("give order to"+i.name)
            c = input("fire=F,not fire=N")
            if c == "F":
                for ii in JPA:
                    i.fire(ii)
                    
        for i in JPA:
            for ii in US:
                i.fire(ii)   
                    
    print("the war is over")
    for i in I:
        if i.v > 0:
            print(i)

troop部分

import random

class infantry:
    def __init__(self,flag,name,location=[0,0],speed=1,v=100,k=10,a=10,free=False):
        self.location = location
        self.speed = speed
        self.v = v
        self.k = k
        self.name = name
        self.flag = flag
        self.a = a
        self.free = free

    def move(self):
        if self.v > 0 and self.free:
            print(self.name+"##########")
            self.speed = max(eval(input("input speed")),self.speed)
            a,b = [eval(i) for i in input("input direction"+self.name).split()]
            d = (a**2+b**2)**0.5
            a = a/d * self.speed if d !=0 else 0
            b = b/d * self.speed if d !=0 else 0
            c = [a,b]
            for i in range(2):
                self.location[i] += c[i]

        if self.v > 0 and not self.free:
            self.speed = random.uniform(0,2) * self.speed
            a,b = [1,1]
            d = (a**2+b**2)**0.5
            a = a/d * self.speed if d !=0 else 0
            b = b/d * self.speed if d !=0 else 0
            c = [a,b]
            for i in range(2):
                self.location[i] += c[i]
 

    def report(self):
        if self.v > 0:
            return self.location
        else:
            pass

    def fire(self,another):
        if self.v > 0 and another.flag != self.flag and self.a > 0:
            d = 0
            for i in range(2):
                d += (self.location[i]-another.location[i])**2
            d = d**0.5
            if d < 1:
                print("fire"+str(self.location))
                another.v -= self.k
            self.a -= 1
        
infantry_1 = infantry(flag="US",name="H",location=[0,0],free=True)
infantry_2 = infantry(flag="US",name="D",location=[2,1],free=True)
infantry_3 = infantry(flag="US",name="J",location=[1.5,1],free=True)
infantry_4 = infantry(flag="US",name="P",location=[1.2,0],free=True)
infantry_5 = infantry(flag="JAP",name="HP",location=[1.5,4])
infantry_6 = infantry(flag="JAP",name="DJ",location=[1.5,2])
infantry_7 = infantry(flag="JAP",name="HR",location=[8,6])
infantry_8 = infantry(flag="JAP",name="DE",location=[2,4])



I = [infantry_1,infantry_2,
     infantry_3,infantry_4,
     infantry_5,infantry_6,
     infantry_7,infantry_8]

US = [i for i in I if i.flag=="US"]
JPA = [i for i in I if i.flag=="JPA"]

游戏效果

 

不错的游戏

耐玩!!

下次再改进!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

River Chandler

谢谢,我会更努力学习工作的!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值