Click here to Skip to main content
15,905,071 members
Home / Discussions / C#
   

C#

 
AnswerRe: creating event handler for picturebox click event Pin
Member 857219720-Apr-12 6:36
Member 857219720-Apr-12 6:36 
AnswerRe: creating event handler for picturebox click event Pin
DaveyM6920-Apr-12 7:05
professionalDaveyM6920-Apr-12 7:05 
GeneralRe: creating event handler for picturebox click event Pin
Member 857219720-Apr-12 7:11
Member 857219720-Apr-12 7:11 
QuestionDodger Game in C# Pin
Qobacha19-Apr-12 9:26
Qobacha19-Apr-12 9:26 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen19-Apr-12 9:49
professionalEddy Vluggen19-Apr-12 9:49 
GeneralRe: Dodger Game in C# Pin
PIEBALDconsult19-Apr-12 12:00
mvePIEBALDconsult19-Apr-12 12:00 
GeneralRe: Dodger Game in C# Pin
Qobacha19-Apr-12 20:04
Qobacha19-Apr-12 20:04 
QuestionRe: Dodger Game in C# Pin
Eddy Vluggen20-Apr-12 0:43
professionalEddy Vluggen20-Apr-12 0:43 
GeneralRe: Dodger Game in C# Pin
Qobacha23-Apr-12 10:06
Qobacha23-Apr-12 10:06 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen23-Apr-12 11:09
professionalEddy Vluggen23-Apr-12 11:09 
GeneralRe: Dodger Game in C# Pin
Qobacha24-Apr-12 2:41
Qobacha24-Apr-12 2:41 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen24-Apr-12 4:55
professionalEddy Vluggen24-Apr-12 4:55 
GeneralRe: Dodger Game in C# Pin
Qobacha26-Apr-12 9:44
Qobacha26-Apr-12 9:44 
AnswerRe: Dodger Game in C# Pin
Eddy Vluggen26-Apr-12 9:54
professionalEddy Vluggen26-Apr-12 9:54 
GeneralRe: Dodger Game in C# Pin
Qobacha27-Apr-12 7:48
Qobacha27-Apr-12 7:48 
GeneralRe: Dodger Game in C# Pin
Eddy Vluggen27-Apr-12 7:57
professionalEddy Vluggen27-Apr-12 7:57 
GeneralRe: Dodger Game in C# Pin
Qobacha26-Apr-12 13:17
Qobacha26-Apr-12 13:17 
GeneralRe: Dodger Game in C# Pin
Eddy Vluggen27-Apr-12 7:54
professionalEddy Vluggen27-Apr-12 7:54 
GeneralRe: Dodger Game in C# Pin
Qobacha27-Apr-12 9:26
Qobacha27-Apr-12 9:26 
GeneralRe: Dodger Game in C# Pin
Eddy Vluggen27-Apr-12 9:45
professionalEddy Vluggen27-Apr-12 9:45 
GeneralGame in C# Pin
Qobacha29-Apr-12 9:26
Qobacha29-Apr-12 9:26 
AnswerRe: Game in C# Pin
Eddy Vluggen30-Apr-12 0:59
professionalEddy Vluggen30-Apr-12 0:59 
GeneralRe: Game in C# Pin
Qobacha30-Apr-12 20:38
Qobacha30-Apr-12 20:38 
I have tried Rectangle.IntersectWith but without success. I'm thinking that it may not be applicable in my case because of the way i have written my code. because i get the error saying something like values of x and y are not static.

Can you check if my code given below and see if I can be able to use IntersectWith.

C#
    public partial class Form1 : Form
    {
        bool collided = false;


        Player player;
        List<Ball> balls;
        const int fps = 60;
        public Form1()
        {
            InitializeComponent();
                        
            balls = new List<Ball>();
            Random r =  new Random();

            for(int i =0; i<1;i ++)
            {
                balls.Add(new Ball(Width, Height,r));
            }
           
            var task = new Task(Run);
            task.Start();

            player = new Player()
            {
                x = this.Width/2,
                y = (Height*9/10),
            
                xvel = 10,
                brush = Brushes.Black,
            };

        }
       
       protected void Run()
        {
            while (true)
            {
                for(int i = 0; i < balls.Count; i++)
                {
                    balls[i].Move(this.Width);                    
                }
                this.Invalidate();
                Thread.Sleep(1000 / fps);
               
            }
        }

        
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(Color.White);
            for(int i = 0; i < balls.Count; i++)
            {               
                balls[i].Draw(g);              
            }            
           player.DrawPlayer(g);     
           
        }

//This is the part where i was trying to check collision between the circle and a ball
        private void CheckCollision(PaintEventArgs e)
        {
          
            if (player.IntersectsWith(balls))
            {
                player.Intersect(balls);
                if (!player.IsEmpty)
                {
                    collided = true;
                    MessageBox.Show("collision detected");
                }
            }
        }           
    }



    public class Player
    {        
        public float x, y, xvel;
        public Brush brush;

        public Player()
        {
        
        }
    
        public void DrawPlayer(Graphics g)
        {
            g.FillRectangle(brush, new RectangleF(x, y, 30,30));       
        }
        
        public void MovePlayerLeft(int gameWidth)
        {
            if (x > 0)
            {
                x -= xvel;
            }
        }
        public void MovePlayerRight(int gameWidth)
        {            
            if (x < gameWidth-47)
            {
                x += xvel;
            }
        }      
    }

    public class Ball
    {
      
        public float x, y, yvel, radius;
        public Brush brush;
        public Ball(int gamewidth,int gameHeight,Random r)        

        {
           
            x = r.Next(gamewidth);
            y = r.Next(gameHeight);

            yvel = r.Next(2) + 5;

            radius = r.Next(10) + 5;
            brush = new SolidBrush(Color.Blue);
            
        }
      
        public void Move(int gameHeight)
                       
        {                
            if (y + radius >= gameHeight)
            {
                y =  0;             
            }
            y += yvel;

       
        }


        public void Draw(Graphics g)
        {
            g.FillEllipse(brush, new RectangleF(x-radius,y - radius, radius * 2, radius * 2));
        }
        
        
    }

GeneralRe: Game in C# Pin
Qobacha1-May-12 2:16
Qobacha1-May-12 2:16 
AnswerRe: Game in C# Pin
Eddy Vluggen1-May-12 8:45
professionalEddy Vluggen1-May-12 8:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.