/// <summary> /// Initializes a new Player /// </summary> /// <param name="id">The id of the player</param> /// <param name="playerName">The name of the player</param> /// <param name="startPos">The start position of the car</param> /// <param name="startRot">The start rotation of the car</param> /// <param name="playerImage">The image used as a car</param> /// <param name="playerKeysToUse">All the keys that work with this player</param> public Player(string id, string playerName, Point startPos, int startRot, Bitmap playerImage, List <Keys> playerKeysToUse) { this.name = id; this.playerName = playerName; //Assign a car to each player this.playerCar = new Car(int.Parse(name), startPos, startRot, playerImage, 0.25f, 0.25f); this.playerKeys = playerKeysToUse; //Register with graphicsEngine GraphicsEngine.AddAsset(new Asset(++GraphicsEngine.assetsToRender, playerCar.image, playerCar.pos, playerCar.rot, playerCar.scaleX, playerCar.scaleY), RenderType.Player); //Player control timer Timer playerTimer = new Timer(); playerTimer.Interval = 1; playerTimer.Tick += Timer_Tick; playerTimer.Start(); //Player event timer Timer EventTimer = new Timer(); EventTimer.Interval = 10; EventTimer.Tick += Event_Tick; EventTimer.Start(); }
/// <summary> /// Main window constructor used to initialize with playernames and a amount of rounds /// </summary> /// <param name="p1Name">Name of the first player</param> /// <param name="p2Name">Name of the second player</param> /// <param name="reqRounds">Amount of rounds to race</param> public MainWindow(string p1Name, string p2Name, int reqRounds) { //Initializes the window InitializeComponent(); //Sets the playername input in the main menu player1Name = p1Name; player2Name = p2Name; //Amount of rounds to race requiredRounds = reqRounds; //Set the window to use double buffering and other settings this.SetStyle( ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); //Set timer parameters for the main game loop GameTimer.Interval = 10; GameTimer.Tick += new EventHandler(GameUpdate); GameTimer.Start(); //Set timer parameters for the player info window InfoTimer.Interval = 100; InfoTimer.Tick += new EventHandler(InfoUpdate); InfoTimer.Start(); //Set the key up and key down callbacks this.KeyDown += new KeyEventHandler(SetKeysDown); this.KeyUp += new KeyEventHandler(SetKeysUp); //Initializes graphics engine with a graphics handle Graphics g = canvas.CreateGraphics(); gEngine = new GraphicsEngine(g); //Create the players players.Add(new Player("1", player1Name, new Point(1948, 352), 0, car1, new List <Keys>() { Keys.W, Keys.S, Keys.A, Keys.D })); players.Add(new Player("2", player2Name, new Point(1948, 484), 0, car2, new List <Keys>() { Keys.Up, Keys.Down, Keys.Left, Keys.Right })); }
/// <summary> /// Initializes a new Car /// </summary> /// <param name="playerId">The id of the player</param> /// <param name="startPos">The start position of the car</param> /// <param name="startRot">The start rotation of the car</param> /// <param name="carImage">The image used as car</param> /// <param name="xScale">The x scale of the car</param> /// <param name="yScale">The y scale of the car</param> public Car(int playerId, Point startPos, int startRot, Bitmap carImage, float xScale = 0, float yScale = 0) { this.playerId = playerId; pos = startPos; rot = startRot; image = carImage; //If the xScale is not assigned set scaleX to 1 if (xScale == 0) { scaleX = 1; } else { scaleX = xScale; } //If the yScale is not assigned set scaleY to 1 if (yScale == 1) { scaleY = 1; } else { scaleY = yScale; } //Initialize the movement loop carTimer.Interval = 1; carTimer.Elapsed += MoveCar; carTimer.Start(); //Scale the new car GraphicsEngine.UpdateScale(playerId, scaleX, scaleY); //Set the unscaled size of the image imageSize = new Point(image.Width, image.Height); }
/// <summary> /// Moves the car by calculating a new point useing the speed and the rotation /// </summary> /// <param name="sender">Not used</param> /// <param name="e">Not used</param> private void MoveCar(object sender, ElapsedEventArgs e) { pos = CalcMovePoint(currentSpeed, rot); //Check if car is not going outside of the window if (pos.X < 0) { pos.X = 0; } if (pos.X > MainWindow.screenSize.Width * (1 / scaleX) - imageSize.X) { pos.X = (int)(MainWindow.screenSize.Width * (1 / scaleX) - imageSize.X); } if (pos.Y < 0) { pos.Y = 0; } if (pos.Y > MainWindow.screenSize.Height * (1 / scaleY) - imageSize.Y) { pos.Y = (int)(MainWindow.screenSize.Height * (1 / scaleY) - imageSize.Y); } //Update position of the car using the GraphicsEngine class GraphicsEngine.UpdatePos(playerId, pos); }
/// <summary> /// Main window constructor used to initialize with playernames and a amount of rounds /// </summary> /// <param name="p1Name">Name of the first player</param> /// <param name="p2Name">Name of the second player</param> /// <param name="reqRounds">Amount of rounds to race</param> public MainWindow(string p1Name, string p2Name, int reqRounds) { //Initializes the window InitializeComponent(); //Sets the playername input in the main menu player1Name = p1Name; player2Name = p2Name; //Amount of rounds to race requiredRounds = reqRounds; //Set the window to use double buffering and other settings this.SetStyle( ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true); //Set timer parameters for the main game loop GameTimer.Interval = 10; GameTimer.Tick += new EventHandler(GameUpdate); GameTimer.Start(); //Set timer parameters for the player info window InfoTimer.Interval = 100; InfoTimer.Tick += new EventHandler(InfoUpdate); InfoTimer.Start(); //Set the key up and key down callbacks this.KeyDown += new KeyEventHandler(SetKeysDown); this.KeyUp += new KeyEventHandler(SetKeysUp); //Initializes graphics engine with a graphics handle Graphics g = canvas.CreateGraphics(); gEngine = new GraphicsEngine(g); //Create the players players.Add(new Player("1",player1Name, new Point(1948, 352), 0, car1, new List<Keys>() { Keys.W, Keys.S, Keys.A, Keys.D })); players.Add(new Player("2", player2Name, new Point(1948, 484), 0, car2, new List<Keys>() { Keys.Up, Keys.Down, Keys.Left, Keys.Right })); }
/// <summary> /// Steering right method /// </summary> public void SteerRight() { rot += 15; Decellerate(); GraphicsEngine.UpdateRot(playerId, rot); }