Регистрация не е нужна, освен при създаване на тема в "Задача на седмицата".

Нещо като стрелба по цел на C#

Нещо като стрелба по цел на C#

Мнениеот Добромир Глухаров » 10 Мар 2019, 21:24

Долната програма изобразява таблица от 16х16 правоъгълничета, зад едно от които е целта. Като кликнете на някое правоъгълниче, което не е целта, в него се изобразява число, показващо на колко единици е от целта. Имате право на 7 опита, при неуспех играта приключва с лош резултат.
Ако обаче откриете целта навреме, печелите текущата игра.

Код: Избери целия код
/*
 * Created by SharpDevelop.
 * User: Добромир
 * Date: 10.3.2019 г.
 * Time: 19:20 ч.
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Цел
{
   /// <summary>
   /// Description of MainForm.
   /// </summary>
   public partial class MainForm : Form
   {
      string[,] Pole;
      int Opiti, TargetRow, TargetCol;
      bool OK, EndTime;
      public MainForm()
      {
         //
         // The InitializeComponent() call is required for Windows Forms designer support.
         //
         InitializeComponent();
         
         //
         // TODO: Add constructor code after the InitializeComponent() call.
         //
         
         Pole = new string[16,16];
         for(int row = 0; row < 16; row++)
            for(int col = 0; col < 16; col++)
               Pole[row, col] = "????";
         Opiti = 7;
         OK = EndTime = false;
         
         Random rnd= new Random();
         TargetRow = rnd.Next(16);
         TargetCol = rnd.Next(16);
      }
      void MainFormKeyPress(object sender, KeyPressEventArgs e)
      {
         if(e.KeyChar == (char)27) Application.Exit();
      }
      void MainFormPaint(object sender, PaintEventArgs e)
      {
         Color clr;
         e.Graphics.DrawString("Остават " + Opiti + " опита...", new Font("Arial", 32.0f),
                               new SolidBrush(Color.Red), 128.0f, 8.0f);
         for(int row = 0; row < 16; row++)
            for(int col = 0; col < 16; col++)
         {
            e.Graphics.DrawRectangle(new Pen(Color.Brown, 2.7f),
                                     40 * row + 3, 128 + 32 * col + 3,
                                     34, 26);
            clr = Color.Yellow;
            if(Pole[row, col] == "OK! ") clr = Color.Red;
            else if(Pole[row, col] != "????") clr = Color.Green;
            e.Graphics.DrawString(Pole[row, col], new Font("Arial", 9.0f),
                                  new SolidBrush(clr),
                                  40 * row + 5, 128 + 32 * col + 7);
         }
         if(EndTime)
         {
            e.Graphics.FillRectangle(new SolidBrush(Color.White),0,0,639,639);
            e.Graphics.DrawString("BAD JOB!", new Font("Arial", 64.0f),
                                  new SolidBrush(Color.Red), 120.0f, 260.0f);
         }
         if(OK)
         {
            e.Graphics.FillRectangle(new SolidBrush(Color.White),0,0,639,639);
            e.Graphics.DrawString("OK!", new Font("Arial", 64.0f),
                                  new SolidBrush(Color.Red), 260.0f, 260.0f);
         }
      }
      void MainFormMouseDown(object sender, MouseEventArgs e)
      {
         int row, col;
         double dlt;
         if(e.Y > 128)
         {
            row = e.X / 40;
            col = (e.Y - 128) / 32;
            dlt = Math.Sqrt((row - TargetRow) * (row - TargetRow) + (col - TargetCol) * (col - TargetCol));
            if(dlt < 0.9)
            {
               Pole[row, col] = "OK! ";
               OK = true;
            }
            else
               Pole[row, col] = ("" + dlt).Substring(0,Math.Min(4,(""+dlt).Length));
            Opiti--;
            if(Opiti <= 0)
               EndTime = true;
         }
         this.Invalidate();
      }
   }
}


Цел.rar
(32.3 KiB) 94 пъти
Аватар
Добромир Глухаров
Математик
 
Мнения: 2080
Регистриран на: 11 Яну 2010, 13:23
Рейтинг: 2178

Re: Нещо като стрелба по цел на C#

Мнениеот Sup3rlum » 12 Мар 2019, 18:45

Реших малко да подобря, ама то играта се оказа безсмислена с моето допълнение :D

Код: Избери целия код
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 MineSweeper
{
    public partial class Form1 : Form
    {
        Graphics g;

        int a, b;

        int[,] s = new int[20, 20];

        int u, v;

        List<KeyValuePair<int, int>> pressed = new List<KeyValuePair<int, int>>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Random r = new Random();
            u = r.Next(20);
            v = r.Next(20);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            this.Invalidate();
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            pressed.Add(new KeyValuePair<int, int>(a-6,b));
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            (a, b) = ((e.X+16)/50, e.Y/50);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;

            g.Clear(Color.Gray);

            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    g.DrawRectangle(new Pen(Color.DarkGray,3), 284 + i*50, j*50, 50, 50);
                }
            }

            g.DrawRectangle(new Pen(Color.Yellow, 3), 284 + u * 50, v * 50, 50, 50);

            g.DrawRectangle(new Pen(Color.Red, 3),a * 50 - 16, b * 50, 50, 50);


            for (int f = 0; f < pressed.Count; f++)
            {
                int radius =
                    (int)Math.Ceiling(
                        Math.Sqrt(
                            (double)(
                            (pressed[f].Key*50 - 50*u-25) * (pressed[f].Key*50 - 50*u-25) +
                            (pressed[f].Value*50 - 50*v-25)*(pressed[f].Value*50 - 50*v-25)
                            )
                            )
                            );
                g.FillEllipse(new SolidBrush(Color.FromArgb(30,250,250,0)), new RectangleF(pressed[f].Key*50-radius+284,pressed[f].Value*50-radius, radius*2, radius*2));
            }           
        }
    }
}


Размерите на прозореца са 1300х1000
Sup3rlum
Фен на форума
 
Мнения: 247
Регистриран на: 19 Фев 2019, 02:08
Рейтинг: 347


Назад към C#, Java



Кой е на линия

Регистрирани потребители: Google [Bot]

Форум за математика(архив)