الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة صغيرة في لعبة صغيرة

بدأه Adios في 13 مايو 2011 · 2 رد · 584 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم , عندي مشكلة صغيرة في كود لعبة الأفعى التي اقوم ببرمجتها على ال #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:

(¯`·._.·[ When I'm good, I'm really good, but when I'm bad I'm better ]·._.·´¯)

#3
System Down كتب:

لم أفهم. ما المشكلة بالضبط؟

المشكلة انه عندى تناول الأفعى للأكل , يجب ان يغير الأكل ( المكعب الأحمر ) مكانه - اذا حملت الكود و قمت بتشغيله سوف تراه - ولكن بما ان مكان الأكل يتم اختياره عشوائيا قيمكن ان يتم اختيار المكان اي احداثيات x و احداثيات y ينفس احداثيات المكعبات التى لا يجب للأفعى ان تصدمها و في هذه الحال لا يظهر الأكل في اي مكان

و يكون خلف احد المكعبات ولا يمكن اكله و اكمال اللعبة , لقت قمت بكتابة الأقتران الذي يقوم بالبحث عن مكان للأكل عشوائيا مع تجنب احداثيات المكعبات ولكن الإقتران لا يعمل , استخدمت مبدأ Recursion ولا اعرف سبب الذي يجعل الأكل يذهب تحت المكعبات بعد عدد محاولات للأكل وا حتى في البداي في بعض الحالات.

ارجوا ان تكون الفكرة وصلت ! :)

(¯`·._.·[ When I'm good, I'm really good, but when I'm bad I'm better ]·._.·´¯)

مواضيع مشابهة