Guest User

Untitled

a guest
Nov 25th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. // Inkludera nödvändiga bibliotek
  2. #include <graphics.h>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. // Initiera slumptalsgeneratorn
  8. void randomize() {
  9.     srand(time(NULL));
  10.     for (int i = 0; i < 100; i++) {
  11.         rand();
  12.     }
  13. }
  14.  
  15. // Några hjälpfunktioner för slumptal
  16. double rnd() {
  17.     double N = RAND_MAX + 1.0;
  18.     return  (rand() + (rand() + rand()/N)/N)/N;
  19. }
  20.  
  21. double rnd(double max) {
  22.     return rnd()*max;
  23. }
  24.  
  25. double rnd(double min, double max) {
  26.     return min + rnd()*(max-min);
  27. }  
  28.  
  29. int nextInt(int size) {
  30.     return (int)(rnd(size));
  31. }
  32.  
  33. int nextColor() {
  34.     int r, g, b;
  35.     r = nextInt(256);
  36.     g = nextInt(256);
  37.     b = nextInt(256);
  38.     return RGB(r, g, b);       
  39. }
  40.  
  41. int nextBrightColor() {
  42.     int color, x = nextInt(256);   
  43.    
  44.     switch (nextInt(6)) {
  45.         case 0: color = RGB(  x, 0, 255); break;
  46.         case 1: color = RGB(  x, 255, 0); break;
  47.         case 2: color = RGB(  0, x, 255); break;
  48.         case 3: color = RGB(255, x,   0); break;
  49.         case 4: color = RGB(0,  255,  x); break;
  50.         case 5: color = RGB(255,  0,  x); break;
  51.     }
  52.    
  53.     return color;
  54. }
  55.  
  56. class Cirkel {
  57.     public:
  58.         void init();
  59.         void move();
  60.         void draw();
  61.         void update();
  62.     private:
  63.         float x, y;
  64.         float r;
  65.         float vx, vy;
  66.         int borderColor, fillColor;
  67. };
  68.  
  69. void Cirkel::init() {
  70.     x = 400;
  71.     y = 300;
  72.     r = rnd(10.0, 30.0);
  73.     vx = rnd(-3.0, 3.0);
  74.     vy = rnd(-3.0, 3.0);
  75.     borderColor = nextBrightColor();
  76.     fillColor   = nextBrightColor();
  77. }
  78.  
  79.  
  80. void Cirkel::move() {
  81.     // Studsa?
  82.     if ((x + vx - r < 0) || (x + vx + r > getmaxx())) {
  83.         vx = -vx;
  84.     }
  85.    
  86.     if ((y + vy - r < 0) || (y + vy + r > getmaxy())) {
  87.         vy = -vy;
  88.     }
  89.    
  90.     // Flytta!
  91.     x += vx;
  92.     y += vy;
  93. }
  94.  
  95. void Cirkel::draw() {
  96.     setcolor(borderColor);
  97.     setfillstyle(SOLID_FILL, fillColor);
  98.     fillellipse(x, y, r, r);
  99.     circle(x, y, r);
  100. }
  101.    
  102. void Cirkel::update() {
  103.     move();
  104.     draw();
  105.    
  106. }    
  107.    
  108.  
  109. // Huvudfunktion
  110. int main() {   
  111.     // Initiera slumptalsgeneratorn
  112.     randomize;
  113.  
  114.     // Skapa ett grafikfönster, dubbelbuffrat   
  115.     initwindow(800, 600, "Cirklar_v4", 0, 0, true);
  116.    
  117.     const int ANTAL = 50;
  118.     Cirkel c[ANTAL];
  119.     for (int i = 0; i < ANTAL; i++) {
  120.         c[i].init();
  121.     }
  122.    
  123.    
  124.     // Rita lite
  125.     while (!kbhit()) {
  126.         // Töm fösntret
  127.         cleardevice();
  128.    
  129.         //  Flytta och rita om alla cirklar
  130.         for (int i = 0; i < ANTAL; i++) {
  131.             c[i].move();
  132.             c[i].draw();       
  133.         }
  134.        
  135.         // Swap!
  136.         swapbuffers();   
  137.         // Delay
  138.         delay(10);
  139.     }
  140.     // Läs bort tecknet vi avbröt loopen med
  141.     getch();
  142.    
  143.     // Stäng ner grafiken
  144.     closegraph();
  145.    
  146.     // Klart, avsluta
  147.     return 0;
  148. }
Add Comment
Please, Sign In to add comment