C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Graphics »

Balls that bounces within a form


Posted Date: 07 Jan 2009    Resource Type: Code Snippets    Category: Graphics
Author: srinidhi.m.gMember Level: Silver    
Rating: 1 out of 5Points: 10



I have developed a code in C# by using which I could bounce and make two balls to move within a form...it may help those who are really interested in graphics...

This is my code snippet....

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

namespace BarNBall
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

int x =0,y=0,x1=0,y1=0;

Graphics g;
Graphics g1;
SolidBrush brush;
SolidBrush brush1;
Bar bar1;
bool bottom = true, left = false, right = false, top = false;
bool bottom1 = true, left1 = false, right1 = false, top1 = false;

private void Form1_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
g1 = this.CreateGraphics();
brush = new SolidBrush(Color.Cyan);
brush1 = new SolidBrush(Color.Red);
bar1 = new Bar(this.Width/ 3 + 40, this.Height - 100);
bar1.Location = new Point(this.Width / 3 + 40, this.Height - 100);
this.Controls.Add(bar1);
x =0;
y = 1;
x1 = this.Width - 20;
y1 = this.Height - 30;
timer1.Tick += new EventHandler(/*drawBublesYplus*/bounceTheBall1);//used timer to move ball for every moment
timer1.Interval = 1;
timer1.Start();
timer2.Tick += new EventHandler(bounceTheBall2);
timer2.Interval = 1;
timer2.Start();

}

public void bounceTheBall1(Object sender, EventArgs e)//for the first ball
{
timer1.Stop();
this.Refresh();
g.FillEllipse(brush, x, y, 18, 18);

if ((y + 40) == (this.Height))//height
{
right = false;
left = false;
top = false;
bottom = true;

}

if ((x + 20) == (this.Width))//width
{
right = true;
left = false;
top = false;
bottom = false;
}

if ((x) <= 0)//x-location
{
right = false;
left = true;
top = false;
bottom = false;
}

if ((y) <= 0)//y-location
{
right = false;
left = false;
top = true;
bottom = false;
}



if (bottom == true)
{
if (x >= (this.Width / 2))
{
++x;
--y;
}
else
{
++x;
y -= 2;
}

}

if (top == true)
{
if (x >= (this.Width / 2))
{
--x;
++y;
}
else
{
x -= 2;
++y;
}

}

if (left == true)
{
++x;
++y;
}

if (right == true)
{
--x;
--y;
}
timer1.Enabled = true;

}

public void bounceTheBall2(Object sender, EventArgs e)//for the second ball
{
timer2.Stop();
this.Refresh();
g1.FillEllipse(brush1, x1, y1, 18, 18);

if ((y1 + 40) == (this.Height))//height
{
right1 = false;
left1 = false;
top1 = false;
bottom1 = true;

}

if ((x1 + 20) == (this.Width))//width
{
right1 = true;
left1 = false;
top1 = false;
bottom1 = false;
}

if ((x1) <= 0)//x-location
{
right1 = false;
left1 = true;
top1 = false;
bottom1 = false;
}

if ((y1) <= 0)//y-location
{
right1 = false;
left1 = false;
top1 = true;
bottom1 = false;
}



if (bottom1 == true)
{
if (x1 >= (this.Width / 2))
{
++x1;
--y1;
}
else
{
++x1;
y1 -= 2;
}

}

if (top1 == true)
{
if (x1 >= (this.Width / 2))
{
--x1;
++y1;
}
else
{
x1 -= 2;
++y1;
}

}

if (left1 == true)
{
++x1;
++y1;
}

if (right1 == true)
{
--x1;
--y1;
}
timer2.Enabled = true;

}




}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Inform mi if this helped u  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Simple C# code to Add Watermark an Image
Previous Resource: Silverlight : Creating Brush from Color name
Return to Discussion Resource Index
Post New Resource
Category: Graphics


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use