行雁书

文章 评论 浏览 29

行雁书 2025-02-21 01:47:49

您已经回答了您的问题;)

是的,您可以。

组成一个可以存储所有输入字段状态的类,并通过state -provider将其公开。

为了有效地这样做What-are-its-use-cases“> copywith 可以手动编写或生成的方法。可能的方法之一是使用 freezed

You already answered your question ;)

Yes, you can.

Make a class that will store the state of all the input fields and expose it through StateProvider.

To do it effectively you will probably need a copyWith method which can be written manually or generated. One of the possible approaches is to use freezed.

Riverpod颤音:一个屏幕的多个提供商

行雁书 2025-02-20 21:59:22

您可以使用 typing.typing.typevar typing 使用参数BOND 模拟:

from random import randint
from typing import TypeVar, Type


Self = TypeVar('Self', bound='Person')


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def get_random_person(cls: Type[Self]) -> Self:
        return cls("Random Guy", randint(18, 65))


class Player(Person):
    pass


def welcome_player(player: Player):
    print(f"Welcome {player.name}")


player: Player = Player.get_random_person()
welcome_player(player)

Python 3.11将添加 self code> self code> 但尚未正式发布。 键入 - 延迟在较旧版本中可以访问,尽管mypy会抱怨错误:变量“ typing_extensions.self”无效作为类型)。

from random import randint
from typing_extensions import Self


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def get_random_person(cls) -> Self:
        return cls("Random Guy", randint(18, 65))


class Player(Person):
    pass

def welcome_player(player: Player):
    print(f"Welcome {player.name}")


player = Player.get_random_person()
welcome_player(player)

You can use typing.TypeVar with parameter bound to simulate:

from random import randint
from typing import TypeVar, Type


Self = TypeVar('Self', bound='Person')


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def get_random_person(cls: Type[Self]) -> Self:
        return cls("Random Guy", randint(18, 65))


class Player(Person):
    pass


def welcome_player(player: Player):
    print(f"Welcome {player.name}")


player: Player = Player.get_random_person()
welcome_player(player)

Python 3.11 will add the Self type, but it has not been officially released yet. This is accessible in older versions with the typing-extensions, although mypy will complain that error: Variable "typing_extensions.Self" is not valid as a type).

from random import randint
from typing_extensions import Self


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def get_random_person(cls) -> Self:
        return cls("Random Guy", randint(18, 65))


class Player(Person):
    pass

def welcome_player(player: Player):
    print(f"Welcome {player.name}")


player = Player.get_random_person()
welcome_player(player)

如何完善python类型的提示,以提示在继承类时返回类实例的方法?

行雁书 2025-02-20 18:33:40

改变
如果event.type == pygame.quit():

如果event.type == pygame.quit:

change
if event.type == pygame.quit():
to
if event.type == pygame.QUIT:

面对这个问题:pygame.error:视频系统未初始化

行雁书 2025-02-20 11:17:04

您可以在每个节点中添加一个onclick处理程序,然后在节点视图中单击此处理程序。

在ONCLICK处理程序中的父组件中,您可以根据需要调用PreparEnode。

useEffect(() => {
setElements(
  elements.map(item => {
    ...item,
    onClick: (i) => {
      console.log(i);
      prepareNode();
    },
  })
)}, 
[]);

You can add an onClick handler to the each node, and within the node view you call this handler on click.

In the parent Component within the onClick handler you can call prepareNode as needed.

useEffect(() => {
setElements(
  elements.map(item => {
    ...item,
    onClick: (i) => {
      console.log(i);
      prepareNode();
    },
  })
)}, 
[]);

单击事件上的React函数组件中定义的呼叫函数形式的另一个函数组件-RECT.JS

行雁书 2025-02-20 09:11:25

看来,计算机中安装的spark的版本与pyspark的版本不匹配。

使用以下命令检查Spark的版本:

<path-to-spark-bin>/spark-submit --version
# example output
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 3.1.3
      /_/

现在正如示例输出所显示的那样,已安装的Spark版本为3.1.3,因此您需要通过执行同一版本安装Spark(Pyspark)的Python库(Pyspark)以下命令:

pip install pyspark==<the-version-of-your-spark>
# Example
pip install pyspark==3.1.3

It appears that the version of spark installed in your machine does not match the version of pyspark.

Check the version of your spark using the following command:

<path-to-spark-bin>/spark-submit --version
# example output
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 3.1.3
      /_/

Now as it appears from the sample output, the version of installed Spark version is 3.1.3, so you need to install the python library of spark (pyspark) with the same version by executing the following command:

pip install pyspark==<the-version-of-your-spark>
# Example
pip install pyspark==3.1.3

py4jexception:constructor org.apache.spark.sql.sparksession([[class org.apache.spark.spark.sparkcontext,class java.util.hashmap])

行雁书 2025-02-20 09:03:53

在某些国家,港口5060被ISP阻止
我了解您在虚拟机中打开了端口。
您还需要在VPS主机面板安全组中打开端口。

In some countries port 5060 is blocked by ISP
what i understand you opened the port in your virtual machine.
you need to open the port in VPS host panel security group as well.

CentOS 7开放端口开放但不起作用

行雁书 2025-02-20 00:23:18

您可以使用“从列表中的选项”,该“选项”仅允许将列表中的值设置为单元格的值,也可以将“如果“函数接受或拒绝”输入值,或者您可以使用“下拉”到做同样的事情

You can use the "options from list" which will only allow the values from the list to be set as value of the cell or you can put "if" function to accept or reject the input values or you can use "drop down" to do the same

卓越对细胞的限制

行雁书 2025-02-19 16:12:26

只需作为新用户运行任何过程。

这是新用户并关闭powershell提示的东西。
这将创建用户配置文件夹,而不会与当前会话断开您的连接。

# Name credentials
$username = 'NewUsername'
$password = 'NewProfilePassword' | ConvertTo-SecureString -AsPlainText -Force
$credential = [PSCredential]::New($username,$password)

Start-Process powershell.exe -Credential $Credential  -ArgumentList "-Command","Write-host 'Hello Profile'"

Just run any process as that new user.

Here is something that will start a powershell prompt as the new user and close it.
This will create the user profile folder without disconnecting you from the current session.

# Name credentials
$username = 'NewUsername'
$password = 'NewProfilePassword' | ConvertTo-SecureString -AsPlainText -Force
$credential = [PSCredential]::New($username,$password)

Start-Process powershell.exe -Credential $Credential  -ArgumentList "-Command","Write-host 'Hello Profile'"

“激活”使用PowerShell创建本地用户

行雁书 2025-02-19 09:01:24

问题中定义的字符串不是char **,它是带有嵌入式新线的单个C字符串。

您可以使用简单的迭代来计算其包含的行的长度:

#include <stdio.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    size_t i, start, line = 1;

    for (i = start = 0; arr[i] != '\0'; i++) {
        if (arr[i] == '\n') {
            printf("The length of line %zu is %zu.\n", line, i - start);
            start = i + 1;
            line++;
        }
    }
    /* special case if the last line does not end with a newline */
    if (i > start) {
        printf("The length of line %zu is %zu.\n", line, i - start);
    }
    return 0;
}

您可以使用strchr()strcspn()来简化循环,该 strcspn()接受一个分隔器的字符串:

#include <stdio.h>
#include <string.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p, *start;
    size_t line = 1;

    for (start = arr; (p = strchr(start, '\n')) != NULL; start = p + 1) {
        printf("The length of line %zu is %td.\n", line, p - start);
        line++;
    }
    /* special case if the last line does not end with a newline */
    if (*start) {
        printf("The length of line %zu is %zu.\n", line, strlen(start));
    }
    return 0;
}

使用strcspn()允许将特殊情况折叠到主循环中:

#include <stdio.h>
#include <string.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p = arr;
    size_t line = 1;

    while (*p != '\0') {
        size_t len = strcspn(p, "\n");
        printf("The length of line %zu is %zu.\n", line, len);
        line++;
        p += len + (p[len] != '\0');
    }
    return 0;
}

The string defined in the question is not a char **, it is a single C string with embedded newlines.

You can compute the lengths of the lines it contains with a simple iteration:

#include <stdio.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    size_t i, start, line = 1;

    for (i = start = 0; arr[i] != '\0'; i++) {
        if (arr[i] == '\n') {
            printf("The length of line %zu is %zu.\n", line, i - start);
            start = i + 1;
            line++;
        }
    }
    /* special case if the last line does not end with a newline */
    if (i > start) {
        printf("The length of line %zu is %zu.\n", line, i - start);
    }
    return 0;
}

You can simplify the loop using strchr() or strcspn(), which accepts a string of separators:

#include <stdio.h>
#include <string.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p, *start;
    size_t line = 1;

    for (start = arr; (p = strchr(start, '\n')) != NULL; start = p + 1) {
        printf("The length of line %zu is %td.\n", line, p - start);
        line++;
    }
    /* special case if the last line does not end with a newline */
    if (*start) {
        printf("The length of line %zu is %zu.\n", line, strlen(start));
    }
    return 0;
}

Using strcspn() allows for the special case to be folded into the main loop:

#include <stdio.h>
#include <string.h>

int main() {
    char arr[] = "x    xxxx\n"  // length 9
                 "x xx\n"       // length 4
                 "\n"           // length 0
                 "x  xxx\n";    // length 6
    char *p = arr;
    size_t line = 1;

    while (*p != '\0') {
        size_t len = strcspn(p, "\n");
        printf("The length of line %zu is %zu.\n", line, len);
        line++;
        p += len + (p[len] != '\0');
    }
    return 0;
}

如何在C中获得Char **的每一行的长度?

行雁书 2025-02-18 02:09:14

您可以根据订购(create_on desc)在上使用在上只能产生每组的第一行(per car_id)。然后,过滤是微不足道的。

例如:

select *
from (
  select distinct on (car_id) *
  from car_wash
  order by car_id, created_on desc
) x
where next_clean <= now() and end_at is null

You can use DISTINCT ON to produce only the first row per group (per car_id) according to an ordering (created_on DESC). Then, filtering is trivial.

For example:

select *
from (
  select distinct on (car_id) *
  from car_wash
  order by car_id, created_on desc
) x
where next_clean <= now() and end_at is null

PostgreSQL:根据条件的位置返回最新记录

行雁书 2025-02-18 01:45:39

您是在通过HTTPS/SSL访问网站吗?如果没有,那可能就是问题。 Chrome不久前对Cookie处理进行了一些更改,当SharePoint发布更新以处理它们时,它破坏了通过没有SSL登录的能力。所有最新的SharePoint更新都要求该网站由SSL托管以进行FBA登录工作。

Are you accessing your site via HTTPS/SSL? If not, that is probably the problem. Chrome made some changes to it's cookie handling a while ago, and when SharePoint released updates to handle them it broke the ability to login via FBA without SSL. All of the latest SharePoint updates require the site to be hosted with SSL for FBA login to work.

FBA在SharePoint 2013 Portal中Windows自动更新后不工作

行雁书 2025-02-17 19:34:46

根据应用程序的部署目标,您可以考虑使用组合传播Websocket事件:

联合收割机框架为随着时间的推移处理值提供了声明的Swift API。这些值可以代表多种异步事件。将声明的发布者结合在一起,以揭示可以随时间变化的值,并从发布者那里接收这些值。

https://develoveling.apple.apple.apple.com/documentation/combine

Depending on the deployment target of your app, you could consider using Combine to propagate websocket events:

The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.

https://developer.apple.com/documentation/combine

Swift-使用NotificationCenter观察价值变化真的不好吗?

行雁书 2025-02-16 18:41:43

尝试此软件包
https://pub.dev/packages/packages/flutter_background_background

请求在后台运行,

bool hasPermissions = await FlutterBackground.hasPermissions;

然后在后台运行它背景使用

bool success = await FlutterBackground.enableBackgroundExecution();

请注意,此功能仅在Android中起作用

Try this package
https://pub.dev/packages/flutter_background

Request permission to run in background

bool hasPermissions = await FlutterBackground.hasPermissions;

Then to run it in background use

bool success = await FlutterBackground.enableBackgroundExecution();

Please note this works only in android

如何使我的Flutter应用程序在背景中运行而不会停止?

行雁书 2025-02-16 11:49:59

最好的解决方案可能是将您的listCreator()函数用作client> client function(您称为启动器函数)。从listCreator()中,您可以生成列表并将其传递到andestrator。最后,从编排器中,将列表传递到活动函数并运行它。

代码草图:

# inside the client function call the orchestrato
client = df.DurableOrchestrationClient(starter)
instance_id = await client.start_new("yourorchestrator", None,  [1,2,3,4])

#inside orchestrator
data = context.get_input()
context.call_activity("activity", data)

probably the best solution is to use your listcreator() function as Client function (the function you called as starter function). From the listcreator(), you can generate the list and pass it to the orchestrator. Finally from the orchestrator, pass the list to the activity function and run it.

A sketch of code:

# inside the client function call the orchestrato
client = df.DurableOrchestrationClient(starter)
instance_id = await client.start_new("yourorchestrator", None,  [1,2,3,4])

#inside orchestrator
data = context.get_input()
context.call_activity("activity", data)

如何将列表发送到Azure耐用功能

行雁书 2025-02-16 10:59:25

问题在于,当您通过所有网格正方形进行发作时,可以通过查找播放器找到您不断发挥作用的播放器。因此,找到并移动了玩家,然后搜索带有玩家的下一个区域,因此再次移动(直到到达墙壁)。该修复程序只是在按键功能中添加返回语句。我将在此处添加一个可行的代码块。

let grid = [];
var player;
var w = 40;
var dir,
  nivel = 1;
let up, down, right, left;
function setup() {
  createCanvas(400, 400);
  for (var i = 0; i < width / w; i++) {
    grid[i] = [];
    for (var j = 0; j < height / w; j++) {
      grid[i][j] = new Cell(i * w, j * w, i, j);
    }
  }
  up = createButton("⬆");
  up.position(70, height + 10);
  down = createButton("⬇");
  down.position(70, height + 70);
  right = createButton("➡");
  right.position(130, height + 40);
  left = createButton("⬅");
  left.position(10, height + 40);
  reset = createButton("RESET")
  reset.position(260, height + 40)
  level(nivel);
}

function draw() {
  background(220);
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      grid[i][j].show();
    }
  }
  up.mousePressed(botonUp);
  down.mousePressed(botonDown);
  right.mousePressed(botonRight);
  left.mousePressed(botonLeft);
  reset.mousePressed(resetear)
}

function resetear() {
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
        grid[i][j].reset();
    }
  }
}

function botonUp() {
  dir = 0;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}
function botonRight() {
  dir = 1;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}
function botonDown() {
  dir = 2;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}

function botonLeft() {
  dir = 3;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}

function keyPressed() {
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (keyCode == 82) {
        resetear();
      }
      if (grid[i][j].player) {
        if (keyCode === UP_ARROW) {
          dir = 0;
        }
        if (keyCode === RIGHT_ARROW) {
          dir = 1;
        }
        if (keyCode === DOWN_ARROW) {
          dir = 2;
        }
        if (keyCode === LEFT_ARROW) {
          dir = 3;
        }
        grid[i][j].move();
        return;
      }
    }
  }
}

function level(n) {
  for (var i = 0; i < width / w; i++) {
    grid[0][i].wall = true;
    grid[width / w - 1][i].wall = true;
  }
  for (var j = 0; j < height / w; j++) {
    grid[j][0].wall = true;
    grid[j][height / w - 1].wall = true;
  }
  if (n == 1) {
    grid[4][4].caja = true;
    grid[4][7].player = true;
    grid[2][4].wall = true;
    grid[3][5].wall = true;
    grid[7][6].spot = true;
  } else if (n == 2) {
  }
}

class Cell {
  constructor(x, y, i, j) {
    this.x = x;
    this.y = y;
    this.i = i;
    this.j = j;
    this.w = w;
    this.caja = false;
    this.player = false;
    this.wall = false;
    this.spot = false;
    this.time = 0;
  }

  show() {
    if (this.caja) {
      fill("brown");
      stroke(57, 0, 8);
      strokeWeight(3);
      rect(this.x + 2.5, this.y + 2.5, w - 5, w - 5);
      line(this.x + 2.5, this.y + 2.5, this.x + w - 2.5, this.y + w - 2.5);
      line(this.x + w - 2.5, this.y + 2.5, this.x + 2.5, this.y + w - 2.5);
    } else {
      fill(255);
      strokeWeight(1);
      stroke(0, 50);
      rect(this.x, this.y, w, w);
    }
    if (this.player) {
      ellipseMode(CORNER);
      fill(0);
      circle(this.x + 3, this.y + 3, w - 6);
    } else if (this.spot) {
      if (this.time % 45 > 15) {
        fill(104, 252, 3);
        noStroke();
        circle(this.x + 2, this.y + 2, w - 4);
      }
    } else if (this.wall) {
      fill(130);
      rect(this.x, this.y, w, w);
    }
    if (this.caja && this.spot) nivel++;
    this.time++;
    if (this.time == 4500) {
      this.time = 0;
    }
  }

  move() {
    if (dir == 0 && !grid[this.i][this.j - 1].wall) {
      if (grid[this.i][this.j - 1].caja) {
        if (!grid[this.i][this.j - 2].wall && !grid[this.i][this.j - 2].caja) {
          this.player = false;
          grid[this.i][this.j - 1].player = true;
          grid[this.i][this.j - 2].caja = true;
          grid[this.i][this.j - 1].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i][this.j - 1].player = true;
      }
    }
    
    if (dir == 1 && !grid[this.i + 1][this.j].wall) {
      if (grid[this.i + 1][this.j].caja) {
        if (!grid[this.i + 2][this.j].wall && !grid[this.i + 2][this.j].caja) {
          this.player = false;
          grid[this.i + 1][this.j].player = true;
          grid[this.i + 2][this.j].caja = true;
          grid[this.i + 1][this.j].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i + 1][this.j].player = true;
      }
    }
    
    if (dir == 2 && !grid[this.i][this.j + 1].wall) {
      if (grid[this.i][this.j + 1].caja) {
        if (!grid[this.i][this.j + 2].wall && !grid[this.i][this.j + 2].caja) {
          this.player = false;
          grid[this.i][this.j + 1].player = true;
          grid[this.i][this.j + 2].caja = true;
          grid[this.i][this.j + 1].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i][this.j + 1].player = true;
      }
    }
    if (dir == 3 && !grid[this.i - 1][this.j].wall) {
      if (grid[this.i - 1][this.j].caja) {
        if (!grid[this.i - 2][this.j].wall && !grid[this.i - 2][this.j].caja) {
          this.player = false;
          grid[this.i - 1][this.j].player = true;
          grid[this.i - 2][this.j].caja = true;
          grid[this.i - 1][this.j].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i - 1][this.j].player = true;
      }
    }
  }

  reset() {
    this.caja = false;
    this.player = false;
    this.wall = false;
    this.spot = false;
    this.time = 0;
    level(nivel);
  }
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>

我还开始了一个真正简化的新移动功能:

move() {
  
    let m = 0;
  
    switch(dir){
        
      case 0: m = -1; break;
      case 1: m =  1; break;
      case 2: m =  1; break;
      case 3: m = -1; break;
      default: throw new Error("dir not 0 - 3");
      
    }
  
    let d = (dir == 0 || dir == 2);
  
    if (!grid[this.i + !d * m][this.j + d * m].wall) {
      if (grid[this.i + !d * m][this.j + d * m].box) {
        if (!grid[this.i + !d * m * 2][this.j + d * m * 2].wall && !grid[this.i + !d * m * 2][this.j + d * m * 2].box) {
          this.player = false;
          grid[this.i + !d * m][this.j + d * m].player = true;
          grid[this.i + !d * m * 2][this.j + d * m * 2].box = true;
          grid[this.i + !d * m][this.j + d * m].box = false;
        }
      } else {
        this.player = false;
        grid[this.i + !d * m][this.j + d * m].player = true;
      }
    }
  
  }

尽管我无法使盒子相撞工作。我不太了解它应该如何工作(在代码中)。

The problem is the when you itterate throught all the grid squares to find the player cell you keep itterating through finding the player. So the the player is found and moved and then you search the next area that has the player, so move it again (until it reaches the walls). The fix is just adding a return statement in the keypressed function. I'll add a workable code block here.

let grid = [];
var player;
var w = 40;
var dir,
  nivel = 1;
let up, down, right, left;
function setup() {
  createCanvas(400, 400);
  for (var i = 0; i < width / w; i++) {
    grid[i] = [];
    for (var j = 0; j < height / w; j++) {
      grid[i][j] = new Cell(i * w, j * w, i, j);
    }
  }
  up = createButton("⬆");
  up.position(70, height + 10);
  down = createButton("⬇");
  down.position(70, height + 70);
  right = createButton("➡");
  right.position(130, height + 40);
  left = createButton("⬅");
  left.position(10, height + 40);
  reset = createButton("RESET")
  reset.position(260, height + 40)
  level(nivel);
}

function draw() {
  background(220);
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      grid[i][j].show();
    }
  }
  up.mousePressed(botonUp);
  down.mousePressed(botonDown);
  right.mousePressed(botonRight);
  left.mousePressed(botonLeft);
  reset.mousePressed(resetear)
}

function resetear() {
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
        grid[i][j].reset();
    }
  }
}

function botonUp() {
  dir = 0;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}
function botonRight() {
  dir = 1;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}
function botonDown() {
  dir = 2;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}

function botonLeft() {
  dir = 3;
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (grid[i][j].player) {
        grid[i][j].move();
      }
    }
  }
}

function keyPressed() {
  for (var i = 0; i < width / w; i++) {
    for (var j = 0; j < height / w; j++) {
      if (keyCode == 82) {
        resetear();
      }
      if (grid[i][j].player) {
        if (keyCode === UP_ARROW) {
          dir = 0;
        }
        if (keyCode === RIGHT_ARROW) {
          dir = 1;
        }
        if (keyCode === DOWN_ARROW) {
          dir = 2;
        }
        if (keyCode === LEFT_ARROW) {
          dir = 3;
        }
        grid[i][j].move();
        return;
      }
    }
  }
}

function level(n) {
  for (var i = 0; i < width / w; i++) {
    grid[0][i].wall = true;
    grid[width / w - 1][i].wall = true;
  }
  for (var j = 0; j < height / w; j++) {
    grid[j][0].wall = true;
    grid[j][height / w - 1].wall = true;
  }
  if (n == 1) {
    grid[4][4].caja = true;
    grid[4][7].player = true;
    grid[2][4].wall = true;
    grid[3][5].wall = true;
    grid[7][6].spot = true;
  } else if (n == 2) {
  }
}

class Cell {
  constructor(x, y, i, j) {
    this.x = x;
    this.y = y;
    this.i = i;
    this.j = j;
    this.w = w;
    this.caja = false;
    this.player = false;
    this.wall = false;
    this.spot = false;
    this.time = 0;
  }

  show() {
    if (this.caja) {
      fill("brown");
      stroke(57, 0, 8);
      strokeWeight(3);
      rect(this.x + 2.5, this.y + 2.5, w - 5, w - 5);
      line(this.x + 2.5, this.y + 2.5, this.x + w - 2.5, this.y + w - 2.5);
      line(this.x + w - 2.5, this.y + 2.5, this.x + 2.5, this.y + w - 2.5);
    } else {
      fill(255);
      strokeWeight(1);
      stroke(0, 50);
      rect(this.x, this.y, w, w);
    }
    if (this.player) {
      ellipseMode(CORNER);
      fill(0);
      circle(this.x + 3, this.y + 3, w - 6);
    } else if (this.spot) {
      if (this.time % 45 > 15) {
        fill(104, 252, 3);
        noStroke();
        circle(this.x + 2, this.y + 2, w - 4);
      }
    } else if (this.wall) {
      fill(130);
      rect(this.x, this.y, w, w);
    }
    if (this.caja && this.spot) nivel++;
    this.time++;
    if (this.time == 4500) {
      this.time = 0;
    }
  }

  move() {
    if (dir == 0 && !grid[this.i][this.j - 1].wall) {
      if (grid[this.i][this.j - 1].caja) {
        if (!grid[this.i][this.j - 2].wall && !grid[this.i][this.j - 2].caja) {
          this.player = false;
          grid[this.i][this.j - 1].player = true;
          grid[this.i][this.j - 2].caja = true;
          grid[this.i][this.j - 1].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i][this.j - 1].player = true;
      }
    }
    
    if (dir == 1 && !grid[this.i + 1][this.j].wall) {
      if (grid[this.i + 1][this.j].caja) {
        if (!grid[this.i + 2][this.j].wall && !grid[this.i + 2][this.j].caja) {
          this.player = false;
          grid[this.i + 1][this.j].player = true;
          grid[this.i + 2][this.j].caja = true;
          grid[this.i + 1][this.j].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i + 1][this.j].player = true;
      }
    }
    
    if (dir == 2 && !grid[this.i][this.j + 1].wall) {
      if (grid[this.i][this.j + 1].caja) {
        if (!grid[this.i][this.j + 2].wall && !grid[this.i][this.j + 2].caja) {
          this.player = false;
          grid[this.i][this.j + 1].player = true;
          grid[this.i][this.j + 2].caja = true;
          grid[this.i][this.j + 1].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i][this.j + 1].player = true;
      }
    }
    if (dir == 3 && !grid[this.i - 1][this.j].wall) {
      if (grid[this.i - 1][this.j].caja) {
        if (!grid[this.i - 2][this.j].wall && !grid[this.i - 2][this.j].caja) {
          this.player = false;
          grid[this.i - 1][this.j].player = true;
          grid[this.i - 2][this.j].caja = true;
          grid[this.i - 1][this.j].caja = false;
        }
      } else {
        this.player = false;
        grid[this.i - 1][this.j].player = true;
      }
    }
  }

  reset() {
    this.caja = false;
    this.player = false;
    this.wall = false;
    this.spot = false;
    this.time = 0;
    level(nivel);
  }
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>

I also started on a new move function that is REALLY simplified:

move() {
  
    let m = 0;
  
    switch(dir){
        
      case 0: m = -1; break;
      case 1: m =  1; break;
      case 2: m =  1; break;
      case 3: m = -1; break;
      default: throw new Error("dir not 0 - 3");
      
    }
  
    let d = (dir == 0 || dir == 2);
  
    if (!grid[this.i + !d * m][this.j + d * m].wall) {
      if (grid[this.i + !d * m][this.j + d * m].box) {
        if (!grid[this.i + !d * m * 2][this.j + d * m * 2].wall && !grid[this.i + !d * m * 2][this.j + d * m * 2].box) {
          this.player = false;
          grid[this.i + !d * m][this.j + d * m].player = true;
          grid[this.i + !d * m * 2][this.j + d * m * 2].box = true;
          grid[this.i + !d * m][this.j + d * m].box = false;
        }
      } else {
        this.player = false;
        grid[this.i + !d * m][this.j + d * m].player = true;
      }
    }
  
  }

Although I can't get the box collision to work. I don't quite understand how it's suppose to work (in the code).

移动功能无法正常运行

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文