public void Move(string movement, SinglePlayerScreen hs) { direction = movement; //move players at movement speed but not off screen if (x > 0) { if (direction == "left") { x = x - speed; } } if (x < hs.Width - size) { if (direction == "right") { x = x + speed; } } if (y > 0) { if (direction == "up") { y = y - speed; } } if (y < hs.Height - size) { if (direction == "down") { y = y + speed; } } }
public void MainScreen_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //if focused on screen or enough time has passed if (myWatch.ElapsedMilliseconds > 4000 || focus == true) { //stop watch myWatch.Stop(); Form f = this.FindForm(); //if key is pressed switch (e.KeyCode) { case Keys.Escape: //close program Application.Exit(); break; case Keys.N: //go to sps screen SinglePlayerScreen sps = new SinglePlayerScreen { Location = new Point(f.Location.X, f.Location.Y) }; f.Controls.Remove(this); sps.Size = f.Size; f.Controls.Add(sps); sps.Focus(); break; case Keys.B: //go to hs screen HighScoreScreen hs = new HighScoreScreen(); hs.Location = new Point((f.Width - hs.Width) / 2, (f.Height - hs.Height) / 2); f.Controls.Remove(this); hs.Size = f.Size; f.Controls.Add(hs); hs.Focus(); break; case Keys.M: //go to ins screen InstructionScreen ins = new InstructionScreen(); ins.Location = new Point((f.Width - ins.Width) / 2, (f.Height - ins.Height) / 2); f.Controls.Remove(this); ins.Size = f.Size; f.Controls.Add(ins); ins.Focus(); break; case Keys.Space: //go to game screen GameScreen gs = new GameScreen { Location = new Point(f.Location.X, f.Location.Y) }; f.Controls.Remove(this); gs.Size = f.Size; f.Controls.Add(gs); gs.Focus(); break; } } }