Ana içeriğe atla

C# - Düşen Şeker Vurma Oyunu





KODLAR
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 WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Graphics tuval;
        Pen kalem = new Pen(Color.Black , 3 );
        int x, y, x1=0, y1=0, x2 = 0, y2 = 0,xk;
        bool atis = false;
        int px1, px2, px3, px4;
        int py1, py2, py3, py4;
        Random Rastgele = new Random();
        int puanlama = 0;

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            x = Cursor.Position.X;
            y = Cursor.Position.Y;
            Point P = PointToScreen(new Point(pictureBox1.Bounds.Left, pictureBox1.Bounds.Top));
            xk = x - P.X;
            x1 = Convert.ToInt16( Math.Abs(450 - (x - P.X)) / 5 );
            y1 = Convert.ToInt16( Math.Abs(400 - (y - P.Y)) / 5);
            x2 = 450;
            y2 = 400;
            atis = true;
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox2.Parent = pictureBox1;
            pictureBox2.BackColor = Color.Transparent;

            pictureBox3.Parent = pictureBox1;
            pictureBox3.BackColor = Color.Transparent;

            pictureBox4.Parent = pictureBox1;
            pictureBox4.BackColor = Color.Transparent;

            pictureBox5.Parent = pictureBox1;
            pictureBox5.BackColor = Color.Transparent;

            tuval = pictureBox1.CreateGraphics();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            timer1.Interval = 100;
            timer1.Start();

            px1 = Rastgele.Next(50,850);
            py1 = 0;
            pictureBox2.Location = new Point(px1,py1);

            px2 = Rastgele.Next(50, 850);
            py2 = 0;
            pictureBox3.Location = new Point(px2, py2);

            px3 = Rastgele.Next(50, 850);
            py3 = 0;
            pictureBox4.Location = new Point(px3, py3);

            px4 = Rastgele.Next(50, 850);
            py4 = 0;
            pictureBox5.Location = new Point(px4, py4);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Refresh();

            x = Cursor.Position.X;
            y = Cursor.Position.Y;
            Point P = PointToScreen(new Point(pictureBox1.Bounds.Left, pictureBox1.Bounds.Top));
            x = x - P.X;
            y = y - P.Y;

            if (atis)
            {
                if (xk < 450)
                {
                    x2 = x2 - x1;
                }
                else
                {
                    x2 = x2 + x1;
                }
                y2 = y2 - y1;
                tuval.DrawRectangle(kalem, x2, y2, 5, 5);
            }
            

            tuval.DrawLine(kalem, 450 , 400 , x , y);
            tuval.DrawLine(kalem, x - 25 , y , x + 25 , y);
            tuval.DrawLine(kalem, x , y - 25 , x , y + 25);
            tuval.DrawEllipse(kalem , x - 10 , y - 10 , 20 , 20);
            tuval.DrawArc(kalem , 425 , 375 , 50 , 50 , 270 , 360);

            //nesnelerin hareketinin saglanması
            py1 = py1 + Rastgele.Next(5,20);
            pictureBox2.Location = new Point(px1,py1);

            py2 = py2 + Rastgele.Next(5, 20);
            pictureBox3.Location = new Point(px2, py2);

            py3 = py3 + Rastgele.Next(5, 20);
            pictureBox4.Location = new Point(px3, py3);

            py4 = py4 + Rastgele.Next(5, 20);
            pictureBox5.Location = new Point(px4, py4);

            //oyun dışına çıkan nesnelerin başlangıç noktasından tekrar başlatılması
            if (py1 > 400)
            {
                py1 = -30;
                px1 = Rastgele.Next(50,850);
            }
            if (py2 > 400)
            {
                py2 = -30;
                px2 = Rastgele.Next(50, 850);
            }
            if (py3 > 400)
            {
                py3 = -30;
                px3 = Rastgele.Next(50, 850);
            }
            if (py4 > 400)
            {
                py4 = -30;
                px4 = Rastgele.Next(50, 850);
            }

            //vurma olayının gerçekleştirilmesi
            if (x2 > px1 && y2 > py1 && x2 < px1 + 30 && y2 < py1 + 30 )
            {
                py1 = -30;
                px1 = Rastgele.Next(50, 850);
                puanlama = puanlama + 10;
            }
            if (x2 > px2 && y2 > py2 && x2 < px2 + 30 && y2 < py2 + 30)
            {
                py2 = -30;
                px2 = Rastgele.Next(50, 850);
                puanlama = puanlama + 10;
            }
            if (x2 > px3 && y2 > py3 && x2 < px3 + 30 && y2 < py3 + 30)
            {
                py3 = -30;
                px3 = Rastgele.Next(50, 850);
                puanlama = puanlama + 10;
            }
            if (x2 > px4 && y2 > py4 && x2 < px4 + 30 && y2 < py4 + 30)
            {
                py4 = -30;
                px4 = Rastgele.Next(50, 850);
                puanlama = puanlama + 10;
            }
            label2.Text = Convert.ToString(puanlama);
        }
    }
}

Yorumlar

Bu blogdaki popüler yayınlar

Matlab Operatörler

Matematiksel operatörler: +  Toplama –  Çıkarma *  Çarpma /  Bölme ^  Üst alma .* Elemanter çarpım ./  Elemanter bölme .^  Elemanter üst alma a+b  Boyutları aynı olan a ve b matrisini toplar. a-b  Boyutları aynı olan a ve b matrislerinin farkını alır. a*b  Sütun sayısı m olan a matrisiyle satır sayısı m olan b matrisini çarpar. a/b  b düzenli kare bir matrisse (determinantı sıfırdan farklıysa), aynı boyutlu a matrisiyle; a*inv(b)işlemini yapar. a.*b  Boyutları aynı olan a ve b matrislerinin elemanlarını karşılıklı olarak çarpar. a./b  Boyutları aynı olan a ve b matrislerinin elemanlarını karşılıklı oranlar. sqrt  Kök alma abs  Mutlak değer rats  Kesirli gösterim Mantıksal operatörler: &  Ve |  Veya ~  Değil /  Bölme Karar operatörleri: >  Büyüktür <  Küçüktür >=  Büyük eşittir <=  Küçük eşittir ==  Eşittir ~=  Eşit değildir

Matlab Random Sayı Üretme

randi() Matlab üzerinde random sayı üretmek için en iyi komut randi() 'dir. rand() fonksiyonu ya da komutu ile de random sayı üretilebilir. Bu yazıda randi() komutunu anlatacağım ve yazının sonundaki link ile birbirinden farklı en tane sayı üretme algoritmasını paylaşacağım. Önemli: Bu sayfadaki matlab fonksiyonları ve açıklamaları matlabın orjinal sayfasından edinilmiştir. https://www.mathworks.com/help/matlab/ref/randi.html Örnek: randi(10,5) > 1'den 10'a kadar 5x5'lik bir random sayı matrisi üret. r = randi(10,5) %matlab komutu r = 5×5 9 1 2 2 7 10 3 10 5 1 2 6 10 10 9 10 10 5 8 10 7 10 9 10 7 Örnek:  randi ([-5,5],10,1) > -5'den başlayıp 5'e kadar 10 satır ve 1 sütuna sahip 10x1'lik bir randım sayı matrisi oluturur. "1" değiştirilerek başka boyutlarda da matris oluşturulabilir. r = randi([-5,