SFML community forums
Help => Graphics => Topic started by: Devil0150 on June 16, 2009, 07:40:41 pm
-
This is my code, im trying to make my tetris game and i cant rotate my pieces. I am doing it by changing the image of the sprite when i press my up key but it doesnt work.
sf::Sprite* show;
int random = sf::Randomizer::Random(1, 5);
switch(random)
{
case 1:
{
show = &(myCube.mySprite);
break;
}
case 2:
{
show = &(myL.mySprite);
break;
}
case 3:
{
show = &(myBackL.mySprite);
break;
}
case 4:
{
show = &(myI.mySprite);
break;
}
case 5:
{
show = &(myT.mySprite);
break;
}
}
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
{
while (Tetris.GetEvent(Termino))
{
if (Termino.Type == sf::Event::Closed)
Tetris.Close();
if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Up))
{
if (random = 1);
else if (random = 2)
(*show).SetImage(myL.NextImage());
else if(random = 3)
(*show).SetImage(myBackL.NextImage());
else if (random = 4)
(*show).SetImage(myI.NextImage());
else
(*show).SetImage(myT.NextImage());
}
if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Escape))
Tetris.Close();
}
Now here is the function to find the next image.
public:
sf::Image ImageT;
int rotation;
sf::Image T1;
sf::Image T2;
sf::Image T3;
sf::Image& NextImage()
{
T1.LoadFromFile("D:\\C++\\T1.jpg");
T2.LoadFromFile("D:\\C++\\T2.jpg");
T3.LoadFromFile("D:\\C++\\T3.jpg");
if (rotation == 0)
{
return T1;
++rotation;
}
else if (rotation == 1)
{
return T2;
++rotation;
}
else if (rotation == 2)
{
return T3;
++rotation;
}
else
{
return ImageT;
rotation = 0;
}
}
Can u help me?
P.S. I initialized my ImageT image in the constructor.
-
if (rotation == 0)
{
return T1;
++rotation;
}
else if (rotation == 1)
{
return T2;
++rotation;
}
else if (rotation == 2)
{
return T3;
++rotation;
}
else
{
return ImageT;
rotation = 0;
}
you should try:
if (rotation == 0)
{
++rotation;
return T1;
}
else if (rotation == 1)
{
++rotation;
return T2;
}
else if (rotation == 2)
{
++rotation;
return T3;
}
else
{
rotation = 0;
return ImageT;
}
:D
-
Still the same...
-
Ok, i fixed it. I just replaced all the (*show).SetImage with (*show).Rotate.
The piece rotates but 1 second after i press the button. Maybe it is because of this part of the code here:
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
{
while (Tetris.GetEvent(Termino))
{
if (Termino.Type == sf::Event::Closed)
Tetris.Close();
if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Up))
(*show).Rotate(90);
if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Escape))
Tetris.Close();
}
// This is the part I was talking about, I made it for the piece to move down 1 block per second.
Tetris.Clear();
(*show).Move(0, 10);
while (myClock.GetElapsedTime() < 1.f);
myClock.Reset();
Tetris.Draw(*show);
Tetris.Display();
}
-
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
Why don't you use Tetris.IsOpened()?
-
because i need to get another piece when this one gets to the bottom of the window.
Here is my main()
int main()
{
Cube myCube("D:\\C++\\cube.jpg");
L myL("D:\\C++\\L.jpg");
backL myBackL("D:\\C++\\LBack.jpg");
I myI("D:\\C++\\I.jpg");
T myT("D:\\C++\\T.jpg");
sf::Event Termino;
while (Tetris.IsOpened())
{
sf::Clock myClock;
sf::Sprite* show;
int random = sf::Randomizer::Random(1, 5);
switch(random)
{
case 1:
{
show = &(myCube.mySprite);
break;
}
case 2:
{
show = &(myL.mySprite);
break;
}
case 3:
{
show = &(myBackL.mySprite);
break;
}
case 4:
{
show = &(myI.mySprite);
break;
}
case 5:
{
show = &(myT.mySprite);
break;
}
}
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
{
while (Tetris.GetEvent(Termino))
{
if (Termino.Type == sf::Event::Closed)
Tetris.Close();
if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Up))
(*show).Rotate(90);
if ((Termino.Type == sf::Event::KeyPressed) && (Termino.Key.Code == sf::Key::Escape))
Tetris.Close();
}
sf::Clock my2Clock
Tetris.Clear();
(*show).Move(0, 10);
while (my2Clock.GetElapsedTime() < 1.f);
my2Clock.Reset();
Tetris.Draw(*show);
Tetris.Display();
}
myClock.Reset();
}
return 0;
}
[/code]
-
while (Tetris.IsOpened())
while (myClock.GetElapsedTime() <= Tetris.GetHeight())
while (Tetris.GetEvent(Termino))
This way you are blocking your game loop.
-
I fixed it. I put an if statement instead of the second.
if (my2Clock.GetElapsedTime() >= 1.f)
{
(*show).Move(0, 10);
my2Clock.Reset();
}