太空侵略者项目

Posted

技术标签:

【中文标题】太空侵略者项目【英文标题】:Space Invader project 【发布时间】:2022-01-09 11:42:33 【问题描述】:

您好,我一直在处理太空入侵者游戏,我对 java/coding 比较陌生。我的问题是我并不真正了解如何使碰撞真正起作用,还有我的外​​星人(即太空船),它们从左到右移动但不会向下移动。这是我很难解决的两个主要问题。这是我下面的代码,任何专业建议都将不胜感激。

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;
  
```

【问题讨论】:

这是相当多的变数。您可能想要制作一个对象来执行此操作。您可以制作一个“敌人”对象,也许还可以制作一个方法来检查它是否正在接触玩家。 【参考方案1】:

这就是我认为的问题所在。首先,处理并不是制作像 Space Invaders 这样的游戏的最佳语言。您不能一次输入多个键。例如,如果我按下左箭头,按住它,然后按下右箭头,然后松开左箭头,处理将忽略左箭头。处理一键输入:key。它只能有一个值。您不能一次跟踪多个输入。您可以用鼠标替换它 - 如果鼠标在左侧很远,请向左移动。如果它在右边很远,向右移动。在中间?保持不动。

除此之外,您还想检查碰撞。首先,把敌人变成一个物体。我假设你知道如何制作物体。如果没有,请查看this tutorial. 现在,我们需要检查碰撞。我建议在敌人类中创建一个碰撞方法,这就是我想象的:

boolean collidingWithPlayer() 
    return (playerX > enemyX && playerX < enemyX + enemyW
    && playerY > enemyY && playerY < enemyY + enemyH);

随意使用它。祝你的游戏好运。

【讨论】:

以上是关于太空侵略者项目的主要内容,如果未能解决你的问题,请参考以下文章

Space Invaders 太空侵略者

如何用Sprite Kit制作《太空入侵者》

WebStorm-Dart导入错误

模块化自组装太空望远镜项目已获得NASA的肯定

C语言开源项目盘点 GitHub 上不错的 4 个C语言项目

Python - 追逐另一个精灵的精灵