gpt4 book ai didi

Python OOP - 足球模拟

转载 作者:太空宇宙 更新时间:2023-11-03 15:29:55 27 4
gpt4 key购买 nike

我是 OOP 的新手。我想模拟一场足球比赛。如何访问 Player/Offender/Defender 类中的 Play 实例变量?如果其他结构更好,请帮助。

class Player:
def __init__(self, x, y):
self.x = x
self.y = y

def move_to(self, x, y):
self.x = (self.x + x) / 2
self.y = (self.y + y) / 2

## Loop through Play.Players to find nearest. How do I access Play.Players?
def nearest(self):
return nearest

class Offender(Player):
def __init__(self, x, y):
super().__init__(x, y)

# Move to ball.
def strategy():
ball_x = Play.ball_x # how do I access Play.ball_x
ball_y = Play.ball_y # how do I access Play.ball_y
self.move_to(ball_x, ball_y)

class Defender(Player):
def __init__(self, x, y):
super().__init__(x, y)

# Move to nearest player
def strategy(self):
nearest = self.nearest()
self.move_to(nearest)

class Play:
def __init__(self, offense_players, defense_players, ball_x, ball_y):
self.offense = [Offender(player) for player in offense_players]
self.defense = [Defender(player) for player in defense_players]
self.players = self.offense + self.defense
self.ball_x = ball_x
self.ball_y = ball_y

def simulate(self):
while True:
for player in self.players:
player.strategy()

if __name__ == "__main__":
Play().simulate()

我没有使用 OffenderDefender 类,而是为每个位置设置一个类,即 Striker(Player)Midfielder( Player), Goalie(Player) 等,这就是为什么我想将他们各自的 strategy 存储在他们的类中而不是 播放类。

最佳答案

不确定这对您有多大帮助,因为实现是用 C++ 实现的

您可以查看我的实现以了解类似的问题陈述 https://github.com/rimpo/footballcpp

要了解为上述机器人编写的街机游戏框架实现,请查看 http://richard-shepherd.github.io/coding-world-cup/index.html

关于Python OOP - 足球模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58652458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com