ReCoolCZ
New member
- Joined
- Dec 22, 2021
- Messages
- 4
- Programming Experience
- Beginner
Hello fellow programmers,
why is my game updating at different times, even it shouldn´t?
Here´s the code:
why is my game updating at different times, even it shouldn´t?
Here´s the code:
C#:
public partial class Form1 : Form
{
bool goLeft, goRight, jumping, isGameOver;
int jumpSpeed;
int force;
int score = 0;
int playerSpeed = 10;
int horizontalSpeed = 1;
int verticalSpeed = 1;
int enemyOneSpeed = 1;
int enemyTwoSpeed = 1;
int enemyThreeSpeed = 1;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goLeft = true;
}
if (e.KeyCode == Keys.Right)
{
goRight = true;
}
if (e.KeyCode == Keys.Space && jumping == false)
{
jumping = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goLeft = false;
}
if (e.KeyCode == Keys.Right)
{
goRight = false;
}
if (jumping == true)
{
jumping = false;
}
if (e.KeyCode == Keys.Enter && isGameOver == true)
{
RestartGame();
}
}
private void MainGameTimerEvent(object sender, EventArgs e)
{
txtScore.Text = "Score: " + score;
player.Top += jumpSpeed;
if (goLeft == true)
{
player.Left -= playerSpeed;
}
if (goRight == true)
{
player.Left += playerSpeed;
}
if (jumping == true && force < 0)
{
jumping = false;
}
if (jumping == true)
{
jumpSpeed = -8;
force -= 1;
}
else
{
jumpSpeed = 10;
}
foreach (Control x in this.Controls)
{
if (x is PictureBox)
{
if ((string)x.Tag == "platform")
{
if (player.Bounds.IntersectsWith(x.Bounds))
{
force = 8;
player.Top = x.Top - player.Height;
if ((string)x.Name == "horizontalPlatform" && goLeft == false || (string)x.Name == "horizontalPlatform" && goRight == false)
{
player.Left -= horizontalSpeed;
}
}
x.BringToFront();
}
if ((string)x.Tag == "coin")
{
if (player.Bounds.IntersectsWith(x.Bounds) && x.Visible == true)
{
x.Visible = false;
score++;
}
}
if ((string)x.Tag == "enemy")
{
if (player.Bounds.IntersectsWith(x.Bounds))
{
gameTimer.Stop();
isGameOver = true;
txtScore.Text = "Score: " + score + Environment.NewLine + "You were killed in your journey!";
}
}
horizontalPlatform.Left -= horizontalSpeed;
if (horizontalPlatform.Left < 10 || horizontalPlatform.Left + horizontalPlatform.Width > this.ClientSize.Width -10)
{
horizontalSpeed = -horizontalSpeed;
}
verticalPlatform.Top += verticalSpeed;
if (verticalPlatform.Top < 108 || verticalPlatform.Top > 390)
{
verticalSpeed = -verticalSpeed;
}
enemyOne.Left -= enemyOneSpeed;
if (enemyOne.Left < platform2.Left || enemyOne.Left + enemyOne.Width > platform2.Left + platform2.Width)
{
enemyOneSpeed = -enemyOneSpeed;
}
enemyTwo.Left += enemyTwoSpeed;
if (enemyTwo.Left < platform4.Left || enemyTwo.Left + enemyTwo.Width > platform4.Left + platform4.Width)
{
enemyTwoSpeed = -enemyTwoSpeed;
}
enemyThree.Left -= enemyThreeSpeed;
if (enemyThree.Left < horizontalPlatform.Left || enemyThree.Left + enemyThree.Width > horizontalPlatform.Left + horizontalPlatform.Width)
{
enemyThreeSpeed = -enemyThreeSpeed;
}
if (player.Top + player.Height > this.ClientSize.Height + 50)
{
gameTimer.Stop();
isGameOver = true;
txtScore.Text = "Score: " + score + Environment.NewLine + "You fell to your death!";
}
if (player.Bounds.IntersectsWith(end.Bounds) && score == 9)
{
gameTimer.Stop();
isGameOver = true;
txtScore.Text = "Score: " + score + Environment.NewLine + "You are one fucking mad man!";
}
else
{
txtScore.Text = "Score: " + score + Environment.NewLine + "Collect all coins!";
}
}
}
}
private void RestartGame()
{
jumping = false;
goLeft = false;
goRight = false;
isGameOver = false;
score = 0;
txtScore.Text = "Score: " + score;
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Visible == false)
{
x.Visible = true;
}
};
//player and enemies position reset
player.Left = 55;
player.Top = 441;
enemyOne.Left = 695;
enemyTwo.Left = 230;
enemyThree.Left = 563;
horizontalPlatform.Left = 480;
verticalPlatform.Top = 390;
gameTimer.Start();
}
}
Last edited by a moderator: