Click here to Skip to main content
15,887,313 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: Game in C# Pin
Qobacha1-May-12 12:50
Qobacha1-May-12 12:50 
GeneralRe: Game in C# Pin
Eddy Vluggen2-May-12 6:47
professionalEddy Vluggen2-May-12 6:47 
AnswerRe: Dodger Game in C# Pin
Abhinav S19-Apr-12 16:28
Abhinav S19-Apr-12 16:28 
GeneralRe: Dodger Game in C# Pin
CDP180220-Apr-12 4:03
CDP180220-Apr-12 4:03 
GeneralRe: Dodger Game in C# Pin
Qobacha20-Apr-12 10:52
Qobacha20-Apr-12 10:52 
GeneralRe: Dodger Game in C# Pin
Qobacha20-Apr-12 10:51
Qobacha20-Apr-12 10:51 
QuestionMenuStrip Erroneously Closing Pin
Skippums19-Apr-12 7:27
Skippums19-Apr-12 7:27 
AnswerRe: MenuStrip Erroneously Closing Pin
Alan N19-Apr-12 10:05
Alan N19-Apr-12 10:05 
GeneralRe: MenuStrip Erroneously Closing Pin
Skippums19-Apr-12 10:46
Skippums19-Apr-12 10:46 
QuestionJust a C# application. Pin
amx_tiger19-Apr-12 6:17
amx_tiger19-Apr-12 6:17 
AnswerRe: Just a C# application. Pin
Eddy Vluggen19-Apr-12 9:46
professionalEddy Vluggen19-Apr-12 9:46 
GeneralRe: Just a C# application. Pin
amx_tiger19-Apr-12 10:26
amx_tiger19-Apr-12 10:26 
AnswerRe: Just a C# application. Pin
Eddy Vluggen19-Apr-12 11:02
professionalEddy Vluggen19-Apr-12 11:02 
GeneralRe: Just a C# application. Pin
amx_tiger19-Apr-12 20:08
amx_tiger19-Apr-12 20:08 
GeneralRe: Just a C# application. Pin
Sentenryu20-Apr-12 1:29
Sentenryu20-Apr-12 1:29 

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.