i tried this,

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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
  
            int carX = 178;
            int carY = 161;
        
  
            public Form1()
  
            {
                InitializeComponent();
                this.KeyDown += new KeyEventHandler(Form1_KeyDown);
                this.KeyUp += new KeyEventHandler(Form1_KeyUp);
            }
  
            private void Form1_Load(object sender, EventArgs e)
            {
  
            }
            
            void Form1_KeyDown(object sender, KeyEventArgs e)
            {

                if (e.KeyCode == Keys.Up)
                {
                    carY--;
                    this.carImage.Location = new Point(carX, carY);
                }

                if (e.KeyCode == Keys.Left)
                {
                    
                }

            }
           
            public static Image RotateImage(Image carImage, float rotationAngle)
            {
                Bitmap bmp = new Bitmap(carImage.Width, carImage.Height);
                Graphics gfx = Graphics.FromImage(bmp);
                gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
                gfx.RotateTransform(rotationAngle);
                gfx.TranslateTransform(-(float)bmp.Width / 2, (float)bmp.Height / 2);
                gfx.DrawImage(carImage, new Point(0, 0));
                return bmp;
            }

            void Form1_KeyUp(object sender, KeyEventArgs e)
            {
                while (e.KeyCode == Keys.Up && carY != 161)
                {
                    
                    if (carY > 161)
                    {
                        carY--;
                    }
                    
                    if (carY < 161)
                    {
                        carY++;
                    }
                    this.carImage.Location = new Point(carX, carY);
                }
            } 
        }
    }

but the car (image) does not rotate at all.

So here are my questions.

1. how can i get the car to rotate clockwise when i hit the left arrow key?
2. is it possible to turn the car whilst it is moving forward, if so how?

help is greatly appreciated!!!

Okay. Here's your complete game. It does exactly what you want.

Here's how it works:

* Use Up key to move forward and Down key to move backward. The car will move forward/backward in the pointed direction.
* Use Left key to rotate anti-clockwise and Right key to rotate clockwise.
* The car will not go out of the form i.e it won't go out of sight.

The Code:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Car's location
        Point carLocation;

        // The speed at which the car will move
        int speed = 5;

        // >>>>>>>>>>>> 0 = Forward, 1 = Right, 2 = Backward, 3 = Left
        int direction = 0;

        private void rotateImage(int angle)
        {
            Bitmap img = new Bitmap(pictureBox1.Image);

            // Clockwise

            if (angle == 1) 
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);

            // Anti-Clockwise

            else if (angle == 2) 
                img.RotateFlip(RotateFlipType.Rotate90FlipXY);

            pictureBox1.Width = img.Width;
            pictureBox1.Height = img.Height;
            pictureBox1.Image = img;
        }

        public Form1()
        {
            InitializeComponent();

            this.KeyDown += new KeyEventHandler(Form1_KeyDown);

            carLocation = pictureBox1.Location;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // Avoid the car from moving out of sight

            if (carLocation.X > this.ClientSize.Width - pictureBox1.Width)
            {
                carLocation.X = this.ClientSize.Width - pictureBox1.Width;
            }

            if (carLocation.X <= 0)
            {
                carLocation.X = 0;
            }

            if (carLocation.Y > this.ClientSize.Height - pictureBox1.Height)
            {
                carLocation.Y = this.ClientSize.Height - pictureBox1.Height;
            }

            if (carLocation.Y <= 0)
            {
                carLocation.Y = 0;
            }

            // Move forward in the direction in which the car is pointed

            if (e.KeyCode == Keys.Up)
            {
                if (direction == 0) // Forward
                {
                    carLocation.Y -= speed;
                }
                if (direction == 2) // Backward
                {
                    carLocation.Y += speed;
                }
                if (direction == 1) // Right
                {
                    carLocation.X += speed;
                }
                if (direction == 3) // Left
                {
                    carLocation.X -= speed;
                }
            }

            // Move backward in the direction in which car is pointed

            if (e.KeyCode == Keys.Down)
            {
                // This is opposite of KeyDown.

                if (direction == 0) // Forward
                {
                    carLocation.Y += speed;
                }
                else if (direction == 2) // Backward
                {
                    carLocation.Y -= speed;
                }
                else if (direction == 1) // Right
                {
                    carLocation.X -= speed;
                }
                else if (direction == 3) // Left
                {
                    carLocation.X += speed;
                }
            }

            // Rotate the car anti-clockwise
            if (e.KeyCode == Keys.Left)
            {
                rotateImage(2); // rotate anti-clockwise
                direction--;

                if (direction < 0)
                    direction = 3;
            }

            // Rotate the car clockwise
            if (e.KeyCode == Keys.Right)
            {
                rotateImage(1); // rotate clockwise
                direction++;

                if (direction > 3)
                    direction = 0;
            }

            pictureBox1.Location = carLocation;
        }
    }
}

Enjoy.

Thanks

Edited 6 Years Ago by farooqaaa: n/a

Comments
Nice!
This question has already been answered. Start a new discussion instead.