所以標題幾乎可以解釋這一切,最近我已經瞭解了2D陣列,但我對如何使2D陣列在我創建的Space Invaders遊戲中正確移動有點困惑。爲太空侵略者遊戲移動2D陣列
現在我有外星人從左到右移動(反之亦然),但是,他們並沒有全部向下移動,而是逐列向下移動。任何人都知道在哪裏編輯代碼?
這裏是我的外國人代碼:
class Aliens {
int x = 100, y = 75, deltaX = 1;
Aliens (int x, int y) {
this.x = x;
this.y = y;
}
void drawAlien() {
fill(255);
rect(x, y, 25, 25);
}
void moveAlien() {
x = x + deltaX;
if (x >= width) {
y = y + 20;
deltaX = - deltaX;
} else if (x <=0) {
y = y + 20;
deltaX = - deltaX;
}
}
void updateAlien() {
drawAlien();
moveAlien();
}
}
和我的主類:
import ddf.minim.*;
//Global Variables
PImage splash;
PFont roboto;
Defender player;
Aliens[][] alienArray = new Aliens[15][3];
Missile missile;
int gameMode = 0;
int score = 0;
void setup() {
size(1000, 750);
rectMode(CENTER);
textAlign(CENTER, CENTER);
splash = loadImage("Splash.png");
player = new Defender();
for (int row = 0; row < 15; row++) {
for (int column = 0; column < 3; column++) {
alienArray[row][column] = new Aliens((row + 1) * 50, (column + 1) * 50);
}
}
roboto = createFont("Roboto-Regular.ttf", 32);
textFont(roboto);
}
void draw() {
if (gameMode == 0) {
background(0);
textSize(75);
text("Space Invaders", width/2, height/8);
textSize(25);
text("Created by Ryan Simms", width/2, height/4);
textSize(45);
text("Press SPACE to Begin", width/2, height - 100);
image(splash, width/2-125, height/4 + 75);
} else if (gameMode == 1) {
background(0);
score();
player.updateDefender();
for (int row = 0; row < 10; row ++) {
for (int column = 0; column < 3; column++) {
alienArray[row][column].updateAlien();
}
}
if (missile != null) {
missile.updateMissile();
}
if (keyPressed) {
if (key == ' ') {
if (missile == null) {
missile = new Missile(player.x);
}
}
}
if (missile != null) {
if (missile.y <= 0) {
missile = null;
}
}
}
}
void score() {
textSize(20);
text("Score: " + score, 40, 15);
}
void keyPressed() {
if (key == ' ') {
gameMode = 1;
}
}
我當時很希望能夠解釋如何做到這一點,或者至少在總體方向上提出一個觀點 – Ryan
@Ryan我告訴過你問題的根源在哪裏,甚至提供了一個警告,說明列將如何開始相互移動。如果你想學習如何編程,你必須考慮如何重構你的代碼。你的問題是在哪裏編輯代碼。我回答說。爲你編碼對你的學習沒有多大價值。 SO不是那種類型的網站。 –
我只在這個網站上問過其他問題,人們很高興能夠幫助我,我可以瞭解需要做什麼。我花了幾個小時才試圖找出問題所在。我經常不喜歡尋求幫助,但是當我這樣做時,至少能告訴我我需要改變什麼路線。我是否需要在外星人類中製作二維陣列才能讓他們全部移動?我是2D陣列的新手,我會從我的大學獲得幫助,但我本週休息。 – Ryan