Za zgodą administratora zamieszczam pytanie z C#, być może ktoś wie, jak to poprawić:
Problem polega na tym, że klasa ruch dziedziczy po klasie mob_01, bo mogę wywołać metody, ale nie mogę pobrać wartości pola pozycja_mobx. Pole hp pobieram prawidłowo, a pozycja_mobx w wyniku podaje 0, zamiast właściwej liczby.
Czy problem może być związany z tym, że dane do zmiennej pozycja_mobx są generowane losowo?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Galaxy
{
class Program
{
static void Main(string[] args)
{
ruch postac = new ruch();
postac.PozycjaStartowa();
mob_01 potworek=new mob_01();
while (postac.koniec != 1)
{
potworek.WyswietlMob_01();
postac.Ruch();
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Galaxy
{
class mob_01
{
int hp=10;
int znacznik;
int pozycja_mobx;
int pozycja_moby;
void LosujPozycje()
{
Random random = new Random();
pozycja_mobx = random.Next(10, 20);
pozycja_moby = random.Next(10, 20);
znacznik=1;
}
public void WyswietlMob_01()
{
if (znacznik==0)
LosujPozycje();
Console.SetCursorPosition(pozycja_mobx, pozycja_moby);
Console.WriteLine("oOo");
}
public void przekaz_dane()
{
Console.WriteLine(pozycja_mobx);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Galaxy
{
class ruch:mob_01
{
int pozycja_x=40;
int pozycja_y=22;
int poz_strzal_x;
int poz_strzal_y;
public int koniec=0;
public void PozycjaStartowa()
{
Console.Clear();
Console.SetCursorPosition(40, 22);
Console.WriteLine("_^_");
}
public void Ruch()
{
ConsoleKeyInfo Klawisz = Console.ReadKey(true);
if (Klawisz.Key == ConsoleKey.LeftArrow)
{
if (pozycja_x > 0)
pozycja_x -= 1;
else
pozycja_x = 0;
}
else if (Klawisz.Key == ConsoleKey.RightArrow)
{
if (pozycja_x < 77)
pozycja_x += 1;
else
pozycja_x = 77;
}
else if (Klawisz.Key == ConsoleKey.UpArrow)
{
Strzelaj();
}
if (Klawisz.Key == ConsoleKey.Q)
{
koniec = 1;
System.Environment.Exit(1);
}
Console.Clear();
Console.SetCursorPosition(pozycja_x, pozycja_y);
Console.WriteLine("_^_");
}
public void Strzelaj()
{
poz_strzal_x = pozycja_x+1;
poz_strzal_y = pozycja_y-1;
while (poz_strzal_y > 0 )
{
Console.SetCursorPosition(poz_strzal_x, poz_strzal_y);
Console.WriteLine("|");
przekaz_dane();
System.Threading.Thread.Sleep(25);
poz_strzal_y--;
}
}
}
}