Attack/Follow 我的播放器
Attack/Follow my player
我想创建一个游戏,您可以在其中尝试避开属于您 "attacking" 的物体(圆圈)。我这样做的方式:
- 首先计算对象和我的x_and y差值(在这段代码中AI_x是对象的x-location(AI_y: y-location)和x, y玩家的位置)
- 计算物体必须形成的角度才能直接朝向玩家(使用 Math.Atan(x 轴之间的差异/y 轴之间的差异))
-然后我使用这些角度来计算每个滴答必须移动的 x 和 y 距离(例如 distancePerTick_x = Math.Cos(angle) * speed)
然而,当我的 distanceToPlayer_x(见代码)为正数或 distanceToPlayer_x 和 distanceToPlayer_y 均为负数时,它不起作用,因此分位数再次变为正数。
我知道为什么会这样,但我已经尝试了 2 个小时来解决它,但再也无法忍受痛苦了:P.
希望大家帮帮我
代码:
namespace AI
{
public partial class mainClass : Form
{
//variables
int health;
double startPosition_x, startPosition_y, angle;
float x, y, speed, AI_x, AI_y, distanceToPlayer_x, distanceToPlayer_y;
//gameloop
Timer gameLoop;
//field
Bitmap bmp;
//player color
Pen playerColor;
//Random
Random random_x, random_y;
//constructor
public mainClass()
{
InitializeComponent();
Initialization();
}
//on startup do this:
private void Initialization()
{
//initializing variables
health = 100;
speed = (float) 0.75;
//startposition
startPosition_x = pbField.Width / 2;
startPosition_y = pbField.Height / 2;
//set x and y value to start position
x = (float) startPosition_x;
y = (float)startPosition_y;
//random
random_x = new Random();
random_y = new Random();
AI_x = 0;//random_x.Next(1, 550);
AI_y = 0;//random_y.Next(1, 320);
//player color
playerColor = new Pen(Color.Red, 5);
//gameloop
gameLoop = new Timer();
gameLoop.Tick += new EventHandler(gameMethode);
gameLoop.Interval = 5;
gameLoop.Start();
}
//the gameloop
private void gameMethode(object sender, EventArgs e)
{
//field
bmp = new Bitmap(550, 320);
pbField.Image = bmp;
using (Graphics g = Graphics.FromImage(bmp))
{
//key manager
if (InputManager.pressed(Keys.W))
{
y -= speed;
}
else if (InputManager.pressed(Keys.S))
{
y += speed;
}
else if (InputManager.pressed(Keys.A))
{
x -= speed;
}
else if (InputManager.pressed(Keys.D))
{
x += speed;
}
g.DrawEllipse(playerColor, AI_x, AI_y, 5, 5);
g.DrawEllipse(playerColor, x, y, 5, 5);
attackPlayer();
}
}
//attack player method
private void attackPlayer()
{
//the distance to the player
distanceToPlayer_x = AI_x - x;
distanceToPlayer_y = AI_y - y;
angle = Math.Atan(distanceToPlayer_y / distanceToPlayer_x);
Console.WriteLine("Radiants: " + angle);
Console.WriteLine("Degrees: "+ angle * (180 / Math.PI));
AI_x += (float) Math.Cos(angle) * (speed/(float)1.25);
AI_y += (float) Math.Sin(angle) * (speed/(float)1.25);
}
//if keydown
private void mainClass_KeyDown(object sender, KeyEventArgs e)
{
//if escape button is pressed
if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
InputManager.changeState(e.KeyCode, true);
}
//if keyup
private void mainClass_KeyUp(object sender, KeyEventArgs e)
{
InputManager.changeState(e.KeyCode, false);
}
}
}
在计算整个范围 [0..2 * pi]
角度时,典型的解决方案涉及Atan2
方法:
angle = Math.Atan2(distanceToPlayer_y, distanceToPlayer_x);
见https://msdn.microsoft.com/en-us/library/system.math.atan2(v=vs.110).aspx
详情
我想创建一个游戏,您可以在其中尝试避开属于您 "attacking" 的物体(圆圈)。我这样做的方式:
- 首先计算对象和我的x_and y差值(在这段代码中AI_x是对象的x-location(AI_y: y-location)和x, y玩家的位置)
- 计算物体必须形成的角度才能直接朝向玩家(使用 Math.Atan(x 轴之间的差异/y 轴之间的差异)) -然后我使用这些角度来计算每个滴答必须移动的 x 和 y 距离(例如 distancePerTick_x = Math.Cos(angle) * speed)
然而,当我的 distanceToPlayer_x(见代码)为正数或 distanceToPlayer_x 和 distanceToPlayer_y 均为负数时,它不起作用,因此分位数再次变为正数。
我知道为什么会这样,但我已经尝试了 2 个小时来解决它,但再也无法忍受痛苦了:P.
希望大家帮帮我
代码:
namespace AI
{
public partial class mainClass : Form
{
//variables
int health;
double startPosition_x, startPosition_y, angle;
float x, y, speed, AI_x, AI_y, distanceToPlayer_x, distanceToPlayer_y;
//gameloop
Timer gameLoop;
//field
Bitmap bmp;
//player color
Pen playerColor;
//Random
Random random_x, random_y;
//constructor
public mainClass()
{
InitializeComponent();
Initialization();
}
//on startup do this:
private void Initialization()
{
//initializing variables
health = 100;
speed = (float) 0.75;
//startposition
startPosition_x = pbField.Width / 2;
startPosition_y = pbField.Height / 2;
//set x and y value to start position
x = (float) startPosition_x;
y = (float)startPosition_y;
//random
random_x = new Random();
random_y = new Random();
AI_x = 0;//random_x.Next(1, 550);
AI_y = 0;//random_y.Next(1, 320);
//player color
playerColor = new Pen(Color.Red, 5);
//gameloop
gameLoop = new Timer();
gameLoop.Tick += new EventHandler(gameMethode);
gameLoop.Interval = 5;
gameLoop.Start();
}
//the gameloop
private void gameMethode(object sender, EventArgs e)
{
//field
bmp = new Bitmap(550, 320);
pbField.Image = bmp;
using (Graphics g = Graphics.FromImage(bmp))
{
//key manager
if (InputManager.pressed(Keys.W))
{
y -= speed;
}
else if (InputManager.pressed(Keys.S))
{
y += speed;
}
else if (InputManager.pressed(Keys.A))
{
x -= speed;
}
else if (InputManager.pressed(Keys.D))
{
x += speed;
}
g.DrawEllipse(playerColor, AI_x, AI_y, 5, 5);
g.DrawEllipse(playerColor, x, y, 5, 5);
attackPlayer();
}
}
//attack player method
private void attackPlayer()
{
//the distance to the player
distanceToPlayer_x = AI_x - x;
distanceToPlayer_y = AI_y - y;
angle = Math.Atan(distanceToPlayer_y / distanceToPlayer_x);
Console.WriteLine("Radiants: " + angle);
Console.WriteLine("Degrees: "+ angle * (180 / Math.PI));
AI_x += (float) Math.Cos(angle) * (speed/(float)1.25);
AI_y += (float) Math.Sin(angle) * (speed/(float)1.25);
}
//if keydown
private void mainClass_KeyDown(object sender, KeyEventArgs e)
{
//if escape button is pressed
if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
InputManager.changeState(e.KeyCode, true);
}
//if keyup
private void mainClass_KeyUp(object sender, KeyEventArgs e)
{
InputManager.changeState(e.KeyCode, false);
}
}
}
在计算整个范围 [0..2 * pi]
角度时,典型的解决方案涉及Atan2
方法:
angle = Math.Atan2(distanceToPlayer_y, distanceToPlayer_x);
见https://msdn.microsoft.com/en-us/library/system.math.atan2(v=vs.110).aspx 详情