Space 入侵者项目
Space Invader project
您好,我一直在处理 space 入侵者游戏,我对 java/coding 比较陌生。我的问题是我真的不明白如何让碰撞真正起作用,还有我的外星人(space 飞船),他们从左到右移动但不向下移动。这些是我很难解决的两个主要问题。这是我下面的代码,任何专业的建议将不胜感激。
PImage SpaceShip;
PImage Laser;
int TankX = 450;
int TankY = 450;
int SpaceshipX = 20;
int SpaceshipY = 20;
int MoveSpaceShipDown = 5;
int TankSizeX = 200;
int TankSizeY = 90;
int ShipSizeX = 90;
int ShipSizeY = 80;
int Xtankspeed = 2;
int LaserX = 9999;
int LaserY = TankY;
int LasersizeX = 10;
int LasersizeY = 40;
int spaceShipDirection = 5;
int spaceshipxspeed = 2;
int spaceshipSpeed = 5;
//int Xtankspeed2 = -6;
boolean moveRight = false;
boolean moveLeft = false;
boolean ShowLaser = false;
int[] ShipX = new int [15];
int [] ShipY = new int [4];
int gameState = 1;
int StopWatch = 0;
void setup() {
size(1000, 500);
imageMode(CENTER);
SpaceShip = loadImage("Spaceship.png");
SpaceShip.resize( ShipSizeX, ShipSizeY );
Laser = loadImage ("LaserBeam.png");
Laser.resize(LasersizeX, LasersizeY);
Tank = loadImage("Tank.png");
Tank.resize(TankSizeX, TankSizeY);
setSpaceShipPositions();
}
void setSpaceShipPositions() {
for(int j = 0; j < ShipY.length; j++){
for (int i = 0; i < ShipX.length; i++) {
ShipX[i] = 50 + 50*i;
}
ShipY[j] = 50 + 50*j;
}
}
void draw() {
if (gameState == 1) {
IntroScreen();
} else if (gameState == 2) {
startGame();
}
}
void IntroScreen() {
background(#000000);
textMode(CENTER);
textSize(50);
text("Space Defenders", 325, 250);
}
void startGame() {
background (#2f3069);
MakeLaser();
Maketank();
Maketankmove();
checkiftankhitedge();
for (int j = 0; j < ShipY.length; j++) {
for (int i = 0; i < ShipX.length; i++) {
MakeSpaceShip(i,j);
moveSpaceShip();
checkiflaserhitspaceship(i,j);
}
}
Shootlaser();
Makelaserreturentoorignalposition();
//makespaceshipgodown();
}
void keyPressed() {
println(keyCode);
if (keyCode == 39) { //right key
moveRight = true;
}
if (keyCode == 37) { //leftkey
moveLeft = true;
}
if (keyCode == 32 && ShowLaser == false) { //Spacebar
ShowLaser = true;
LaserX = TankX;
LaserY = TankY;
}
if (keyCode == 10) {
gameState = 2;
}
}
void keyReleased() {
if (keyCode == 39) { //right key
moveRight = false;
}
if (keyCode == 37) { //leftkey
moveLeft = false;
}
if (keyCode == 32) { //Spacebar
}
}
void Maketank() {// this is to make the tank
//PImage.resize(0,5);
imageMode(CENTER);
image(Tank, TankX, TankY);
}
void MakeLaser() {
if (ShowLaser) {
image(Laser, LaserX, LaserY);
}
}
void MakeSpaceShip(int i, int j) {
image(SpaceShip, ShipX[i], ShipY[j]);
//for (int i = 0; i < spaceShipX.length; i++){
// image( spaceShipX[i] - 15, ,SpaceshipX, SpaceshipY ); Confused
}
int lastlaptime = 0;
int timesincelastlap = 0;
//void moveSpaceShip(int i, int j) {
/*StopWatch = millis();
timesincelastlap = StopWatch - lastlaptime;
if (timesincelastlap > 500) {
spaceShipX[i] += spaceshipxspeed;
lastlaptime = millis();
// if (spaceShipX[i] > 990) {
// SpaceshipY = SpaceshipY + 30;
//spaceShipDirection = -1*spaceShipDirection;
// }
//if (spaceShipX[i] < 10) {
// SpaceshipY = SpaceshipY + 30;
//spaceShipDirection = -1*spaceShipDirection;
//}
//}
}*/
void moveSpaceShip() {
/*for (int i = 0; i < 15; i++){
if (spaceShipX[40] > 990){
spaceShipDirection = -1*spaceShipDirection;
SpaceshipY = SpaceshipY + 30;
}
if (spaceShipX[1] < 10){
spaceShipDirection = -1*spaceShipDirection;
SpaceshipY = SpaceshipY + 30;
}
}
for (int i = 0; i < 15; i++){
spaceShipX[i] = spaceShipX[i] + spaceShipDirection;
}*/
if (millis() - StopWatch > 1000) {
StopWatch = millis();
checkIfHitEdge();
moveShips();
}
}
void moveShips() {
for (int i = 0; i < 15; i++) {
ShipX[i] = ShipX[i] + spaceShipDirection*spaceshipSpeed;
}
}
void checkIfHitEdge() {
for (int i = 0; i < 15; i++) {
if (ShipX[14] > 990) {
SpaceshipY = SpaceshipY + 5;
spaceShipDirection = -1*spaceShipDirection;
}
if (ShipX[0] < 10) {
spaceShipDirection = -1*spaceShipDirection;
SpaceshipY = SpaceshipY + 5;
}
}
}
//void makespaceshipgodown(){
// if(SpaceshipX + 45 > width){
//SpaceshipY++;
// }
//}
void Maketankmove() {
if (moveLeft == true) {
TankX--; //this makes the tank go left
TankX = TankX - Xtankspeed;//this is for the speed of the tank
}
if (moveRight == true) {
TankX++;//this makes the tank go right
TankX = TankX + Xtankspeed;//the speed of the tank
}
}
void checkiftankhitedge() {
if (TankX > width-50) {
TankX = 950;
moveRight = false;
}
if (TankX-100 < -50) {
TankX = 50;
moveLeft = false;
}
}
void Makelaserreturentoorignalposition() {
//if(TankX++){
if (LaserY == 0) {
LaserY = TankY;
LaserX = 9999;
ShowLaser = false;
}
}
void checkiflaserhitspaceship(int i, int j) {
/* int Lasertop = LasersizeY - 20;
int Laserbottom = LasersizeY + 20;
int LaserLeft= LasersizeX - 5;
int LaserRight = LasersizeX +5;
int SpaceShipBottom = ShipY[j] - 40;
int SpaceShipTop = ShipY[j] + 40;
int SpaceShipleft = spaceShipX[i] - 45;
int SpaceShipright = spaceShipX[i] + 45;
boolean LaserhitSpaceship = Lasertop > SpaceShipBottom &&
Laserbottom < SpaceShipTop &&
LaserLeft < SpaceShipright &&
LaserRight > SpaceShipleft;
if(LaserhitSpaceship == true){
spaceShipX[0] = 90000;
}*/
}
void Shootlaser() {
if (ShowLaser == true) {
LaserY-=5;
}
}```
这是我认为的问题所在。首先,处理并不是制作像 Space Invaders 这样的游戏的最佳语言。您不能一次有多个键输入。例如,如果我按下左箭头,按住它,然后按下右箭头,然后松开左箭头,处理将忽略左箭头。 Processing有一键输入:key
。它只能有一个值。您不能一次跟踪多个输入。你也许可以用你的鼠标替换它——如果鼠标离左边很远,向左移动。如果它离右边很远,请向右移动。在中间?保持静止。
除此之外,您还想检查碰撞。首先,把敌人变成一个物体。我假设你知道如何制作物体。如果没有,请查看 this tutorial. 现在,我们需要检查碰撞。我建议在敌人内部制作一个碰撞方法 class,这是我想象的样子:
boolean collidingWithPlayer() {
return (playerX > enemyX && playerX < enemyX + enemyW
&& playerY > enemyY && playerY < enemyY + enemyH);
}
随意使用。祝你游戏顺利。
您好,我一直在处理 space 入侵者游戏,我对 java/coding 比较陌生。我的问题是我真的不明白如何让碰撞真正起作用,还有我的外星人(space 飞船),他们从左到右移动但不向下移动。这些是我很难解决的两个主要问题。这是我下面的代码,任何专业的建议将不胜感激。
PImage SpaceShip;
PImage Laser;
int TankX = 450;
int TankY = 450;
int SpaceshipX = 20;
int SpaceshipY = 20;
int MoveSpaceShipDown = 5;
int TankSizeX = 200;
int TankSizeY = 90;
int ShipSizeX = 90;
int ShipSizeY = 80;
int Xtankspeed = 2;
int LaserX = 9999;
int LaserY = TankY;
int LasersizeX = 10;
int LasersizeY = 40;
int spaceShipDirection = 5;
int spaceshipxspeed = 2;
int spaceshipSpeed = 5;
//int Xtankspeed2 = -6;
boolean moveRight = false;
boolean moveLeft = false;
boolean ShowLaser = false;
int[] ShipX = new int [15];
int [] ShipY = new int [4];
int gameState = 1;
int StopWatch = 0;
void setup() {
size(1000, 500);
imageMode(CENTER);
SpaceShip = loadImage("Spaceship.png");
SpaceShip.resize( ShipSizeX, ShipSizeY );
Laser = loadImage ("LaserBeam.png");
Laser.resize(LasersizeX, LasersizeY);
Tank = loadImage("Tank.png");
Tank.resize(TankSizeX, TankSizeY);
setSpaceShipPositions();
}
void setSpaceShipPositions() {
for(int j = 0; j < ShipY.length; j++){
for (int i = 0; i < ShipX.length; i++) {
ShipX[i] = 50 + 50*i;
}
ShipY[j] = 50 + 50*j;
}
}
void draw() {
if (gameState == 1) {
IntroScreen();
} else if (gameState == 2) {
startGame();
}
}
void IntroScreen() {
background(#000000);
textMode(CENTER);
textSize(50);
text("Space Defenders", 325, 250);
}
void startGame() {
background (#2f3069);
MakeLaser();
Maketank();
Maketankmove();
checkiftankhitedge();
for (int j = 0; j < ShipY.length; j++) {
for (int i = 0; i < ShipX.length; i++) {
MakeSpaceShip(i,j);
moveSpaceShip();
checkiflaserhitspaceship(i,j);
}
}
Shootlaser();
Makelaserreturentoorignalposition();
//makespaceshipgodown();
}
void keyPressed() {
println(keyCode);
if (keyCode == 39) { //right key
moveRight = true;
}
if (keyCode == 37) { //leftkey
moveLeft = true;
}
if (keyCode == 32 && ShowLaser == false) { //Spacebar
ShowLaser = true;
LaserX = TankX;
LaserY = TankY;
}
if (keyCode == 10) {
gameState = 2;
}
}
void keyReleased() {
if (keyCode == 39) { //right key
moveRight = false;
}
if (keyCode == 37) { //leftkey
moveLeft = false;
}
if (keyCode == 32) { //Spacebar
}
}
void Maketank() {// this is to make the tank
//PImage.resize(0,5);
imageMode(CENTER);
image(Tank, TankX, TankY);
}
void MakeLaser() {
if (ShowLaser) {
image(Laser, LaserX, LaserY);
}
}
void MakeSpaceShip(int i, int j) {
image(SpaceShip, ShipX[i], ShipY[j]);
//for (int i = 0; i < spaceShipX.length; i++){
// image( spaceShipX[i] - 15, ,SpaceshipX, SpaceshipY ); Confused
}
int lastlaptime = 0;
int timesincelastlap = 0;
//void moveSpaceShip(int i, int j) {
/*StopWatch = millis();
timesincelastlap = StopWatch - lastlaptime;
if (timesincelastlap > 500) {
spaceShipX[i] += spaceshipxspeed;
lastlaptime = millis();
// if (spaceShipX[i] > 990) {
// SpaceshipY = SpaceshipY + 30;
//spaceShipDirection = -1*spaceShipDirection;
// }
//if (spaceShipX[i] < 10) {
// SpaceshipY = SpaceshipY + 30;
//spaceShipDirection = -1*spaceShipDirection;
//}
//}
}*/
void moveSpaceShip() {
/*for (int i = 0; i < 15; i++){
if (spaceShipX[40] > 990){
spaceShipDirection = -1*spaceShipDirection;
SpaceshipY = SpaceshipY + 30;
}
if (spaceShipX[1] < 10){
spaceShipDirection = -1*spaceShipDirection;
SpaceshipY = SpaceshipY + 30;
}
}
for (int i = 0; i < 15; i++){
spaceShipX[i] = spaceShipX[i] + spaceShipDirection;
}*/
if (millis() - StopWatch > 1000) {
StopWatch = millis();
checkIfHitEdge();
moveShips();
}
}
void moveShips() {
for (int i = 0; i < 15; i++) {
ShipX[i] = ShipX[i] + spaceShipDirection*spaceshipSpeed;
}
}
void checkIfHitEdge() {
for (int i = 0; i < 15; i++) {
if (ShipX[14] > 990) {
SpaceshipY = SpaceshipY + 5;
spaceShipDirection = -1*spaceShipDirection;
}
if (ShipX[0] < 10) {
spaceShipDirection = -1*spaceShipDirection;
SpaceshipY = SpaceshipY + 5;
}
}
}
//void makespaceshipgodown(){
// if(SpaceshipX + 45 > width){
//SpaceshipY++;
// }
//}
void Maketankmove() {
if (moveLeft == true) {
TankX--; //this makes the tank go left
TankX = TankX - Xtankspeed;//this is for the speed of the tank
}
if (moveRight == true) {
TankX++;//this makes the tank go right
TankX = TankX + Xtankspeed;//the speed of the tank
}
}
void checkiftankhitedge() {
if (TankX > width-50) {
TankX = 950;
moveRight = false;
}
if (TankX-100 < -50) {
TankX = 50;
moveLeft = false;
}
}
void Makelaserreturentoorignalposition() {
//if(TankX++){
if (LaserY == 0) {
LaserY = TankY;
LaserX = 9999;
ShowLaser = false;
}
}
void checkiflaserhitspaceship(int i, int j) {
/* int Lasertop = LasersizeY - 20;
int Laserbottom = LasersizeY + 20;
int LaserLeft= LasersizeX - 5;
int LaserRight = LasersizeX +5;
int SpaceShipBottom = ShipY[j] - 40;
int SpaceShipTop = ShipY[j] + 40;
int SpaceShipleft = spaceShipX[i] - 45;
int SpaceShipright = spaceShipX[i] + 45;
boolean LaserhitSpaceship = Lasertop > SpaceShipBottom &&
Laserbottom < SpaceShipTop &&
LaserLeft < SpaceShipright &&
LaserRight > SpaceShipleft;
if(LaserhitSpaceship == true){
spaceShipX[0] = 90000;
}*/
}
void Shootlaser() {
if (ShowLaser == true) {
LaserY-=5;
}
}```
这是我认为的问题所在。首先,处理并不是制作像 Space Invaders 这样的游戏的最佳语言。您不能一次有多个键输入。例如,如果我按下左箭头,按住它,然后按下右箭头,然后松开左箭头,处理将忽略左箭头。 Processing有一键输入:key
。它只能有一个值。您不能一次跟踪多个输入。你也许可以用你的鼠标替换它——如果鼠标离左边很远,向左移动。如果它离右边很远,请向右移动。在中间?保持静止。
除此之外,您还想检查碰撞。首先,把敌人变成一个物体。我假设你知道如何制作物体。如果没有,请查看 this tutorial. 现在,我们需要检查碰撞。我建议在敌人内部制作一个碰撞方法 class,这是我想象的样子:
boolean collidingWithPlayer() {
return (playerX > enemyX && playerX < enemyX + enemyW
&& playerY > enemyY && playerY < enemyY + enemyH);
}
随意使用。祝你游戏顺利。