ردود الذكاء الاصطناعي
سؤالك:
إجابة الشبكة العصبية:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SnakeGame
{
public partial class Form1 : Form
{
private Timer timer;
private PictureBox pictureBox;
private Point snakePosition;
private int snakeSize;
private int direction;
public Form1()
{
InitializeComponent();
this.Text = "Snake Game";
this.Size = new Size(800, 600);
this.pictureBox = new PictureBox();
this.pictureBox.Size = new Size(10, 10);
this.pictureBox.BackColor = Color.Black;
this.snakePosition = new Point(100, 100);
this.snakeSize = 1;
this.direction = 1;
this.Controls.Add(this.pictureBox);
this.timer = new Timer();
this.timer.Interval = 100;
this.timer.Tick += Timer_Tick;
this.timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
MoveSnake();
CheckCollision();
Refresh();
}
private void MoveSnake()
{
switch (direction)
{
case 1:
snakePosition.Y -= 10;
break;
case 2:
snakePosition.X += 10;
break;
case 3:
snakePosition.Y += 10;
break;
case 4:
snakePosition.X -= 10;
break;
}
}
private void CheckCollision()
{
if (snakePosition.X < 0 || snakePosition.X >= this.ClientSize.Width ||
snakePosition.Y < 0 || snakePosition.Y >= this.ClientSize.Height)
{
MessageBox.Show("Game Over!");
ResetGame();
}
}
private void ResetGame()
{
snakePosition = new Point(100, 100);
snakeSize = 1;
direction = 1;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Black, new Rectangle(snakePosition, new Size(10, 10)));
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Up:
if (direction != 3)
direction = 1;
break;
case Keys.Right:
if (direction != 4)
direction = 2;
break;
case Keys.Down:
if (direction != 1)
direction = 3;
break;
case Keys.Left:
if (direction != 2)
direction = 4;
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
يرجى ملاحظة: قد لا تكون الإجابات المقدمة بواسطة الذكاء الاصطناعي دقيقة دائمًا. لا تعتمد عليها في القضايا الحساسة مثل الطب، القانون، الأمور المالية أو القضايا الأمنية. من الأفضل دائمًا الرجوع إلى المتخصصين المؤهلين عند اتخاذ القرارات المهمة. إدارة الموقع غير مسؤولة عن المحتوى الذي يتم إنتاجه تلقائيًا.