我想创建一个视错觉,现在只使用rects而不是其他任何东西,我能用2个rects将它拉下来,但我想不出任何方法可以继续生成rects。现在我的代码只生成2个rects但我想知道如何更改我的代码继续生成rects并且在用户按下exit之前不要停止。以下是我的代码,任何帮助将不胜感激。
#include <iostream>
#include <math.h>
#include "SDL.h"
using namespace std;
const int screen_width = 500;
const int screen_height = 500;
int main(int argc, char ** argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("SDLDEMOSCENE", 50, 50, screen_width, screen_height, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
SDL_Rect rectangle, rectangle2;
double rect1_x = screen_width / 2;
double rect1_y = screen_height / 2;
double rect1_width = 5;
double rect1_height = 5;
double rect2_x = screen_width / 2;
double rect2_y = screen_height / 2;
double rect2_width = 5;
double rect2_height = 5;
bool quit = false;
while (quit == false)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
}
rectangle.x = rect1_x;
rectangle.y = rect1_y;
rectangle.h = rect1_height;
rectangle.w = rect1_width;
rectangle2.x = rect2_x;
rectangle2.y = rect2_y;
rectangle2.h = rect2_height;
rectangle2.w = rect2_width;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &rectangle);
rect1_x -= 0.01;
rect1_y -= 0.01;
rect1_width += 0.02;
rect1_height += 0.02;
if (rect1_x < 200 || rect1_y < 200)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &rectangle2);
rect2_x -= 0.01;
rect2_y -= 0.01;
rect2_width += 0.02;
rect2_height += 0.02;
}
if (rect2_x < 0 || rect2_y < 0)
{
rect2_height = 0;
rect2_width = 0;
rect2_x = 250;
rect2_y = 250;
rect1_height = 5;
rect1_width = 5;
rect1_x = 250;
rect1_y = 250;
}
SDL_RenderPresent(renderer);
}
SDL_Quit();
return 0;
}
现在有了这个编辑过的代码,2个方块正在进行,程序正在连续运行,但它们就像脉冲一样,两个区域正在形成现在我只是希望帮助在没有延迟的情况下进行更改,并且应该形成2个rects不断地给它带来幻觉的感觉。
答案 0 :(得分:0)
在这里:你忘了在rect2完成它之后重置rect1的属性。
将块if(rect2_x<0 || rect2_y<0)
更改为:
if (rect2_x < 0 || rect2_y < 0)
{
rect2_height = 0;
rect2_width = 0;
rect2_x = 250;
rect2_y = 250;
rect1_height = 5;
rect1_width = 5;
rect1_x = 250;
rect1_y = 250;
}
为什么if(rect1_x<0 || rect1_y<0)
里面包含if(rect1_x<250 || rect1_y<250)
?
rect_x<0
隐含rect1_x<250
因此,您可以移除额外的嵌套if-block并将其放在上一个嵌套中。
不是答案的一部分,但继承了我对你的程序做的随机事情: https://pastebin.com/zBBn7eBQ 在任何SDL_Window里面调用它!