C# Tutorial – Create a Gravity Run Game in Windows Form and Visual Studio
- Subject: C# Tutorials
- Learning Time: 1 hour
Hi, welcome to this gravity run game tutorial. In this tutorial we will be making a fun game using windows, .net, C# and visual studio. We will not be using any third-party framework for this game. Rules of this game is to use the space key to jump between the top and bottom platforms. There are two obstacles that placed on both platforms, and they will be moving towards the player. If the player collides with any of those obstacles the game will end. A score system will keep track of the obstacles the player manages to evade. Also, a high score system is in the game where players will be able to save their high score for the play through and see if they can beat it on the next play.
Lesson Objectives –
- Create a fun gravity run game using windows form and visual studio
- Use labels, picture boxes and timer for this game
- Use transparent GIF in windows form
- Change GIF according to where the player lands
- Create collision detection in windows form between the player and obstacles
- Keep score and high score in the game
- Restart the game with the enter key
- Use various data types such as string, Boolean, integers and random
Download the Gravity Run Game Images Here
Full Source Code for the Gravity Run Game –
It is best practice when you are learning programming to write the codes yourself, use the source code below as guides, try to write them yourself, this way it will be easier for you understand the syntax’s better and slowly become more confident in your own programming.
namespace Gravity_Run_Game_Win_Forms_MOOICT { public partial class Form1 : Form { // created by MOO ICT 2022 // for educational purpose only // global variables int gravity; int gravityValue = 8; int obstacleSpeed = 10; int score = 0; int highScore = 0; bool gameOver = false; Random random = new Random(); public Form1() { InitializeComponent(); RestartGame(); } private void GameTimerEvent(object sender, EventArgs e) { lblScore.Text = "Score: " + score; lblhighScore.Text = "High Score: " + highScore; player.Top += gravity; // when the player land on the platforms. if (player.Top > 343) { gravity = 0; player.Top = 343; player.Image = Properties.Resources.run_down0; } else if (player.Top < 38) { gravity = 0; player.Top = 38; player.Image = Properties.Resources.run_up0; } // move the obstacles foreach (Control x in this.Controls) { if (x is PictureBox && (string)x.Tag == "obstacle") { x.Left -= obstacleSpeed; if (x.Left < -100) { x.Left = random.Next(1200,3000); score += 1; } if (x.Bounds.IntersectsWith(player.Bounds)) { gameTimer.Stop(); lblScore.Text += " Game Over!! Press Enter to Restart."; gameOver = true; // set the high score if (score > highScore) { highScore = score; } } } } // increase the speed of player and obstacles if (score > 10) { obstacleSpeed = 20; gravityValue = 12; } } private void KeyIsUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Space) { if (player.Top == 343) { player.Top -= 10; gravity = -gravityValue; } else if (player.Top == 38) { player.Top += 10; gravity = gravityValue; } } if (e.KeyCode == Keys.Enter && gameOver == true) { RestartGame(); } } private void RestartGame() { lblScore.Parent = pictureBox1; lblhighScore.Parent = pictureBox2; lblhighScore.Top = 0; player.Location = new Point(180, 149); player.Image = Properties.Resources.run_down0; score = 0; gravityValue = 8; gravity = gravityValue; obstacleSpeed = 10; foreach (Control x in this.Controls) { if (x is PictureBox && (string)x.Tag == "obstacle") { x.Left = random.Next(1200, 3000); } } gameTimer.Start(); } } }
Hi,
I hope that you are doing well. I have a question about your gravity run game that you make it very creative and beautiful but there is one mistake for in it that can you please fix it when the player hit the boxes in the game he didn’t stop but he keeps moving can you please stop it or provide me the code thanks. Any other things is very creative and beautiful I really like it.
Thanks