Untitled

 avatar
unknown
c_cpp
a year ago
4.1 kB
3
Indexable
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#define NUM_CLOUDS 4
#define NUM_BIRDS 5
 
void drawCloud(int x, int y);
void drawRiver(int y);
void drawBird(int x, int y);
void drawSun(int x, int y, int radius);
void drawTrees(int river_y);
 
int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");
    int max_width = getmaxx();
    int max_height = getmaxy();
    // sky color
    setbkcolor(LIGHTBLUE);
    cleardevice();
    // clouds
    srand(time(NULL));
    int cloud_x[NUM_CLOUDS];
    int cloud_y[NUM_CLOUDS];
    for (int i = 0; i < NUM_CLOUDS; i++) {
        cloud_x[i] = rand() % max_width;
        cloud_y[i] = rand() % (max_height / 3);
    }
    // river
    int river_y = max_height * 2 / 3;
    drawRiver(river_y);
    // sun
    int sun_radius = 40; // Adjust sun size
    int sun_x = max_width - sun_radius - 25; // Position at right corner with padding
    int sun_y = sun_radius + 10; // Position at top corner with padding
    drawSun(sun_x, sun_y, sun_radius);
    // birds
    int bird_x[NUM_BIRDS];
    int bird_y[NUM_BIRDS];
    for (int i = 0; i < NUM_BIRDS; i++) {
        bird_x[i] = rand() % max_width;
        bird_y[i] = rand() % (max_height / 2);
    }
    // Animation loop
    int boat_x = max_width + 100; // Initial position of the boat outside the screen
    int boat_y = river_y + 50;     // Adjust the boat's height on the river
    while (!kbhit()) {
        // Clear previous frame
        cleardevice();
        // sky
        setbkcolor(LIGHTBLUE);
        cleardevice();
        // clouds
        for (int i = 0; i < NUM_CLOUDS; i++) {
            drawCloud(cloud_x[i], cloud_y[i]);
            cloud_x[i] += 1;
 
            if (cloud_x[i] > max_width) {
                cloud_x[i] = 0;
                cloud_y[i] = rand() % (max_height / 3);
            }
        }
        drawRiver(river_y);
        drawSun(sun_x, sun_y, sun_radius);
        for (int i = 0; i < NUM_BIRDS; i++) {
            drawBird(bird_x[i], bird_y[i]);
            bird_x[i] += 2;
 
            if (bird_x[i] > max_width) {
                bird_x[i] = 0;
                bird_y[i] = rand() % (max_height / 3);
            }
        }
        drawTrees(river_y);
        delay(30);
    }
    getch();
    closegraph();
    return 0;
}
 
void drawCloud(int x, int y) {
    setcolor(WHITE);
    setfillstyle(SOLID_FILL, WHITE);
    ellipse(x, y, 0, 360, 40, 20);
    floodfill(x, y, WHITE);
    ellipse(x + 20, y + 10, 0, 360, 40, 20);
    floodfill(x + 20, y + 10, WHITE);
    ellipse(x + 60, y, 0, 360, 40, 20);
    floodfill(x + 60, y, WHITE);
    ellipse(x + 80, y + 10, 0, 360, 40, 20);
    floodfill(x + 80, y + 10, WHITE);
    ellipse(x + 100, y, 0, 360, 40, 20);
    floodfill(x + 100, y, WHITE);
}
 
void drawRiver(int y) {
    setcolor(BLUE);
    rectangle(0, y, getmaxx(), getmaxy());
    setfillstyle(SOLID_FILL, BLUE);
    floodfill(1, y + 1, BLUE);
 
    int river_width = getmaxx();
    int num_lines = 20;
    for (int i = 0; i < num_lines; i++) {
        int line_y = y + rand() % (y / 3);
        line(0, line_y, river_width, line_y);
    }
}
 
void drawBird(int x, int y) {
  setcolor(BLACK);
  // body
  setfillstyle(SOLID_FILL, BLACK);
  line(x - 5, y, x, y + 5);
  line(x - 10, y, x + 5, y - 5);
  line(x, y + 5, x + 5, y - 5);
  floodfill(x - 2, y + 2, BLACK);
  // head
  setfillstyle(SOLID_FILL, BLACK);
  line(x + 3, y, x + 8, y - 2);
  line(x + 5, y, x + 8, y + 3);
  line(x + 7, y - 3, x + 8, y + 3);
  floodfill(x + 6, y, BLACK);
}
 
void drawSun(int x, int y, int radius) {
    setcolor(RGB(255, 165, 0));
    setfillstyle(SOLID_FILL, RGB(255, 165, 0));
    circle(x, y, radius);
    floodfill(x, y, RGB(255, 165, 0));
}
 
void drawTrees(int river_y) {
  setcolor(GREEN);
  setfillstyle(SOLID_FILL, GREEN);
  int leaves_base_y = river_y - 4;
  for (int i = 0; i < 8; i++) {
    int leaves_x = i * 100;
    for (int j = 0; j < 4; j++) {
      int circle_y = leaves_base_y;
      int circle_x = leaves_x + j * 20 + rand() % 6;
      circle(circle_x, circle_y, 10);
      floodfill(circle_x, circle_y, GREEN);
    }
  }
}
Editor is loading...
Leave a Comment