Simple game created with Windows Forms
Code to create a simple fishing game that allows a graphic "character" - in this case a boat - to move around an area in a Windows Forms Application. With each movement random events can happen such as catching a fish.
Fishing Game Code
//ProgrammingIsFun.com
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace FishingGame5000
{
public partial class GameBoard : Form
{
private int NumberMoves = 0;
private int NumberFishes = 0;
private bool Run = true;
private int WinNumber = 10;
static Random randomNumber = new Random();
private int number = 1;
List LakeItems = new List();
List FoundItems = new List();
public GameBoard()
{
InitializeComponent();
}
private void MoveBoat()
{
NumberMoves++;
MoveNumber.Text = Convert.ToString(NumberMoves);
number = randomNumber.Next(10) + 1;
if (number == 3)
{
number = randomNumber.Next(LakeItems.Count);
if (number >2)
{
number = randomNumber.Next(LakeItems.Count);
FoundItems.Add((string)LakeItems[number]);
Other.Text = "You caught a " + LakeItems[number] + "!";
}
Boat.Image = Properties.Resources.caughtfish;
NumberFishes++;
if (NumberFishes < 2)
{
Information.Text = "You caught a fish!";
}
else
{
Information.Text = "Catch " + (WinNumber - NumberFishes) + " more fish to win!";
}
FishNumber.Text = Convert.ToString(NumberFishes);
if (NumberFishes == WinNumber)
{
//they win!
string itemsFound = "";
foreach (string i in FoundItems)
{
if (itemsFound == "")
{
itemsFound = itemsFound + i;
}
else
{
itemsFound = itemsFound + "\n" + i;
}
}
Information.Text = "You win!\nYou caught " + WinNumber + " fishes, and it took you " + NumberMoves + " moves. You also found:\n" + itemsFound;
Other.Text = "";
Feedback.Show();
Run = false;
}
}
}
private void GameBoard_KeyDown(object sender, KeyEventArgs e)
{
if (Run)
{
if (e.KeyCode == Keys.Up)
{
if (Boat.Top >= 06)
{
Boat.Image = Properties.Resources.boat_up;
Boat.Top -= 10;
MoveBoat();
}
}
if (e.KeyCode == Keys.Left)
{
if (Boat.Left >= 06)
{
Boat.Image = Properties.Resources.boat_left;
Boat.Left -= 10;
MoveBoat();
}
}
if (e.KeyCode == Keys.Down)
{
if (Boat.Top <= 304)
{
Boat.Image = Properties.Resources.boat_down;
Boat.Top += 10;
MoveBoat();
}
}
if (e.KeyCode == Keys.Right)
{
if (Boat.Right <= 398)
{
Boat.Image = Properties.Resources.boat_right;
Boat.Left += 10;
MoveBoat();
}
}
}
}
private void GameBoard_Load(object sender, EventArgs e)
{
Feedback.Hide();
Information.Text = "Welcome! Sail around the lake by using the arrow keys. Find " + WinNumber + " fishes to win!";
LakeItems.Add("boot");
LakeItems.Add("jellyfish");
LakeItems.Add("shiny anchor");
LakeItems.Add("plastic water bottle");
LakeItems.Add("rusty can");
LakeItems.Add("strand of seaweed");
LakeItems.Add("snail");
LakeItems.Add("soggy straw hat");
LakeItems.Add("plastic bag");
LakeItems.Add("wood duck");
LakeItems.Add("plastic spoon");
}
private void GameBoard_KeyUp(object sender, KeyEventArgs e)
{
Boat.Image = Properties.Resources.boat;
}
}
}


