as the title says im currently working on a Console Game in c#. The problem is, i fail to make a working gameloop everytime... Also I have a Problem with "spawning" multiple objects like in this case bullets or enemys (Im still new to c# and also stakoverflow ;P)
I hope u can help me getting it working!! :)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace Try
{
class Program
{
public static bool gameloop = true;
public static int counter = 3;
public static Timer travel = new Timer();
public static string playermodel = GetConfig();
static void Main(string[] args)
{
GameLoop();
Console.Read();
}
private static string GetConfig()
{
string player = "";
while (player.Length != 7)
{
Console.Write("Enter 7 chars (in 1 line) for your Playermodell:");
player = Console.ReadLine();
}
return player;
}
private static void GameLoop()
{
Player player = new Player();
travel.Interval = 1;
travel.Elapsed += OnTimedEventTravel;
player.SetPlayer(Program.playermodel);
while (gameloop == true)
{
travel.Start();
}
}
private static void OnTimedEventTravel(Object source, System.Timers.ElapsedEventArgs e)
{
Player player = new Player();
Bullet bullet = new Bullet();
Enemy enemy = new Enemy();
PowerUP powerUP = new PowerUP();
player.Move();
player.Shoot();
if (Bullet.isFlying == true)
{
bullet.Travle();
}
}
}
class Player
{
public static int PlayerPosX = 0;
public static int PlayerPosY = 0;
public static int PlayerSize = 0;
public Player()
{
PlayerSize = Program.playermodel.Length;
}
public void SetPlayer(string player)
{
Console.SetCursorPosition(Console.WindowWidth / 2 - PlayerSize / 2, Console.WindowHeight - 3);
PlayerPosX = Console.WindowWidth / 2 - PlayerSize / 2;
PlayerPosY = Console.WindowHeight - 3;
Console.WriteLine(player);
}
public virtual void Move()
{
ConsoleKeyInfo key;
if (Console.KeyAvailable)
{
key = Console.ReadKey();
if (key.Key == ConsoleKey.LeftArrow)
{
if (PlayerPosX > Console.WindowLeft)
{
Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX - 1, PlayerPosY);
PlayerPosX -= 1;
}
}
if (key.Key == ConsoleKey.RightArrow)
{
if (PlayerPosX + PlayerSize < Console.WindowWidth)
{
Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX + 1, PlayerPosY);
PlayerPosX += 1;
}
}
if (key.Key == ConsoleKey.UpArrow)
{
if (PlayerPosY > Console.WindowTop)
{
Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX, PlayerPosY - 1);
PlayerPosY -= 1;
}
}
if (key.Key == ConsoleKey.DownArrow)
{
if (PlayerPosY < Console.WindowHeight)
{
Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX, PlayerPosY + 1);
PlayerPosY += 1;
}
}
}
}
public virtual void Shoot()
{
Bullet bullet = new Bullet();
ConsoleKeyInfo key;
key = Console.ReadKey();
if (key.Key == ConsoleKey.Spacebar)
{
Bullet.Spawn();
}
}
public virtual void Collide()
{
}
}
class Bullet
{
private static int SpawnPosX;
private static int SpawnPosY;
public static int CurrentPosX;
public static int CurrentPosY;
public static bool isFlying = false;
public static int speed = 1;
public static void Spawn()
{
isFlying = true;
SpawnPosX = Player.PlayerPosX + 3;
SpawnPosY = Player.PlayerPosY - 1;
Console.SetCursorPosition(SpawnPosX, SpawnPosY);
CurrentPosX = SpawnPosX;
CurrentPosY = SpawnPosY;
Console.WriteLine("'");
}
public void Travle()
{
if (Bullet.CurrentPosX > Console.WindowTop)
{
Console.MoveBufferArea(Bullet.CurrentPosX, Bullet.CurrentPosY, 1, 1, Bullet.CurrentPosX, Bullet.CurrentPosY - 1);
Bullet.CurrentPosY -= 1;
}
else
{
//TODO Delete bullet
Bullet.isFlying = false;
}
}
private static void Hit()
{
}
}
class Enemy
{
public int EnemyPosX;
public int EnemyPosY;
public Enemy()
{
}
public static void Spawn()
{
}
public void Move()
{
}
public void Shoot()
{
}
public void Collide()
{
}
}
class PowerUP
{
public int random;
public string WhichUP()
{
Random rnd = new Random();
string whichUP = "";
random = rnd.Next(0, 4);
switch (random)
{
case 0:
whichUP = ":";
break;
case 1:
whichUP = "O";
break;
case 2:
whichUP = "^";
break;
case 3:
whichUP = "~";
break;
}
return whichUP;
}
}
}