2017-03-16 68 views
2

现在当我点击空格键时,它会向左或向右移动,我希望球首次直线移动。然后,当球击中墙壁,阻挡线后,我希望球以“-1”不知何故。这是我的第一个学校游戏项目,这是一个单线乒乓游戏。如何在不同的方向移动球?

编辑:编辑我已经添加了“boll_speed.X = random.Next(-1,1);”,而且完美地工作!

  • linje =行
  • LIV =生活
  • 铃=球
  • =POÄNG波昂点
  • 我不使用 “blockröd= blockred” 现在
  • blockgrön= blockgreen

    public class Game1 : Game 
    { 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    SpriteFont spritefont; 
    Texture2D linje_texture; 
    Texture2D boll_texture; 
    Texture2D blockröd_texture; 
    Texture2D blockgrön_texture; 
    Texture2D gameover_texture; 
    Rectangle linje_rect; 
    Rectangle boll_rect; 
    Rectangle blockröd_rect; 
    Rectangle blockgrön_rect; 
    Rectangle gameover_rect; 
    
    Vector2 linje_speed; 
    Vector2 boll_speed; 
    
    Random random; 
    
    int liv; 
    int poang; 
    int highscore; 
    
    List<Rectangle> block = new List<Rectangle>(); 
    
    bool Start = false; 
    
    public Game1() 
    { 
        graphics = new GraphicsDeviceManager(this); 
        Content.RootDirectory = "Content"; 
    
        graphics.PreferredBackBufferWidth = 760; 
        graphics.PreferredBackBufferHeight = 620; 
    } 
    
    protected override void Initialize() 
    { 
        linje_speed.X = 5f; 
        boll_speed.X = boll_speed.X = random.Next(-1, 1); 
        boll_speed.Y = 6f; 
        liv = 3; 
        poang = 0; 
    
        random = new Random(); 
    
        base.Initialize(); 
    } 
    
    protected override void LoadContent() 
    { 
        spriteBatch = new SpriteBatch(GraphicsDevice); 
        spritefont = Content.Load<SpriteFont>("Fonts/Myfont"); 
        linje_texture = Content.Load<Texture2D>("Pics/linje-lång"); 
        boll_texture = Content.Load<Texture2D>("Pics/boll"); 
        blockgrön_texture = Content.Load<Texture2D>("Pics/block-grön"); 
        blockröd_texture = Content.Load<Texture2D>("Pics/block-röd"); 
        gameover_texture = Content.Load<Texture2D>("Pics/GameOver"); 
        linje_rect = new Rectangle((Window.ClientBounds.Width - linje_texture.Width)/2, 580, linje_texture.Width, linje_texture.Height); 
        boll_rect = new Rectangle((Window.ClientBounds.Width - boll_texture.Width)/2, 556, boll_texture.Width, boll_texture.Height); 
        gameover_rect = new Rectangle((Window.ClientBounds.Width/2) - (gameover_texture.Width/2), (Window.ClientBounds.Height/2) - gameover_texture.Height/2, gameover_texture.Width, gameover_texture.Height); 
    
        block.Add(blockgrön_rect); 
        block.Add(blockröd_rect); 
        for (int i = 1; i < 5; i++) 
        { 
         for (int g = 1; g < 13; g++) 
         { 
          block.Add(new Rectangle((g * 63) - 60, (i * 40), blockgrön_texture.Width, blockgrön_texture.Height)); 
         } 
        } 
    } 
    
    protected override void UnloadContent() 
    { 
        // TODO: Unload any non ContentManager content here 
    } 
    
    protected override void Update(GameTime gameTime) 
    { 
        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(Start == false) 
        { 
         boll_rect.X = linje_rect.X + ((linje_texture.Width/2) - (boll_texture.Width/2)); 
        } 
        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; 
    
        if (boll_rect.Y > Window.ClientBounds.Height - boll_texture.Height) 
        { 
         liv -= 1; 
         Start = false; 
         boll_rect.X = (Window.ClientBounds.Width - boll_texture.Width)/2; 
         boll_rect.Y = 556; 
         linje_rect.X = (Window.ClientBounds.Width - linje_texture.Width)/2; 
         linje_rect.Y = 580; 
        } 
    
        KeyboardState ks = Keyboard.GetState(); 
        if (ks.IsKeyDown(Keys.Left)) 
        { 
         linje_rect.X -= (int)linje_speed.X; 
        } 
        else if (ks.IsKeyDown(Keys.Right)) 
        { 
         linje_rect.X += (int)linje_speed.X; 
        } 
        else if (ks.IsKeyDown(Keys.Space)) 
        { 
         Start = true; 
        } 
    
        if (linje_rect.X > Window.ClientBounds.Width - linje_texture.Width) 
         linje_rect.X = (Window.ClientBounds.Width - linje_texture.Width); 
    
        if (linje_rect.X < 0) 
         linje_rect.X = 0; 
    
        if (linje_rect.Intersects(boll_rect)) 
        { 
         boll_speed.Y *= -1; 
        } 
    
        base.Update(gameTime); 
    } 
    
    protected override void Draw(GameTime gameTime) 
    { 
        GraphicsDevice.Clear(Color.Black); 
    
        spriteBatch.Begin(); 
        if (liv > 0) 
        { 
         spriteBatch.Draw(linje_texture, linje_rect, Color.White); 
         spriteBatch.Draw(boll_texture, boll_rect, Color.White); 
         spriteBatch.DrawString(spritefont, "Liv kvar: " + liv, Vector2.Zero, Color.White); 
         spriteBatch.DrawString(spritefont, "Points: " + poang, new Vector2(350, 0), Color.White); 
         spriteBatch.DrawString(spritefont, "Highscore: " + highscore, new Vector2(660, 0), Color.White); 
         foreach (Rectangle g in block) 
         { 
          spriteBatch.Draw(blockgrön_texture, g, Color.White); 
         } 
        } 
        if (liv == 0) 
        { 
         spriteBatch.Draw(gameover_texture, gameover_rect, Color.White); 
        } 
        spriteBatch.End(); 
    
        base.Draw(gameTime); 
    } 
    } 
    
这些部件

遇到问题:我想

 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; 
     } 
+0

因此,这段代码的哪个部分是你遇到问题? – UnholySheep

+0

乒乓球游戏中的球通常会根据桨的击球位置以及桨的速度而改变'y'速度(或'x'速度,我不确定桨的方向如何)。此外,似乎同样的问题已经[在gamedev上回答](http://gamedev.stackexchange.com/q/4253/38920)。这个答案的作者在[richardcarter.org](http://richardcarter.org/)上有一个演示游戏的主页。 – Groo

+0

我可以复制所有的代码,让你了解更多。基本上我想第一次让球直行。当我在开始时按空格键,但它会按不同的方向。现在它一直在上升和下降。我会查看该页面! –

回答

0

你的意思是你想要的球改变方向随机当它碰到窗口边界或线时。如果是这样的话:

您正在使用常量值来改变方向。添加一个随机数发生器来增加你的boll_speed。

例如,在伪代码中,而不是-1,执行random(-1)。

+0

Okey,所以我试图做到这一点,但有点混乱,因为你可以在我编辑的代码中看到。当我尝试单击SPACE释放球时,游戏崩溃:P –

+0

@LuckeGG,当按下SPACE按钮时,您仍然需要上下的常量。回到原来的代码:'boll_speed.Y * = -1'让你的球在Y轴上上下移动。如果它正在上升,它会下降。如果它正在下降,它会上升。 'boll_speed.X * = -1'让你的球左转或右转(在X轴上)。当球从墙壁或者桨上弹开时,你想给它一个除'-1'以外的值以使其以某个角度出现。试着用你的原始代码来看看会发生什么。然后用'random'进行实验。 – leanne

+0

谢谢你,我确实把它改成了'boll_speed.X = random.Next(-1,1);'那完美的。对你有点触动,现在它起作用了! :) –

1

Random.Next(-1)应该会给你出界的错误。

你可以使用类似Random.Next(-200, 0)/100.0f的东西,它会返回-2和0之间的负数(包括-2,不包括0)。

但请注意,这种乘法会随着时间的推移导致球的放慢。它可能会加速到当前速度的两倍,但它可以在一个步骤中减慢到接近0。所以我宁愿先在y方向上颠倒球的速度,如果你击中水平线,保持它与x相同,如果你击中垂直线,则反转x并保持y。然后做一个零均值常量的随机添加。

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. 
+0

嗯,如果你现在看我的代码。我已经修复了像你所说的一些事情。但它有点令人困惑,确切地说我需要做的事情。但它现在不会崩溃,球可能卡在左侧/右侧。但球没有任何时间,只是左右。 –

+0

@LuckeGG对不起,下次我会试着更清楚一点:) 它不是直线,因为你用6f而不是0初始化y的速度。把y的速度初始化为0,它将直接开始。 –

+0

没问题,但是当我将X设置为“0f”而不是“6f”时,按下SPACE时它会直线上下移动。但是现在它会一直持续下去,即使它撞上了顶层屋顶? :) –

2

我会给你通用的想法。请把它适应您的例子

比方说,你要移动到板上各个方向,现在想象你的板作为两维数组(所有的像素有一个x,y表示对板)

(0,0)表示棋盘的左上角,您的(宽度,高度)标记为右下角。您的球位于(x,y)位置的棋盘中间,其中x> 0和x <宽度(棋盘)和y> 0和y <高度。

您有以下

enter image description here

上的画面8不同的方向基现在是时候那些方向翻译到的逻辑结构。你需要为此创建一个数组,可以说我们想从NW方向开始。我将创建一个包含需要添加到(x,y)位置的数字的数组,以便继续按照您选择的方向前进。如果你看到我创建一个bidimentional阵列

enter image description here

现在,这是为你的想法伪代码:

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]; 
      } 
     } 
+0

那里有非常聪明的想法,但是没有比这更简单的解决方案。所以当按下SPACE时球首先向上移动,然后在随机方向移动? :) –

+0

是的,如果你想移动到N(直线向上),那么你需要将默认方向设置为1,然后按Space,int direction = 1 =>(以获得所有时间-1,0 ),那么你可以移动到其他人 – Zinov