Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApp1
  5. {
  6. class Entite
  7. {
  8. //les propriétés
  9. const int TX = 80, TY = 22;
  10. Random Rnd = new Random();
  11.  
  12. float x, y, dx, dy;
  13. int color;
  14. int lettre;
  15. //constructeurs (fonctions d'initialisation + allocation)
  16. public Entite()
  17. {
  18. Init_Alea();
  19. }
  20. //méthodes (fonctions membres de la classe)
  21. public void Init_Alea()
  22. {
  23. x = Rnd.Next(TX);
  24. y = Rnd.Next(TY);
  25. dx = (float)Rnd.NextDouble() * 4 - 2;
  26. dy = (float)Rnd.NextDouble() * 4 - 2;
  27. color = Rnd.Next(15) + 1;
  28. lettre = 'A' + Rnd.Next(26);
  29. }
  30. public void Move()
  31. {
  32. x += dx;
  33. y += dy;
  34.  
  35. // contrôle des bords
  36. if (x < 0)
  37. {
  38. x = 0;
  39. dx = (float)Rnd.NextDouble() * 2;
  40. }
  41. if (x >= TX)
  42. {
  43. x = TX - 1;
  44. dx = (float)Rnd.NextDouble() * -2;
  45. }
  46.  
  47. if (y < 0)
  48. {
  49. y = 0;
  50. dy = (float)Rnd.NextDouble() * 2;
  51. }
  52. if (y >= TY)
  53. {
  54. y = TY - 1;
  55. dy = (float)Rnd.NextDouble() * -2;
  56. }
  57. }
  58. private void Affiche(int color)
  59. {
  60. ConsoleColor c = (ConsoleColor)color;
  61. Console.SetCursorPosition((int)x, (int)y);
  62. Console.ForegroundColor = c;
  63. Console.Write(Convert.ToChar(lettre)); //pour avoir la lettre
  64. }
  65. //surcharge
  66. public void Affiche()
  67. {
  68. Affiche(color);
  69. }
  70. public void Effacer()
  71. {
  72. Affiche(0);
  73. }
  74. public void Run()
  75. {
  76. Effacer();
  77. Move();
  78. Affiche();
  79. }
  80. }
  81. class Program
  82. {
  83. static void Main(string[] args)
  84. {
  85. Entite e = new Entite(); //constructeur par defaut
  86. e.Init_Alea();
  87. while(true)
  88. {
  89. e.Run();
  90.  
  91. Thread.Sleep(50);
  92. }
  93. Console.ReadKey();
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement