无法摆脱循环

时间:2015-05-22 19:19:49

标签: c random sdl-2

我试图通过着色SDL窗口的情况来创建一个随机路径,但似乎我的功能无法摆脱循环,因此它给了我跳舞的形状。这是代码

#include<stdio.h>
#include<SDL2/SDL.h>
#include<stdlib.h>
#include<time.h>

#define N 0
#define S 2
#define W 3
#define E 1

void drawRectangle(int n, SDL_Renderer* renderer, int i, int j){
    SDL_Rect rectangle;
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    rectangle.x = j*(WIDTH/n);
    rectangle.y = i*(WIDTH/n);
    rectangle.w = WIDTH/n;
    rectangle.h = WIDTH/n;
    SDL_RenderFillRect(renderer, &rectangle);
}

void create_path(int n,SDL_Renderer* renderer){
    int i,j,k;
    drawRectangle(n,renderer,0,1);
    drawRectangle(n,renderer,1,1);
    for(i=1;i<n;i++){
        for(j=0;j<n;j++){
            k=rand()%4;
            if(k==N){
                drawRectangle(n,renderer, i-1, j);
            }
            else if(k==E){
                drawRectangle(n,renderer,i,j+1);
            }
            else if(k==S){
                drawRectangle(n,renderer,i+1,j);
            }
            else if(k==W){
                drawRectangle(n,renderer,i,j-1);
            }
        }
    }
}

0 个答案:

没有答案