2013-10-13 16 views
5

मेरे पास एक एक्सएनए टावर रक्षा के लिए कुछ कोड है। मैंने यह निर्धारित किया है कि दुश्मन (बग) ग्रिड के एक निश्चित तरफ से एक यादृच्छिक पथ पर चला जाता है जब तक कि वह घर (गंतव्य) या घर के किनारे की पंक्ति को हिट नहीं करता।मेरा यादृच्छिक पथ कोड क्यों काम नहीं करेगा?

मैंने इस परियोजना को डीबग किया और बग खींचा और एक विकर्ण (प्रकार) पथ में आगे बढ़ना शुरू कर दिया। यह हर बार एक ही है (यह यादृच्छिक नहीं है)। मैं नहीं करता कि मैं क्या गलत कर रहा हूं। यह कभी-कभी कोड में उस स्थान पर OutOfMemory अपवाद देता है जहां मैंने इसे निर्दिष्ट किया था। मुझे पता नहीं है कि मैंने इसे बहुत अच्छी तरह से समझाया है तो सवाल पूछने में संकोच न करें। ग्रिड एक जंगल से घिरा हुआ है जो ग्रिड का हिस्सा नहीं है, इसलिए जंगल का मतलब यही है।

public class Bug : Sprite 
{ 
    public float startHealth; 
    protected float currentHealth; 
    protected bool alive = true; 
    protected float speed = 0.5f; 
    protected int bountyGiven; 
    public int startplace; 
    public bool at_house; 
    public Queue<Vector2> path = new Queue<Vector2>(); 
    Random random = new Random(); 
    int x; 
    int y; 
    public int end_row; 
    public int end_column; 

    //Defines the space where the house is 
    Vector2 house1 = new Vector2(10, 13) * 91; 
    Vector2 house2 = new Vector2(10, 12) * 91; 
    Vector2 house3 = new Vector2(10, 11) * 91; 
    Vector2 house4 = new Vector2(10, 10) * 91; 
    Vector2 house5 = new Vector2(11, 13) * 91; 
    Vector2 house6 = new Vector2(11, 12) * 91; 
    Vector2 house7 = new Vector2(11, 11) * 91; 
    Vector2 house8 = new Vector2(11, 10) * 91; 
    Vector2 house9 = new Vector2(12, 13) * 91; 
    Vector2 house10 = new Vector2(12, 12) * 91; 
    Vector2 house11 = new Vector2(12, 11) * 91; 
    Vector2 house12 = new Vector2(12, 10) * 91; 

    public float CurrentHealth 
    { 
     get { return currentHealth; } 
     set { currentHealth = value; } 
    } 

    public bool IsDead 
    { 
     get { return currentHealth <= 0; } 
    } 

    public int BountyGiven 
    { 
     get { return bountyGiven; } 
    } 

    public float DistanceToDestination 
    { 
     get { return Vector2.Distance(position, path.Peek()); } 
    } 

    public Bug(Texture2D texture, Vector2 position, float health, 
     int bountyGiven, float speed) 
     : base(texture, position) 
    { 

     this.startHealth = health; 
     this.currentHealth = startHealth; 
     this.bountyGiven = bountyGiven; 
     this.speed = speed; 
     int startq = random.Next(1, 4); 
     set_start(ref startq); 
     //end_row and end_column detremine the row or column at which the bug turns toward the house. 
     end_row = random.Next(10, 13); 
     end_column = random.Next(10, 12); 
     set_path(ref startq); 
    } 

    public void set_start(ref int startq) 
    { 
     //here i am initializing the "0,0" point for the bug so it's 0,0 is't in the forest 
     //startx and starty should equal the number of tiles between the forest edge and the grass edge 
     //startx is the x co-ord in the start place and starty is the y co-ord in the start 
     int startx = 4; 
     int starty = 4; 
     //This generates a random number which determines the start for the bug 
     //Between 0 and 22 because that is the number of edge tiles on one side 
     int start = random.Next(0, 22); 
     //start determines what place on a side the buggie spawns at 
     //startq is a random int (1-4)(defined in constructor) which determnes which side the bug spawns at 

     if (startq == 1) 
     { 
      starty += 22; 
      startx += start; 
     } 

     if (startq == 2) 
     { 
      startx += 22; 
      starty += start; 
     } 

     if (startq == 3) 
     { 
      startx += start; 
     } 

     if (startq == 4) 
     { 
      starty += start; 
     } 
     x = startx; 
     y = starty; 
     path.Enqueue(new Vector2(startx, starty) * 91); 
    } 

    public bool check_for_path_end(ref int startq, ref bool at_house) 
    { 
     bool path_ends; 
     //checks if the bug has reached the house and if so signals using at_house 
     if (path.Peek() == house1 || path.Peek() == house2 || path.Peek() == house3 || path.Peek() == house4 
      || path.Peek() == house5 || path.Peek() == house6 || path.Peek() == house7 || path.Peek() == house8 
      || path.Peek() == house9 || path.Peek() == house10 || path.Peek() == house11 || path.Peek() == house12) 
     { 
      at_house = true; 
      return true; 
     } 
     //Should i add at_house = true to the else ifs? 
     else if (startq == 1 || startq == 3 && path.Peek().Y == end_row) 
     { 
      path.Enqueue(new Vector2(11, end_row) * 91); 
      return true; 
     } 

     else if (startq == 2 || startq == 4 && path.Peek().X == end_column) 
     { 
      path.Enqueue(new Vector2(end_column, 11) * 91); 
      path_ends = true; 
     } 

     else 
     { 
      path_ends = false; 
     } 
     return path_ends; 
    } 

    public void set_path(ref int startq) 
    { 
     bool path_ends; 
     bool legal = true; 
     int X = x; 
     int Y = y; 
     do 
     { 
      //determines which way the bug turns at it's different waypoints 1 = left, 2 = right, 3 = forward 
      int turn = random.Next(1, 3); 
      do 
      { 
       if (startq == 1) 
       { 
        switch (turn) 
        { 
         case 1: 
          x += 1; 
          break; 
         case 2: 
          x -= 1; 
          break; 
         case 3: 
          y += 1; 
          break; 
        } 
       } 

       else if (startq == 2) 
        switch (turn) 
        { 
         case 1: 
          y -= 1; 
          break; 
         case 2: 
          y += 1; 
          break; 
         case 3: 
          x -= 1; 
          break; 
        } 

       else if (startq == 3) 
       { 
        switch (turn) 
        { 
         case 1: 
          x -= 1; 
          break; 
         case 2: 
          x += 1; 
          break; 
         case 3: 
          y += 1; 
          break; 
        } 
       } 

       else if (startq == 4) 
       { 
        switch (turn) 
        { 
         case 1: 
          y += 1; 
          break; 
         case 2: 
          y -= 1; 
          break; 
         case 3: 
          x += 1; 
          break; 
        } 
       } 

       if (y > 3 && y < 28 && x > 3 && x < 28) 
       { 
        //sets up a backup in case the bug goes off track 
        X = x; 
        Y = y; 

वह स्थान है जहां अपवाद है:

     //Right here is where it gives the out of memory exception 
         path.Enqueue(new Vector2(x, y) * 91); 
         legal = true; 
        } 

        else 
        { 
         //restores x and y to backups X and Y 
         x = X; 
         y = Y; 
         //adds to turn and repeats without randomizing turn or adding waypoints 
         turn += 1; 
         legal = false; 
        } 
       } while (legal == false); 
       path_ends = check_for_path_end(ref startq, ref at_house); 
      } while (path_ends == false); 
     } 

     public bool check_corners() 
     { 
      bool start_is_corner; 
      if (x == 2 && y == 24 || x == 24 && y == 24 || x == 24 && y == 2 || x == 2 && y == 2) 
      { 
       start_is_corner = true; 
       int X = x; 
       int Y = y; 
       if (x == 4 && y == 27) 
       { 
        bool z = true; 
        for (int i = 0; i < 13; ++i) 
        { 
         if (z == true) 
         { 
          Y -= 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = false; 
         } 
         if (z == false) 
         { 
          X += 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = true; 
         } 
        } 
       } 
       if (x == 27 && y == 27) 
       { 
        bool z = true; 
        for (int i = 0; i < 13; ++i) 
        { 
         if (z == true) 
         { 
          Y -= 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = false; 
         } 
         if (z == false) 
         { 
          X -= 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = true; 
         } 
        } 
       } 
       if (x == 27 && y == 4) 
       { 
        bool z = true; 
        for (int i = 0; i < 13; ++i) 
        { 
         if (z == true) 
         { 
          Y += 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = false; 
         } 
         if (z == false) 
         { 
          X -= 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = true; 
         } 
        } 
       } 
       if (x == 4 && y == 4) 
       { 
        bool z = true; 
        for (int i = 0; i < 13; ++i) 
        { 
         if (z == true) 
         { 
          Y += 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = false; 
         } 
         if (z == false) 
         { 
          X += 1; 
          path.Enqueue(new Vector2(X, Y) * 91); 
          z = true; 
         } 
        } 
       } 
      } 
      else 
      { 
       start_is_corner = false; 
      } 
      return start_is_corner; 
     } 

     public override void Update(GameTime gameTime) 
     { 
      base.Update(gameTime); 
      if (path.Count > 0) 
      { 
       if (DistanceToDestination < speed) 
       { 
        position = path.Dequeue(); 
       } 

       else 
       { 
        Vector2 direction = path.Peek() - position; 
        direction.Normalize(); 

        velocity = Vector2.Multiply(direction, speed); 

        position += velocity; 
       } 
      } 
      else 
       alive = false; 

      if (currentHealth <= 0) 
       alive = false; 
     } 

     public override void Draw(SpriteBatch spriteBatch) 
     { 
      if (alive) 
      { 
       float healthPercentage = (float)currentHealth/(float)startHealth; 

       base.Draw(spriteBatch); 
      } 
     } 
    } 
} 

उत्तर

7

यह विधि आपकी कन्स्ट्रक्टर है।

public Bug(Texture2D texture, Vector2 position, float health, int bountyGiven, float speed) 
    : base(texture, position) 
{ 

    this.startHealth = health; 
    this.currentHealth = startHealth; 
    this.bountyGiven = bountyGiven; 
    this.speed = speed; 
    int startq = random.Next(1, 4); 
    set_start(ref startq); 
    //end_row and end_column detremine the row or column at which the bug turns toward the house. 
    end_row = random.Next(10, 13); 
    end_column = random.Next(10, 12); 
    set_path(ref startq); // <<<<<<<<<<< HERE! 
} 

क्या तुम यहाँ कर रहे हैं, कि आप रहे आरंभीकरण पर बग के पथ predetermining है: यह जहां समस्या से आता है। और आप Queue में पूरे पथ को संग्रहीत कर रहे हैं। और अंत में कतार इतनी बड़ी हो जाती है कि आप स्मृति से बाहर हो जाते हैं।

समाधान सरल है। एक Bug कि हर एक कदम वह जन्म में घर की ओर बनाने के लिए जा रहा है जानता है आरंभ करने के बजाय, प्रत्येक अद्यतन पर एक Bug कि कहीं शुरू होता है और, बनाने के लिए, निर्धारित करता है क्या अगले कदम घर की ओर जा रहा है। कुछ भी कतार मत करो।

आपकी गलती यह है कि आप अपने निर्माता में सामान कर रहे हैं जो आपके Update विधि में है। अपने set_path विधि से छुटकारा पाएं। शुरुआत से घर के रास्ते को जानने के लिए एक बग पर्याप्त शानदार नहीं है। यही आपके खेल को रोक रहा है। Update विधि का उपयोग कतार से खींचने के बजाय अगली चाल की गणना करने के लिए करें: आपको उस कतार की आवश्यकता नहीं है।

ऐसा लगता है कि आप शतरंज खेल रहे हैं और इससे पहले कि आप यह भी जान सकें कि आप प्रतिद्वंद्वी का पहला कदम क्या होने जा रहा है, आप अपने सिर में पूरे गेम की गणना कर रहे हैं। गेम शुरू होने से पहले आपकी नाक खून बह जाएगी।

+0

धन्यवाद! मुझे अब एहसास है कि बहुत बेवकूफ था – Detinator10

2

मैं वास्तव में पूरी बात बाहर पहेली समय नहीं है अभी, लेकिन मैं आपको बता सकता है क्या स्मृति त्रुटि के अपने बाहर के साथ क्या हो रहा है - यह जंगली इमारत कहीं और एक अनंत मार्ग चल रहा है क्योंकि समाप्ति की स्थिति इसे पकड़ नहीं पाया।

+0

तो क्या मैं पथ निर्माण को रोक नहीं रहा हूं? मैं कल उसको जांचूंगा। मुझे अभी भी सीखने के लिए बहुत कुछ मिला है ... (मैं एक शुरुआती प्रोग्रामर हूं) – Detinator10

+0

@ user2770939 कुछ मामलों में (लेकिन सभी मामलों में नहीं) आप पथ निर्माण को रोक नहीं रहे हैं। यही कारण है कि यह हमेशा उड़ता नहीं है। मुझे लगता है कि बाधाएं बहुत अधिक हैं, कुछ मामला है जो समाप्ति की स्थिति के माध्यम से रिसाव कर सकता है। –

+0

ठीक है, मैं कल अपने कंप्यूटर पर वापस आने पर जांच करूंगा। क्या यह संभव है कि मेरा दुश्मन हर समय एक ही रास्ता ले रहा है क्योंकि मेमोरी अपवाद से बाहर किया जा सकता है? जिस तरह से मैं user2770939 हूं, मैंने अपना नाम बदल दिया – Detinator10

संबंधित मुद्दे