C# 在winform中检查动画是否位于屏幕边缘
我正在制作一个船只从windows窗体的一侧移动到另一侧的动画,但是当船只触及屏幕边缘时,我必须通知用户,因为他赢了。有人知道怎么做吗? 这就是我到目前为止所做的:C# 在winform中检查动画是否位于屏幕边缘,c#,winforms,animation,C#,Winforms,Animation,我正在制作一个船只从windows窗体的一侧移动到另一侧的动画,但是当船只触及屏幕边缘时,我必须通知用户,因为他赢了。有人知道怎么做吗? 这就是我到目前为止所做的: private void playerTimer_Tick(object sender, EventArgs e) { for (int i = 0; i < 6; i++) { if (PlayersActive[i]) {
private void playerTimer_Tick(object sender, EventArgs e)
{
for (int i = 0; i < 6; i++)
{
if (PlayersActive[i])
{
Players[i].Move(randomNumber.Next(3,10));
//HERE IS WHERE I SHOULD CHECK TO SEE IF A BIKE HAS WON
//If player location is at the edge of the screen
}
}
this.Invalidate();
}
private void playerTimer\u勾选(对象发送方,事件参数e)
{
对于(int i=0;i<6;i++)
{
如果(playerActive[i])
{
玩家[i]。移动(随机数。下一步(3,10));
//这是我应该检查的地方,看看自行车是否赢了
//如果播放器位置在屏幕边缘
}
}
这个。使无效();
}
移动船只的代码:
private void GameForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// Set variables used to store logic to default values.
int playersStillRacing = 0;
for (int i = 0; i < 6; i++)
{
// Check to see if the current player is still racing.
if (playersRacing[i])
{
// Incremement playersStillRacing.
playersStillRacing++;
// Set the playerPictureBox associated with this player to the new location
// the player is located at.
playerPictureBoxes[i].Image = Players[i].Image;
playerPictureBoxes[i].Left = Players[i].X;
playerPictureBoxes[i].Top = Players[i].Y;
g.DrawImage(playerPictureBoxes[i].Image, Players[i].X, Players[i].Y);
}
}
}
private void GameForm_Paint(对象发送者,PaintEventArgs e)
{
图形g=e.图形;
//将用于存储逻辑的变量设置为默认值。
int playersStillRacing=0;
对于(int i=0;i<6;i++)
{
//检查当前玩家是否还在比赛。
如果(玩家赛车[i])
{
//越来越多的玩家仍然在比赛。
PlayerStillRacing++;
//将与此播放器关联的playerPictureBox设置为新位置
//玩家位于。
PlayerPictureBoxs[i].Image=Players[i].Image;
PlayerPictureBoxs[i]。左=玩家[i]。X;
PlayerPictureBoxs[i].Top=玩家[i].Y;
g、 DrawImage(playerPictureBoxs[i].Image,Players[i].X,Players[i].Y);
}
}
}
这样做会奏效吗?我根据你给我的最新信息做了一些编辑
private void playerTimer_Tick(object sender, EventArgs e)
{
for (int i = 0; i < 6; i++)
{
if (PlayersActive[i])
{
Players[i].Move(randomNumber.Next(3,10));
// EDIT BELOW in the if statement!
// if the edge of the picture touches the end of the screen.
if (Players[i].X + Players[i].Image.Width >= this.Width)
MessageBox.Show("Congrats! Player" + i.ToString() + "won!");
//Players[i].X is the X cordinates(The length) from the left side of the winform.
//Players[i].Image.Width is the width of the picture ofcourse :D.
// if X cordinates + picture width is greater than or equal to screen width. YOU WON :D
}
}
this.Invalidate();
}
private void playerTimer\u勾选(对象发送方,事件参数e)
{
对于(int i=0;i<6;i++)
{
如果(playerActive[i])
{
玩家[i]。移动(随机数。下一步(3,10));
//在if语句中编辑以下内容!
//如果图片的边缘接触到屏幕的末端。
if(Players[i].X+Players[i].Image.Width>=此.Width)
MessageBox.Show(“恭喜!玩家”+i.ToString()+“赢了!”);
//玩家[i].X是winform左侧的X坐标(长度)。
//玩家[i].Image.Width是图片的宽度,当然是D。
//如果X cordinates+图片宽度大于或等于屏幕宽度。您赢了:D
}
}
这个。使无效();
}
或者你还想要别的吗?它涉及到你的
自行车的长度,你如何渲染你的自行车(直接使用一些控件还是仅仅绘制它?)要绘制自行车,我正在使用绘画事件,然后是drawImage,因为这是我从互联网图片中调整大小的图像。你如何增加玩家[I].X?如果使用播放器[i].X值和播放器[i].Image的宽度,则可以查看图像是否触及末端。使用playertimer_勾选,我将使表单无效,以便它可以使用新坐标重新绘制自身。如果以下编辑不起作用,请确定。然后试着显示整个代码:DWhat's left and width?我想是这样的,但我的变量players是一个包含玩家名字的数组。然后告诉我,你如何移动图片?你能告诉我全部代码吗?