C# GUI+绘图系统---下落的小球

该实例演示了如何把逻辑代码和显示代码分开写到两个类中

其中控件用到了Timer,时刻更新小球位置并且判断

生成逻辑代码FallBall.dll

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FallBall
{
    public enum ballState{
        Continue,
        catchBall,
        loseBall
    }
    public class SmallBall
    {
        private const int rectWidth = 300;
        private const int recHeight = 600;
        private const int radix = 20;
        private int boardWidth = 40;
        private int boardHeight = 10;
        private int ballX = 0;
        private int ballY = 0;
        private int boardX = 0;
        private int boardY;
        private int downStep;
        public SmallBall()
        {
            boardY = recHeight - boardHeight;

            downStep = 10;
        }
        public SmallBall(int dstep)
        {
            boardY = recHeight - boardHeight;
            downStep = dstep;
        }
        public int BallX
        {
            get
            {
                return ballX;
            }
            set
            {
                if (value < 0)
                    ballX = 0;
                if (value > rectWidth - 2 * radix)
                    ballX = rectWidth - 2 * radix;
                else
                    ballX = value;
            }
        }
        public int BallY
        {
            get
            {
                return ballY;
            }
            set
            {
                ballY = value;
            }
        }
        public int BoardX
        {
            get
            {
                return boardX;
            }
            set
            {
                if (value < 0)
                    boardX = 0;
                if (value > rectWidth - boardWidth)
                    boardX = rectWidth - boardWidth;
                else
                    boardX = value;
            }
        }

        public int BoardY
        {
            get { return boardY; }
            set { boardY = value; }
        }
        public ballState Judge()
        {
            if (ballY < recHeight - boardHeight)
                return ballState.Continue;
            if (ballY >= recHeight - boardHeight)
            {
                if (BallX + radix >= BoardX && BallX + radix <= BoardX + boardWidth)
                    return ballState.catchBall;
                else
                    return ballState.loseBall;
            }

            return ballState.loseBall;
        }
        public void NewBall()
        {
            Random rand = new Random();
            BallX = rand.Next(0, rectWidth-2*radix);
            ballY = 0;
        }
 
        //rectangle
        public Rectangle GetGameRectangle()
        {
            return new Rectangle(0, 0, rectWidth, recHeight);

        }
        public int Radix { get { return radix; } }

    //board
        public int BoardWide
        {
            get{
                return boardWidth;
            }
        }
        public int BoardHeight { get { return boardHeight; } }

        public int DownStep { get { return downStep; } }
    }
}

显示代码:

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 FallBall;
namespace FallDown
{
    public partial class Form1 : Form
    {
        SmallBall smallBall = new SmallBall();
        private int leftX = 50;
        private int leftY = 50;

        public Form1()
        {
            InitializeComponent();
            Width = 400;
            Height = 800;
            smallBall.NewBall();
            //时间间隔
            timer1.Interval=300;
            //打开计时器
            timer1.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            Graphics g = e.Graphics;
            Rectangle gameRect = new Rectangle(leftX, leftY, smallBall.GetGameRectangle().Width, smallBall.GetGameRectangle().Height);
            g.FillRectangle(blueBrush, gameRect);

            SolidBrush whiteBrush = new SolidBrush(Color.White);
            Rectangle ballRectangle = new Rectangle(leftX+smallBall.BallX,leftY+smallBall.BallY,2*smallBall.Radix,2*smallBall.Radix);
            g.FillEllipse(whiteBrush, ballRectangle);

            SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
            Rectangle boardRectangle = new Rectangle(leftX + smallBall.BoardX, leftY + smallBall.BoardY, smallBall.BoardWide, smallBall.BoardHeight);
            g.FillRectangle(yellowBrush, boardRectangle);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            smallBall.BallY += smallBall.DownStep;
            Invalidate();
            if (smallBall.Judge() == ballState.catchBall)
            {
                smallBall.NewBall();
                Invalidate();
                //return;
            }
            if (smallBall.Judge() == ballState.loseBall)
            {
                timer1.Enabled = false;
                MessageBox.Show("GameOver!");
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //键盘消息j键左右
            if (e.KeyCode == Keys.Left)
            {
                smallBall.BoardX -= 10;
            }
            if (e.KeyCode == Keys.Right)
            {
                smallBall.BoardX += 10;
            }
            Invalidate();
        }
    }
}

 

结果:

其中Rectangle绘图时出现System.Drawing引用问题时--程序集

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值