Moving Smiling Face C# -


below i've created program using c# creates smiley face. moves across screen. cannot figure out how smiley face bounce off edges , around screen. please help. thank you.

    */using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace happyface {     public partial class happyface : form     {         int xpos = 0;         int ypos = 0;         int width = 0;         int length = 0;         int startangle = 45;         int sweepangle = 90;          public happyface()         {             initializecomponent();         }          private void happyface_load(object sender, eventargs e)         {          }          private void happyface_paint(object sender, painteventargs e)         {             graphics g = e.graphics;             pen mypen = new pen(brushes.red, 7);             pen mypen2 = new pen(brushes.green, 7);             //g.drawline(mypen, 0, 0, 500, 500);             //g.drawline(mypen, 0, 0, this.clientrectangle.width, this.clientrectangle.height);             //g.drawline(mypen2, 0, this.clientrectangle.height, this.clientrectangle.width, 0);             //g.drawline(mypen2, this.clientrectangle.left, this.clientrectangle.bottom, this.clientrectangle.right, clientrectangle.top);              int endx = this.clientrectangle.width;             int endy = this.clientrectangle.height;              //string msg = string.format("endx = {0} endy = {1}", endx, endy);             //messagebox.show(msg);              int xcenter = this.clientrectangle.left + (this.clientrectangle.width / 2);             int ycenter = this.clientrectangle.top + (this.clientrectangle.height / 2);              pen circlepen = new pen(brushes.black, 9);              //g.drawellipse(circlepen, xcenter - 50, ycenter - 50, 100, 100);             // g.fillellipse(brushes.orange, xcenter -50, ycenter - 50, 100, 100);              font myfont = new font("monotype corsiva", 43, fontstyle.bold);             g.drawstring("happy face", myfont, brushes.aqua, 300, 25);              //g.drawarc(circlepen, xpos, width, length, startangle, sweepangle);              g.drawellipse(circlepen, xpos, ypos + 130, 250, 250);             g.fillellipse(brushes.peachpuff, xpos, ypos + 130, 250, 250);             g.drawellipse(circlepen, xpos + 65, ypos + 200, 20, 35);             g.fillellipse(brushes.black, xpos + 65, ypos + 200, 20, 35);             g.drawellipse(circlepen, xpos + 160, ypos + 200, 20, 35);             g.fillellipse(brushes.black, xpos + 160, ypos + 200, 20, 35);             g.drawarc(circlepen, xpos + 60, ypos + 215, 130, 120, 35, 115);          }          private void timer1_tick(object sender, eventargs e)         {             xpos = xpos + 3;             if(xpos >= this.clientrectangle.right - 250)             {                 xpos = 0;             }             this.invalidate();         }     } }*/ 

well, bit bored. i'll assume object going move in 45 degrees trajectory,and when collides bounds change 90ยบ.

what (this simple solution) is, first of all, define direction in both axes in want "smiley" move,the step in each timer tick, position of center , size of object, like:

int xpos = 0; int ypos = 130; int step = 10; int width = 250; int height = 250; int directionx = +1; int directiony = -1; 

the timer increase x , y positions:

private void timer1_tick(object sender, eventargs e) {      xpos += 10*directionx;      ypos += 10*directiony;      checkbounds();  //this check if object collides bounds       this.invalidate(); } 

the checkbounds method check if object collides bounds:

private void checkbounds() {    if (ypos < 0 + step || ypos + height+ step > clientrectangle.height)    {        directiony *= -1;    }     if (xpos < 0 + step || xpos + width + step > clientrectangle.width)    {        directionx *= -1;    } } 

finally, paint method similar yours, adjusting values:

private void form2_paint(object sender, painteventargs e) {     graphics g = e.graphics;     pen mypen = new pen(brushes.red, 7);     pen mypen2 = new pen(brushes.green, 7);       int endx = this.clientrectangle.width;     int endy = this.clientrectangle.height;       int xcenter = this.clientrectangle.left + (this.clientrectangle.width / 2);     int ycenter = this.clientrectangle.top + (this.clientrectangle.height / 2);       pen circlepen = new pen(brushes.black, 9);      font myfont = new font("monotype corsiva", 43, fontstyle.bold);     g.drawstring("happy face", myfont, brushes.aqua, 300, 25);      g.drawellipse(circlepen, xpos, ypos, 250, 250);     g.fillellipse(brushes.peachpuff, xpos, ypos, 250, 250);     g.drawellipse(circlepen, xpos + 65, ypos -130  + 200, 20, 35);     g.fillellipse(brushes.black, xpos + 65, ypos-130 + 200, 20, 35);     g.drawellipse(circlepen, xpos + 160, ypos-130 + 200, 20, 35);     g.fillellipse(brushes.black, xpos + 160, ypos-130 + 200, 20, 35);     g.drawarc(circlepen, xpos + 60, ypos-130 + 215, 130, 120, 35, 115); } 

this code highly improved, may think how should done. hope helps.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -