aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Stendahl <jakob.stendahl@outlook.com>2017-11-12 21:35:43 +0100
committerJakob Stendahl <jakob.stendahl@outlook.com>2017-11-12 21:35:43 +0100
commiteff4413449dc9dfe365ded241c524fe0bab104c2 (patch)
tree58e7514bfa2aaf5cbbb024ca115940fc44dacbb2
parent76d19004e770c360feecd7a0f5c9b1c875d44bf1 (diff)
downloadSpace-Invaders-CS-Console-eff4413449dc9dfe365ded241c524fe0bab104c2.tar.gz
Space-Invaders-CS-Console-eff4413449dc9dfe365ded241c524fe0bab104c2.zip
Game is now working in its primitive state
-rw-r--r--Space Invaders/GameEngine/GameEngine.cs4
-rw-r--r--Space Invaders/GameObjects.cs159
-rw-r--r--Space Invaders/Program.cs32
3 files changed, 179 insertions, 16 deletions
diff --git a/Space Invaders/GameEngine/GameEngine.cs b/Space Invaders/GameEngine/GameEngine.cs
index bba5838..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;
@@ -139,7 +141,7 @@ namespace GameEngine {
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);
+ return (x1 <= xPos && xPos < x2 && y1 <= yPos && yPos <= y2);
}
public void ScaleSprite() {
diff --git a/Space Invaders/GameObjects.cs b/Space Invaders/GameObjects.cs
index 26533d6..9f1db1d 100644
--- a/Space Invaders/GameObjects.cs
+++ b/Space Invaders/GameObjects.cs
@@ -6,7 +6,13 @@ 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() {
@@ -18,9 +24,9 @@ namespace SpaceInvaders {
}
}
- class Bullet : GameObject {
+ class PlayerBullet : GameObject {
- int moveTimer = 10;
+ public int MoveTimer = 10;
public override void Start() {
Tag = "Projectile";
@@ -30,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;
}
@@ -56,6 +62,62 @@ namespace SpaceInvaders {
if (other.Tag == "Obstacle") {
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;
}
}
@@ -88,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;
@@ -101,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'},
@@ -136,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 769bc0c..a005c56 100644
--- a/Space Invaders/Program.cs
+++ b/Space Invaders/Program.cs
@@ -41,10 +41,40 @@ namespace SpaceInvaders {
// 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