如何在一个选项卡中将程序分成 类
How to separate program into classes after making it in one tab
我使用 java 语言创建了一个简单的处理游戏,但是我在一个选项卡中创建了所有游戏,现在我需要将所有内容放入它自己的 class 中,这样它看起来井井有条并且是尽管我在努力将我的代码放入单独的 classes 并让它们工作之前已经创建了 classes,但更易于阅读。下面是我的 classes
之一的示例
int pixelsize = 5; // size of pixels on screen
int gridsize = pixelsize * 8 + 5;
Player player;
ArrayList enemies = new ArrayList();
ArrayList bullets = new ArrayList();
int direction = 1; // where the wave moves to
boolean incy = false;
void setup() {
background(0);
fill(255);
size(800, 800);
}
void draw() {
background(0);
player.draw();
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = (Bullet) bullets.get(i);
bullet.draw();
}
void createEnemies() {
for (int i = 0; i < width/gridsize/2; i++) {
for (int j = 0; j <= 4; j++) {
enemies.add(new Enemy(i*gridsize, j*gridsize + 20 ));
}
}
}
第 1 步: 单击此按钮:
第 2 步: 单击 新标签 选项。为您的选项卡指定一个与您希望在该选项卡中显示的 class 名称相匹配的名称。我将使用 Ball
:
第 3 步: 在该选项卡中为您的 class 编写代码:
class Ball {
float x;
float y;
void setPosition(float x, float y) {
this.x = x;
this.y = y;
}
void drawBall() {
fill(ballColor);
ellipse(x, y, 25, 25);
}
}
第 4 步: 在第一个选项卡中编写草图的代码:
Ball ball = new Ball();
color ballColor = #00ff00;
void setup(){
size(500, 500);
}
void draw(){
background(0);
ball.setPosition(mouseX, mouseY);
ball.drawBall();
}
请注意,此草图可以使用其他选项卡中的 class,而 class 可以使用草图选项卡中定义的函数或变量。
可以在 the Processing reference 中找到更多信息。
我使用 java 语言创建了一个简单的处理游戏,但是我在一个选项卡中创建了所有游戏,现在我需要将所有内容放入它自己的 class 中,这样它看起来井井有条并且是尽管我在努力将我的代码放入单独的 classes 并让它们工作之前已经创建了 classes,但更易于阅读。下面是我的 classes
之一的示例int pixelsize = 5; // size of pixels on screen
int gridsize = pixelsize * 8 + 5;
Player player;
ArrayList enemies = new ArrayList();
ArrayList bullets = new ArrayList();
int direction = 1; // where the wave moves to
boolean incy = false;
void setup() {
background(0);
fill(255);
size(800, 800);
}
void draw() {
background(0);
player.draw();
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = (Bullet) bullets.get(i);
bullet.draw();
}
void createEnemies() {
for (int i = 0; i < width/gridsize/2; i++) {
for (int j = 0; j <= 4; j++) {
enemies.add(new Enemy(i*gridsize, j*gridsize + 20 ));
}
}
}
第 1 步: 单击此按钮:
第 2 步: 单击 新标签 选项。为您的选项卡指定一个与您希望在该选项卡中显示的 class 名称相匹配的名称。我将使用 Ball
:
第 3 步: 在该选项卡中为您的 class 编写代码:
class Ball {
float x;
float y;
void setPosition(float x, float y) {
this.x = x;
this.y = y;
}
void drawBall() {
fill(ballColor);
ellipse(x, y, 25, 25);
}
}
第 4 步: 在第一个选项卡中编写草图的代码:
Ball ball = new Ball();
color ballColor = #00ff00;
void setup(){
size(500, 500);
}
void draw(){
background(0);
ball.setPosition(mouseX, mouseY);
ball.drawBall();
}
请注意,此草图可以使用其他选项卡中的 class,而 class 可以使用草图选项卡中定义的函数或变量。
可以在 the Processing reference 中找到更多信息。