所以标题几乎可以解释这一切,最近我已经了解了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