Plik program:
using System;
using System.Collections.Generic;
using System.Text;
namespace Obiekty
{
class Program
{
static void Main(string[] args)
{
UFO kosmita;
ConsoleKeyInfo guzik = new ConsoleKeyInfo();
kosmita = new UFO();
kosmita.x = 20;
kosmita.y = 0;
while (guzik.Key != ConsoleKey.Escape)
{
System.Threading.Thread.Sleep(10);
kosmita.Rysuj();
if (Console.KeyAvailable)
{
guzik = Console.ReadKey();
}
else { guzik = new ConsoleKeyInfo(); }
switch (guzik.Key)
{
case ConsoleKey.RightArrow:
kosmita.LecPrawo();
break;
case ConsoleKey.LeftArrow:
kosmita.LecLewo();
break;
case ConsoleKey.UpArrow:
kosmita.LecGora();
break;
case ConsoleKey.DownArrow:
kosmita.LecDol();
break;
case ConsoleKey.T:
kosmita.Tankowanie(10);
break;
}
}
}
}
}[/code]
Plik ufo:
[code=auto:0]using System;
using System.Collections.Generic;
using System.Text;
namespace Obiekty
{
class UFO
{
private double paliwo;
public int x, y;
private void Info()
{
Console.CursorLeft = 0;
Console.CursorTop = 0;
Console.WriteLine("Paliwo:{0}, czas {1}", paliwo, DateTime.Now.ToLongTimeString());
if (paliwo==0)
{
Console.WriteLine("Worning! Nie masz paliwa musisz zatankowac inaczej");
Console.WriteLine("wciagnie cie czarna dziura i przepadniesz w nicosci");
}
}
public void Rysuj()
{
Console.Clear();
Console.CursorLeft = x;
Console.CursorTop = Console.WindowHeight - y;
Console.WriteLine("d(*.*)b");
Console.CursorVisible = false;
Info();
}
public void LecLewo()
{
if (paliwo > 0)
{
if (x > 0)
{
x -= 1;
Spalaj();
}
else
{
Console.Beep(80, 25);
}
}
}
public void LecPrawo()
{
if (paliwo > 0)
{
if (x < Console.WindowWidth - 1)
{
x += 1;
Spalaj();
}
else
{
Console.Beep(80, 25);
}
}
}
public void LecGora()
{
if (paliwo > 0)
{
if (y < Console.WindowHeight - 1)
{
y += 1;
Spalaj();
}
else
{
Console.Beep(80, 25);
}
}
}
public void LecDol()
{
if (paliwo > 0)
{
if (y > 0)
{
y -= 1;
Spalaj();
}
else
{
Console.Beep(80, 25);
}
}
}
public void Tankowanie(double ileLitrów)
{
if (paliwo+ileLitrów <= 100)
{
paliwo += ileLitrów;
}
}
private void Spalaj()
{
if (paliwo > 0)
{
paliwo--;
}
if (paliwo < 10)
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
else
{
Console.ForegroundColor = ConsoleColor.White;
}
if (paliwo == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
}
}
}
}