2 Replies - 841 Views - Last Post: 06 September 2012 - 09:49 AM Rate Topic: -----

#1 NeonSoda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 12

Creating Multiple Instances Of A Class C# windows Form

Posted 06 September 2012 - 08:15 AM

im trying to create a simpe snake game in windows form i cant seem to get it to load Multiple Instances Of A Class that i have stored in an array when i run debug it just draws an red x could someone help me out

Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Snake_Game
{
    public partial class Form1 : Form
    {
        Graphics paper;
        Snake snake = new Snake();
        Random randFood = new Random();
        int counter = 0;
        food[] food;

        bool Left = false;
        bool Right = true;
        bool Up = false;
        bool Down = false;
        int[,] level = {
                           {1,1,1,1,1},
                           {1,0,0,0,0},
                           {1,0,0,0,0},
                           {1,0,0,0,0},
                           {1,1,1,1,1},

                       };

        public Form1()
        {
            InitializeComponent();
            for (int y = 0; y < 5; y++)
            {
                for (int x = 0; x < 5; x++)
                {
                    food = new food[counter];
                    counter++;
                }
            }
            counter = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            paper = e.Graphics;
            snake.drawSnake(paper);
            
            for (int y = 0; y < 5; y++)
            {
                for (int x = 0; x < 5; x++)
                {
                    if (level[y, x] == 0)
                    {
                        food[counter].drawFood(paper, x, y);
                        counter++;
                    }
                    
                    
                }
            }
            
          counter=0;  
           
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Down && Up == false)
            {
                Down = true;
                Up = false;
                Left = false;
                Right = false;
            }

            if (e.KeyData == Keys.Up && Down == false)
            {
                Down = false;
                Up = true;
                Left = false;
                Right = false;
            } 
            
            if (e.KeyData == Keys.Left && Right == false)
            {
                Down = false;
                Up = false;
                Left = true;
                Right = false;
            } 
            
            if (e.KeyData == Keys.Right && Left == false)
            {
                Down = false;
                Up = false;
                Left = false;
                Right = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Down)
            {
                snake.moveDown();
            }
            if (Up)
            {
                snake.moveUp();
            }
            if (Right)
            {
                snake.moveRight();
            }
            if (Left)
            {
                snake.moveLeft();
            }

            this.Invalidate();
        }
    }
}



Food
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Snake_Game
{
    public class food
    {
        private int x, y, width, height;
        private SolidBrush brush;
        private Rectangle foodRec;
        

        public food()
        {

           brush = new SolidBrush(Color.Black);

            width = 10;
            height = 10;

            foodRec = new Rectangle(x, y, width, height);

        }

      

        public void drawFood(Graphics paper, int x, int y)
         {

             foodRec.X = x*10;
             foodRec.Y = y*10; 

             paper.FillRectangle(brush, foodRec);

    }
    }
}

Snake
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Snake_Game
{
     public class Snake
    {
         public Rectangle snakeRec;
         private SolidBrush brush;
         private int x, y, width, height;

         public Snake()
         {
             snakeRec = new Rectangle();
             brush = new SolidBrush(Color.Blue);

             x = 30;
             y = 30;
             width = 10;
             height = 10;

             for (int i = 0; i < 1; i++)
             {
                 snakeRec = new Rectangle(x, y, width, height);
                 x -= 10;
             }

        }//constructor
         
         public void drawSnake(Graphics paper)
         {
            
                 paper.FillRectangle(brush, snakeRec);
            
         }

         public void drawSnake()
         {
             
                 snakeRec = snakeRec;
           

         }

         public void moveDown()
         {
             drawSnake();
             snakeRec.Y += 10;
         }
         public void moveUp()
         {
             drawSnake();
             snakeRec.Y -= 10;
         }
         public void moveLeft()
         {
             drawSnake();
             snakeRec.X -= 10;
         }
         public void moveRight()
         {
             drawSnake();
             snakeRec.X += 10;
         }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Creating Multiple Instances Of A Class C# windows Form

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1928
  • View blog
  • Posts: 5,738
  • Joined: 05-May 12

Re: Creating Multiple Instances Of A Class C# windows Form

Posted 06 September 2012 - 08:58 AM

I'm not seeing anywhere in the code where you are putting into an array. Can you point us to which line of code where you think your putting things into an array?

The only thing I've seen related to an array is line 40 of Form1, but there you are inexplicably reallocating a larger and larger array.

On thing that may help is to be consistent with your naming. The .NET naming guidelines recommends that classes be named using Pascal casing (eg. begin with a capital letter for each word), and variables be named using Camel casing (eg. begin with a lower case letter, and then succeeding words start with a capital) Your food class starts with a lower case, as does your food variable for your array.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Creating Multiple Instances Of A Class C# windows Form

Posted 06 September 2012 - 09:49 AM

Let me just say Whiskey Tango Foxtrot? WTF are you trying to do here?


40         public void drawSnake()
41         {              
43                 snakeRec = snakeRec; 
46         }



What is going through your mind with this? snakeRec is a rectangle. That's it. Just a rectangle consisting of a .Location and some points that define its size. How exactly do you think that set it equal to itself is going to magically make your snake draw on the screen?

This is the method you call from all your other methods. So yeah, nothing is going to paint.

I think you need to stop. Think this through. Draw it out on a whiteboard. And plan a lot more before you type.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1