C#如何沿矩形路径移动圆
我需要在按钮的命令下沿矩形路径移动一个点。我希望它从矩形路径的右上角开始,但我不知道如何让它沿着路径一直走到原点。屏幕以用户在输入框中提供的速度刷新。提前谢谢你C#如何沿矩形路径移动圆,c#,winforms,input,path,C#,Winforms,Input,Path,我需要在按钮的命令下沿矩形路径移动一个点。我希望它从矩形路径的右上角开始,但我不知道如何让它沿着路径一直走到原点。屏幕以用户在输入框中提供的速度刷新。提前谢谢你 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace Assignment_2
{
public partial class Form1 : Form
{
private const int formwidth = 1280;
private const int formheight = 720;
private const int ball_a_radius = 10;
private const int horizontaladjustment = 8;
private const double ball_a_distance_moved_per_refresh = 1.6;
private double ball_a_real_coord_x = 515;
private double ball_a_real_coord_y = 40;
private int ball_a_int_coord_x;
private int ball_a_int_coord_y;
private const double graphicrefreshrate = 30.0;
private static System.Timers.Timer graphic_area_refresh_clock = new System.Timers.Timer();
private static System.Timers.Timer ball_a_control_clock = new System.Timers.Timer();
private bool ball_a_clock_active = false;
public double speed = 0;
public Form1()
{
InitializeComponent();
ball_a_int_coord_x = (int)(ball_a_real_coord_x);
ball_a_int_coord_y = (int)(ball_a_real_coord_y);
System.Console.WriteLine("Initial coordinates: ball_a_int_coord_x = {0}. ball_a_int_coord_y = {1}.",
ball_a_int_coord_x, ball_a_int_coord_y);
graphic_area_refresh_clock.Enabled = false;
graphic_area_refresh_clock.Elapsed += new ElapsedEventHandler(Updatedisplay);
ball_a_control_clock.Enabled = false;
ball_a_control_clock.Elapsed += new ElapsedEventHandler(Updateballa);
Startgraphicclock(graphicrefreshrate);
Startballaclock(speed);
}
public class NumericTextBox : TextBox
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
private void panel2_Paint_1(object sender, PaintEventArgs e)
{
//Create pen
Pen blackPen = new Pen(Color.Black, 1);
//Create rectangle
Rectangle rect = new Rectangle(125, 50, 400, 400);
//Draw rectangle to screen
e.Graphics.DrawRectangle(blackPen, rect);
Graphics graph = e.Graphics;
graph.FillEllipse(Brushes.Green, ball_a_int_coord_x, ball_a_int_coord_y, 2 * ball_a_radius, 2 * ball_a_radius);
base.OnPaint(e);
}
public void button7_Click(object sender, EventArgs e)
{
speed = Convert.ToInt32(textBox3.Text);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
protected void Startgraphicclock(double refreshrate)
{
double elapsedtimebetweentics;
if (refreshrate < 1.0) refreshrate = 1.0;
elapsedtimebetweentics = 1000.0 / refreshrate;
graphic_area_refresh_clock.Interval = (int)System.Math.Round(elapsedtimebetweentics);
graphic_area_refresh_clock.Enabled = true;
}
protected void Startballaclock(double updaterate)
{
double elapsedtimebetweenballmoves;
if (updaterate < 1.0) updaterate = 1.0;
elapsedtimebetweenballmoves = 1000.0 / updaterate;
ball_a_control_clock.Interval = (int)System.Math.Round(elapsedtimebetweenballmoves);
ball_a_control_clock.Enabled = true;
ball_a_clock_active = true;
}
protected void Updatedisplay(System.Object sender, ElapsedEventArgs evt)
{
Invalidate();
if (!(ball_a_clock_active))
{
graphic_area_refresh_clock.Enabled = false;
System.Console.WriteLine("The graphical area is no longer refreshing. You may close the window.");
}
}
protected void Updateballa(System.Object sender, ElapsedEventArgs evt)
{
ball_a_real_coord_x = ball_a_real_coord_x - 5;
ball_a_real_coord_y = ball_a_real_coord_y - 5;
ball_a_int_coord_x = (int)System.Math.Round(ball_a_real_coord_x);
ball_a_int_coord_y = (int)System.Math.Round(ball_a_real_coord_y);
if (ball_a_int_coord_x >= formwidth || ball_a_int_coord_y + 2 * ball_a_radius <= 0 || ball_a_int_coord_y >= formheight)
{
ball_a_clock_active = false;
ball_a_control_clock.Enabled = false;
System.Console.WriteLine("The clock controlling ball a has stopped.");
}
}
private void button4_Click(object sender, EventArgs e)
{
ball_a_control_clock.Enabled = true;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统计时器;
名称空间分配\u 2
{
公共部分类Form1:Form
{
private const int formwidth=1280;
私有常量int formheight=720;
私人康斯特球半径=10;
私有常量int水平调整=8;
私人康斯特双人球每次刷新移动距离=1.6;
私人双人球a__real_coord_x=515;
私人双人球a__real_coord_y=40;
私人国际足球俱乐部;
私人国际足球俱乐部;
私人常数双速率=30.0;
私有静态System.Timers.Timer图形_区域_刷新_时钟=新系统.Timers.Timer();
私有静态System.Timers.Timer ball_a_control_clock=新系统.Timers.Timer();
私人布尔球、时钟、活动=假;
公共双速=0;
公共表格1()
{
初始化组件();
ball_a_int_coord_x=(int)(ball_a_real_coord_x);
ball_a_int_coord_y=(int)(ball_a_real_coord_y);
System.Console.WriteLine(“初始坐标:ball_a_int_coord_x={0}。ball_a_int_coord_y={1}.”,
ball_a_int_coord_x,ball_a_int_coord_y);
图形\u区域\u刷新\u clock.Enabled=false;
graphic_area_refresh_clock.Passed+=新的ElapsedEventHandler(更新显示);
ball\u a\u control\u clock.Enabled=false;
ball_a_control_clock.appeased+=新的ElapsedEventHandler(updateball);
开始图形时钟(图形刷新速率);
开始时钟(速度);
}
公共类NumericTextBox:TextBox
{
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有void panel2_Paint(对象发送器、PaintEventArgs e)
{
}
私有空白面板2_Paint_1(对象发送器,PaintEventArgs e)
{
//画笔
钢笔黑色钢笔=新钢笔(Color.Black,1);
//创建矩形
矩形rect=新矩形(125,50,400,400);
//在屏幕上绘制矩形
e、 图形.绘图矩形(黑笔,矩形);
图形=e.图形;
图形填充椭圆(画笔.Green,ball_a_int_coord_x,ball_a_int_coord_y,2*ball_a_半径,2*ball_a_半径);
基础漆(e);
}
公共无效按钮7_单击(对象发送者,事件参数e)
{
速度=转换为32(textBox3.Text);
}
私有void textBox3\u TextChanged(对象发送方,事件参数e)
{
}
受保护的无效开始图形时钟(双刷新率)
{
两组之间的双延时时间;
如果(刷新率<1.0)刷新率=1.0;
ELAPSEDTIMETBETWEENTICS=1000.0/刷新率;
graphic_area_refresh_clock.Interval=(int)System.Math.Round(elapsedtimebetweentics);
graphic_area_refresh_clock.Enabled=真;
}
受保护的无效启动时钟(双重更新速率)
{
球移动之间的双延迟时间;
如果(updaterate<1.0)updaterate=1.0;
elapsedtimebetweenballmoves=1000.0/更新率;
ball\u a\u control\u clock.Interval=(int)System.Math.Round(球移动之间的延迟时间);
ball\u a\u control\u clock.Enabled=真;
ball_a_clock_active=真;
}
受保护的void Updatedisplay(System.Object sender,ElapsedEventArgs evt)
{
使无效();
如果(!(球\时钟\活动))
{
图形\u区域\u刷新\u clock.Enabled=false;
System.Console.WriteLine(“图形区域不再刷新。您可以关闭窗口。”);
}
}
受保护的void Updateballa(System.Object发送方,ElapsedEventArgs evt)
{
ball_a_real_coord_x=ball_real_coord_x-5;
ball_a_real_coord_y=ball_real_coord_y-5;
ball_a_int_coord_x=(int)System.Math.Round(ball_a_real_coord_x);
ball_a_int_coord_y=(int)System.Math.Round(ball_a_real_coord_y);
如果(ball_a_int_coord_x>=formwidth | | ball_a_int_coord_y+2*ball_a_半径=formheight)
{
ball_a_clock_active=错误;
ball\u a\u control\u clock.Enabled=false;
System.Console.WriteLine(“控制球a的时钟已停止”);
}
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
ball\u a\u control\u clock.Enabled=真;
}
}
}
我有一种更直接的方法来移动圆。你的代码太长,我看不懂。看看你是否喜欢这个
如果我是你,我会用一个图片盒
。我首先创建一个圆的图像,然后将该图像放入图片框中。然后,您可以使用计时器更改图片盒的位置
您应该将计时器的间隔设置为33 ms,大约为30 fps。这是您对计时器进行编程的方式:
保留一个计数器以指示圆移动了多少像素。假设您希望它在100px x 50px的矩形路径中移动
For every 33ms,
If the counter is less than 100,
increase the X position and the counter by 1,
if the counter is between 101 and 150,
increase the Y position and the counter by 1,
if the counter is between 151 and 250,
decrease the X position by 1 and increment the counter
if the counter is between 251 and 300,
decrease the Y position by 1 and increment the counter
if the counter is greater than 300,
stop the timer
我真的不喜欢用OnPaint
事件在屏幕上画东西。我是说,我是说