0
点赞
收藏
分享

微信扫一扫

C# GUI弹球动画


学习慕课的资源来源:
​​​ https://www.icourse163.org/spoc/learn/GZU-1455988161​​ 创建一个弹球类:Ball.cs文件里

using System;
using System.Drawing;

namespace fallingBall
{
public enum ballState
{
Continue,
catchBall,
loseBall
}
public class Ball
{
private const int rectWidth = 300;
private const int rectHight = 600;

private const int radius = 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 Ball()
{
boardY = rectHight - boardHeight;
downStep = 10;
}
public Ball(int dstep)
{
downStep = dstep;
boardY = rectHight - boardHeight;
}
public int BallX {
set {
if (value < 0) ballX = 0;
else if (value > rectWidth - 2 * radius) ballX = rectWidth - 2 * radius;
else
ballX = value;
}
get { return ballX; }
}
public int BallY
{
set
{
if (value < 0) ballY = 0;
else if (value > rectHight - 2 * radius) ballY = rectHight - 2 * radius;
else ballY = value;
}
get { return ballY; }
}
public int BoardX
{
get { return boardX; }
set
{
if (value < 0) boardX = 0;
else if (value > rectWidth - boardWidth) boardX = rectWidth - boardWidth;
else
boardX = value;
}
}
public int BoardY
{
get { return boardY; }
set { boardY = value; }
}
public int Radius { get {return radius; } }
public int BoardWidth { get { return boardWidth; } }
public int BoardHeight { get { return boardHeight; } }
public ballState Judge()
{
if (BallY + 2 * radius < rectHight)// - boardHeight )
return ballState.Continue;
else
{
if (BallX + radius >= BoardX && BallX + radius <= BoardX + boardWidth )
return ballState.catchBall;
else
return ballState.loseBall;
}

}
public void NewGame()
{
Random r = new Random();
ballX = r.Next(0,rectWidth-2*radius);
ballY = 0;
}
public Rectangle GetRectangle()
{
return new Rectangle(0, 0, rectWidth, rectHight);
}
public int DownStep { get { return downStep; } }
}
}

Form1.cs文件内容:(我们需要点击Form1.cs[设计]的Time控件(在属性中设置毫秒数)、鼠标点击事件keydown、paint事件,draw事件, 以及点击属性,我觉得有点像去年学的mfc,不过感觉C#友好多了)

C# GUI弹球动画_动画


C# GUI弹球动画_控件_02


增加事件,根据Form1.cs里文件的事件配置对应的事件

C# GUI弹球动画_动画_03

using fallingBall;
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 第12章测试习题
{
public partial class Form1 : Form
{
Ball ball = new Ball();
private int leftX = 50;
private int leftY = 50;
public Form1()
{
InitializeComponent();
Width = 400;
Height = 800;
ball.NewGame();
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,
ball.GetRectangle().Width,
ball.GetRectangle().Height);//50 50 300 600
g.FillRectangle(blueBrush, gameRect);

SolidBrush whiteBrush = new SolidBrush(Color.White);
Rectangle ballRectangle = new Rectangle(leftX + ball.BallX,//50+X
leftY + ball.BallY, //50
2 * ball.Radius, //40
2 * ball.Radius);//40
g.FillEllipse(whiteBrush, ballRectangle);

SolidBrush redBrush = new SolidBrush(Color.Red);
Rectangle boardRect = new Rectangle(
leftX+ball.BoardX,//500
leftY+ball.BoardY,//50+(rectHight - boardHeight)即50+(600-10)
ball.BoardWidth,//40
ball.BoardHeight);//10
g.FillRectangle(redBrush, boardRect);
}

private void timer1_Tick(object sender, EventArgs e)
{
ball.BallY += ball.DownStep;
Invalidate();
if (ball.Judge() == ballState.catchBall)
{
ball.NewGame();
Invalidate();
}
if(ball.Judge() == ballState.loseBall)
{
timer1.Enabled = false;
MessageBox.Show("GameOver!!");
System.Environment.Exit(0); //这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。 参数0:代表程序正常退出;参数1:代表程序非正常退出。
}

}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
ball.BoardX -= 10;
}
if(e.KeyCode == Keys.Right)
{
ball.BoardX += 10;
}
Invalidate();
}
}
}

C# GUI弹球动画_点击事件_04


拓展:

C# GUI弹球动画_动画_05


举报

相关推荐

0 条评论