处理 3.06b 合并程序
Processing 3.06b merging programs
我一直在处理一些代码并合并我创建的两个程序,但我知道我遗漏了很多信息,因为我有很多错误,但我似乎无法修复错误。下面我包含了已分成 classes 的整个代码。我正在制作 space 入侵者的版本。
主要class:
Bullets [] bullets = new Bullets[10];
Player player = new Player();
Boolean keyLftPressed = false, keyRghtPressed = false;
//Enemies[] enemies = new Enemies();
int state;
String gameLevel = "Main menu";
Boolean startTime = false;
void setup() {
for(int i=0; i<bullets.length; i++)
{
// if(i%2==0)
// bullets[i] = new Bullets();
// else
// bullets[i] = new Bullets(i);
}
size(800, 600);
state = 0;
}
void draw() {
background(255);
gameState();
player1.display();
movePlayer1();
handleEnemies();
handleBullets();
gamewon();
}
void gameState() {
if (gameLevel == "Main menu") {
background(0);
fill(255, 255, 255);
rect(270, 270, 280, 50, 20, 20, 20, 20);
//Draws rectangle for play game
fill(0);
textSize(30);
text("Play Game", 330, 305);
fill(255, 0, 0);
textSize(50);
text("Space Invaders", 220, 120);
if (mousePressed && mouseX > 250 && mouseX < 250 + 280 && mouseY > 270 && mouseY < 270 + 50)
{
gameLevel = "Level 1";
}
} else if (gameLevel == "Level 1")
{
background (255, 2555 , 255);
}
}
Bullets Class:
class Bullets {
class Bullet {
float x, y;
float velocity;
Bullet(float x, float y, float velocity) {
this.x = x;
this.y = y;
this.velocity = velocity;
}
void display(){
fill(80);
rect(this.x, this.y, 5,15);
}
void move(){
this.y+=this.velocity;
if (this.y > height || this.y < 0){
bullets.remove(this);
}
}
Class Enemies:
class Enemies {
float x, y;
float velocity;
Enemy(float x, float y) {
this.x = x;
this.y = y;
this.velocity = 3;
}
void display() {
fill(0,255,0);
ellipse(this.x, this.y, 30, 30);
noFill();
}
void move() {
this.x+=this.velocity;
if (this.x > width*.9) {
this.x = width*.9;
this.velocity *= -1;
this.y+=30;
}
if (this.x < width*.1) {
this.velocity*=-1;
this.x = width*.1;
this.y+=30;
}
}
void hitCheck() {
for (int i = 0; i < bullets.size(); i++){
Bullet b = (Bullet) bullets.get(i);
float distBetween = dist(b.x, b.y, this.x, this.y);
if (distBetween < 15 && b.velocity < 0){
score++;
enemies.remove(this);
float x = width*.1 + i%numCol*50;
float y = 60 + int(i/numCol)*60 ;
enemies.add(new Enemy(x, y));
}
}
}
}
Class Player:
class Player {
void movePlayer1() {
if (keyLftPressed) {
player1.x -=10;
}
if (keyRghtPressed) {
player1.x +=10;
}
}
void keyPressed() {
if (keyCode == LEFT) {
keyLftPressed = true;
}
else {
if (keyCode == RIGHT) {
keyRghtPressed = true;
}
else {
if (key == 'f') {
player1.shoot();
}
}
}
}
void keyReleased() {
if (keyCode == LEFT) {
keyLftPressed = false;
}
else {
if (keyCode == RIGHT) {
keyRghtPressed = false;
}
}
}
Class Score:
void gameFinish() {
{
for (int i = 0; i < 3; i++)
{
fill(color(255,0,0));
fill(255, 0, 0);
textAlign(CENTER);
text("Game over", width/2, height/2 - 50);
text(" Final score : "+ score, width/2, height/2 + 50);
}
}
}
}
void gamewon()
{
if (score == 10)
{
background(0);
fill(color(255,0,0));
fill(255, 0, 0);
textAlign(CENTER);
text("Congratulations you won!", width/2, height/5);
text(" Your final score is : "+ score, width/2, height/5 + 30);
text("Do you wish to continue?",width/2, height/2);
text(" If so press Y to Continue or N to exit ", width/2, height/2+30);
noLoop();
}
}
我不想令人沮丧,但这段代码一团糟。你这里有很多错误,要求我们检查所有这些错误是一个很大的要求。
话虽这么说,我会尽力让您朝着正确的方向开始。
首先,您在其中的几个 class 中缺少右大括号。适当的缩进将帮助您缩小范围,或者您可以考虑将每个 class 放在其自己的选项卡中。
然后在您的 Player
class 中使用一个名为 player1
的变量。该变量在哪里定义?您的意思是使用 player
变量吗?就此而言,既然您已经在 Player
class 中,那么您为什么要引用变量呢?您不应该直接在该实例中使用 x
变量吗?
这给我们带来了下一个问题:您的 Player
class 实际上没有定义 x
变量!
同样,您的 Player
class 调用了一个 shoot()
函数,该函数似乎不存在。您必须定义该函数。
那么让我们看看这里...您的 Score
class 使用了一个似乎没有在任何地方声明的 score
变量。
此外,您的 bullets
变量是一个数组,但您在其上调用的函数仅适用于 ArrayList
对象。任选其一。
您还调用了一堆不存在的函数:例如 movePlayer1()
、handleEnemies()
、handleBullets()
和 gameWon()
。其中一些函数是在 classes 中定义的,因此您需要使用该 class 的实例来访问这些函数。像这样:
Example e = new Example();
e.function();
class Example{
void function(){
println("here");
}
}
那么你的 Enemies
class 有一个 Enemy
的构造函数,这是无效的。选择一个或另一个。
你不会喜欢听到这个,但老实说,你最好的选择可能是从头开始。我猜想您是在尝试从不同来源复制粘贴所有这些代码,而没有真正理解代码的作用,这是一个可怕的想法。这永远行不通,即使对于有经验的程序员也是如此。
相反,请尝试将您的问题分解成更小的部分。在屏幕上获取一个矩形,然后在您按下箭头键时让它四处移动,然后添加射击功能。然后尝试添加一些敌人,但前提是前面的步骤完美无缺!
如果您遇到困难,post 一个 MCVE 显示您遇到困难的一小步 - 而不是整个项目!
我一直在处理一些代码并合并我创建的两个程序,但我知道我遗漏了很多信息,因为我有很多错误,但我似乎无法修复错误。下面我包含了已分成 classes 的整个代码。我正在制作 space 入侵者的版本。
主要class:
Bullets [] bullets = new Bullets[10];
Player player = new Player();
Boolean keyLftPressed = false, keyRghtPressed = false;
//Enemies[] enemies = new Enemies();
int state;
String gameLevel = "Main menu";
Boolean startTime = false;
void setup() {
for(int i=0; i<bullets.length; i++)
{
// if(i%2==0)
// bullets[i] = new Bullets();
// else
// bullets[i] = new Bullets(i);
}
size(800, 600);
state = 0;
}
void draw() {
background(255);
gameState();
player1.display();
movePlayer1();
handleEnemies();
handleBullets();
gamewon();
}
void gameState() {
if (gameLevel == "Main menu") {
background(0);
fill(255, 255, 255);
rect(270, 270, 280, 50, 20, 20, 20, 20);
//Draws rectangle for play game
fill(0);
textSize(30);
text("Play Game", 330, 305);
fill(255, 0, 0);
textSize(50);
text("Space Invaders", 220, 120);
if (mousePressed && mouseX > 250 && mouseX < 250 + 280 && mouseY > 270 && mouseY < 270 + 50)
{
gameLevel = "Level 1";
}
} else if (gameLevel == "Level 1")
{
background (255, 2555 , 255);
}
}
Bullets Class:
class Bullets {
class Bullet {
float x, y;
float velocity;
Bullet(float x, float y, float velocity) {
this.x = x;
this.y = y;
this.velocity = velocity;
}
void display(){
fill(80);
rect(this.x, this.y, 5,15);
}
void move(){
this.y+=this.velocity;
if (this.y > height || this.y < 0){
bullets.remove(this);
}
}
Class Enemies:
class Enemies {
float x, y;
float velocity;
Enemy(float x, float y) {
this.x = x;
this.y = y;
this.velocity = 3;
}
void display() {
fill(0,255,0);
ellipse(this.x, this.y, 30, 30);
noFill();
}
void move() {
this.x+=this.velocity;
if (this.x > width*.9) {
this.x = width*.9;
this.velocity *= -1;
this.y+=30;
}
if (this.x < width*.1) {
this.velocity*=-1;
this.x = width*.1;
this.y+=30;
}
}
void hitCheck() {
for (int i = 0; i < bullets.size(); i++){
Bullet b = (Bullet) bullets.get(i);
float distBetween = dist(b.x, b.y, this.x, this.y);
if (distBetween < 15 && b.velocity < 0){
score++;
enemies.remove(this);
float x = width*.1 + i%numCol*50;
float y = 60 + int(i/numCol)*60 ;
enemies.add(new Enemy(x, y));
}
}
}
}
Class Player:
class Player {
void movePlayer1() {
if (keyLftPressed) {
player1.x -=10;
}
if (keyRghtPressed) {
player1.x +=10;
}
}
void keyPressed() {
if (keyCode == LEFT) {
keyLftPressed = true;
}
else {
if (keyCode == RIGHT) {
keyRghtPressed = true;
}
else {
if (key == 'f') {
player1.shoot();
}
}
}
}
void keyReleased() {
if (keyCode == LEFT) {
keyLftPressed = false;
}
else {
if (keyCode == RIGHT) {
keyRghtPressed = false;
}
}
}
Class Score:
void gameFinish() {
{
for (int i = 0; i < 3; i++)
{
fill(color(255,0,0));
fill(255, 0, 0);
textAlign(CENTER);
text("Game over", width/2, height/2 - 50);
text(" Final score : "+ score, width/2, height/2 + 50);
}
}
}
}
void gamewon()
{
if (score == 10)
{
background(0);
fill(color(255,0,0));
fill(255, 0, 0);
textAlign(CENTER);
text("Congratulations you won!", width/2, height/5);
text(" Your final score is : "+ score, width/2, height/5 + 30);
text("Do you wish to continue?",width/2, height/2);
text(" If so press Y to Continue or N to exit ", width/2, height/2+30);
noLoop();
}
}
我不想令人沮丧,但这段代码一团糟。你这里有很多错误,要求我们检查所有这些错误是一个很大的要求。
话虽这么说,我会尽力让您朝着正确的方向开始。
首先,您在其中的几个 class 中缺少右大括号。适当的缩进将帮助您缩小范围,或者您可以考虑将每个 class 放在其自己的选项卡中。
然后在您的 Player
class 中使用一个名为 player1
的变量。该变量在哪里定义?您的意思是使用 player
变量吗?就此而言,既然您已经在 Player
class 中,那么您为什么要引用变量呢?您不应该直接在该实例中使用 x
变量吗?
这给我们带来了下一个问题:您的 Player
class 实际上没有定义 x
变量!
同样,您的 Player
class 调用了一个 shoot()
函数,该函数似乎不存在。您必须定义该函数。
那么让我们看看这里...您的 Score
class 使用了一个似乎没有在任何地方声明的 score
变量。
此外,您的 bullets
变量是一个数组,但您在其上调用的函数仅适用于 ArrayList
对象。任选其一。
您还调用了一堆不存在的函数:例如 movePlayer1()
、handleEnemies()
、handleBullets()
和 gameWon()
。其中一些函数是在 classes 中定义的,因此您需要使用该 class 的实例来访问这些函数。像这样:
Example e = new Example();
e.function();
class Example{
void function(){
println("here");
}
}
那么你的 Enemies
class 有一个 Enemy
的构造函数,这是无效的。选择一个或另一个。
你不会喜欢听到这个,但老实说,你最好的选择可能是从头开始。我猜想您是在尝试从不同来源复制粘贴所有这些代码,而没有真正理解代码的作用,这是一个可怕的想法。这永远行不通,即使对于有经验的程序员也是如此。
相反,请尝试将您的问题分解成更小的部分。在屏幕上获取一个矩形,然后在您按下箭头键时让它四处移动,然后添加射击功能。然后尝试添加一些敌人,但前提是前面的步骤完美无缺!
如果您遇到困难,post 一个 MCVE 显示您遇到困难的一小步 - 而不是整个项目!