Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D; //Added for double buffering
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media; //needed for SoundPlayer Class
namespace Frogger
{
public partial class FrmFrogger : Form
{
public FrmFrogger()
{
InitializeComponent();
this.DoubleBuffered = true;
}
Graphics myGraphics;
Random myrandom = new Random();
// Sound file strings
SoundPlayer alert = new SoundPlayer();
SoundPlayer dead = new SoundPlayer();
SoundPlayer coin = new SoundPlayer();
SoundPlayer froggerhop = new SoundPlayer();
SoundPlayer squash = new SoundPlayer();
SoundPlayer backgroundmusic = new SoundPlayer();
int[] LeftCarX = new int[7];
int[] LeftCarY = new int[7];
int[] LeftCarSpeed = new int[7];
// Image arrays of cars
Image[] LeftCars = new Image[7];
int[] RightCarX = new int[5];
int[] RightCarY = new int[5];
int[] RightCarSpeed = new int[5];
Image[] RightCars = new Image[5];
int[] LogX = new int[11];
int[] LogY = new int[11];
int[] LogSpeed = new int[11];
Image[] Logs = new Image[11];
// Image arrays of logs
bool flag = false;
Image background;
Image FroggerFrog;
Image Safety;
Image Safety2;
Image Safety3;
Image Safety4;
Image Safety5;
int x, y;//starting position of frog
//Keep track of how you are doing
int score;
int lives;
int countdown;
int SafetyX;
int SafetyY;
int Safety2X;
int Safety2Y;
int Safety3X;
int Safety3Y;
int Safety4X;
int Safety4Y;
int Safety5X;
int Safety5Y;
//Size of moving Objects
int LeftCarSize;
int RightCarSize;
int LogSize;
int SafetySize;
int Safety2Size;
int Safety3Size;
int Safety4Size;
int Safety5Size;
private void FrmFrogger_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawStuff(g);
}
private void DrawStuff(Graphics g)
{
g.DrawImage(background, 0, 0);
Font font = new Font("Arial", 9, FontStyle.Bold);
SolidBrush yellowBrush = new SolidBrush(Color.Yellow);
Font GameOverfont = new Font("Arial", 40, FontStyle.Bold);
SolidBrush blueBrush = new SolidBrush(Color.Blue);
g.DrawString("Move Up and down with the 8 and 2 Keys and left or Right with 4 and 6", font, yellowBrush, 200, 3);
g.DrawString("Current Score -> " + score.ToString(), font, yellowBrush, 5, 5);
g.DrawString("Time Left -> " + lives.ToString(), font, yellowBrush, 9, 20);
g.DrawImage(FroggerFrog, x, y);
g.DrawImage(LeftCars, LeftCarX, LeftCarY);
g.DrawImage(Safety, SafetyX, SafetyY);
g.DrawImage(Safety2, Safety2X, Safety2Y);
g.DrawImage(Safety3, Safety3X, Safety3Y);
g.DrawImage(Safety4, Safety4X, Safety4Y);
g.DrawImage(Safety5, Safety5X, Safety5Y);
if (countdown <= 0)
{
g.DrawString("GAME OVER !!!", GameOverfont, blueBrush, ClientSize.Width / 2 - 200, ClientSize.Height / 2);
g.DrawString("Press the Start Button to Play Again", font, yellowBrush, ClientSize.Width / 2 - 100, ClientSize.Height / 2 + 60);
// StopGame();
}
}
private void FrmFrogger_Load(object sender, EventArgs e)
{
background = Image.FromFile(Application.StartupPath + @"\frogger2.jpg");
FroggerFrog = Image.FromFile(Application.StartupPath + @"\froggerfrog.gif");
for (int i = 1; i <= 6; i++)
{
LeftCars[i] = Image.FromFile(Application.StartupPath + @"\LeftCar" + i.ToString() + ".gif");
}
for (int x = 1; x <= 4; x++)
{
RightCars[x] = Image.FromFile(Application.StartupPath + @"\RightCar" + x.ToString() + ".gif");
}
for (int l = 1; l <= 10; l++)
{
Logs[l] = Image.FromFile(Application.StartupPath + @"\Log" + l.ToString() + ".gif");
}
// Have the balls spread across the panel with 20 pixels borders
LeftCarSize = (int)((ClientSize.Width - 6 * 20) / 4);
RightCarSize = (int)((ClientSize.Width - 6 * 20) / 4);
LogSize = (int)((ClientSize.Width - 6 * 20) / 4);
int c = 10;
for (int i = 1; i <= 6; i++)
{
LeftCarY[i] = y;
c = c + LeftCarSize + 20;
}
for (int x = 1; x <= 4; x++)
{
RightCarY[x] = y;
c = c + RightCarSize + 20;
}
for (int l = 1; l <= 10; l++)
{
LogY[x] = y;
c = c + LogSize + 20;
}
// Give form focus
this.Focus();
Safety = Image.FromFile(Application.StartupPath + @"\Safety.gif");
Safety2 = Image.FromFile(Application.StartupPath + @"\Safety2.gif");
Safety3 = Image.FromFile(Application.StartupPath + @"\Safety3.gif");
Safety4 = Image.FromFile(Application.StartupPath + @"\Safety4.gif");
Safety5 = Image.FromFile(Application.StartupPath + @"\Safety5.gif");
score = 0;
lives = 3;
SafetyX = 25;
SafetyY = 30;
Safety2X = 155;
Safety2Y = 30;
Safety3X = 285;
Safety3Y = 30;
Safety4X = 410;
Safety4Y = 30;
Safety5X = 540;
Safety5Y = 30;
x = ClientSize.Width / 3;
y = ClientSize.Height - FroggerFrog.Height;
alert.SoundLocation = Application.StartupPath + @"\alert.wav";
backgroundmusic.SoundLocation = Application.StartupPath + @"\backgroundmusic.mp3";
coin.SoundLocation = Application.StartupPath + @"\coin.wav";
dead.SoundLocation = Application.StartupPath + @"\dead.wav";
froggerhop.SoundLocation = Application.StartupPath + @"\froggerhop.wav";
squash.SoundLocation = Application.StartupPath + @"\squash.wav";
alert.LoadAsync();
backgroundmusic.LoadAsync();
coin.LoadAsync();
dead.LoadAsync();
froggerhop.LoadAsync();
squash.LoadAsync();
this.Focus();
}
private void NewGame()
{
score = 0;
countdown = 60;
lives = 3;
//backgroundmusic.Play(); background stops playing when shooting bullet
//axWindowsMediaPlayer1.URL = Application.StartupPath + @"\opening.wav";
}
private void StopGame()
{
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = false;
timer5.Enabled = false;
timer6.Enabled = false;
//axWindowsMediaPlayer1.close();
}
private void FrmFrogger_KeyDown(object sender, KeyEventArgs e)
{
//Make sure to turn Num Lock on
if (e.KeyCode == Keys.NumPad6)
{
x += 5;
}
else if (e.KeyCode == Keys.NumPad4)
{
x -= 5;
}
else if (e.KeyCode == Keys.NumPad8)
{
y -= 5;
}
else if (e.KeyCode == Keys.NumPad2)
{
y += 5;
}
Invalidate();
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 1; i <= 6; i++)
{
// move falling obejct [i]
LeftCarX[i] = LeftCarX[i] + LeftCarSpeed[i];
// check if falling object [i] has been hit
if ((LeftCarY[i] + LeftCarSize) > (ClientSize.Width - FroggerFrog.Width))
{
if (LeftCarX[i] - 20 < x) //-20/+20 added to make sure falling
{ //ojbect bigger than dribbler
if ((LeftCarX[i] + LeftCarSize + 20) > (x + FroggerFrog.Width))
{
// object [i] has been hit
// Increase score - move back to top
Console.Beep();
//txtScore.Text = Convert.ToString(Convert.ToInt32(txtScore.Text) + 1);
LeftCarY[i] = -LeftCarSize;
LeftCarSpeed[i] = myrandom.Next(4) + 3;
}
}
}
// check for moving off bottom
if ((LeftCarY[i] + LeftCarSize) > ClientSize.Height)
{
// object [i] reaches bottom without being hit
// Move back to top with new speed
LeftCarY[i] = -LeftCarSize;
LeftCarSpeed[i] = myrandom.Next(4) + 3;
}
}
}
private void BtnStart_Click_1(object sender, EventArgs e)
{
NewGame();
timer1.Enabled = true;
timer2.Enabled = true;
timer3.Enabled = true;
timer4.Enabled = true;
timer5.Enabled = true;
timer6.Enabled = true;
// set each ball off side of panel and give new speed
for (int i = 0; i < 6; i++)
{
LeftCarX[i] = -LeftCarSize;
LeftCarSpeed[i] = myrandom.Next(4) + 3;
}
// Set man near center
x = (int)(ClientSize.Width / 2);
// Give form focus so it can accept KeyDown events
this.Focus();
}
}
}
Heres the whole program maybe it will help.