教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

面向对象程序设计实训(吃金豆游戏设计与实现)(3)

来源:网络收集 时间:2026-09-05
导读: //窗体大小改变时触发Resize事件 private void Form1_Resize(object sender, EventArgs e){ this.Invalidate(); //使窗体或控件无效,并人为引发其Paint事件,进行重绘 this.Update(); } 例二:在面板中绘制如下的

//窗体大小改变时触发Resize事件

private void Form1_Resize(object sender, EventArgs e){

this.Invalidate(); //使窗体或控件无效,并人为引发其Paint事件,进行重绘 this.Update(); }

例二:在面板中绘制如下的图形:

要求:在文本框中输入行和列,单击clear按钮,对应位置的小金豆被清除。

法一:直接在面板中绘图

private void panel1_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; int r, c;

r = c = 8; //r行,c列

int h, w; //每个方块的高度和宽度 w = panel1.Width / c; h = panel1.Height / r;

for (int i = 0; i < r; i++){ for (int j = 0; j < c; j++){

g.DrawRectangle(Pens.Blue, j * w, i * h, w, h); g.FillEllipse( Brushes.Yellow,

j * w + (w - 20) / 2, i * h + (h - 20) / 2, 20, 20);

} } }

private void btnClear_Click(object sender, EventArgs e){ int r = int.Parse(txtRow.Text); int c = int.Parse(txtCol.Text); int w = panel1.Width / 8; int h = panel1.Height / 8;

Graphics g = panel1.CreateGraphics();

Brush b1 = new SolidBrush(panel1.BackColor);

g.FillEllipse(b1, c * w + (w - 20) / 2, r *h +(h - 20) / 2, 20, 20); }

法二:将一个方块封装成一个Cell对象。

Cell -r:int //行 -c:int //列 -w:int //宽度 -h:int //高度 +Cell(int r,int c,int w,int h); +Draw(Graphics g):void +Clear(Graphics g,Color foreColor):void class Cell{

private int r,c,w,h;

public Cell(int r, int c, int w, int h){ this.r = r; this.c = c; this.w = w; this.h = h; }

public void Draw(Graphics g){ int x, y; x = w * c; y = h * r;

g.DrawRectangle(Pens.Blue, x, y, w, h);

g.FillEllipse(Brushes.Yellow, x + (w - 20) / 2, y + (h - 20) / 2, 20, 20); }

public void Clear(Graphics g, Color foreColor){ int x, y; x = w * c; y = h * r;

g.FillEllipse(new SolidBrush(foreColor), x, y, w, h); } }

在Form1类中如下定义:

private Cell[,] cellArr; public Form1(){

InitializeComponent();

面向对象程序设计实训(吃金豆游戏设计与实现)(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/596690.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)