السلام عليكم , عندي مشكلة صغيرة في كود لعبة الأفعى التي اقوم ببرمجتها على ال #C كأول مشروع اقوم به بأستخدام هذه اللغة سوف اقوم بأدراج المشروع كاملا بحيث اذا اراد احد استخدامه فلا مانع و عند الإنتهاء منه و حل جميع المشاكل سوف اقوم بشرحة حتى يكون ذا فائدة للجميع . سوف ادرج الكود و اشير الي المشكلة !
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
namespace SnakeGame
{
public class Block
{
//the food cordinates
private int x, y, height, width;
public int getX
{
get {
return this.x;
}
}
public int getY
{
get
{
return this.y;
}
}
//brush object to paint the food rectangles
public Brush brush;
//the Food rectangle
public Rectangle blockRec;
//Creates new Food Object with random cordinates ;
public Block(Random rand, Block[] barrier)
{
randomSetTheCordinates(rand, barrier);
// x = rand.Next(0, 29) * 10;
// y = rand.Next(0, 29) * 10;
brush = new SolidBrush(Color.Red);
width = 10;
height = 10;
blockRec = new Rectangle(x, y, width, height);
}
public Block(Random rand, int width, int height, Brush brush)
{
x = rand.Next(0, 29) * 10;
y = rand.Next(0, 29) * 10;
this.brush = brush;
this.width = width;
this.height = height;
blockRec = new Rectangle(x, y, width, height);
}
public Block(int x,int y, int width, int height, Brush brush)
{
this.x = x;
this.y = y;
this.brush = brush;
this.width = width;
this.height = height;
blockRec = new Rectangle(this.x, this.y, width, height);
}
/**
* desn't work !
* Relocate the Block object to a random cordinates
* @param randFood , A Random Object to set random cordinates
*/
public void randomSetTheCordinates(Random rand,Block[] barrier) {
//random assign X and Y cordinates!
int tempX = rand.Next(0, 29) * 10;
int tempY = rand.Next(0, 29) * 10;
foreach (Block block in barrier)
{
if (block.getX == tempX && block.getY == tempY)
{
this.randomSetTheCordinates(rand, barrier);
}
else
{
this.x = tempX;
this.y = tempY;
}
}
}
public void FoodLocation(Random randFood)
{
x = randFood.Next(0, 29) * 10;
y = randFood.Next(0, 29) * 10;
}
/**
* Locates the Object on the form cordinate system and fills the Food rectangle .
* @param paper , Graphic Object to fill the Food rectangle .
* */
public void drawFood(Graphics paper)
{
blockRec.X = x;
blockRec.Y = y;
paper.FillRectangle(brush, blockRec);
}
public Rectangle getRectangle()
{
return this.blockRec;
}
}
}
المشكلة في randomSetTheCordinates حيث انه عند تناول الأفعى للأكل يجب ان يوضع بمكان عشوائيا ولكن دون ان بأتي خلف المكعبات Barriers يمكن تحميل المشروع من الرابط و حتى يعمل المشروع هناك بعض الأمور يجب تغيرها مثل مصد الأصوات و الصور !
http://www.mediafire.com/?2nudusohrxvzk4p :cool: :cool: :cool:


