该实例演示了如何把逻辑代码和显示代码分开写到两个类中
其中控件用到了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引用问题时--程序集