// main.cs // C:>csc /t:winexe main.cs using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.ComponentModel; // Ticker using System.IO; using System.Windows.Forms; // PictureBox class Metiors { private int m_iWidth = 0; private int m_iHeight = 0; private Bitmap m_Image = null; private bool m_bAlive = false; public float m_xPos = 20; public float m_yPos = 20; public float m_xVelocity = 0.8f; public float m_yVelocity = 0.55f; static long Seed; // = DateTime.Now.Ticks; static Random TheRandom; // = new Random( (int)Seed); public void Create( String strFile ) { m_Image = new Bitmap( strFile ); m_iWidth = m_Image.Width; m_iHeight = m_Image.Height; m_bAlive = true; m_xPos = randommaker(300 - m_iWidth ); m_yPos = randommaker(246 - 50 - 50); m_yPos += 50; //Seed = DateTime.Now.Ticks; //TheRandom = new Random( (int)Seed); m_xVelocity = (float)TheRandom.NextDouble(); m_yVelocity = (float)TheRandom.NextDouble(); } public void Update() { m_xPos += m_xVelocity; m_yPos += m_yVelocity; //----------------- // Update so that they bounce off the walls if( m_xPos <= 0 ) m_xVelocity = -m_xVelocity; if( (m_xPos+m_iWidth) >= 300 ) m_xVelocity = -m_xVelocity; if( m_yPos <= 50 ) m_yVelocity = -m_yVelocity; if( (m_yPos+m_iHeight) >= (246 - 50)) m_yVelocity = -m_yVelocity; // -50! } public void Render( System.Drawing.Graphics g) { if( !m_bAlive ) return; g.DrawImage(m_Image, new Point((int)m_xPos,(int)m_yPos) ); }// End of Render(..) public int randommaker(int range) { //long Seed = DateTime.Now.Ticks; //Random TheRandom = new Random( (int)Seed); float fRand = TheRandom.Next(0, range); //int rand=(int)Math.Floor(range*fRand); //if (rand==0) { rand=Math.Ceil(range/2); } //int iRand = (int)(fRand * range); return (int)fRand; } public void SetRandSeed() { Seed = DateTime.Now.Ticks; TheRandom = new Random( (int)Seed); } public bool isInsideBox(int x, int y) { float m_xCenter = m_xPos + m_iWidth/2; float m_yCenter = m_yPos + m_iHeight/2; // Simple sphere-point intersection test. float xx = Math.Abs(m_xCenter - x); float yy = Math.Abs(m_yCenter - y); if( (xx < (m_iWidth/2) ) && ( yy < (m_iHeight/2)) ) return true; return false; } }; class AWindow : System.Windows.Forms.Form { private IContainer components; private Timer Ticker; // -------------- Metiors[] m_Metiors; /* // average speed of the flying deathstones. Higher means faster private float floatingspeed=1; // width of gamezone (pixels) private int bgpicturewidth=300; // height of gamezone (pixels) private int bgpictureheight=246; // leftposition of sttusmessages in relation to gamezone private int statustextdistanceleft=20; // topposition of statusmessages in relation to gamezone private int statustextdistancetop=215; // leftposition of timesign in relation to backgroundpicture private int timesigndistanceleft=220; // topposition of timesign in relation to backgroundpicture private int timesigndistancetop=215; // leftposition of levelmarker in relation to backgroundpicture private int markerdistanceleft=0; // topposition of levelmarker in relation to backgroundpicture private int markerdistancetop=183; private int x,y; */ private bool youlostgame=false; private bool gamehasstarted=false; private bool alreadycrashed=false; /* String statustextcontent=""; int tempo=20; float timer; float timetinmer; float starttime; float nowtime; float check_standtimer; */ int numberofimages=3; /* int stepx=new Array(); int stepy=new Array(); */ int marginleft= 0; int marginright = 246; int margintop = 50; int marginbottom = 300 - 50; //float timer; //int spancontent=new Array() //int imgpreload=new Array() // -------------- private int m_mouse_x; private int m_mouse_y; int[] coords0 = new int[] {1,1,0,198,299,198,298,1,280,63,285,71,283,80,279,91,271,102,276,134,273,160,265,176,252,187,231,188,209,174,203,157,215,132,227,118,228,111,209,110,188,91,171,78,152,80,146,85,146,93,158,97,179,100,191,107,198,122,191,142,172,152,154,151,135,137,128,131,105,130,101,135,100,156,88,173,67,183,41,183,24,172,16,146,21,125,36,111,46,108,64,105,70,99,66,84,50,77,33,82,30,85,21,87,15,77,19,67,41,60,60,60,78,67,89,89,87,107,80,116,65,124,48,126,34,137,34,153,44,165,70,164,83,151,85,129,94,116,114,111,134,113,149,124,160,133,170,133,178,126,168,115,143,113,127,97,131,71,154,61,179,60,195,70,210,88,214,90,234,92,245,99,246,120,233,142,222,155,222,159,239,169,248,166,256,152,256,124,251,96,254,92,264,80,264,69,270,62,278,60}; private PictureBox Screen; void startgame() { resetvalues(); //startnewtime(); statustext("Move on!"); setValues(); } void resetvalues() { youlostgame=false; gamehasstarted=true; alreadycrashed=false; if( alreadycrashed ) { ; } } void statustext( String str ) { this.Text = str; } void setValues() { for (int i=0;i<=numberofimages;i++) { //var thisspan = eval("document.all.span"+i) //thisspan.innerHTML=spancontent[i] //var thisspan = eval("document.all.span"+(i)+".style") int randomposleft=marginright-marginleft; int startstoneleft=marginleft+randomposleft/3; randomposleft=randomstartposition(randomposleft); randomposleft=randomposleft+marginleft; int randompostop=marginbottom-margintop; randompostop=randomstartposition(randompostop); randompostop=randompostop+margintop; if (randomposleft=thisspan.posLeft&&x=thisspan.posTop&&ymarginright-imgwidth[i]) { thisspan.posLeft-=Math.abs(stepx[i]+1) stepx[i]=randommaker(floatingspeed)*-1 } if (thisspan.posLeftmarginbottom-imgheight[i]) { thisspan.posTop-=Math.abs(stepy[i]) stepy[i]=randommaker(floatingspeed)*-1 } if (thisspan.posTop 0 ) totalIntersections++; } if( (totalIntersections % 2)==0 ) // even return true; else return false; }// End insidePoly(..) int IntersectLines2D( double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double p3x, double p3y) { // Uses parametric lines. (e.g. p = p0 + (p1-p0)*t ) where t is from 0 to 1; double p0vx = p1x - p0x; double p0vy = p1y - p0y; double p1vx = p3x - p2x; double p1vy = p3y - p2y; // Test -1- Check for parallel ines. If the direction vectors are scalar // multiples then the lines are parallel and can't possibly intersect unless // the lines overlap double det_p0p1 = (p0vx*p1vy - p0vy*p1vx); if( Math.Abs( det_p0p1) <= 0.000001 ) return 0; // Parallel lines - hence no intersection // Test -2- Computer the t0 and t1 values for intersection, where we have // two lines of hte form // p = p0 + v*t // p1 = p10 + v1*t1 // p1.x = p10.x + v1.x*t1 // p1.y = p10.y + v1.y*t1 // p2 = p20 + v2*t2 // p2.x = p20.x + v2.x*t2 // p2.y = p20.y + v2.y*t2 // Solve the system when x1 = x2 and y1 = y2 double t0 = (p1vx*(p0y - p2y) - p1vy*(p0x - p2x) ) / det_p0p1; double t1 = (p0vx*(p0y - p2y) - p0vy*(p0x - p2x) ) / det_p0p1; if( (t0>=0) && (t0<=1) && (t1>=0) && (t1<=1) ) return 1; // We have an intersection else return 0; // Intersects out of bounds. }//End IntersectLines2D(..) private void OnMouseMoveEvent(object sender, System.Windows.Forms.MouseEventArgs e) { int mouse_x = e.X; int mouse_y = e.Y; m_mouse_x = mouse_x; m_mouse_y = mouse_y; if( insidePoly( coords0, mouse_x, mouse_y) ) this.Text = "In"; else this.Text = "Out"; //Render(); } private void Ticker_Tick(object sender, EventArgs e) { /* if( m_bGameOver == false ) { Render(); } else { Render(); } */ UpdateGame(); Render(); } // End of Ticker_Tick(..) private void Create() { m_Metiors = new Metiors[5]; m_Metiors[0] = new Metiors(); m_Metiors[0].SetRandSeed(); for(int i=0; i<5; i++) { m_Metiors[i] = new Metiors(); String str = "img\\" + "dangerpic" + (i+1).ToString() + ".gif"; m_Metiors[i].Create(str); } } private void UpdateGame() { for(int i=0; i<5; i++) { m_Metiors[i].Update(); } bool bCollision = false; for(int i=0; i<5; i++) { bCollision = m_Metiors[i].isInsideBox(m_mouse_x, m_mouse_y); if( bCollision ) break; } if( bCollision ) this.Text = "Collision"; } AWindow() { this.ClientSize = new System.Drawing.Size(300,246); this.Screen = new System.Windows.Forms.PictureBox(); this.Screen.Name = "Screen"; this.Screen.Size = new System.Drawing.Size(300, 246); this.Screen.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveEvent); // We need to add our screen buffer to our window this.Controls.AddRange(new System.Windows.Forms.Control[] {this.Screen}); /* ------------ */ this.components = new System.ComponentModel.Container(); /* ------------ */ this.Ticker = new System.Windows.Forms.Timer(this.components); this.Ticker.Interval = 25; this.Ticker.Tick += new System.EventHandler(this.Ticker_Tick); /* ------------ */ Ticker.Enabled = true; /* ------------ */ // Load all game images Create(); // Render to our backbuffer/screen. Render(); }// End of AWindow() // Application Entry Point static void Main() { System.Windows.Forms.Application.Run(new AWindow()); }// End Main() }// End of AWindow class