Java 太空入侵者游戏中的移动2D阵列
所以这个标题很好地解释了这一切,最近我学习了2D阵列,但是我对如何在我正在创建的这个空间入侵者游戏中正确移动2D阵列有点困惑 现在我看到外星人从左向右移动(反之亦然),但是,他们并不是同时向下移动,而是一列一列地向下移动。有人知道在哪里编辑代码吗 这是我给外星人的密码:Java 太空入侵者游戏中的移动2D阵列,java,arrays,processing,Java,Arrays,Processing,所以这个标题很好地解释了这一切,最近我学习了2D阵列,但是我对如何在我正在创建的这个空间入侵者游戏中正确移动2D阵列有点困惑 现在我看到外星人从左向右移动(反之亦然),但是,他们并不是同时向下移动,而是一列一列地向下移动。有人知道在哪里编辑代码吗 这是我给外星人的密码: class Aliens { int x = 100, y = 75, deltaX = 1; Aliens (int x, int y) { this.x = x; this.y = y; }
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();
}
}
类外星人{
int x=100,y=75,deltaX=1;
外星人(整数x,整数y){
这个.x=x;
这个。y=y;
}
作废{
填充(255);
rect(x,y,25,25);
}
void moveAlien(){
x=x+deltaX;
如果(x>=宽度){
y=y+20;
deltaX=-deltaX;
}else if(x逻辑缺陷出现在moveAlien()
方法中。将外星人的x
位置移动一定的增量。如果检测到外星人已通过某个屏幕边界(x>=width
和x)我有点希望解释一下如何做到这一点,或者至少是一般意义上的一点direction@Ryan我告诉过你问题的根源在哪里,甚至还提供了一个关于列如何开始相互移动的警告o重构代码。您的问题是在哪里编辑代码。我回答了。为您编写代码对您的学习没有什么价值。这类网站也不是这样。我在这个网站上只问过另外一个问题,人们都很乐意帮助我,我可以学到需要做的事情。我花了几个小时以前试图找出问题所在。我不喜欢经常寻求帮助,但当我这样做时,最好至少告诉我需要更改哪条线。我是否需要在外星人类中创建一个2D数组来让它们全部移动?我是2D数组新手,我会从我的大学获得帮助,但我这周不在这里。@Ryan我告诉过你问题是(在您的moveAlien()
方法中)以及问题是什么:当一个alien超过阈值(左边缘或右边缘)时,会向下移动它,但这种情况一次只发生一列。如果您已经编写了向我们展示的代码,那么您推理中的错误应该是显而易见的。但是,这不是一行或甚至一个方法修复,而是需要重新思考和重组您的程序。因此,如果不为您做家庭作业,我无法提供代码修复。另请参见:也看见
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;
}
}