#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Unhappy Space Invaders C#


    I have programmed the game Space Invaders for a class project, I am having some troubles getting the ship to stop at the end of each side of the screen. Could anyone help me with this?
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803

    Post


    I think in space invaders that is a gun, not a ship.

    At a guess, (because you have given us nothing to go on), something like:

    C# Code:
     
    if( gun_left )
    {
        gun_pos -= GUN_POS_STEP ;
        gun_pos < 0 )
       {
           gun_pos = 0 ;
       }
    }
    else if( gun_right )
    {
        gun_pos += GUN_POS_STEP ;
        if( gun_pos > (window_width - ship_width) )
        {
            gun_pos = (window_width - ship_width) ;
        }
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0
    I am using Microsoft Visual C# Express, in my game the ship moves left to right at the bottom of the screen.

    //move ship
    KeyboardState kb = Keyboard.GetState();
    if (kb.IsKeyDown(Keys.Left))
    rectship.X = rectship.X - 6;
    if (kb.IsKeyDown(Keys.Right))
    rectship.X = rectship.X + 6;
    if (kb.IsKeyDown(Keys.Space) && bulletvisible.Equals("NO"))
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by Energy180
    I am using Microsoft Visual C# Express, in my game the ship moves left to right at the bottom of the screen.

    //move ship
    KeyboardState kb = Keyboard.GetState();
    if (kb.IsKeyDown(Keys.Left))
    rectship.X = rectship.X - 6;
    if (kb.IsKeyDown(Keys.Right))
    rectship.X = rectship.X + 6;
    if (kb.IsKeyDown(Keys.Space) && bulletvisible.Equals("NO"))
    OK fine, so my answer stands. In your original question you said you were "having some troubles getting the ship to stop", but your code does not show even any attempt to solve that problem.

    I feel like I am spoon feeding you now, since I have already given you a suitable answer - you just need to map the logic to your actual code. In your case:

    C# Code:
      //move ship
      KeyboardState kb = Keyboard.GetState();
      if( kb.IsKeyDown(Keys.Left) )
      {
          rectship.X -= 6;
          if( rectship.X < 0 )
          {
               rectship.X += 6 ;
          }
      }
     
      if( kb.IsKeyDown(Keys.Right) )
      {
           // I'll leave the rest to you, compare the 
           // code above to the corresponding code in my 
           // outline.
      }


    I used "else if" in my outline, but that gives priority to left if both keys are pressed, whereas your approach would result in no movement which is probably better.

    I have changed my original suggestion from rectship.X = 0 to rectship.X +=6 to ensure that the gun always remains on the same multiple of 6 relative to the start position. A subtlety that may be important.

    You have a "magic number" 6 for the movement step, I strongly suggest that you define a symbolic constant for this as in my outline. The benefit comes in code maintenance when you can just change one value and have it changed everywhere it is needed. If for example you used the number 6 for some entirely different reason elsewhere, that may not be obvious - how would the code maintainer (which may be you in months to come) necessarily know which needed to be changed to change the behaviour?
    Last edited by clifford; December 7th, 2013 at 07:21 PM.

IMN logo majestic logo threadwatch logo seochat tools logo