Lucke GG
Lucke GG

Reputation: 99

How to move the ball in different directions?

Now when I hit spacebar it will go left or right, I want the ball to go straight first time.Then when the ball hit a wall, block or line after that I want the ball to go random directions with "-1" somehow. This is my first school game project, it's a one line pong game.

Edit: Edit I've added "boll_speed.X = random.Next(-1, 1);", and that works perfectly!

Having trouble with these parts:

        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();
        if (Start == true) 
        {
            boll_rect.X += (int)boll_speed.X;
            boll_rect.Y += (int)boll_speed.Y;
        }

        if (boll_rect.X > Window.ClientBounds.Width - boll_texture.Width || boll_rect.X < 0) 
            boll_speed.X *= -1;

        if (boll_rect.Y > Window.ClientBounds.Height - boll_texture.Height || boll_rect.Y < 0) 
            boll_speed.Y *= -1;

        else if (ks.IsKeyDown(Keys.Space)) 
        {
            Start = true;
        }

        if (linje_rect.Intersects(boll_rect)) 
        {
            boll_speed.Y *= -1;
            boll_speed.Y += random.Next(-100, 101) / 100.0f;
            boll_speed.X *= -1;
            boll_speed.X += random.Next(-100, 101) / 100.0f;
        }

Upvotes: 2

Views: 1445

Answers (3)

Zinov
Zinov

Reputation: 4119

I will give you the generic idea. Please adapt it to your example

Lets say you want to move to all directions on the board, now imagine your board as a bi-dimensional array(all your pixels has an x,y representation on that board)

The (0,0) marks the left upper corner of your board, and your (width, height) mark the lower right corner. Your ball is on the middle of the board on the (x, y) position, where x > 0 and x < width(of the board) and y > 0 and y < height.

You have 8 different directions base on the picture below

enter image description here

Now it is time to translate those directions to a logical structure. You need to create an array for that, lets say we want to start from the NW direction. I will create an array with the numbers that you need to add to your (x, y) position in order to keep going in the direction you choose. If you see I am creating a bidimentional array

enter image description here

Now this is the pseudocode for your idea:

    int[,] directions = new int[,]{{-1,-1,-1,0,1,1,1,0}, {-1,0,1,1,1,0,-1,-1}};

    public void Move(Game game)
    {
       Point currentPosition = GetYourCurrentPosition(); //the x, y
       int direction = GetYourRandomDirection() //this should be a random number between 0 and 7, beucase the array for directions has 8 positions. 


           int xDirection = directions[0, direction];
           int yDirection = directions[1, direction];

            //move inside the board forever
            while(true)
            {
               while(ICanMoveTo(currentPosition.X + xDirection, currentPosition.Y + yDirection)
               {
                 //keep moving on the same direction
                 currentPosition.X += xDirection;
                 currentPosition.Y += yDirection;
               } 

               //if you can't move anymore in the same direction, then change
               direction = GetYourRandomDirection();
               xDirection = directions[0, direction];
               yDirection = directions[1, direction];
             }
        }

Upvotes: 2

Zizy Archer
Zizy Archer

Reputation: 1390

Random.Next(-1) should give you out of bounds error.

You can use something like Random.Next(-200, 0)/100.0f, which will return a negative number between -2 and 0 (including -2, excluding 0).

But notice that this multiplication will lead to slowdown of ball over time. It might speed up to double of current speed, but it can slow down to nearly 0 in a single step. So I would rather first invert ball speed in y direction and keep it the same in x if you hit horizontal lines, and invert in x and keep in y if you hit vertical lines. Then do a random addition of a zero-means constant.

boll_speed.Y *= -1;
boll_speed.Y += random.Next(-100, 101)/100.0f; // Add random number between -1 and 1 with 0.01 precision.
// and nearly the same for X. Depending on the lines hit.

Upvotes: 1

leanne
leanne

Reputation: 8709

I'm thinking you mean you want the ball to change directions randomly when it hits the window boundaries or line. If so:

You're using constant values for your direction changes. Add a randomizer to multiply your boll_speed by.

For example, in pseudocode, instead of -1, do random(-1).

Upvotes: 0

Related Questions