Form İle Tuşların Kullanımı
Basit bir oyun gerçekleştirme

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;
using System.Windows.Forms.VisualStyles;
namespace WindowsFormsApp9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int Adim = 20;
int Cik = 0;
Random RastGele = new Random();
private void Form1_Load(object sender, EventArgs e)
{
Karikatur.BackgroundImage = Image.FromFile(@"top.gif");
Karikatur.BackgroundImageLayout = ImageLayout.Zoom;
Karikatur.Height = Karikatur.BackgroundImage.Height /4;
Karikatur.Width = Karikatur.BackgroundImage.Width / 4;
Karikatur.Location = new Point(RastGele.Next(0,this.Width), RastGele.Next(50, this.Height));
Alev.BackgroundImage = Image.FromFile(@"alev.jpg");
Alev.BackgroundImageLayout = ImageLayout.Zoom;
/*pictureBox2.Height = pictureBox1.BackgroundImage.Height / 4;
pictureBox2.Width = pictureBox1.BackgroundImage.Width / 4;*/
Alev.Location = new Point(RastGele.Next(0, this.Width), RastGele.Next(50, this.Height));
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Escape:
Close();
break;
case Keys.Up:
Karikatur.Top -= Adim;
break;
case Keys.Down:
Karikatur.Top += Adim;
break;
case Keys.Left:
Karikatur.Left -= Adim;
break;
case Keys.Right:
Karikatur.Left += Adim;
break;
}
}
private void Form1_Activated(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle r1 = new Rectangle(Karikatur.Left, Karikatur.Top, Karikatur.Width, Karikatur.Height);
Rectangle r2 = new Rectangle(Alev.Left, Alev.Top, Alev.Width, Alev.Height);
//if ((Karikatur.Left< Alev.Left && Alev.Left< Karikatur.Left + Karikatur.Width) && (Alev.Bottom > Karikatur.Top && Alev.Bottom < Karikatur.Top + Karikatur.Width))
//{
if (r1.IntersectsWith(r2)) //Çarpışma kontrolü
{
timer1.Stop();
MessageBox.Show("Yandın...");
}
else if (Alev.Top > this.Height)
{
Alev.Top = 0;
Alev.Left = RastGele.Next(0, this.Width);
timer1.Interval = RastGele.Next(10, 50);
}
else
{
Alev.Top += 20;
}
}
}
}
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;
using System.Windows.Forms.VisualStyles;
namespace WindowsFormsApp9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int Adim = 20;
int Cik = 0;
Random RastGele = new Random();
private void Form1_Load(object sender, EventArgs e)
{
Karikatur.BackgroundImage = Image.FromFile(@"top.gif");
Karikatur.BackgroundImageLayout = ImageLayout.Zoom;
Karikatur.Height = Karikatur.BackgroundImage.Height /4;
Karikatur.Width = Karikatur.BackgroundImage.Width / 4;
Karikatur.Location = new Point(RastGele.Next(0,this.Width), RastGele.Next(50, this.Height));
Alev.BackgroundImage = Image.FromFile(@"alev.jpg");
Alev.BackgroundImageLayout = ImageLayout.Zoom;
/*pictureBox2.Height = pictureBox1.BackgroundImage.Height / 4;
pictureBox2.Width = pictureBox1.BackgroundImage.Width / 4;*/
Alev.Location = new Point(RastGele.Next(0, this.Width), RastGele.Next(50, this.Height));
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Escape:
Close();
break;
case Keys.Up:
Karikatur.Top -= Adim;
break;
case Keys.Down:
Karikatur.Top += Adim;
break;
case Keys.Left:
Karikatur.Left -= Adim;
break;
case Keys.Right:
Karikatur.Left += Adim;
break;
}
}
private void Form1_Activated(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle r1 = new Rectangle(Karikatur.Left, Karikatur.Top, Karikatur.Width, Karikatur.Height);
Rectangle r2 = new Rectangle(Alev.Left, Alev.Top, Alev.Width, Alev.Height);
//if ((Karikatur.Left< Alev.Left && Alev.Left< Karikatur.Left + Karikatur.Width) && (Alev.Bottom > Karikatur.Top && Alev.Bottom < Karikatur.Top + Karikatur.Width))
//{
if (r1.IntersectsWith(r2)) //Çarpışma kontrolü
{
timer1.Stop();
MessageBox.Show("Yandın...");
}
else if (Alev.Top > this.Height)
{
Alev.Top = 0;
Alev.Left = RastGele.Next(0, this.Width);
timer1.Interval = RastGele.Next(10, 50);
}
else
{
Alev.Top += 20;
}
}
}
}