如何向不同方向移动球?
How to move the ball in different directions?
现在当我按下空格键时它会向左或向右移动,我希望球先直线移动 time.Then 当球撞到墙、块或线之后我希望球随机移动以某种方式使用“-1”。这是我的第一个学校游戏项目,它是一个单线乒乓球游戏。
编辑:编辑我添加了 "boll_speed.X = random.Next(-1, 1);",效果非常好!
- linje = 行
- liv = 生命
- boll = 球
- poang = 分
- 我现在不使用 "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;
}
我想你的意思是你希望球在击中 window 边界或线时随机改变方向。如果是:
您正在使用常量值来改变方向。添加一个随机化器以将您的 boll_speed 乘以。
例如,在伪代码中,执行 random(-1) 而不是 -1。
Random.Next(-1)
应该给你越界错误。
你可以使用类似Random.Next(-200, 0)/100.0f
的东西,它会return一个介于-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.
我给你大概的思路。请根据您的示例进行调整
假设您想移动到棋盘上的所有方向,现在将您的棋盘想象成一个二维数组(您的所有像素在该棋盘上都有一个 x,y 表示)
(0,0) 标记板的左上角,(width, height) 标记右下角。你的球位于棋盘中间的 (x, y) 位置,其中 x > 0 且 x < 宽度(棋盘的宽度)且 y > 0 且 y < 高度。
根据下图,你有8个不同的方向
现在是时候将这些说明转化为逻辑结构了。您需要为此创建一个数组,假设我们想从 NW 方向开始。我将创建一个数组,其中包含您需要添加到 (x, y) 位置的数字,以便继续朝着您选择的方向前进。如果你看到我正在创建一个双向数组
现在这是您的想法的伪代码:
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];
}
}
现在当我按下空格键时它会向左或向右移动,我希望球先直线移动 time.Then 当球撞到墙、块或线之后我希望球随机移动以某种方式使用“-1”。这是我的第一个学校游戏项目,它是一个单线乒乓球游戏。
编辑:编辑我添加了 "boll_speed.X = random.Next(-1, 1);",效果非常好!
- linje = 行
- liv = 生命
- boll = 球
- poang = 分
- 我现在不使用 "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;
}
我想你的意思是你希望球在击中 window 边界或线时随机改变方向。如果是:
您正在使用常量值来改变方向。添加一个随机化器以将您的 boll_speed 乘以。
例如,在伪代码中,执行 random(-1) 而不是 -1。
Random.Next(-1)
应该给你越界错误。
你可以使用类似Random.Next(-200, 0)/100.0f
的东西,它会return一个介于-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.
我给你大概的思路。请根据您的示例进行调整
假设您想移动到棋盘上的所有方向,现在将您的棋盘想象成一个二维数组(您的所有像素在该棋盘上都有一个 x,y 表示)
(0,0) 标记板的左上角,(width, height) 标记右下角。你的球位于棋盘中间的 (x, y) 位置,其中 x > 0 且 x < 宽度(棋盘的宽度)且 y > 0 且 y < 高度。
根据下图,你有8个不同的方向
现在是时候将这些说明转化为逻辑结构了。您需要为此创建一个数组,假设我们想从 NW 方向开始。我将创建一个数组,其中包含您需要添加到 (x, y) 位置的数字,以便继续朝着您选择的方向前进。如果你看到我正在创建一个双向数组
现在这是您的想法的伪代码:
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];
}
}