微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我怎样才能阻止乌龟重复相同的动作?

如何解决我怎样才能阻止乌龟重复相同的动作?

我试图创建吃豆人游戏并为敌人设置随机移动。 但是当我运行它时。它重复相同的动作,我怎样才能阻止敌人做出相同的动作并让敌人在地图上四处移动。 请解决我的问题并告诉我为什么会发生这种情况。 """"""""按"空格键"开始游戏""""""""

const Shop = ({ Products,Cart }) => {
  const [cart,setCart] = useState([]);

  const addToCart = (item: IProduct) => {
    setCart([...cart,item]);
  };

  const removeFromCart = (item: IProduct) => {
    const itemWillBeRemoved = cart.find(e => e.id === item.id);
    const index = cart.indexOf(itemWillBeRemoved);
    const newCart = [...cart];
    newCart.splice(index,1);
    setCart(newCart);
  };

  const items = [
    {
      id: 1,name: "Product One",price: 20
    },{
      id: 2,name: "Product Two",price: 56
    },{
      id: 3,name: "Product Three",price: 13
    }
  ];

  return (
    <div>
      <Products items={items} addToCart={addToCart} />
      <Cart items={cart} removeFromCart={removeFromCart} />
    </div>
  );
};

解决方法

我终于找到了解决方案

def computermove(game):
    for i in game.enemy:
        normal_move_logic(game.enemy[i],game)


def normal_move_logic(enemy_in,game_in):
    rn = 0
    if enemy_in.last_dir is not None and enemy_in.last_key is not None:
        if enemy_in.last_key in game_in.wall:
            random.seed(time.time())
            rn = random.randint(0,3)
        else:
            rn = enemy_in.last_key
    else:
        random.seed(time.time())
        rn = random.randint(0,3)
    if rn == 0:
        enemy_in.go_up(game_in)
    elif rn == 1:
        enemy_in.go_down(game_in)
    elif rn == 2:
        enemy_in.go_left(game_in)
    else:
        enemy_in.go_right(game_in)

并稍微改变了 Enemy 类

class Enemy_creator(Turtle):
    def __init__(self,row,col,sprite,colorcode,width,height):
        super().__init__(shape=sprite)
        self.row = row
        self.col = col

        self.color(colorcode)
        self.shapesize(width,height,3)
        self.penup()
        self.speed('slowest')
        self.goto(self.coords(row,col))

        self.last_key = None
        self.last_dir = None

    @staticmethod
    def coords(row,col):
        x = col * 25 - 250
        y = 137.5 - row * 25

        return x,y

    @staticmethod
    def inv_coords(x,y):
        col = round((x + 250) / 25)
        row = round((137.5 - y) / 25)

        return row,col

    def go_left(self,game):
        global cruns,notout
        if cruns == False:
            cruns = True
            self.game = game
            position = self.xcor() - 25,self.ycor()

            key = self.inv_coords(*position)
            if position == (game.player[9,9].xcor(),game.player[9,9].ycor()):
                screen.clear()
                notout = False
            if key not in game.wall:
                self.goto(self.coords(*key))
            self.last_key = key
            self.last_dir = 2

            cruns = False

    def go_right(self,notout

        if cruns == False:
            cruns = True
            self.game = game

            position = self.xcor() + 25,9].ycor()):
                screen.clear()
                notout = False

            if key not in game.wall:
                self.goto(self.coords(*key))

            self.last_key = key
            self.last_dir = 3
            cruns = False

    def go_up(self,notout

        if cruns == False:
            cruns = True
            self.game = game

            position = self.xcor(),self.ycor() + 25

            key = self.inv_coords(*position)
            if position == (game.player[9,9].ycor()):
                screen.clear()
                notout = False

            if key not in game.wall:
                self.goto(self.coords(*key))

            self.last_key = key
            self.last_dir = 0
            cruns = False

    def go_down(self,self.ycor() - 25

            key = self.inv_coords(*position)
            if position == (game.player[9,9].ycor()):
                screen.clear()
                notout = False

            if key not in game.wall:
                self.goto(self.coords(*key))
            self.last_key = key
            self.last_dir = 1
            cruns = False

而且效果很好!

,

看起来像

def computermove(game):
    for i in game.enemy:
        a=game.enemy[i]
        random.choice([game.enemy[i].go_up(game),game.enemy[i].go_down(game),game.enemy[i].go_left(game),game.enemy[i].go_right(game)])
    

运行每个移动选项一次(例如 game.enemy[i].go_up(game)),收集所有返回值(都是 None),然后从四个 None 值的列表中随机选择一个 None ;-)

也许试试

movement = random.choice([a.go_up,a.go_down,a.go_left,a.go_right])
movement(game)

让你先随机选择方法,然后执行。

,

插入

random.seed()

在你之前

random.choice([game.enemy[i].......

命令将随机发生器置于随机状态。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。