zie geen sterren op form

Status
Niet open voor verdere reacties.

savant11

Gebruiker
Lid geworden
7 jan 2008
Berichten
153
Hoi heb dit:

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 Bitmap shipAnimation;
      private Stars stars;
      private Point Location;
      public List<Star> starList = new List<Star>();
      //private Direction invaderDirection;
      //private List<Invader>invaders;
      private PlayerShip playership;
      //private List<Shot> playerShots;
     // private List<Shot> invaderShots;
     

      public Game(Form1 form1,Stars stars,PlayerShip playerShip)
      {
          this.form1 = form1;
          this.stars = stars;
          this.playership = playership;
          InitializeImages();
      }
      
      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()
       {
          PictureBox starPicture = new PictureBox();
          starPicture.Size = new Size(30, 30);
          shipAnimation = ResizeImage(Properties.Resources.player, 40, 40);
          StarAnimation = new Bitmap[4];
          StarAnimation[0] = ResizeImage(Properties.Resources.star1, 30, 30);
          StarAnimation[1] = ResizeImage(Properties.Resources.star2, 30, 30);
          StarAnimation[2] = ResizeImage(Properties.Resources.star3, 30, 30);
          StarAnimation[3] = ResizeImage(Properties.Resources.star4, 30, 30);
          //shipAnimation = new Bitmap[4];
         
          
      }//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, form1.ClientRectangle);
          foreach (Star star in starList)
          {
              g.DrawImageUnscaled(StarAnimation[animationCell], star.point.X, star.point.Y);
          }
          //Stars stars;
          //stars.Draw(g);
              //g.FillRectangle(Brushes.Black, 0, 0,
                       //form1.ClientSize.Width, form1.ClientSize.Height);
              //PlayerShip playerShip = new PlayerShip(Location, boundaries);
          //}
          
          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();//To redraw part of the form that is "dirty".The Refresh() method is: Invalidate() + Update().
          //stars.Draw(g);
          //Stars stars;
          
            
      }//End method

      public void Twinkle()
      {         
      }

      
      public void AnimateObject()
      {          
      }//End method     

      public void MovePlayer(Direction direction)
      { 
        
      }

     
    }
}

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;
        Form1 form1;
        //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(Form1 form1,Point location,Rectangle boundaries, Star star)
        {
           this.form1 = form1;
           this.star = star;           
           starList  = new List<Star>();
           this.Location = location;
           this.boundaries = boundaries;           
            

            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;
        }//End constructor

        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(1, 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
}

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
    {
        Form1 form1;// = new Form1();
        public int anmationCell { get; set; }
        PlayerShip playerShip;
       //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;
            StartGame();
        }

        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 StartGame()
        {
            framesRun = 0;
            game = new Game(this,stars,playerShip);
            //stars = new Stars();            
        }       
    }
}

Maar ik zie alleen een zwart laken??
Maar geen sterren :(.

en dit is de method om de sterren te tekenen:
PHP:
 private int animationCell = 0;
      private int Frame = 0;
      public void Draw(Graphics g, int anmationCell)
      {         
          g.FillRectangle(Brushes.Black, form1.ClientRectangle);
          foreach (Star star in starList)
          {
              g.DrawImageUnscaled(StarAnimation[animationCell], star.point.X, star.point.Y);
          }
          //Stars stars;
          //stars.Draw(g);
              //g.FillRectangle(Brushes.Black, 0, 0,
                       //form1.ClientSize.Width, form1.ClientSize.Height);
              //PlayerShip playerShip = new PlayerShip(Location, boundaries);
          //}          
          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();//To redraw part of the form that is "dirty".The Refresh() method is: Invalidate() + Update().           
      }//End method

THX voor help
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan