用程序模拟小球的下落

shijies 2024-12-04 09:37:52

在控件顶部画出三个小球,用定时器刷新,显示小球逐渐垂直下落,当小球落到底部时,再将小球的Y坐标置0,希望显示小球不断从顶部落下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;


namespace WindowsFormsApp1
{
    public class Ball
    {
        public int x;//坐标
        public int y;
        public int r;//半径
        public int direction = 0; //方向,0表示停止,1表示向上走,2下,3左,4右
        public System.Timers.Timer timer;//球的timer
        //构造函数
        public Ball(int x, int y, int r)
        {
            this.x = x;
            this.y = y;
            this.r = r;
            timer = new System.Timers.Timer();
            timer.Interval = 30;
            timer.AutoReset = true;
        }
        public Ball() { }
        //将自己画到gr上
        public void Show(Graphics gr)
        {
            Rectangle rt = new Rectangle(x - r, y - r, 2 * r, 2 * r);
            gr.FillEllipse(Brushes.Red, rt);
        }

    }

}

 

 

 

 

 

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;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Ball myBall1, myBall2, myBall3; //自己
        public Graphics gr1,gr2,gr3; //PicShow的Graphics对象

   

     

 

        private void Form1_Load(object sender, EventArgs e)
        {
            myBall1 = new Ball(100, 0, 10);
            gr1 = pictureBox1.CreateGraphics();

            myBall2 = new Ball(200, 0, 10);
            gr2 = pictureBox1.CreateGraphics();

            myBall3 = new Ball(300, 0, 10);
            gr3 = pictureBox1.CreateGraphics();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            GameShow();
        }

        public void GameShow() //游戏画面画出
        {
            //###位图缓冲,防止画面闪烁

            //创建位图
            Bitmap bmp = new Bitmap(600, 600);
            //位图的Graphics对象
            Graphics gr1 = Graphics.FromImage(bmp);
            //将位图清黑
            gr1.Clear(Color.Black);

            //...[...在位图上画出要画的东西
            if (myBall1 != null)//自己控制的球
            {
                myBall1.Show(gr1);
            }
            else
            {
                myBall1 = new Ball(100, 0, 10);
                gr1 = pictureBox1.CreateGraphics();

                myBall1.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
                myBall1.timer.Start(); //myBall 开始受控制,wasd控制方向
                timer1.Start();//游戏时钟
                ball1Show();
            }
            if (myBall2 != null)//自己控制的球
            {
                myBall2.Show(gr1);
            }
            else
            {
                myBall2 = new Ball(200, 0, 10);
                gr1 = pictureBox1.CreateGraphics();

                myBall2.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
                myBall2.timer.Start(); //myBall 开始受控制,wasd控制方向
                timer2.Start();//游戏时钟
                myBall2.Show(gr1);
            }
            if (myBall3 != null)//自己控制的球
            {
                myBall3.Show(gr1);
            }
            else
            {
                myBall3 = new Ball(300, 0, 10);
                gr1 = pictureBox1.CreateGraphics();

                myBall3.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
                myBall3.timer.Start(); //myBall 开始受控制,wasd控制方向
                timer3.Start();//游戏时钟
                myBall3.Show(gr1);
            }

            //...]
            //将位图画到窗体的PictureBox控件
            Graphics gr2 = pictureBox1.CreateGraphics();
            gr2.DrawImage(bmp, 0, 0);
            //释放内存!!这一步必须要,不然内存会爆炸的
            bmp.Dispose();
            bmp = null;
            gr1.Dispose();
            gr1 = null;
            gr2.Dispose();
            gr2 = null;
            //刷新积分
            try
            {
    //            SetScore(score.ToString());
            }
            catch (Exception ex)
            {
        //        Console.WriteLine(ex);
            }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            GameShow();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            GameShow();
        }

    
     

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ball1Show();
            myBall1.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
            myBall1.timer.Start(); //myBall 开始受控制,wasd控制方向
            timer1.Start();//游戏时钟
          
            myBall2.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
            myBall2.timer.Start(); //myBall 开始受控制,wasd控制方向
            timer2.Start();//游戏时
       
            myBall3.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
            myBall3.timer.Start(); //myBall 开始受控制,wasd控制方向
            timer3.Start();//游戏时

        }

        public static void Delay1(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)
            {
                Application.DoEvents();
            }
        }


        private void TickMyTank(object sender, EventArgs e)//自己的myBall的时钟事件
        {
            myBall1.y += 5;
            myBall2.y += 5;
            myBall3.y += 5;
            if (myBall1.y >= pictureBox1.Height)
            {
                myBall1.y = 0;
                Delay1(2000);
            }
            if (myBall2.y >= pictureBox1.Height)
            {
                myBall2.y = 0;
                Delay1(2000);
            }
           
            if (myBall3.y >= pictureBox1.Height)
            {
                myBall3.y = 0;
                Delay1(2000);
            }
          
        }

 


        public void ball1Show() //游戏画面画出
        {
            //###位图缓冲,防止画面闪烁

            //创建位图
            Bitmap bmp = new Bitmap(600, 600);
            //位图的Graphics对象
            Graphics gr1 = Graphics.FromImage(bmp);
            //将位图清黑
            gr1.Clear(Color.Black);

            //...[...在位图上画出要画的东西
            if (myBall1 != null)//自己控制的球
            {
                myBall1.Show(gr1);
            }
            else
            {
                myBall1 = new Ball(100, 0, 10);
                gr1 = pictureBox1.CreateGraphics();
             
                myBall1.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
                myBall1.timer.Start(); //myBall 开始受控制,wasd控制方向
                timer1.Start();//游戏时钟
                ball1Show();
            }
            if (myBall2 != null)//自己控制的球
            {
                myBall2.Show(gr1);
            }
            else
            {
                myBall2 = new Ball(200, 0, 10);
                gr1 = pictureBox1.CreateGraphics();
            
                myBall2.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
                myBall2.timer.Start(); //myBall 开始受控制,wasd控制方向
                timer2.Start();//游戏时钟
                myBall2.Show(gr1);
            }
            if (myBall3 != null)//自己控制的球
            {
                myBall3.Show(gr1);
            }
            else
            {
                myBall3 = new Ball(300, 0, 10);
                gr1 = pictureBox1.CreateGraphics();

                myBall3.timer.Elapsed += new System.Timers.ElapsedEventHandler(TickMyTank);
                myBall3.timer.Start(); //myBall 开始受控制,wasd控制方向
                timer3.Start();//游戏时钟
                myBall3.Show(gr1);
            }
            //...]
            //将位图画到窗体的PictureBox控件
            Graphics gr2 = pictureBox1.CreateGraphics();
            gr2.DrawImage(bmp, 0, 0);
            //释放内存!!这一步必须要,不然内存会爆炸的
            bmp.Dispose();
            bmp = null;
            gr1.Dispose();
            gr1 = null;
            gr2.Dispose();
            gr2 = null;
            //刷新积分
            try
            {
       
            }
            catch (Exception ex)
            {
          
            }

        }

    }
}

...全文
123 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
shijies 2024-12-04
  • 打赏
  • 举报
回复

点击button1后,在控件的顶部出现三个小球,三个小球缓慢落下后,在延迟2秒后控件的顶部再次出现,但是小球并不是出现在顶部,而是顶部偏下,且三个小球出现的高度(Y坐标)并不一致。

111,044

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧