Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <chrono>
- #include <thread>
- using namespace std;
- const int WIDTH = 80; // Console szélessége
- const int HEIGHT = 20; // Console magassága
- void clearScreen()
- {
- // Törlés azzal, hogy minden sorban kiírunk WIDTH darab space karaktert
- for (int i = 0; i < HEIGHT; ++i)
- {
- for (int j = 0; j < WIDTH; ++j)
- {
- cout << " ";
- }
- cout << endl;
- }
- }
- void drawBall(int x, int y)
- {
- cout << "\033[" << y << ";" << x << "Ho"; // Labda kirajzolása adott pozícióban
- }
- int main()
- {
- srand(time(NULL)); // Véletlenszám generátor inicializálása
- int x = WIDTH / 2; // Kezdeti x pozíció
- int y = HEIGHT / 2; // Kezdeti y pozíció
- int dx = 1; // x irányú sebesség
- int dy = 1; // y irányú sebesség
- while (true)
- {
- clearScreen(); // Console törlése
- // Labda pozíciójának frissítése
- x += dx;
- y += dy;
- // Fal ütközés ellenőrzése és irány megfordítása
- if (x <= 0 || x >= WIDTH)
- {
- dx *= -1;
- }
- if (y <= 0 || y >= HEIGHT)
- {
- dy *= -1;
- }
- drawBall(x, y); // Labda kirajzolása az új pozícióban
- // Késleltetés
- this_thread::sleep_for(chrono::milliseconds(100));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment