大家晚上好! 我正在为自己制作一个小项目,以便更好地理解处理,而且我的进展非常顺利。但是,我最近偶然发现了一个我不知道如何正确制造多个敌人的问题。
就目前而言,游戏很简单: 你可以移动并射击子弹,如果你击中白色块,块就会消失并产生一个新的子弹。 现在我想要生成多个块,但我现在知道如何在不重复大量代码的情况下执行此操作(我对数组非常新,所以它对我来说有点复杂)。 如果有人有任何建议/例子,将不胜感激。
PImage bg;
PImage shipUp;
PImage shipDown;
PImage shipLeft ;
PImage shipRight;
PImage prevDirection;
static final int WIDTH = 1080;
static final int HEIGHT = 720;
static final int SHIPSIZE = 40;
int shipLR = 0; //Variable for the ship to go left/right
int shipUD = 0; //Variable for the ship to go up / down
int stepSize = 20;
int enemySize = 60;
int score = 0;
float randomX;
float randomY;
int enemyCount;
boolean enemyHit = false;
//import processing.sound.*;
//SoundFile file;
//import controlP5.*;
//ControlP5 theControl;
enum Direction {
LEFT, RIGHT, UP, DOWN, LEFTUP, LEFTDOWN, RIGHTUP, RIGHTDOWN, STATIONARY
};
Direction direction = Direction.STATIONARY;
ArrayList <Bullet> bullets;//
void setup() {
size(1080, 720);
frameRate(60);
bg = loadImage("Space2.jpg");
bullets = new ArrayList();
prevDirection = loadImage("Ship.jpg");
shipUp = loadImage("Ship.jpg");
shipDown = loadImage("ShipDown.jpg");
shipLeft = loadImage("ShipLeft.jpg");
shipRight = loadImage("ShipRight.jpg");
// file = new SoundFile(this, "/Users/bruusriezebos/Documents/Processing/Shooter_Project/Music.mp3");
// file.play();
}
void draw() {
background(bg);
shuttle(); //Call ship
move(); //Allows you to move
//enemyHit();
removeToLimit(100); //Set the max amount of bullets to 100
moveBullets(); //Move the bullets
displayBullets(); //Show the bullets
enemy(); //Spawn enemy
//fill(255);
//text(score,0,10);
}
void shuttle() {
switch (direction) {
case UP:
case LEFTUP:
case RIGHTUP:
image(shipUp, shipLR, shipUD);
prevDirection = shipUp;
break;
case DOWN:
case LEFTDOWN:
case RIGHTDOWN:
image(shipDown, shipLR, shipUD);
prevDirection = shipDown;
break;
case LEFT:
image(shipLeft, shipLR, shipUD);
prevDirection = shipLeft;
break;
case RIGHT:
image(shipRight, shipLR, shipUD);
prevDirection = shipRight;
break;
case STATIONARY:
image(prevDirection, shipLR, shipUD);
break;
default:
//Do nothing
}
}
void keyPressed(KeyEvent event) {
switch (key) {
case 'l':
Bullet temp = new Bullet(shipLR + (SHIPSIZE/2), shipUD + (SHIPSIZE/2), direction, 7.5);
bullets.add(temp);
break;
case 'f':
direction = Direction.LEFT;
break;
case 'h':
direction = Direction.RIGHT;
break;
case 't':
direction = Direction.UP;
break;
case 'g':
direction = Direction.DOWN;
break;
case 'q':
direction = Direction.LEFTUP;
break;
case 'e':
direction = Direction.RIGHTUP;
break;
case 'z':
direction = Direction.LEFTDOWN;
break;
case 'c':
direction = Direction.RIGHTDOWN;
break;
default:
break;
}
}
void move() {
int lr = shipLR;
int ud = shipUD;
switch (direction) {
case LEFT:
lr = lr - stepSize;
break;
case RIGHT:
lr = lr + stepSize;
break;
case UP:
ud = ud - stepSize;
break;
case DOWN:
ud = ud + stepSize;// Go down
break;
case LEFTUP:
ud = ud - stepSize/2; //Diagonal movement
lr = lr - stepSize/2; //Left + up
break;
case RIGHTUP:
ud = ud - stepSize/2; //Diagonal movement
lr = lr + stepSize/2; //Right + up
break;
case LEFTDOWN:
ud = ud + stepSize/2; //Diagonal movement
lr = lr - stepSize/2; //Left + down
break;
case RIGHTDOWN:
ud = ud + stepSize/2; //Diagonal movement
lr = lr + stepSize/2; //Right + down
break;
default:
break;
}
lr = constrain(lr, 0, WIDTH - SHIPSIZE ); //Constrain the ship in the window
ud = constrain(ud, 0, HEIGHT - SHIPSIZE); //Constrain the ship in the window
shipLR = lr;
shipUD = ud;
}
void keyReleased() {
if (key != 'l') {
direction = Direction.STATIONARY;
}
}
class Bullet
{
Direction bulletDirection;
float bulletX;
float bulletY;
float speed;
Bullet(float bulletX, float bulletY, Direction bulletDirection, float speed)
{
this.bulletX = bulletX;
this.bulletY = bulletY;
this.bulletDirection =( bulletDirection == Direction.STATIONARY? Direction.UP: bulletDirection);
this.speed = speed;
}
void display()
{
stroke(255);
rectMode(CORNER);
ellipse(this.bulletX, this.bulletY, 10, 10);
{
if (dist(bulletX, bulletY, randomX + (enemySize / 2), randomY + (enemySize / 2) )<35) {
enemyCount = - 1;
score = score + 100;
enemyHit = false;
}
}
}
void moveBullet() {
switch (bulletDirection) {
case UP:
this.bulletY -= speed;
break;
case LEFT:
this.bulletX -= speed;
break;
case RIGHT:
this.bulletX += speed;
break;
case DOWN:
this.bulletY += speed;
break;
default:
break;
}
}
}
void removeToLimit(int maxLength)
{
while (bullets.size() > maxLength)
{
bullets.remove(0);
}
}
void moveBullets()
{
for (Bullet temp : bullets)
{
temp.moveBullet();
}
}
void displayBullets()
{
for (Bullet temp : bullets)
{
temp.display();
}
}
void enemy() {
if (enemyCount < 10) {
random();
rect(randomX, randomY, enemySize, enemySize);
}
//if (enemyCount < 10) {
//randomX = random(0, WIDTH - SHIPSIZE);
//randomY = random(0, HEIGHT - SHIPSIZE);
//enemyHit = true;
}
void random() {
if (enemyHit == false){
randomX = random(0, WIDTH - SHIPSIZE);
randomY = random(0, HEIGHT - SHIPSIZE);
enemyHit = true;
}
}
敌人的功能,目前只产生一个区块,我想扩大它以创造超过1.但我有效地做到这一点的尝试甚至已经足够近以致于值得,所以任何想法非常感谢。
我知道这有很多代码可以引向某人的方向,但我在这里有点茫然。
答案 0 :(得分:0)
你的子弹非常类似,但是使用其他东西(前一个敌人死亡,已经达到某个时间等)来触发它。 你想为敌人使用ArrayList,因为它们的数量会随着频率的变化而变化,并且使用ArrayLists(与数组相比)处理这种情况更容易(也可能更快)。