42 - Hırsız Polis -class
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 oyuncu_zipla
{
public partial class Form1 : Form
{
Point p, s;
public Form1()
{
InitializeComponent();
p = btnThief.Location;
s = btnPolice.Location;
btnThief.LocationChanged += ButonKonumDegisti;
btnPolice.LocationChanged += ButonKonumDegisti;
}
public class hirsizpolis//sınıf
{
public void zipla (Button button)//metot
{
button.Top -= 20;//üstten mesafeyi kısalt yukarı çık
}
public void asagigit(Button button)//metot
{
button.Top += 20;//üstten mesafeyi kısalt yukarı çık
}
public void solagit(Button button)
{
button.Left -= 20;//soldan mesafeyi kısalt yani sola git
}
public void sagagit(Button button)
{
button.Left += 20;//soldan mesafeyi kısalt yani sola git
}
}
hirsizpolis Police = new hirsizpolis();
//hırsızpolis sınıfından türetilmiş Police nesnesi(oyun karakteri)
hirsizpolis Thief = new hirsizpolis();
private void ButonKonumDegisti(object sender, EventArgs e)
{
// Çarpışma kontrolü
if (btnThief.Bounds.IntersectsWith(btnPolice.Bounds))
{
MessageBox.Show("Yakalandın!!");
btnThief.Location = p;
btnPolice.Location = s;
}
//formdan çıkmayı engelle
CheckBorders(btnThief);
CheckBorders(btnPolice);
}
private void CheckBorders(Button button)
{
// Formun sınırlarını al
Rectangle formBounds = this.ClientRectangle;
// Butonun sınırlarını al
Rectangle buttonBounds = button.Bounds;
// Formun sınırlarından taşıyorsa düzelt
if (!formBounds.Contains(buttonBounds))
{
// Butonun yeni konumu hesapla
int newX = Math.Max(formBounds.Left, Math.Min(formBounds.Right - buttonBounds.Width, button.Left));
//x düzleminde formun sınırında kalacağı mesafe hesabı
int newY = Math.Max(formBounds.Top, Math.Min(formBounds.Bottom - buttonBounds.Height, button.Top));
//y düzleminde formun sınırında kalacağı mesafe hesabı
// Yeni konumu butona ata
button.Location = new Point(newX, newY);
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//Hırsız için yönlendirme tuşları
if (e.KeyChar=='a' || e.KeyChar=='A')
{
Thief.solagit(btnThief);
}
if (e.KeyChar == 'd' || e.KeyChar == 'D')
{
Thief.sagagit(btnThief);
}
if (e.KeyChar == 'w' || e.KeyChar == 'W')
{
Thief.zipla(btnThief);
}
if (e.KeyChar == 's' || e.KeyChar == 'S')
{
Thief.asagigit(btnThief);
}
//Police için yön tuşları
if (e.KeyChar == '4')
{
Police.solagit(btnPolice);
}
if (e.KeyChar == '6')
{
Police.sagagit(btnPolice);
}
if (e.KeyChar == '8')
{
Police.zipla(btnPolice);
}
if (e.KeyChar == '5')
{
Police.asagigit(btnPolice);
}
}
}
}
Yorumlar
Yorum Gönder