Advertisement
Kaelygon

random walk ant

Oct 31st, 2023 (edited)
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL2/SDL.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6.  
  7.  
  8. int energy = 16;
  9. const int UPDATE_RATE = 0;
  10. const int SCREEN_WIDTH = 1024;
  11. const int SCREEN_HEIGHT = 1024;
  12. const int GRID_SIZE = 4;  // Size of each grid cell
  13. const int GRID_ROWS = SCREEN_HEIGHT / GRID_SIZE;
  14. const int GRID_COLS = SCREEN_WIDTH / GRID_SIZE;
  15.  
  16. int antPos[2] = {GRID_ROWS/2,GRID_COLS/2};
  17. int antCol[3] = {64,64,255};
  18. int gridCell[GRID_ROWS][GRID_COLS];
  19.  
  20. int antDir=0;
  21.  
  22. void renderWholeGrid(SDL_Renderer* renderer) {
  23.     // Clear the renderer
  24.     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  25.  
  26.     // Set a different color for each cell
  27.     for (int i = 0; i < GRID_ROWS; ++i) {
  28.         for (int j = 0; j < GRID_COLS; ++j) {
  29.             int red   = gridCell[i][j]*32;
  30.             int green = gridCell[i][j]*32;
  31.             int blue  = gridCell[i][j]*32;
  32.  
  33.             SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
  34.  
  35.             SDL_Rect cell = {j * GRID_SIZE, i * GRID_SIZE, GRID_SIZE, GRID_SIZE};
  36.             SDL_RenderFillRect(renderer, &cell);
  37.         }
  38.     }
  39.  
  40.     SDL_SetRenderDrawColor(renderer, antCol[0], antCol[1], antCol[2], 255);
  41.  
  42.  
  43.     SDL_Rect antCell = {antPos[0] * GRID_SIZE, antPos[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE};
  44.     SDL_RenderFillRect(renderer, &antCell);
  45.  
  46.     // Present the renderer
  47.     SDL_RenderPresent(renderer);
  48. }
  49.  
  50. void updateAnt(SDL_Renderer* renderer){
  51.  
  52.  
  53.     //draw ant old position
  54.         int red   = gridCell[antPos[1]][antPos[0]]*32;
  55.         int green = gridCell[antPos[1]][antPos[0]]*32;
  56.         int blue  = gridCell[antPos[1]][antPos[0]]*32;
  57.  
  58.         SDL_SetRenderDrawColor(renderer, red, green, blue, 255);
  59.  
  60.         SDL_Rect cell = {antPos[0] * GRID_SIZE, antPos[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE};
  61.         SDL_RenderFillRect(renderer, &cell);
  62.  
  63.     //move ant
  64.         if(gridCell[antPos[1]][antPos[0]]){
  65.             antDir+=1;
  66.         }else{
  67.             antDir-=1;
  68.         }
  69.         antDir=(antDir+4)%4;
  70.  
  71.         int deltay[] = {0,1,0,-1};
  72.         int deltax[] = {-1,0,1,0};
  73.  
  74.         antPos[0]+=deltax[antDir];
  75.         antPos[1]+=deltay[antDir];
  76.  
  77.         antPos[0] = (antPos[0] + GRID_ROWS) % GRID_ROWS;
  78.         antPos[1] = (antPos[1] + GRID_COLS) % GRID_COLS;
  79.    
  80.     //update cell
  81.         gridCell[antPos[1]][antPos[0]]=!gridCell[antPos[1]][antPos[0]];
  82.  
  83.  
  84.     //draw ant
  85.         SDL_SetRenderDrawColor(renderer, antCol[0], antCol[1], antCol[2], 255);
  86.         SDL_Rect antCell = {antPos[0] * GRID_SIZE, antPos[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE};
  87.         SDL_RenderFillRect(renderer, &antCell);
  88.  
  89.     // Present the renderer
  90.         SDL_RenderPresent(renderer);
  91.        
  92.     printf("x: %d ",antPos[0]);
  93.     printf("y: %d   ",antPos[1]);
  94.     printf("%d\n",gridCell[antPos[0]][antPos[1]]);
  95. }
  96.  
  97.  
  98. int main() {
  99.     srand(time(NULL));
  100.  
  101.     for (int i = 0; i < GRID_ROWS; ++i) {
  102.         for (int j = 0; j < GRID_COLS; ++j) {
  103.             gridCell[i][j] = ((i+4) / 8 + (j+3) / 8) % 2;
  104.         }
  105.     }
  106.    
  107.  
  108.     // Initialize SDL
  109.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  110.         std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
  111.         return 1;
  112.     }
  113.  
  114.     // Create a window
  115.     SDL_Window* window = SDL_CreateWindow("Grid Renderer", 3400, 600, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  116.     if (window == nullptr) {
  117.         std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
  118.         return 2;
  119.     }
  120.  
  121.     // Create a renderer for the window
  122.     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  123.     if (renderer == nullptr) {
  124.         std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
  125.         return 3;
  126.     }
  127.  
  128.     bool quit = false;
  129.     SDL_Event e;
  130.  
  131.     renderWholeGrid(renderer);
  132.  
  133.     Uint32 startTime = SDL_GetTicks();
  134.     startTime+=UPDATE_RATE;
  135.     int updateWholeRate=1000; //update whole screen per x frames
  136.     int frameNum=0;
  137.  
  138.     while (!quit) {
  139.         frameNum++;
  140.         if(frameNum>updateWholeRate){
  141.             frameNum=0;
  142.             renderWholeGrid(renderer);
  143.         }
  144.  
  145.         while (SDL_PollEvent(&e) != 0) {
  146.             if (e.type == SDL_QUIT) {
  147.                 quit = true;
  148.             }
  149.             if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) {
  150.                 quit = true;
  151.             }
  152.         }
  153.  
  154.         // Calculate the time to render a frame
  155.         Uint32 frameTime = SDL_GetTicks() - startTime;
  156.  
  157.         // Wait for the remainder of the 1-second frame
  158.         if (frameTime < UPDATE_RATE) {
  159.             SDL_Delay(UPDATE_RATE - frameTime);
  160.         }
  161.  
  162.         startTime = SDL_GetTicks();
  163.  
  164.         updateAnt(renderer);
  165.     }
  166.  
  167.     // Clean up and exit
  168.     SDL_DestroyRenderer(renderer);
  169.     SDL_DestroyWindow(window);
  170.     SDL_Quit();
  171.  
  172.     return 0;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement