Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Inkludera nödvändiga bibliotek
- #include <graphics.h>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- // Initiera slumptalsgeneratorn
- void randomize() {
- srand(time(NULL));
- for (int i = 0; i < 100; i++) {
- rand();
- }
- }
- // Några hjälpfunktioner för slumptal
- double rnd() {
- double N = RAND_MAX + 1.0;
- return (rand() + (rand() + rand()/N)/N)/N;
- }
- double rnd(double max) {
- return rnd()*max;
- }
- double rnd(double min, double max) {
- return min + rnd()*(max-min);
- }
- int nextInt(int size) {
- return (int)(rnd(size));
- }
- int nextColor() {
- int r, g, b;
- r = nextInt(256);
- g = nextInt(256);
- b = nextInt(256);
- return RGB(r, g, b);
- }
- int nextBrightColor() {
- int color, x = nextInt(256);
- switch (nextInt(6)) {
- case 0: color = RGB( x, 0, 255); break;
- case 1: color = RGB( x, 255, 0); break;
- case 2: color = RGB( 0, x, 255); break;
- case 3: color = RGB(255, x, 0); break;
- case 4: color = RGB(0, 255, x); break;
- case 5: color = RGB(255, 0, x); break;
- }
- return color;
- }
- class Cirkel {
- public:
- void init();
- void move();
- void draw();
- void update();
- private:
- float x, y;
- float r;
- float vx, vy;
- int borderColor, fillColor;
- };
- void Cirkel::init() {
- x = 400;
- y = 300;
- r = rnd(10.0, 30.0);
- vx = rnd(-3.0, 3.0);
- vy = rnd(-3.0, 3.0);
- borderColor = nextBrightColor();
- fillColor = nextBrightColor();
- }
- void Cirkel::move() {
- // Studsa?
- if ((x + vx - r < 0) || (x + vx + r > getmaxx())) {
- vx = -vx;
- }
- if ((y + vy - r < 0) || (y + vy + r > getmaxy())) {
- vy = -vy;
- }
- // Flytta!
- x += vx;
- y += vy;
- }
- void Cirkel::draw() {
- setcolor(borderColor);
- setfillstyle(SOLID_FILL, fillColor);
- fillellipse(x, y, r, r);
- circle(x, y, r);
- }
- void Cirkel::update() {
- move();
- draw();
- }
- // Huvudfunktion
- int main() {
- // Initiera slumptalsgeneratorn
- randomize;
- // Skapa ett grafikfönster, dubbelbuffrat
- initwindow(800, 600, "Cirklar_v4", 0, 0, true);
- const int ANTAL = 50;
- Cirkel c[ANTAL];
- for (int i = 0; i < ANTAL; i++) {
- c[i].init();
- }
- // Rita lite
- while (!kbhit()) {
- // Töm fösntret
- cleardevice();
- // Flytta och rita om alla cirklar
- for (int i = 0; i < ANTAL; i++) {
- c[i].move();
- c[i].draw();
- }
- // Swap!
- swapbuffers();
- // Delay
- delay(10);
- }
- // Läs bort tecknet vi avbröt loopen med
- getch();
- // Stäng ner grafiken
- closegraph();
- // Klart, avsluta
- return 0;
- }
Add Comment
Please, Sign In to add comment