Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace entite_CS_1
  9. {
  10.     public class Entite
  11.     {
  12.         // Propriétés (données membres)
  13.         const int TX = 80;
  14.         const int TY = 24;
  15.         Random rnd = new Random(); // chiffre aléatoire
  16.  
  17.         float x, y, dx, dy;
  18.         int color;
  19.         int lettre;
  20.         // Constructeurs (fonctions d'initialisation / allocation)
  21.         public Entite()
  22.         {
  23.             Init_alea();
  24.         }
  25.         // Methodes (fonctions membres)
  26.         public void Init_alea()
  27.         {
  28.             x = rnd.Next(TX);
  29.             y = rnd.Next(TY);
  30.             dx = (float)rnd.NextDouble() * 4 - 2;
  31.             dy = (float)rnd.NextDouble() * 4;
  32.             color = rnd.Next(15) + 1;
  33.             lettre = 'A' + rnd.Next(26);
  34.  
  35.         }
  36.         public void Move()
  37.         {
  38.             x += dx;
  39.             y += dy;
  40.  
  41.             // contrôle des bords
  42.             if (x < 0)
  43.             {
  44.                 x = 0;
  45.                 dx = (float)rnd.NextDouble() * 2;
  46.             }
  47.             if (x >= TX)
  48.             {
  49.                 x = TX - 1;
  50.                 dx = (float)rnd.NextDouble() * -2;
  51.             }
  52.  
  53.             if (y < 0)
  54.             {
  55.                 y = 0;
  56.                 dy = (float)rnd.NextDouble() * 2;
  57.             }
  58.             if (y >= TY)
  59.             {
  60.                 y = TY - 1;
  61.                 dy = (float)rnd.NextDouble() * -2;
  62.             }
  63.         }
  64.         private void Affiche(int color) // C'est une autre variable, il ne s'agit pas de celle au dessus
  65.         {
  66.             ConsoleColor c = (ConsoleColor)color;
  67.             Console.SetCursorPosition((int)x, (int)y);
  68.             Console.ForegroundColor = c;
  69.             Console.Write(Convert.ToChar(lettre)); // Pour avoir la lettre
  70.  
  71.             /*
  72.             // Ces deux là affichent le code ASCII et pas la lettre correspondante
  73.             Console.Write(lettre);
  74.             Console.Write(lettre.ToString());
  75.             */
  76.         }
  77.         public void Affiche() // Ceci est une surchage (même nom mais pas les mêmes paramètres)
  78.         {
  79.             Affiche(color); // Par contre, celui là, appelle celui tout en haut
  80.         }
  81.         public void Efface()
  82.         {
  83.             Affiche(0); //Il affichera du noir
  84.         }
  85.  
  86.         public void run()
  87.         {
  88.             Efface();
  89.             Move();
  90.             Affiche();
  91.         }
  92.         static void Main()
  93.         {
  94.             Entite e = new Entite(); // Constructeur par défaut
  95.  
  96.             while (true)
  97.             {
  98.                 e.run();
  99.  
  100.                 Thread.Sleep(150);
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement