Sterren op form tekenen

Status
Niet open voor verdere reacties.

savant11

Gebruiker
Lid geworden
7 jan 2008
Berichten
153
HOi allen,

heb dit:

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace StarInvader2
{

    public class Stars
    {
        Game game;
        Star star;
        private Form1 form1 = new Form1();
        Form2 form2;
        public List<Star> starList = new List<Star>();
        public Point Location { get; private set; }
        public Random random;
        private Rectangle boundaries;
        Bitmap Star;

        private const int FieldMinX = 15;
        private const int FieldMinY = 177;
        private const int FieldMaxX = 690;
        private const int FieldMaxY = 290;

        public Stars(Point location,Rectangle boundaries)
        {
           starList  = new List<Star>();
            this.Location = location;
            this.boundaries = boundaries;
            
            //form2.game = this;
            //this.random = random;

            Random random = new Random();
            for (int i = 0; i < 300; i++)
                starList.Add(new Star(new Point(random.Next(boundaries.Width), random.Next(boundaries.Height)),RandomPen(random)));
                //AddStar(random);
        }//End constuctor

        public Stars(Game game)
        {
            //this.game = game;
            //this.form2 = form2;
            //form2.stars = this;
        }

        public void Draw(Graphics g)
        {
            using (Pen brownPen = new Pen(Color.Brown, 6.0F))
            g.FillRectangle(Brushes.Black, 0, 0, form1.ClientSize.Width, form1.ClientSize.Height);
            foreach (Star star in starList)
            {
                g.DrawEllipse(star.pen, star.point.X, star.point.Y,1,1);
            }
        }//End method

        public void Twinkle(Random random)
        {   
            for(int i = 0; i < 5; i++)
            {
                //random.Next(manyStar.Remove(5));
                //starList.Remove(random.Next(i));
            }
        }

        public void RandomPen(Random random)
        {
            Random color = new Random();
            using (Pen brownPen = new Pen(Color.Brown, 6.0F))
            {
                random.Next(0, 4);
            }
            //random.Next(0,4);

            //return color;
        }

       
        /*
        private void AddStar(Random random)
        {
            Point location = new Point(random.Next(FieldMinX, FieldMaxX),
                                       random.Next(FieldMinY, FieldMaxY));
            Star newStar = new Star(location, random);
            manyStar.Add(newStar);
        }
         */ 
         
    }//End class
}

Maar krijg deze error:Error 2 Argument 2: cannot convert from 'void' to 'System.Drawing.Pen' C:\Users\savantKing\Documents\Visual Studio 2010\Projects\Calculator\Calculator\StarInvader2\Stars.cs 38 112 StarInvader2
bij deze line:

starList.Add(new Star(new Point(random.Next(boundaries.Width), random.Next(boundaries.Height)),RandomPen(random)));

THX!! for helping
 
De constructor van jouw Star verwacht kennelijk een tweede parameter van het type Pen. Jij roept daarvoor jouw functie RandomPen aan die een void terug geeft. Geef deze een returntype van het type Pen en return de brownPen die je daarin noemt. Overigens is me niet helemaal duidelijk wat jouw randomPen functie tot dusver zou moeten doen.
 
Hoi morfanaion,

De method: RandomPen moet een van de vijf kleuren(Random) retourneren die een ster kan hebben, dit door een nummer van 0 tot 4 te genereren. Daarom moet er ook een Pen object worden geselecteerd.

Maar ik heb al veel geprobeerd.
Heb je wellicht een opstap code voorbeeldje.

Alvast thx.

Niels
 
HOi,

jA, ik het nu zo ff snel gedaan:

PHP:
public Pen RandomPen(Random random)
        {
            Random color = new Random();
            using (Pen brownPen = new Pen(Color.Brown, 6.0F))
            {
                random.Next(0, 4);

                return brownPen;
            }           
        }

Maar nu krijg ik hier een error:

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace StarInvader2
{
  public  class Game
    {
      //Form1 form1 { get; set;}    
      Form1 form1;

      private int score = 0;
      private int livesLeft = 2;
      private int wave = 0;
      private int framesSkipped = 0;

      private Rectangle boundaries;
      private Random random;

      

      //public List<Star>stars;
      

      private Bitmap[] StarAnimation;
      //private Direction invaderDirection;
      //private List<Invader>invaders;
      //private PlayerShip playership;
      //private List<Shot> playerShots;
     // private List<Shot> invaderShots;
      private Stars stars;

      public Game()
      {         
          //this.form2 = form2;
          //form2.game = this;
          
          //InitializeImages();
      }

      //public Game()
      //{ }

        

      
      public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
      {
          Bitmap bitmap = new Bitmap(Width, Height);
          using (Graphics graphics = Graphics.FromImage(bitmap))//Draw FromImage
          {
              graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
          }
          return bitmap;
      }//End method
       

      
       public void InitializeImages()
       {         
          StarAnimation = new Bitmap[4];
          StarAnimation[0] = ResizeImage(Properties.Resources.star1, 10, 10);
          StarAnimation[1] = ResizeImage(Properties.Resources.star2, 10, 10);
          StarAnimation[2] = ResizeImage(Properties.Resources.star3, 10, 10);
          StarAnimation[3] = ResizeImage(Properties.Resources.star4, 10, 10);         
          PictureBox starPicture = new PictureBox();
          starPicture.Size = new Size(10, 10);
      }//End method InitializeImages
        
       

      
      private int animationCell = 0;
      private int Frame = 0;
      public void Draw(Graphics g, int anmationCell)
      {
         using (Pen brownPen = new Pen(Color.Brown, 6.0F))
          {
              g.FillRectangle(Brushes.SkyBlue, 0, 0,
                       form1.ClientSize.Width, form1.ClientSize.Height);

          }
          
          Frame++;
          if (Frame >= 6)
          {
              Frame = 0;
          }
          switch (Frame)
          {
              case 0: animationCell = 0; break;
              case 1: animationCell = 1; break;
              case 2: animationCell = 2; break;
              case 3: animationCell = 3; break;
              case 4: animationCell = 2; break;
              case 5: animationCell = 1; break;
              default: animationCell = 0; break;
          }
          form1.Invalidate();
            
      }//End method

      public void Twinkle()
      {         
      }

      
      public void AnimateObject()
      {          
      }//End method

      
    }
}

Error: Object reference not set to an instance of an object(form1 = null).

bij deze lijn:
PHP:
private int animationCell = 0;
      private int Frame = 0;
      public void Draw(Graphics g, int anmationCell)
      {
         using (Pen brownPen = new Pen(Color.Brown, 6.0F))
          {
              g.FillRectangle(Brushes.SkyBlue, 0, 0,
                       form1.ClientSize.Width, form1.ClientSize.Height);

          }
 
Je Game class heeft een referentie naar een form, maar die initialiseer je nooit. Met andere woorden, de game weet dat hij een form zou moeten hebben, maar dat form heeft hij niet.

Code:
public Game(Form form)
      {         
this.form1 = form;
          //this.form2 = form2;
          //form2.game = this;
          
          //InitializeImages();
      }

Bij het aanroepen van de constructor van je Game class (new Game()) zul je vervolgens het formulier waar je naar verwijst mee moeten geven. Wordt Game door deze klasse zelf aangemaakt, kun je volstaan met "new Game(this);"

Hoe veel ervaring heb je met C# en heb je een degelijk boek om mee te werken. Het ziet er naar uit dat je nog even goed moet gaan kijken naar reference types en classes en hoe deze onderling werken.
 
Hoi Morfanaion,

enorm bedankt voor de tips.

Ik heb het nu zo en het werkt:class Form1

PHP:
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 StarInvader2
{
    public partial class Form1 : Form
    {
        //Form2 form1 = new Form1();
        public int anmationCell { get; set; }
       //public Game game = new Game();
        private Renderer renderer;
        public Game game;// = new Game();
       private int framesRun = 0;
       Stars stars;

        public Form1()
        {
            InitializeComponent();
            //form2.Show(this);
            animationTimer.Interval = 50;
            animationTimer.Tick += new EventHandler(RunFrame);
            animationTimer.Enabled = false;
            ResetSimulator();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           //Color color1 = Color.Black;
           // this.BackColor = Color.Black;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Console.WriteLine(DateTime.Now.ToString());

        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show(e.Location.ToString());
        }



        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            
               game.Draw(e.Graphics,anmationCell);
            
           
        }//End method

        public void RunFrame(object sender, EventArgs e)
        {
            framesRun++;         
            //TimeSpan frameDuration = end - start;           
        }

        private void ResetSimulator()
        {
            framesRun = 0;
            game = new Game(this);
            //renderer = new Renderer(game,form2);
        }

       
    }
}

en class:

Game:

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace StarInvader2
{
  public  class Game
    {
      //Form1 form1 { get; set;}    
      Form1 form1;

      private int score = 0;
      private int livesLeft = 2;
      private int wave = 0;
      private int framesSkipped = 0;

      private Rectangle boundaries;
      private Random random;

      

      //public List<Star>stars;
      

      private Bitmap[] StarAnimation;
      //private Direction invaderDirection;
      //private List<Invader>invaders;
      //private PlayerShip playership;
      //private List<Shot> playerShots;
     // private List<Shot> invaderShots;
      private Stars stars;

      public Game(Form1 form1)
      {
          this.form1 = form1;
          //this.stars = stars;
          //this.form1 = form1;
          //form2.game = this;
          
          //InitializeImages();
      }

      //public Game()
      //{ }

        

      
      public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
      {
          Bitmap bitmap = new Bitmap(Width, Height);
          using (Graphics graphics = Graphics.FromImage(bitmap))//Draw FromImage
          {
              graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
          }
          return bitmap;
      }//End method
       

      
       public void InitializeImages()
       {         
          StarAnimation = new Bitmap[4];
          StarAnimation[0] = ResizeImage(Properties.Resources.star1, 10, 10);
          StarAnimation[1] = ResizeImage(Properties.Resources.star2, 10, 10);
          StarAnimation[2] = ResizeImage(Properties.Resources.star3, 10, 10);
          StarAnimation[3] = ResizeImage(Properties.Resources.star4, 10, 10);         
          PictureBox starPicture = new PictureBox();
          starPicture.Size = new Size(10, 10);
      }//End method InitializeImages
        
       

      
      private int animationCell = 0;
      private int Frame = 0;
      public void Draw(Graphics g, int anmationCell)
      {
         using (Pen brownPen = new Pen(Color.Brown, 6.0F))
          {
              g.FillRectangle(Brushes.SkyBlue, 0, 0,
                       form1.ClientSize.Width, form1.ClientSize.Height);

          }
          
          Frame++;
          if (Frame >= 6)
          {
              Frame = 0;
          }
          switch (Frame)
          {
              case 0: animationCell = 0; break;
              case 1: animationCell = 1; break;
              case 2: animationCell = 2; break;
              case 3: animationCell = 3; break;
              case 4: animationCell = 2; break;
              case 5: animationCell = 1; break;
              default: animationCell = 0; break;
          }
          //form1.Invalidate();
            
      }//End method

      public void Twinkle()
      {         
      }

      
      public void AnimateObject()
      {          
      }//End method      
    }
}

Maar mijn vraag is nu: is dit de beste manier? Of heb je nog andere sugesties?
 
Hoi het is nu de bedoeling dat ik de sterren te zien krijg:
PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace StarInvader2
{

    public class Stars
    {
        Game game;
        Star star;
        private Form1 form1 = new Form1();       
        public List<Star> starList = new List<Star>();
        public Point Location { get; private set; }
        public Random random;
        private Rectangle boundaries;
        Bitmap Star;

        private const int FieldMinX = 15;
        private const int FieldMinY = 177;
        private const int FieldMaxX = 690;
        private const int FieldMaxY = 290;

        public Stars(Point location,Rectangle boundaries, Form1 form1)
        {
            this.form1 = form1;
           starList  = new List<Star>();
           this.Location = location;
           this.boundaries = boundaries;           
            //this.random = random;

            Random random = new Random();
            for (int i = 0; i < 300; i++)
                starList.Add(new Star(new Point(random.Next(boundaries.Width), random.Next(boundaries.Height)),RandomPen(random)));
                //AddStar(pen);
        }//End constuctor

        public Stars(Game game)
        {
            this.game = game;
            //this.game = game;
            //this.form2 = form2;
            //form2.stars = this;
        }

        public void Draw(Graphics g)
        {
            foreach (Star star in starList)
            {
                g.DrawEllipse(star.pen, star.point.X, star.point.Y,1,1);
            }
        }//End method

        public void Twinkle(Random random)
        {   
            for(int i = 0; i < 5; i++)
            {
                //random.Next(manyStar.Remove(5));
                //starList.Remove(random.Next(i));
            }
        }

        public Pen RandomPen(Random random)
        {
            Random color = new Random();
            using (Pen brownPen = new Pen(Color.Brown, 6.0F))
            {
                random.Next(0, 4);

                return brownPen;
            }           
        }

       
        
        //private void AddStar(Pen pen)
        //{
           // Point location = new Point(random.Next(FieldMinX, FieldMaxX),
           //                            random.Next(FieldMinY, FieldMaxY));
           // Star newStar = new Star(location, pen);
           // starList.Add(newStar);
        //}
          
         
    }//End class
}

en de Game class:
PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace StarInvader2
{
  public  class Game
    {
      
      Form1 form1;      
      private int score = 0;
      private int livesLeft = 2;
      private int wave = 0;
      private int framesSkipped = 0;
      private Rectangle boundaries;
      private Random random;
      private Bitmap[] StarAnimation;
      private Stars stars;
      //private Direction invaderDirection;
      //private List<Invader>invaders;
      //private PlayerShip playership;
      //private List<Shot> playerShots;
     // private List<Shot> invaderShots;
     

      public Game(Form1 form1,Stars stars)
      {
          this.form1 = form1;
          this.stars = stars;
          //InitializeImages();
      }

      //public Game()
      //{ }

        

      
      public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
      {
          Bitmap bitmap = new Bitmap(Width, Height);
          using (Graphics graphics = Graphics.FromImage(bitmap))//Draw FromImage
          {
              graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
          }
          return bitmap;
      }//End method
       

      
       public void InitializeImages()
       {         
          StarAnimation = new Bitmap[4];
          StarAnimation[0] = ResizeImage(Properties.Resources.star1, 10, 10);
          StarAnimation[1] = ResizeImage(Properties.Resources.star2, 10, 10);
          StarAnimation[2] = ResizeImage(Properties.Resources.star3, 10, 10);
          StarAnimation[3] = ResizeImage(Properties.Resources.star4, 10, 10);         
          PictureBox starPicture = new PictureBox();
          starPicture.Size = new Size(10, 10);
      }//End method InitializeImages
        
       

      
      private int animationCell = 0;
      private int Frame = 0;
      public void Draw(Graphics g, int anmationCell)
      {
         using (Pen brownPen = new Pen(Color.Brown, 6.0F))
          {
              g.FillRectangle(Brushes.Black, 0, 0,
                       form1.ClientSize.Width, form1.ClientSize.Height);

          }
          
          Frame++;
          if (Frame >= 6)
          {
              Frame = 0;
          }
          switch (Frame)
          {
              case 0: animationCell = 0; break;
              case 1: animationCell = 1; break;
              case 2: animationCell = 2; break;
              case 3: animationCell = 3; break;
              case 4: animationCell = 2; break;
              case 5: animationCell = 1; break;
              default: animationCell = 0; break;
          }
          //form1.Invalidate();
          stars.Draw(g);
            
      }//End method

      public void Twinkle()
      {         
      }

      
      public void AnimateObject()
      {          
      }//End method

      
    }
}

Maar nu krijg ik bij deze methode:
PHP:
      private int animationCell = 0;
      private int Frame = 0;
      public void Draw(Graphics g, int anmationCell)
      {
         using (Pen brownPen = new Pen(Color.Brown, 6.0F))
          {
              g.FillRectangle(Brushes.Black, 0, 0,
                       form1.ClientSize.Width, form1.ClientSize.Height);

          }
          
          Frame++;
          if (Frame >= 6)
          {
              Frame = 0;
          }
          switch (Frame)
          {
              case 0: animationCell = 0; break;
              case 1: animationCell = 1; break;
              case 2: animationCell = 2; break;
              case 3: animationCell = 3; break;
              case 4: animationCell = 2; break;
              case 5: animationCell = 1; break;
              default: animationCell = 0; break;
          }
          //form1.Invalidate();
          stars.Draw(g);
            
      }//End method

bij: stars.Draw(g) weer de melding: Object reference not set to an instance of an object. En in de constructor van de Game class instantieer ik de Stars class, zoals te zien is.
Iemand suggesties.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan