Plant VSZombies
Plant VSZombies
Submitted by:
Umme Aymen 2023-CS-112
Supervised by:
Mam Maida Mirza
Course:
CSC-103 Object Oriented Programming
• OOP Concepts:
All the OOP Pillars are included in this game:
1. INHERITANCE:
Inheritance is implemented in interfaces where an interface is inherited by other such
as VerticalMovement Class inherits IMovement interface similarly
PlayerVSEnemyCollision Class implements ICollision Class.
2. POLYMORPHISM:
Polymorphism is implemented in some functions of Game Class where two different
types of functions Add GameObjects. It is also implemented in the form of other
functions in other classes.
3. INTERFACES:
Two types of interfaces are implemented one is IMovement for movement class and
other one is ICollision which detects the type of collisions.
• CLASSES:
Following classes are implemented in this game framework:
1. Game: It is the main class that provides a interface for the addition of game objects in
a class.
2. GameObject: It consists of all the gameobjects contained in the game.
3. VerticalMovement.HorizontalMovement,ZigZagMovement these classes implement
IMovement interface and provides different Types of Movements.
4. CollisionClasses:They implement ICollision and provides different types of collision.
5. Enum: Direction,Action and Object Type Enumeration is used.
WIREFRAMES
START PAGE
Game Menu
GameOver Page
• GAME RULES:
1. If Player Collides with the enemy it will decrease the player health.
2. If Player bullet hits the zombie it will kill the zombie.
3. Player has to kill all the enemies to make it to next level or win the game.
4. If player health reduces to zero without killing the enemy it will die and lose the game.
5. Some players require more than one bullet to kill them.
• CRC:
• CODE:
GAME CLASS:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GameLibrary.Enum;
using GameLibrary.Movement;
namespace GameLibrary.GameObjects
{
public class Game
{ //Declare all the list objects of game;
private static List<GameObject> gameObjectList;
private static Form container;
private static Game Instance;
private static List<GameObject> EnemyObjects;
private static List<GameObject> PlayerObjects;
private static List<GameObject> Bullets;
//Implementation of singleton principle
private Game(Form container)
{
gameObjectList = new List<GameObject>();
EnemyObjects = new List<GameObject>();
PlayerObjects = new List<GameObject>();
Bullets = new List<GameObject>();
SetContainer(container);
}
//function to return a valid instance
public static Game GetValidInstance(Form Container)
{
if (Instance == null)
{
Instance = new Game(Container);
}
return Instance;
}
//it adds the game objects in all specific lists
public void AddGameObject(Image img, int left, int top, IMovement
controller, ObjectType Type, bool isVisible)
{
GameObject gameObject = GameObject.GetObjectInstance(img, left, top,
controller, Type, isVisible);
if (gameObject != null)
{
if (Type == ObjectType.Enemy)
{
EnemyObjects.Add(gameObject);
}
else if (Type == ObjectType.Player)
{
PlayerObjects.Add(gameObject);
}
else if (Type == ObjectType.Bullet)
{
Bullets.Add(gameObject);
}
AddObject(gameObject);
container.Controls.Add(gameObject.GetPictureBox());
}
}
//It sets the container form
public void SetContainer(Form form)
{
container = form;
}
public Form GetForm()
{
return container;
}
public void Update()
{
foreach (GameObject go in gameObjectList)
{
go.update(go.GetPosition());
//CollissionDetector.PlayerVSEnemy(ObjectType.Player,
ObjectType.Enemy);
}
}
//function to update the object list
public static void Update(GameObject go)
{
gameObjectList.Add(go);
}
//Getter setters for the lists
public static List<GameObject> GetPlayers()
{
return PlayerObjects;
}
public static List<GameObject> GetEnemies()
{
return EnemyObjects;
}
public static List<GameObject> GetBullets()
{
return Bullets;
}
public static List<GameObject> GetAllObjects()
{
return gameObjectList;
}
public void AddObject(GameObject go)
{
gameObjectList.Add(go);
}
public static void RemoveObject(GameObject go)
{
gameObjectList.Remove(go);
container.Controls.Remove(go.GetPictureBox());
}
}
}
GAMEOBJECT CLASS:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GameLibrary.Enum;
using GameLibrary.Movement;
using GameLibrary;
namespace GameLibrary.GameObjects
{
public class GameObject
{ //sets the attributes of a game object
private PictureBox Pb;
private IMovement controller;
private System.Drawing.Point Position;
private static GameObject ObjectInstance;
private ObjectType Type;
private int Score = 0;
private float Health = 100;
//singleton principle for th eimplementation of factory pattern
private GameObject(Image img, int left, int top, IMovement controller,
ObjectType Type, bool isVisible)
{
Position = new System.Drawing.Point(left, top);
Pb = new PictureBox();
Pb.Image = img;
Pb.Location = Position;
Pb.Size = new Size(img.Width, img.Height);
}
//getter setter for the private attributes
public PictureBox GetPictureBox()
{
return Pb;
}
public void SetPictureBox(PictureBox Pb)
{
this.Pb = Pb;
}
public void SetMovement(IMovement movement)
{
this.controller = movement;
}
public IMovement GetMovement()
{
return this.controller;
}
public int GetScore()
{
return Score;
}
public void SetScore(int Score)
{
this.Score = Score;
}
public void SetHealth(float Health)
{
this.Health = Health;
}
public float GetHealth()
{
return Health;
}
//it sets the types of objects and increment them in th efactory
public void SetObjectTypes(ObjectType Type)
{
if (Type == ObjectType.Player)
{
FactoryPattern.SetMaxPlayers(FactoryPattern.GetMaxPlayers() + 1);
}
else if (Type == ObjectType.Enemy)
{
FactoryPattern.SetMaxEnemy(FactoryPattern.GetMaxEnemies() + 1);
}
else if (Type == ObjectType.Bullet)
{
FactoryPattern.SetMaxBullets(FactoryPattern.GetMaxBullets() + 1);
}
this.Type = Type;
}
public ObjectType GetObjectType()
{
return Type;
}
public void SetPosition(System.Drawing.Point Position)
{
this.Position = Position;
}
public System.Drawing.Point GetPosition()
{
return Position;
}
//Updates the picture boc and sets it to new location
public void update(System.Drawing.Point Location)
{
Position = controller.Move(Location);
Pb.Location = Position;
}
}
VERTICAL MOVEMENT
using GameLibrary.Firing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EZInput;
namespace GameLibrary.Movement
{
public class KeyBoardMovement : IMovement
{
int Speed;
System.Drawing.Point Position;
System.Drawing.Point ContainerCoordinates;
IPlayer player;
public KeyBoardMovement(int Speed, System.Drawing.Point Location,
System.Drawing.Point FormLocation, IPlayer Player)
{
this.Speed = Speed;
this.Position = Location;
this.ContainerCoordinates = FormLocation;
this.player = Player;
}
}
else if (Keyboard.IsKeyPressed(Key.DownArrow))
{
Location.Y += Speed;
if (Location.Y - 30 >= ContainerCoordinates.Y)
{
Location.Y = 0;
}
}
else if (Keyboard.IsKeyPressed(Key.UpArrow))
{
Location.Y -= Speed;
if (Location.Y + 30 <= 0)
{
Location.Y = 0;
}
}
return Location;
}
}
}
ENEMY VS PLAYER COLLISION
• using GameLibrary.Enum;
• using GameLibrary.GameObjects;
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• using GameLibrary;
•
• namespace GameLibrary.Collission
• {
• //it implements the collision of enemy with bullet
• public class EnemyVSBullet : ICollision
• {
• public string DetectCollision(Actions action)
• { //gets the list of required things and traverse them
• List<GameObject> Enemy = Game.GetEnemies();
• List<GameObject> Bullets = Game.GetBullets();
• foreach (GameObject enemy in Enemy)
• {
• foreach (GameObject bullet in Bullets)
• { //if they collide perform the required action
• if
(bullet.GetPictureBox().Bounds.IntersectsWith(enemy.GetPictureBox().Bounds))
• {
• if (action == Actions.PlayerScoreIncrement)
• {
• bullet.SetScore(bullet.GetScore() + 10);
• return bullet.GetScore().ToString();
• }
• else if (action == Actions.EnemyDead)
• {
• Game.RemoveObject(enemy);
• return Actions.EnemyDead.ToString();
• }
• }
• }
• }
• return null;
• }
• }
• }
FACTORY PATTERN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameLibrary
{
//A class to implement factory pattern by securing a count for player and other
game objects
public class FactoryPattern
{ //set attributes
private static int MaxPlayers;
private static int MaxEnemies;
private static int MaxBullets;
private static int MaxObstacles;
//Getter and setter for Attributes
public static int GetMaxPlayers()
{
return MaxPlayers;
}
public static int GetMaxEnemies()
{
return MaxEnemies;
}
public static int GetMaxBullets()
{
return MaxBullets;
}
public static int GetMaxObstacles()
{
return MaxObstacles;
}
public static void SetMaxPlayers(int maxPlayers)
{
MaxPlayers = maxPlayers;
}
public static void SetMaxEnemy(int maxEnemy)
{
MaxEnemies = maxEnemy;
}
public static void SetMaxBullets(int maxBullets)
{
MaxBullets = maxBullets;
}
public static void SetMaxObstacles(int maxObstacles)
{
MaxObstacles = maxObstacles;
}
}
}