wzee1

pattogolabda.cpp

Apr 25th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <chrono>
  5. #include <thread>
  6.  
  7. using namespace std;
  8.  
  9. const int WIDTH = 80;  // Console szélessége
  10. const int HEIGHT = 20; // Console magassága
  11.  
  12. void clearScreen()
  13. {
  14.   // Törlés azzal, hogy minden sorban kiírunk WIDTH darab space karaktert
  15.   for (int i = 0; i < HEIGHT; ++i)
  16.   {
  17.     for (int j = 0; j < WIDTH; ++j)
  18.     {
  19.       cout << " ";
  20.     }
  21.     cout << endl;
  22.   }
  23. }
  24.  
  25. void drawBall(int x, int y)
  26. {
  27.   cout << "\033[" << y << ";" << x << "Ho"; // Labda kirajzolása adott pozícióban
  28. }
  29.  
  30. int main()
  31. {
  32.   srand(time(NULL)); // Véletlenszám generátor inicializálása
  33.  
  34.   int x = WIDTH / 2;  // Kezdeti x pozíció
  35.   int y = HEIGHT / 2; // Kezdeti y pozíció
  36.  
  37.   int dx = 1; // x irányú sebesség
  38.   int dy = 1; // y irányú sebesség
  39.  
  40.   while (true)
  41.   {
  42.     clearScreen(); // Console törlése
  43.  
  44.     // Labda pozíciójának frissítése
  45.     x += dx;
  46.     y += dy;
  47.  
  48.     // Fal ütközés ellenőrzése és irány megfordítása
  49.     if (x <= 0 || x >= WIDTH)
  50.     {
  51.       dx *= -1;
  52.     }
  53.     if (y <= 0 || y >= HEIGHT)
  54.     {
  55.       dy *= -1;
  56.     }
  57.  
  58.     drawBall(x, y); // Labda kirajzolása az új pozícióban
  59.  
  60.     // Késleltetés
  61.     this_thread::sleep_for(chrono::milliseconds(100));
  62.   }
  63.  
  64.   return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment