Jump to content
Xtreme .Net Talk

heater19

Members
  • Posts

    3
  • Joined

  • Last visited

Everything posted by heater19

  1. thanks for the help, I'll give it a try
  2. I'm starting out with learning C# and GDI+; and was trying to come up with ways of implementing mouseclick movement for a game. The method I came up with is below. Would welcome any suggestions for making it better. Relevant code:--- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace clickmoveproject { public partial class Form1 : Form { Bitmap backup; Graphics gfx, fgfx; PointF charloc; private bool mclick; float speed = 3f; float dx = 0f; float dy = 0f; float mX, mY; float ddx = 0f; float ddy = 0f; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { backup = new Bitmap(640, 480); gfx = Graphics.FromImage(backup); gfx.FillRectangle(Brushes.Black, this.ClientRectangle); fgfx = this.CreateGraphics(); //*2** charloc = new Point(4, 4); } void Present() { fgfx.DrawImage(backup, 0, 0); } private void movement() { if (mclick) { charloc.X += ddx; charloc.Y += ddy; } } private void timer1_Tick_1(object sender, EventArgs e) { //*5** movement(); //*6** gfx.FillRectangle(Brushes.Black, this.ClientRectangle); gfx.DrawRectangle(Pens.Green, charloc.X, charloc.Y, 30, 30); gfx.DrawLine(Pens.Aqua, charloc.X, charloc.Y, mX, mY); Present(); } private void Form1_MouseClick(object sender, MouseEventArgs e) { mclick = true; mX = e.X; mY = e.Y; dx = mX - charloc.X; dy = mY - charloc.Y; double dist = Math.Sqrt((double)(dx * dx) + (double)(dy * dy)); ddx = (dx / (float)dist) * speed; ddy = (dy / (float)dist) * speed; } } } ----- end of code ---- thanks to IcePlug for his help http://www.Iceplug.us
  3. I've got a bad version of this algorithm working, bad because my graphic object which i'm trying to move to where i clicked the mouse, still doesn't move in a straight line to the final x,y position I realise i could do this easier with XNA and vectors, but i'm just obsessed at this point since people have been doing this sort of movement in games for over 15 years (Diablo, Baldurs Gate, Age of Empires, blah, blah, blah...) It's just a straight on movement, so i don't need any sort of pathfinding(A* or something like that) here is the relevant code, I know I'm losing precision when I update the playerShip position, which is the cause of the problem, but how to fix that? thanks in advance private void Form1_Load(object sender, EventArgs e) { // Setup clock interval. timer1.Interval = 100; timer1.Enabled = true; this.Size = new Size(themapwidth + 8, themapheight + 24); backbuffer = new Bitmap(themapwidth, themapheight); // gfx is aimed at the backbuffer. gfx = Graphics.FromImage(backbuffer); // formgfx is aimed at the form. formgfx = this.CreateGraphics(); playerShip = new Rectangle(themapwidth/2, themapheight/2,6,6); // Instantiate the player location. // Make our drawing when the timer ticks. updateartwork = true; } private void drawonform() { formgfx.DrawImage(backbuffer, 0, 0); // The backbuffer was drawn on by the gfx. } void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { updateartwork = true; } private void timer1_Tick_1(object sender, EventArgs e) { if (mouseClik) { deltaX = MousePos.X - playerShip.X; deltaY = MousePos.Y - playerShip.Y; dist = Math.Sqrt(deltaX * deltaX + deltaY * deltaY); if (dist >=2) { theta = Math.Atan2(deltaY, deltaX); thetaX = (float)(movespeed * (Math.Cos(theta))); thetaY = (float)(movespeed * (Math.Sin(theta))); playerShip.X += (int)(thetaX); playerShip.Y += (int)(thetaY); } else { playerShip.Offset(0, 0); } updateartwork = true; } if (updateartwork) { gfx.FillRectangle(Brushes.Black, Rectangle.FromLTRB(0, 0, themapwidth, themapheight)); gfx.FillEllipse(Brushes.Azure, playerShip); updateartwork = false; // set it back to false. } this.drawonform(); } private void Form1_MouseClick(object sender, MouseEventArgs e) { MousePos = PointToClient(Cursor.Position); mouseClik = true; } } }
×
×
  • Create New...