简体   繁体   English

p5 中的多个画布

[英]Multiple canvases in p5

I'm writing code for a neuro evolution project that should learn to play the game snake.我正在为一个应该学习玩蛇游戏的神经进化项目编写代码。 I'm using js and p5.js.我正在使用 js 和 p5.js。 I want to have 10 game canvases parallel and each game should individually play the game.我想让 10 个游戏画布平行,每个游戏都应该单独玩游戏。

What I'm struggling with is the multiple canvases part.我正在努力解决的是多个画布部分。 When I use this:当我使用这个时:

 let screens = 10; for(let k = 0; k < screens;k++){ var t = function( p ) { p.setup = function() { p.createCanvas(200, 200); let x = Math.random()*100; console.log(x); snake = new Snake(x,50); food = new Food(); }; p.draw = function() { p.background(0); snake.placeSnake(p); snake.think(p); if(counter % 50 == 0){ snake.moveSnake(); } if(snake.offScreen(p)){ //kill snake } } food.placeFood(p); if(food.hitsFood(snakes)){ food.generateFood(p); } counter++; }; }; var myp5 = new p5(t); }

All the screens are identical to the last one spawned.所有屏幕都与最后一个生成的屏幕相同。 How would I do different so that each canvas is unique?我将如何做不同的事情以使每个 canvas 都是独一无二的? thanks in advance.提前致谢。

This might be a variable scope issue (untested, as I don't have all your code).这可能是一个变量 scope 问题(未经测试,因为我没有你的所有代码)。

Try adding let snake, food, counter = 0;尝试添加let snake, food, counter = 0; inside the var t= function(p){} defintion, like this:var t= function(p){}定义中,如下所示:

    let screens = 10;
    for(let k = 0; k < screens ;k++){
        
      var t = function( p ) { 
        let snake, food, counter = 0;    // declare here as shared between setup and draw
        p.setup = function() {
            p.createCanvas(200, 200);
            let x = Math.random()*100;
            console.log(x);
            snake = new Snake(x,50);
            food = new Food();
        };
      
        p.draw = function() {
            p.background(0);
            snake.placeSnake(p);
            snake.think(p);

            if(counter % 50 == 0){
                snake.moveSnake();
            }
            if(snake.offScreen(p)){
                //kill snake
            }
            //}

            food.placeFood(p);

            if(food.hitsFood(snakes)){
                food.generateFood(p);
            }
            counter++;
        };
      };

      var myp5 = new p5(t);
    }

The explanation for this would be that, if you don't declare the variables, they are implicitly declared in the global scope, and so every snake points to the same snake (and likewise with food).对此的解释是,如果您不声明变量,它们会在全局 scope 中隐式声明,因此每条蛇都指向同一条蛇(同样还有食物)。 The canvases are unique, but the snake and food variables for each one only point to the last snake or food created.画布是独一无二的,但每个画布的蛇和食物变量仅指向最后创建的蛇或食物。

(Depending on the context, it might be worth preventing this kind of problem from occurring by using strict mode , which does not allow undeclared variables). (根据上下文,可能值得通过使用不允许未声明的变量的严格模式来防止此类问题的发生)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM