Приветствую обитателей форума и прошу помощи. C# начал изучать совсем недавно и до этого сталкивался только с Паскалем По заданию нужно разработать игру "Парные картинки" Приложение сделать получилось но работает как попало (пары не всегда убираются, парой открываются по 3 картинки и т.д.). Полагаю ошибка где то в логике обработки кликов по картинкам но как исправить не знаю.
| Код | 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 Курсовая { public partial class mein : Form { const int nw = 6; const int nh = 6; const int np = (nw * nh) / 2; System.Drawing.Graphics g; Bitmap pics; int cw, ch; int[,] field = new int[nw, nh]; int nopened = 0; int copened = 0; int[] open1 = new int[2]; int[] open2 = new int[2]; System.Windows.Forms.Timer timer1; private void cell(int i, int j) { int x, y; x = i * (cw + 2); y = j * (ch + 2) + meinmenu.Height; if (field[i, j] > 200) g.FillRectangle(SystemBrushes.Control, x, y, cw + 2, ch + 2); if ((field[i, j] > 100) && (field[i, j] < 200)) { g.DrawImage(pics, new Rectangle(x + 1, y + 1, cw, ch), new Rectangle((field[i, j] - 101) * cw, 0, cw, ch), GraphicsUnit.Pixel); g.DrawRectangle(Pens.Black, x + 1, y + 1, cw, ch); } if ((field[i, j] > 0) && (field[i, j] < 100)) { g.FillRectangle(SystemBrushes.Control, x + 1, y + 1, cw, ch); g.DrawRectangle(Pens.Black, x + 1, y + 1, cw, ch); } }
private void drawfield() { for (int i = 0; i < nw; i++) for (int j = 0; j < nh; j++) this.cell(i, j); }
private void newgame() { Random rnd; int rndn; rnd = new Random(); int[] buf = new int[np]; for (int i = 0; i < nw; i++) for (int j = 0; j < nh; j++) { do { rndn = rnd.Next(np) + 1; } while (buf[rndn - 1] == 2); field[i, j] = rndn; buf[rndn - 1]++; } nopened = 0; copened = 0; this.drawfield(); }
public mein() {
InitializeComponent(); this.MouseClick += new MouseEventHandler(mein_MouseClick); try { pics = new Bitmap("pictures.bmp"); } catch (Exception exc) { MessageBox.Show("Файл 'pictures.bmp' не найден.\n" + exc.ToString(), "Парные картинки", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); return; } cw = (int)(pics.Width / np); ch = pics.Height; this.ClientSize = new System.Drawing.Size(nw * (cw + 2) + 1, nh * (ch + 2) + 1 + meinmenu.Height); g = this.CreateGraphics();
timer1 = new Timer(); timer1.Tick += new System.EventHandler(this.timer1_Tick); timer1.Interval = 200; newgame(); } private void mein_MouseClick(object sender, MouseEventArgs e) { { int i, j; i = e.X / (cw + 3); j = (e.Y - meinmenu.Height) / (ch + 3); if ((timer1.Enabled) && (field[i, j] > 200)) { return; } if (field[i, j] > 200) return; if (copened == 0) { copened++; open1[0] = 1; open1[1] = j; field[i, j] += 100; this.cell(i, j); return; } if (copened == 1) { open2[0] = i; open2[1] = j; if ((open1[0] == open2[0]) && (open1[1] == open2[1])) return; else { copened++; field[i, j] += 100; this.cell(i, j); if (field[open1[0], open1[1]] == field[open2[0], open2[1]]) { nopened++; copened = 0; timer1.Enabled = true; } } return; } if (copened == 2) { field[open1[0], open1[1]] -= 100; field[open2[0], open2[1]] -= 100; this.cell(open1[0], open1[1]); this.cell(open2[0], open2[1]); open1[0] = i; open1[1] = j; copened = 1; field[i, j] += 100; this.cell(i, j); } } }
private void newgameToolStripMenuItem_Click(object sender, EventArgs e) { newgame(); } private void timer1_Tick(object sender, System.EventArgs e) { this.cell(open1[0], open1[1]); this.cell(open2[0], open2[1]); timer1.Enabled = false; if (nopened == np) { MessageBox.Show("Вы спавились с поставленной задачей!"); } } private void mein_Paint(object sender, PaintEventArgs e) { drawfield(); }
private void infoprogramToolStripMenuItem_Click(object sender, EventArgs e) { AboutBox1 aboutbox; aboutbox = new AboutBox1(); aboutbox.Show(); } } }
|
|