Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 57 additions & 55 deletions 01 Tetris/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;


// C++ program for tetris game
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

const int M = 20;
const int N = 10;

Expand All @@ -19,56 +21,56 @@ int figures[7][4] =
2,3,5,7, // L
3,5,7,6, // J
2,3,4,5, // O
};

};
bool check()
{
for (int i=0;i<4;i++)
if (a[i].x<0 || a[i].x>=N || a[i].y>=M) return 0;
else if (field[a[i].y][a[i].x]) return 0;

return 1;
};


int main()
{
srand(time(0));

RenderWindow window(VideoMode(320, 480), "The Game!");

Texture t1,t2,t3;
};
int main()
{
srand(time(0));
RenderWindow window(VideoMode(320, 480), "The Game!");
Texture t1,t2,t3;
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/background.png");
t3.loadFromFile("images/frame.png");

Sprite s(t1), background(t2), frame(t3);

int dx=0; bool rotate=0; int colorNum=1;
float timer=0,delay=0.3;

Clock clock;

while (window.isOpen())
{
int dx=0; bool rotate=0; int colorNum=1;
float timer=0,delay=0.3;
Clock clock;
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer+=time;

Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();

timer+=time;
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
if (e.type == Event::KeyPressed)
if (e.key.code==Keyboard::Up) rotate=true;
else if (e.key.code==Keyboard::Left) dx=-1;
else if (e.key.code==Keyboard::Right) dx=1;
}

else if (e.key.code==Keyboard::Right) dx=1;
}
if (Keyboard::isKeyPressed(Keyboard::Down)) delay=0.05;

//// <- Move -> ///
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];
Expand All @@ -86,7 +88,7 @@ int main()
}
if (!check()) for (int i=0;i<4;i++) a[i]=b[i];
}

///////Tick//////
if (timer>delay)
{
Expand All @@ -105,9 +107,9 @@ int main()
}
}

timer=0;
}

timer=0;
}
///////check lines//////////
int k=M-1;
for (int i=M-1;i>0;i--)
Expand All @@ -119,13 +121,13 @@ int main()
field[k][j]=field[i][j];
}
if (count<N) k--;
}

}
dx=0; rotate=0; delay=0.3;

/////////draw//////////
window.clear(Color::White);
window.draw(background);
window.clear(Color::White);
window.draw(background);

for (int i=0;i<M;i++)
for (int j=0;j<N;j++)
Expand All @@ -135,19 +137,19 @@ int main()
s.setPosition(j*18,i*18);
s.move(28,31); //offset
window.draw(s);
}

}
for (int i=0;i<4;i++)
{
s.setTextureRect(IntRect(colorNum*18,0,18,18));
s.setPosition(a[i].x*18,a[i].y*18);
s.move(28,31); //offset
window.draw(s);
}

window.draw(frame);
window.display();
}

return 0;
}
}

window.draw(frame);
window.display();
}
return 0;
}