aboutsummaryrefslogtreecommitdiff
path: root/Space Invaders
diff options
context:
space:
mode:
Diffstat (limited to 'Space Invaders')
-rw-r--r--Space Invaders/GameEngine/GameEngine.cs12
-rw-r--r--Space Invaders/GameObjects.cs173
-rw-r--r--Space Invaders/Program.cs37
3 files changed, 193 insertions, 29 deletions
diff --git a/Space Invaders/GameEngine/GameEngine.cs b/Space Invaders/GameEngine/GameEngine.cs
index 6c5c5b0..019b0aa 100644
--- a/Space Invaders/GameEngine/GameEngine.cs
+++ b/Space Invaders/GameEngine/GameEngine.cs
@@ -12,6 +12,8 @@ namespace GameEngine {
public List<GameObject> GameObjects = new List<GameObject>();
public Thread _listnerThread = new Thread(Input.Listner);
+ public Random Rand = new Random();
+
private Graphics _graphics;
private bool _running;
@@ -132,6 +134,16 @@ namespace GameEngine {
// This method is called after the frame is rendered
}
+ public bool CollidingWith(GameObject other) {
+ // This method checks if 'this' collides with another GameObject
+ // TODO Make this check if the position contains '\0', if so we are not colliding. This will make hitboxes more natural
+ int x1 = other.xPos;
+ int x2 = other.xPos + other.Sprite.GetLength(1);
+ int y1 = other.yPos;
+ int y2 = other.yPos + other.Sprite.GetLength(0);
+ return (x1 <= xPos && xPos < x2 && y1 <= yPos && yPos <= y2);
+ }
+
public void ScaleSprite() {
// This is called after the Update method, and scales the sprite if it has changed
if (RenderedSprite == Sprite) { return; }
diff --git a/Space Invaders/GameObjects.cs b/Space Invaders/GameObjects.cs
index f7297a1..9f1db1d 100644
--- a/Space Invaders/GameObjects.cs
+++ b/Space Invaders/GameObjects.cs
@@ -5,6 +5,16 @@ namespace SpaceInvaders {
class GameLogic : GameObject {
+ public override void Start() {
+ for (int i = 0; i < 12; i++) {
+ GameObject monster = new Monster();
+ monster.Parent = Parent;
+ monster.xPos = i * 35 + 90;
+ monster.yPos = 20;
+ Parent.GameObjects.Add(monster);
+ }
+ }
+
public override void Update() {
if (Input.KeyPressed(ConsoleKey.Escape)) {
@@ -14,9 +24,9 @@ namespace SpaceInvaders {
}
}
- class Bullet : GameObject {
+ class PlayerBullet : GameObject {
- int moveTimer = 10;
+ public int MoveTimer = 10;
public override void Start() {
Tag = "Projectile";
@@ -26,14 +36,14 @@ namespace SpaceInvaders {
public override void Update() {
- moveTimer--;
- if (moveTimer <= 0) {
+ MoveTimer--;
+ if (MoveTimer <= 0) {
yPos = yPos - 1;
- moveTimer = 10;
+ MoveTimer = 10;
}
- if (yPos <= 0) {
+ if (yPos <= 0 || yPos >= Parent.ConsoleHeight-1) {
isDead = true;
}
@@ -44,22 +54,70 @@ namespace SpaceInvaders {
GameObject other = Parent.GameObjects[i];
if (other.Tag == "Enemy") {
- int x1 = other.xPos;
- int x2 = other.xPos + other.Sprite.GetLength(1);
- int y1 = other.yPos;
- int y2 = other.yPos + other.Sprite.GetLength(0);
- if (x1 <= xPos && xPos <= x2 && y1 <= yPos && yPos <= y2) {
+ if (CollidingWith(other)) {
other.isDead = true;
isDead = true;
}
}
if (other.Tag == "Obstacle") {
- int x1 = other.xPos;
- int x2 = other.xPos + other.Sprite.GetLength(1);
- int y1 = other.yPos;
- int y2 = other.yPos + other.Sprite.GetLength(0);
- if (x1 <= xPos && xPos <= x2 && y1 <= yPos && yPos <= y2) {
+ if (CollidingWith(other)) {
+ Obstacle obstacle = (Obstacle)other;
+ obstacle.HP = obstacle.HP - 1;
+ isDead = true;
+ }
+ }
+ }
+ }
+
+ }
+
+ class MonsterBullet : GameObject {
+
+ public int MoveTimer = 10;
+
+ public override void Start() {
+ Tag = "Projectile";
+ Scale = 1;
+ Sprite = new char[1, 1] { { '\u2588' } };
+ }
+
+ public override void Update() {
+
+ MoveTimer--;
+ if (MoveTimer <= 0) {
+ yPos = yPos + 1;
+ MoveTimer = 10;
+ }
+
+
+ if (yPos <= 0 || yPos >= Parent.ConsoleHeight - 1) {
+ isDead = true;
+ }
+
+ }
+
+ public override void LateUpdate(Frame thisFrame) {
+ for (int i = 0; i < Parent.GameObjects.Count; i++) {
+ GameObject other = Parent.GameObjects[i];
+
+ if (other.Tag == "Enemy") {
+ if (CollidingWith(other)) {
+ isDead = true;
+ }
+ }
+
+ if (other.Tag == "Player") {
+ if (CollidingWith(other)) {
+ other.isDead = true;
+ isDead = false;
+ }
+ }
+
+ if (other.Tag == "Obstacle") {
+ if (CollidingWith(other)) {
+ Obstacle obstacle = (Obstacle)other;
+ obstacle.HP = obstacle.HP - 1;
isDead = true;
}
}
@@ -92,7 +150,7 @@ namespace SpaceInvaders {
}
if (Input.KeyPressed(ConsoleKey.Spacebar)) {
- GameObject tmpBullet = new Bullet();
+ GameObject tmpBullet = new PlayerBullet();
tmpBullet.Parent = Parent;
tmpBullet.xPos = xPos + 8;
tmpBullet.yPos = yPos - 1;
@@ -105,27 +163,60 @@ namespace SpaceInvaders {
}
class Obstacle : GameObject {
+
+
+ public int HP = 4;
+ private int lastHP = 4;
public override void Start() {
Tag = "Obstacle";
Scale = 4;
- Sprite = new char[2, 5] {
- {'\u2588', '\u2588', '\u2588', '\u2588', '\u2588'},
- {'\u2588', '\0', '\0', '\0', '\u2588'}
- };
+ Sprite = new char[1, 1] { { '\u2588' } };
+ }
+
+ public override void Update() {
+
+ if (lastHP != HP) {
+ lastHP = HP;
+
+ if (HP == 4) {
+ Sprite = new char[1, 1] { { '\u2588' } };
+ }
+ if (HP == 3) {
+ Sprite = new char[1, 1] { { '\u2593' } };
+ }
+ if (HP == 2) {
+ Sprite = new char[1, 1] { { '\u2592' } };
+ }
+ if (HP == 1) {
+ Sprite = new char[1, 1] { { '\u2591' } };
+ }
+ if (HP <= 0) {
+ isDead = true;
+ }
+
+ }
+
}
}
class Monster : GameObject {
+ private int _shootTimer = 400000;
+ private int _moveTimer = 400;
+
+ private bool _movingRight = true;
+ private int _UBound = 20;
+ private int _currStep = 10;
+
public override void Start() {
Tag = "Enemy";
- xPos = 10;
- yPos = 10;
Scale = 1;
+ _shootTimer = Parent.Rand.Next(1500, 3500);
+
Sprite = new char[8, 11] {
{'\0', '\0', '\u2588', '\0', '\0', '\0', '\0', '\0', '\u2588', '\0', '\0'},
{'\0', '\0', '\0', '\u2588', '\0', '\0', '\0', '\u2588', '\0', '\0', '\0'},
@@ -140,6 +231,42 @@ namespace SpaceInvaders {
}
public override void Update() {
+
+ if (_moveTimer <= 0) {
+ _moveTimer = 400;
+ if (_currStep >= _UBound) {
+ _currStep = 0;
+ _movingRight = !_movingRight;
+
+ if (_movingRight) {
+ yPos = yPos + 8;
+ }
+ }
+
+ if (_movingRight) {
+ xPos = xPos + 5;
+ }
+ else {
+ xPos = xPos - 5;
+ }
+
+ _currStep++;
+ }
+ _moveTimer--;
+
+ }
+
+ public override void LateUpdate(Frame thisFrame) {
+ if (_shootTimer <= 0) {
+ _shootTimer = Parent.Rand.Next(1500, 3500); ;
+ MonsterBullet bullet = new MonsterBullet();
+ bullet.Parent = Parent;
+ bullet.xPos = xPos + 10;
+ bullet.yPos = yPos + Sprite.GetLength(0) + 1;
+ bullet.Start();
+ Parent.GameObjects.Add(bullet);
+ }
+ _shootTimer--;
}
}
diff --git a/Space Invaders/Program.cs b/Space Invaders/Program.cs
index bf9751d..a005c56 100644
--- a/Space Invaders/Program.cs
+++ b/Space Invaders/Program.cs
@@ -37,19 +37,44 @@ namespace SpaceInvaders {
player.yPos = 115;
player.Scale = 1;
GameObjects.Add(player);
-
- // Init a monster
- GameObject monster1 = new Monster();
- GameObjects.Add(monster1);
// Init Obstacles
-
for (int i = 0; i < 6; i++) {
GameObject obstacle = new Obstacle();
- player.Parent = this;
+ obstacle.Parent = this;
obstacle.xPos = 50 + (i * 90);
obstacle.yPos = 90;
GameObjects.Add(obstacle);
+ obstacle = new Obstacle();
+ obstacle.Parent = this;
+ obstacle.xPos = 50 + (i * 90) + 8;
+ obstacle.yPos = 90;
+ GameObjects.Add(obstacle);
+ obstacle = new Obstacle();
+ obstacle.Parent = this;
+ obstacle.xPos = 50 + (i * 90) + 16;
+ obstacle.yPos = 90;
+ GameObjects.Add(obstacle);
+ obstacle = new Obstacle();
+ obstacle.Parent = this;
+ obstacle.xPos = 50 + (i * 90) + 24;
+ obstacle.yPos = 90;
+ GameObjects.Add(obstacle);
+ obstacle = new Obstacle();
+ obstacle.Parent = this;
+ obstacle.xPos = 50 + (i * 90) + 32;
+ obstacle.yPos = 90;
+ GameObjects.Add(obstacle);
+ obstacle = new Obstacle();
+ obstacle.Parent = this;
+ obstacle.xPos = 50 + (i * 90);
+ obstacle.yPos = 90 + 4;
+ GameObjects.Add(obstacle);
+ obstacle = new Obstacle();
+ obstacle.Parent = this;
+ obstacle.xPos = 50 + (i * 90) + 32;
+ obstacle.yPos = 90 + 4;
+ GameObjects.Add(obstacle);
}
base.Start(); // Do start from inherited class, Required for the engine to actually start