试图将开放处理代码转换为pj.5s
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了试图将开放处理代码转换为pj.5s相关的知识,希望对你有一定的参考价值。
我不知道如何将其更改为p5.js。只是尝试进行转化。请帮助我让此代码在p5.js中工作。
我知道这是旧代码,p5.js具有不同的规则。也许该阵列无法正常工作。我不知道。
Myself myself;
ArrayList<Enemy> enemies;
`enter code here`ArrayList<Enemy> enemies;
ArrayList<Bullet> myBullets;
ArrayList<Bullet> eneBullets;
void setup()
size(640, 640);
rectMode(CENTER);
myself = new Myself();
enemies = new ArrayList<Enemy>();
myBullets = new ArrayList<Bullet>();
eneBullets = new ArrayList<Bullet>();
void draw()
background(0);
myself.display();
for(Enemy enemy: enemies)
enemy.display();
for(Bullet bullet: myBullets)
bullet.display();
for(Bullet bullet: eneBullets)
bullet.display();
myself.update();
ArrayList<Enemy> nextEnemies = new ArrayList<Enemy>();
for(Enemy enemy: enemies)
enemy.update();
if(!enemy.isDead)
nextEnemies.add(enemy);
enemies = nextEnemies;
ArrayList<Bullet> nextMyBullets = new ArrayList<Bullet>();
for(Bullet bullet: myBullets)
bullet.update();
if(!bullet.isDead)
nextMyBullets.add(bullet);
myBullets = nextMyBullets;
ArrayList<Bullet> nextEneBullets = new ArrayList<Bullet>();
for(Bullet bullet: eneBullets)
bullet.update();
if(!bullet.isDead)
nextEneBullets.add(bullet);
eneBullets = nextEneBullets;
if(random(1) < 0.02)
enemies.add(new Enemy());
class Myself
PVector loc;
float size;
int coolingTime;
boolean isDead;
Myself()
size = 25;
loc = new PVector(width / 2, height - size / 2 - 10);
coolingTime = 0;
isDead = false;
void display()
if(isDead)
fill(255, 255, 0);
stroke(0, 255, 0);
else
noFill();
stroke(0, 255, 0);
rect(loc.x, loc.y, size, size);
void update()
isDead = false;
float dmx = mouseX - loc.x;
dmx = constrain(dmx, -5, 5);
loc.x += dmx;
coolingTime++;
if(mousePressed && coolingTime >= 10)
myBullets.add(new Bullet());
coolingTime = 0;
for(Bullet b: eneBullets)
if((loc.x - size / 2 <= b.loc.x && b.loc.x <= loc.x + size / 2)
&& (loc.y - size / 2 <= b.loc.y && b.loc.y <= loc.y + size / 2))
isDead = true;
b.isDead = true;
break;
for(Enemy e: enemies)
if(abs(loc.x - e.loc.x) < size / 2 + e.size / 2 && abs(loc.y - e.loc.y) < size / 2 + e.size / 2)
isDead = true;
e.isDead = true;
break;
class Bullet
PVector loc;
float vel;
boolean isMine;
boolean isDead;
Bullet()
loc = new PVector(myself.loc.x, myself.loc.y);
vel = -10;
isMine = true;
Bullet(Enemy enemy)
loc = new PVector(enemy.loc.x, enemy.loc.y);
vel = 5;
isMine = false;
void display()
if(isMine)
stroke(0, 255, 0);
else
stroke(255, 0, 0);
line(loc.x, loc.y, loc.x, loc.y + vel);
void update()
loc.y += vel;
if((vel > 0 && loc.y > height) || (vel < 0 && loc.y < 0))
isDead = true;
class Enemy
PVector loc;
float vel;
float size;
int coolingTime;
boolean isDead;
Enemy()
size = 25;
loc = new PVector(random(size / 2, width - size / 2), -size / 2);
vel = 3;
coolingTime = int(random(60));
isDead = false;
void display()
noFill();
stroke(255, 0, 0);
rect(loc.x, loc.y, size, size);
void update()
loc.y += vel;
if(loc.y > height)
isDead = true;
coolingTime++;
if(coolingTime >= 60)
eneBullets.add(new Bullet(this));
coolingTime = 0;
for(Bullet b: myBullets)
if((loc.x - size / 2 <= b.loc.x && b.loc.x <= loc.x + size / 2)
&& (loc.y - size / 2 <= b.loc.y && b.loc.y <= loc.y + size / 2))
isDead = true;
b.isDead = true;
break;
如果您能告诉我需要更改或修复的部件,将会非常感谢您。
答案
您不应该尝试逐行翻译代码。相反,您需要退后一步,了解程序的功能,然后以目标语言实现该程序。
它们的处理语法(基于Java)与p5.js的语法(基于javascript)非常不同。
而不是试图“转换”代码,您需要考虑代码实际做什么。然后从p5.js中的新程序开始,然后使用p5.js进行操作。并不总是要逐行转换。
以上是关于试图将开放处理代码转换为pj.5s的主要内容,如果未能解决你的问题,请参考以下文章
如何在python中解压12个字节? php代码可用!试图将其转换为 python