'Can I know why I only get one puzzle not the figure I wanted?
I attached an image which is a row of different colours puzzles and the code runs perfectly fine except for the figures that supposed to be shown ... the figures I initialized in 2d array for example ( 1,3,7,5,) this is the first one though it doesn't appear that way , to draw them I also used for loop
Here is the code
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
if (event.type == Event::KeyPressed)
if (event.key.code == Keyboard::Up) rotate = true;
else if (event.key.code == Keyboard::Left) dx = -1;
else if (event.key.code == Keyboard::Right) dx = 1;
}
if (Keyboard::isKeyPressed(Keyboard::Down)) delay = 0.05;
// < moving >>
for (int i = 0; i < 4; i++) {b[i] = a[i]; a[i].x+= dx; }
if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i];
// rotate
if (rotate) {
point c = a[1]; //center point
for (int i = 0; i < 4; i++) {
int x = a[i].y - c.y;
int y = a[i].x - c.x;
a[i].x = c.x - x;
a[i].y = c.y + y;
}
if (!check()) for (int i = 0; i < 4; i++) a[i] = b[i];
}
//tick//
if (timer > delay)
{
for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].y += 1; }
if (!check())
{
for (int i = 0; i < 4; i++) puzzles[b[i].y][b[i].x] = colorNum;
colorNum = 1+ rand() % 5;
int n = rand() % 5;
for (int i = 0; i < 4; i++) {
a[i].x = puzzles[n][i] % 2;
a[i].y = puzzles[n][i] / 2;
}
}
timer = 0;
}
//// check lines
int k = M - 1;
for (int i = M-1; i > 0; i--) {
int count = 0;
for (int j = 0; j < N; j++)
{
if (puzzles[i][j]) count++;
puzzles[k][j] = puzzles[i][j];
}
if (count < N) k--;
}
dx = 0; rotate = 0; delay = 0.3;
window.clear();
for (int i=0;i<M;i++)
for (int j = 0; j < N; j++)
{
if (puzzles[i][j] == 0) continue;
sprite.setTextureRect(IntRect(puzzles[i][j]*122, 0, 110, 122));
sprite.setPosition(j *34 ,i *34 );
sprite.move(28, 31);
window.draw(sprite);
}
for (int i = 0; i < 4; i++) {
sprite.setTextureRect(IntRect(colorNum*122, 0, 110, 122));
sprite.setPosition(a[i].x * 34, a[i].y * 34);
sprite.move(28, 31);
window.draw(sprite);
}
window.display();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
