private void Form1_KeyUp(object sender, KeyEventArgs e) { if (s.state != possibleState.road) { keyPressed = possibleKeys.none; //on road continous movement } }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Up) { keyPressed = possibleKeys.up; previousKey = possibleKeys.up; return(true); } else if (keyData == Keys.Down) { keyPressed = possibleKeys.down; previousKey = possibleKeys.down; return(true); } else if (keyData == Keys.Right) { keyPressed = possibleKeys.right; previousKey = possibleKeys.right; return(true); } else if (keyData == Keys.Left) { keyPressed = possibleKeys.left; previousKey = possibleKeys.left; return(true); } else if ((keyData == Keys.Enter) && (s.state == possibleState.notStarted)) { startGame = true; return(true); } else if (s.state == possibleState.road) { keyPressed = previousKey; return(true); } return(base.ProcessCmdKey(ref msg, keyData)); }
/*LEADING TIMER*/ private void gameTimer_Tick(object sender, EventArgs e) { switch (s.state) { case possibleState.road: /*LABELS & TIMER*/ scoreTableText = String.Format("{3,1}♥ CASH {0,3}$ SCORE {1,4} HI {2,4}", s.cash, s.score, s.hi, s.life); Lscoretable.Text = scoreTableText; Lscoretable.BackColor = Color.Transparent; Lscoretable.Visible = true; LpresentedByText.Visible = false; LpressEnter.Visible = false; if (spawningTimer.Enabled == false) { spawningTimer.Enabled = true; //spawning timer management for state transfering } /*LOGIC*/ if (s.roadHoraceHit) //hit by car { spawningTimer.Enabled = false; previousKey = possibleKeys.none; //after hit dont move keyPressed = possibleKeys.none; s.moveWithAmbulanceForHorace(ClientSize.Width, ClientSize.Height); s.drawScreen(g, font, picture, ClientSize.Width, ClientSize.Height); s.roadHorace.imagePath = "roadHoraceD1.png"; //starting image is without skis } else //normal { if (s.cash < 0) //out of cash { scoreTableText = String.Format("{3,1}♥ CASH {0,3}$ SCORE {1,4} HI {2,4}\n\nGAME OVER ", s.cash, s.score, s.hi, s.life); Lscoretable.Text = scoreTableText; s.state = possibleState.gameOver; } else //normal { /*GAME CONTROL*/ s.moveRoadHorace(keyPressed, ClientSize.Width, ClientSize.Height); s.moveWithCars(ClientSize.Width, ClientSize.Height); s.detectCarCollisions(); s.detectRoadHoraceCollisions(ClientSize.Width, ClientSize.Height); s.drawScreen(g, font, picture, ClientSize.Width, ClientSize.Height); } } if (s.roadHorace.moveToSlope) //success, move from the road to the slope { previousKey = possibleKeys.none; //after hit dont move keyPressed = possibleKeys.none; s.roadHorace.moveToSlope = false; s.state = possibleState.skiing; finishTimer.Enabled = true; } break; case possibleState.skiing: /*LABELS & TIMER*/ if (spawningTimer.Enabled == false) { spawningTimer.Enabled = true; //spawning timer management for state transfering } LpresentedByText.Visible = false; LpressEnter.Visible = false; /*LOGIC*/ if (s.life <= 0) //out of life { scoreTableText = String.Format("{3,1}♥ CASH {0,3}$ SCORE {1,4} HI {2,4}\n\nGAME OVER ", s.cash, s.score, s.hi, s.life); Lscoretable.Text = scoreTableText; s.state = possibleState.gameOver; } else //normal { if (s.score % cashPlus == 0) //check for the n*1000th point { s.cash += 10; cashPlus += 1000; } if (s.finishFlag) //finish flag was created { if (s.finish.y < 30) //finish flag is already under the score table - do not move { System.Threading.Thread.Sleep(3000); //wait for a second and then continue if (s.cash > 0) //safe check, not really necesarry { s.roadHorace.x = (ClientSize.Width / 2) - 80; s.roadHorace.rectangle.X = (ClientSize.Width / 2) - 80; s.roadHorace.y = ClientSize.Height - 40; s.roadHorace.rectangle.Y = ClientSize.Height - 40; previousKey = possibleKeys.none; //dont move on the road in the beginning keyPressed = possibleKeys.none; s.state = possibleState.road; s.finishFlag = false; //start a new slope spawningTimer.Enabled = true; //create moving objects again } } } /*GAME CONTROL*/ s.moveHorace(keyPressed); s.moveWithObjects(); s.detectHoraceCollisions(); s.drawScreen(g, font, picture, ClientSize.Width, ClientSize.Height); s.checkMissedFlags(); /*LABELS*/ scoreTableText = String.Format("{3,1}♥ CASH {0,3}$ SCORE {1,4} HI {2,4}", s.cash, s.score, s.hi, s.life); Lscoretable.Text = scoreTableText; Lscoretable.Font = font; Lscoretable.BackColor = s.color; } break; case possibleState.gameOver: if (s.score > s.hi) { System.IO.File.WriteAllText("high_score.txt", s.score.ToString()); //high score write } System.Threading.Thread.Sleep(2000); s.state = possibleState.notStarted; break; case possibleState.notStarted: spawningTimer.Enabled = false; cashPlus = 1000; string text = System.IO.File.ReadAllText("high_score.txt"); //load high-score s.hi = Int32.Parse(text); if (!menuAlreadyPainted) //just draw the menu once { menuAlreadyPainted = true; g.Clear(Color.FromArgb(196, 198, 196)); g.DrawImage(Bitmap.FromFile("theme.png"), 0, 40); //theme picture } /*LABELS & TIMER*/ LpresentedByText.Visible = true; LpressEnter.Visible = true; Lscoretable.Visible = false; finishTimer.Enabled = false; //dont count for finish flags /*LOGIC*/ if (!startGame) //if enter was not pressed { /*TEXT ANIMATION*/ if (LpressEnter.ForeColor == Color.Black) { System.Threading.Thread.Sleep(500); LpressEnter.ForeColor = Color.Gray; } else { System.Threading.Thread.Sleep(500); LpressEnter.ForeColor = Color.Black; } } else //enter was pressed { /*FLAGS*/ s.cash = 30; s.score = 100; s.life = 3; spawningTimer.Enabled = true; s.roadHorace.haveSkis = false; s.state = possibleState.road; //switch to road startGame = false; //back to previous value menuAlreadyPainted = false; /*LABELS*/ LpressEnter.Visible = false; LpresentedByText.Visible = false; } break; case possibleState.finishing: /*ON FINISH TIMER TICK*/ spawningTimer.Enabled = false; //do not create any other moving objects s.state = possibleState.skiing; //move to skiing state break; default: break; } }
/*moving horace on the slope based on the pressed key * changing horace's picture*/ public void moveHorace(possibleKeys pk) { if (horace.y > 190) { moveFlag = false; } else { moveFlag = true; } if (jumpFlag) //turning off the jumpflag after 10 ticks { xMove = 8; yMove = 2; jumpCount++; if (jumpCount >= 10) { jumpCount = 0; jumpFlag = false; } } else { xMove = 10; yMove = 8; } switch (pk) { case possibleKeys.left: if (horace.x > 10) { horace.x -= xMove; } if (horace.y > 10) { horace.y -= yMove; } horaceDirection = possibleKeys.down; if (jumpFlag) { horace.imagePath = "jumpH.png"; } else { horace.imagePath = "left.png"; } break; case possibleKeys.right: if (horace.x < widthW - 75) { horace.x += xMove; } if (horace.y > 10) { horace.y -= yMove; } horaceDirection = possibleKeys.down; if (jumpFlag) { horace.imagePath = "jumpH.png"; } else { horace.imagePath = "right.png"; } break; case possibleKeys.up: horaceDirection = possibleKeys.down; if (horace.y > 10) { horace.y -= yMove; } if (jumpFlag) { horace.imagePath = "jumpH.png"; } else { horace.imagePath = "down.png"; } break; case possibleKeys.down: if (moveFlag) { horace.y += xMove; //unfortunate naming, but needs the same value } if (jumpFlag) { horace.imagePath = "jumpH.png"; } else { horace.imagePath = "down.png"; } break; default: if (moveFlag) { horace.y += xMove; } horaceDirection = possibleKeys.down; if (jumpFlag) { horace.imagePath = "jumpH.png"; } else { horace.imagePath = "down.png"; } break; } }
/*--------------*/ /*---MOVEMENT---*/ /*--------------*/ /*moving with horace on the road based on the pressed key * changing horace's pictures*/ public void moveRoadHorace(possibleKeys pk, int windowWidth, int windowHeight) { xMove = 10; yMove = 4; if (roadHorace.haveSkis) { sString = "s"; } else { sString = ""; } switch (pk) { case possibleKeys.left: if (roadHorace.x > 10) { roadHorace.x -= xMove; roadHorace.rectangle.X -= xMove; } horaceDirection = possibleKeys.down; if (roadHorace.imagePath == string.Format("{0}roadHoraceL1.png", sString)) { roadHorace.imagePath = string.Format("{0}roadHoraceL2.png", sString); //making steps } else { roadHorace.imagePath = string.Format("{0}roadHoraceL1.png", sString); } break; case possibleKeys.right: if (roadHorace.x < windowWidth - 50) { roadHorace.x += xMove; roadHorace.rectangle.X += xMove; } horaceDirection = possibleKeys.down; if (roadHorace.imagePath == string.Format("{0}roadHoraceR1.png", sString)) { roadHorace.imagePath = string.Format("{0}roadHoraceR2.png", sString); } else { roadHorace.imagePath = string.Format("{0}roadHoraceR1.png", sString); } break; case possibleKeys.up: horaceDirection = possibleKeys.up; if (roadHorace.y > 18) { roadHorace.y -= xMove; roadHorace.rectangle.Y -= xMove; } if (roadHorace.imagePath == string.Format("{0}roadHoraceU1.png", sString)) { roadHorace.imagePath = string.Format("{0}roadHoraceU2.png", sString); } else { roadHorace.imagePath = string.Format("{0}roadHoraceU1.png", sString); } break; case possibleKeys.down: if (roadHorace.y < windowHeight - 40) { roadHorace.y += xMove; //unfortunate naming, but it fits the same value roadHorace.rectangle.Y += xMove; } if (roadHorace.imagePath == string.Format("{0}roadHoraceD1.png", sString)) { roadHorace.imagePath = string.Format("{0}roadHoraceD2.png", sString); } else { roadHorace.imagePath = string.Format("{0}roadHoraceD1.png", sString); } break; default: break; } }