Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace ConsoleApp1
- {
- class Entite
- {
- //les propriétés
- const int TX = 80, TY = 22;
- Random Rnd = new Random();
- float x, y, dx, dy;
- int color;
- int lettre;
- //constructeurs (fonctions d'initialisation + allocation)
- public Entite()
- {
- Init_Alea();
- }
- //méthodes (fonctions membres de la classe)
- public void Init_Alea()
- {
- x = Rnd.Next(TX);
- y = Rnd.Next(TY);
- dx = (float)Rnd.NextDouble() * 4 - 2;
- dy = (float)Rnd.NextDouble() * 4 - 2;
- color = Rnd.Next(15) + 1;
- lettre = 'A' + Rnd.Next(26);
- }
- public void Move()
- {
- x += dx;
- y += dy;
- // contrôle des bords
- if (x < 0)
- {
- x = 0;
- dx = (float)Rnd.NextDouble() * 2;
- }
- if (x >= TX)
- {
- x = TX - 1;
- dx = (float)Rnd.NextDouble() * -2;
- }
- if (y < 0)
- {
- y = 0;
- dy = (float)Rnd.NextDouble() * 2;
- }
- if (y >= TY)
- {
- y = TY - 1;
- dy = (float)Rnd.NextDouble() * -2;
- }
- }
- private void Affiche(int color)
- {
- ConsoleColor c = (ConsoleColor)color;
- Console.SetCursorPosition((int)x, (int)y);
- Console.ForegroundColor = c;
- Console.Write(Convert.ToChar(lettre)); //pour avoir la lettre
- }
- //surcharge
- public void Affiche()
- {
- Affiche(color);
- }
- public void Effacer()
- {
- Affiche(0);
- }
- public void Run()
- {
- Effacer();
- Move();
- Affiche();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Entite e = new Entite(); //constructeur par defaut
- e.Init_Alea();
- while(true)
- {
- e.Run();
- Thread.Sleep(50);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement