diff --git a/Tetris.vcxproj b/Tetris.vcxproj
index d858ed8..cefc670 100644
--- a/Tetris.vcxproj
+++ b/Tetris.vcxproj
@@ -90,9 +90,9 @@
C:\SFML-2.5.1\include;%(AdditionalIncludeDirectories)
- Console
+ Windows
C:\SFML-2.5.1\lib;%(AdditionalLibraryDirectories)
- sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-audio-d.lib;%(AdditionalDependencies)
+ sfml-main-d.lib;sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-audio-d.lib;%(AdditionalDependencies)
@@ -122,11 +122,11 @@
SFML_STATIC;_MBCS;%(PreprocessorDefinitions)
- Console
+ Windows
true
true
C:\SFML-2.5.1\lib;%(AdditionalLibraryDirectories)
- winmm.lib;opengl32.lib;freetype.lib;sfml-graphics-s.lib;sfml-window-s.lib;sfml-system-s.lib;sfml-audio-s.lib;%(AdditionalDependencies)
+ winmm.lib;opengl32.lib;freetype.lib;openal32.lib;sfml-main.lib;sfml-system-s.lib;flac.lib;vorbisenc.lib;vorbisfile.lib;vorbis.lib;ogg.lib;sfml-graphics-s.lib;sfml-window-s.lib;sfml-audio-s.lib;%(AdditionalDependencies)
diff --git a/fonts/MnPixel.ttf b/fonts/MnPixel.ttf
new file mode 100644
index 0000000..e3a2d97
Binary files /dev/null and b/fonts/MnPixel.ttf differ
diff --git a/images/bg.png b/images/bg.png
new file mode 100644
index 0000000..68cef1a
Binary files /dev/null and b/images/bg.png differ
diff --git a/images/bg2.png b/images/bg2.png
new file mode 100644
index 0000000..0e06dc7
Binary files /dev/null and b/images/bg2.png differ
diff --git a/images/bg3.png b/images/bg3.png
new file mode 100644
index 0000000..ed563d0
Binary files /dev/null and b/images/bg3.png differ
diff --git a/images/layout.png b/images/layout.png
new file mode 100644
index 0000000..b8d803b
Binary files /dev/null and b/images/layout.png differ
diff --git a/images/tiles.png b/images/tiles.png
index 6ed389d..d1fc8c0 100644
Binary files a/images/tiles.png and b/images/tiles.png differ
diff --git a/images/xie.png b/images/xie.png
index 4b13f11..c9f97c9 100644
Binary files a/images/xie.png and b/images/xie.png differ
diff --git a/main.cpp b/main.cpp
index 26134a9..49a024a 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,6 +1,7 @@
#include
+#include
#include
-#include
+#include
#include
#include
#define HEIGHT 768
@@ -215,9 +216,9 @@ int grid[20][10] = {
};
int screen_x = 150;
int screen_y = 150;
-int tilePos_x = 9;
+int tilePos_x = 8;
int tilePos_y = 0;
-int coordX = 4;
+int coordX = 3;
int coordY = -1;
int type = 0;
int gg = 0;
@@ -225,7 +226,12 @@ int rotation = 0;
int positionX;
int positionY;
int check = 1;
-int collision(int type, int tilePos_xx,int tilePos_yy,char Direction,int newRotation) {
+int holdPiece = 7;
+int holdCount = 0;
+int score = 0;
+float speed = 0.3;
+bool pieceDropped = FALSE;
+int collision(int type, int tilePos_xx, int tilePos_yy, char Direction, int newRotation) {
int newPositionX;
int newPositionY;
newRotation += rotation;
@@ -241,22 +247,39 @@ int collision(int type, int tilePos_xx,int tilePos_yy,char Direction,int newRota
newPositionX = (tilePos_xx + x) * 30;
newPositionY = screen_y + ((tilePos_yy + y) * 30 - 30);
if (Direction == 'l') {
- if ((newPositionX < screen_x || grid[coordY + y][coordX + x - 1] != 7)) {
- return TRUE;
+ if (tilePos_y < 2) {
+ if (newPositionX < screen_x) {
+ return TRUE;
+ }
+ }
+ else if (tilePos_y >= 2) {
+ if ((newPositionX < screen_x || grid[coordY + y][coordX + x - 1] != 7)) {
+ return TRUE;
+ }
}
}
if (Direction == 'x') {
- if ((grid[coordY + y][coordX + x + 1] != 7) || newPositionX >= screen_x + (10 * 30)) {
- return TRUE;
+ if (tilePos_y < 2) {
+ if (newPositionX >= screen_x + (10 * 30)) {
+ return TRUE;
+ }
+ }
+ else if (tilePos_y >= 2) {
+ if ((grid[coordY + y][coordX + x + 1] != 7) || newPositionX >= screen_x + (10 * 30)) {
+ return TRUE;
+ }
}
}
- else if(Direction == 'y') {
- if (newPositionY >= screen_y + (20 * 30) || (grid[coordY + y + check][coordX + x] != 7)){
+ else if (Direction == 'y') {
+ if (newPositionY >= screen_y + (20 * 30) || (grid[coordY + y + check][coordX + x] != 7)) {
return TRUE;
}
}
else {
// (newPositionY >= screen_y + (19 * 30)
+ if ((grid[coordY + y][coordX + x] != 7)) {
+ return TRUE;
+ }
if (newPositionX < screen_x) {
tilePos_x += 1;
coordX += 1;
@@ -264,10 +287,6 @@ int collision(int type, int tilePos_xx,int tilePos_yy,char Direction,int newRota
else if (newPositionX >= screen_x + (10 * 30)) {
tilePos_x -= 1;
coordX -= 1;
- }
- else if (newPositionY >= screen_y + (19 * 30)) {
- tilePos_y -= 1;
- coordY -= 1;
}
}
@@ -276,33 +295,69 @@ int collision(int type, int tilePos_xx,int tilePos_yy,char Direction,int newRota
}
return FALSE;
}
-int main(){
+int main() {
+
ShowWindow(GetConsoleWindow(), SW_SHOW);
- sf::RenderWindow window(sf::VideoMode(600, 900), "Mykes", sf::Style::Close | sf::Style::Titlebar);
-
+ sf::RenderWindow window(sf::VideoMode(600, 900), "Tetris Metris", sf::Style::Close | sf::Style::Titlebar);
+
//Frame Rate and Game Speed
window.setFramerateLimit(60);
sf::Clock clock;
+ sf::SoundBuffer clearBuffer;
+ clearBuffer.loadFromFile("sounds/clear.wav");
+ sf::Sound clear;
+ clear.setBuffer(clearBuffer);
+ sf::SoundBuffer placeBuffer;
+ placeBuffer.loadFromFile("sounds/place.wav");
+ sf::Sound place;
+ place.setBuffer(placeBuffer);
+ sf::Font font;
+ font.loadFromFile("fonts/MnPixel.ttf");
+ sf::Text text;
+ text.setFont(font);
+ text.setCharacterSize(45);
+ text.setFillColor(sf::Color::White);
+ text.setString("0");
+ text.setPosition(345, 818);
sf::Event e;
sf::Texture tileTexture;
tileTexture.loadFromFile("images/tiles.png");
- //sf::Texture gridTexture;
- //gridTexture.loadFromFile("images/gridTile.png");
sf::Image icon;
-
- //sf::RectangleShape grid(sf::Vector2f(30, 30));
-
- //grid.setTexture(&gridTexture);
+ sf::Texture layoutTexture;
+ layoutTexture.loadFromFile("images/layout.png");
+ sf::Sprite layout(layoutTexture);
+ layout.setTextureRect(sf::IntRect(0, 0, 600, 900));
+ sf::Texture bgTexture;
+ bgTexture.loadFromFile("images/bg2.png");
+ sf::Sprite bg(bgTexture);
+ bg.setTextureRect(sf::IntRect(0, 0, 600, 900));
sf::Sprite tile(tileTexture);
tile.setTextureRect(sf::IntRect(0, 0, 30, 30));
icon.loadFromFile("images/xie.png");
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
-
+ srand(time(NULL));
+ int rand1 = rand() % 7 + 0;
+ srand(time(NULL));
+ int rand2 = rand() % 7 + 0;
+ type = rand1;
+ if (rand1 == rand2) {
+ if (rand2 <= 0) {
+ rand2++;
+ }
+ else {
+ rand2--;
+ }
+ }
while (window.isOpen())
{
+ for (int x = 0; x < 10; x++) {
+ if (grid[0][x] != 7) {
+ exit(0);
+ }
+ }
static float prev = clock.getElapsedTime().asSeconds();
- if (clock.getElapsedTime().asSeconds() - prev >= 0.8)
+ if (clock.getElapsedTime().asSeconds() - prev >= speed)
{
if (collision(type, tilePos_x, tilePos_y + 1, 'y', 0)) {
for (int x = 0; x < 4; x++) {
@@ -318,37 +373,41 @@ int main(){
if (grid[x][y] != 7) {
count += 1;
}
- if (count == 10) {
- for (int r = x; r > 1; r--) {
- for (int y = 0; y < 10; y++) {
- grid[r][y] = grid[r - 1][y];
+ if (count == 10) {
+ score += 1;
+ clear.play();
+ text.setString(std::to_string(score));
+ for (int r = x; r > 1; r--) {
+ for (int y = 0; y < 10; y++) {
+ grid[r][y] = grid[r - 1][y];
+ }
+ //grid[8][10] = grid[7][10];
+ }
+ for (int x = 0; x < 10; x++) {
+ grid[0][x] = 7;
}
- grid[8][10] = grid[7][10];
- }
- for (int x = 0; x < 10; x++) {
- grid[0][x] = 7;
}
- }
- tile.setPosition(sf::Vector2f(screen_x + (x * 30), screen_y + (y * 30)));
- tile.setTextureRect(sf::IntRect(grid[y][x] * 30, 0, 30, 30));
- window.draw(tile);
+
}
}
-
- tilePos_x = 9;
+ place.play();
+ tilePos_x = 8;
tilePos_y = 0;
- coordX = 4;
+ coordX = 3;
coordY = -1;
+ rand1 = rand2;
+ type = rand1;
srand(time(NULL));
-
- type = rand() % 7 + 0;
+ rand2 = rand() % 7 + 0;
rotation = 0;
+ holdCount = 0;
+ pieceDropped = FALSE;
}
else {
prev = clock.getElapsedTime().asSeconds();
tilePos_y += 1;
coordY += 1;
- printf("%d\n", tilePos_y);
+ pieceDropped = FALSE;
}
}
while (window.pollEvent(e))
@@ -356,31 +415,74 @@ int main(){
if (e.type == sf::Event::Closed)
{
window.close();
+ exit(0);
}
}
- if (e.key.code == sf::Keyboard::Space) {
- }
window.clear();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Tab)) {
+ window.draw(bg);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
+ bool blockInWay = FALSE;
+ pieceDropped = TRUE;
static float prev = clock.getElapsedTime().asSeconds();
- if (clock.getElapsedTime().asSeconds() - prev >= 0.8)
+ if (clock.getElapsedTime().asSeconds() - prev >= 0.75)
{
prev = clock.getElapsedTime().asSeconds();
- type += 1;
- if (type >= 7) {
- type = 0;
+ while (check <= 22 && blockInWay == FALSE) {
+ if (blockInWay == FALSE) {
+ tilePos_y += 1;
+ coordY += 1;
+ check = 1;
+ }
+ if (collision(type, tilePos_x, tilePos_y + check, 'y', 0)) {
+ tilePos_y = tilePos_y + check - 1;
+ coordY = coordY + check - 1;
+ check = 1;
+ blockInWay = TRUE;
+ }
+ check += 1;
+ }
+ }
+ }
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Tab)) {
+ static float prev = clock.getElapsedTime().asSeconds();
+ if (clock.getElapsedTime().asSeconds() - prev >= 0.2 && pieceDropped == FALSE)
+ {
+ if (holdPiece >= 7 && holdCount < 1) {
+ prev = clock.getElapsedTime().asSeconds();
+ rand1 = rand2;
+ holdPiece = type;
+ type = rand1;
+ srand(time(NULL));
+ rand2 = rand() % 7 + 0;
+ tilePos_x = 8;
+ tilePos_y = 0;
+ coordX = 3;
+ coordY = -1;
+ rotation = 0;
+ holdCount += 1;
+ }
+ else if (holdPiece < 7 && holdCount < 1) {
+ int placeHolder;
+ placeHolder = type;
+ type = holdPiece;
+ holdPiece = placeHolder;
+ tilePos_x = 8;
+ tilePos_y = 0;
+ coordX = 3;
+ coordY = -1;
+ rotation = 0;
+ holdCount += 1;
}
- rotation = 0;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
if (collision(type, tilePos_x - 1, tilePos_y, 'l', 0)) {
- printf("SHIT");
+ std:: cout << "Collision\n";
}
- else {
+ else if (pieceDropped == FALSE) {
static float prev = clock.getElapsedTime().asSeconds();
- if (clock.getElapsedTime().asSeconds() - prev >= 0.06)
+ if (clock.getElapsedTime().asSeconds() - prev >= 0.04)
{
prev = clock.getElapsedTime().asSeconds();
tilePos_x -= 1;
@@ -390,11 +492,11 @@ int main(){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
if (collision(type, tilePos_x + 1, tilePos_y, 'x', 0)) {
- printf("SHIT");
+ std::cout << "Collision\n";
}
- else {
+ else if (pieceDropped == FALSE) {
static float prev = clock.getElapsedTime().asSeconds();
- if (clock.getElapsedTime().asSeconds() - prev >= 0.06)
+ if (clock.getElapsedTime().asSeconds() - prev >= 0.04)
{
prev = clock.getElapsedTime().asSeconds();
tilePos_x += 1;
@@ -404,10 +506,10 @@ int main(){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
check = 1;
- if (collision(type, tilePos_x, tilePos_y + 1, 'y', 0)) {
- printf("SHIT");
+ if (collision(type, tilePos_x, tilePos_y + 1, 'y', 2)) {
+ std::cout << "Collision\n";
}
- else {
+ else if (pieceDropped == FALSE) {
static float prev = clock.getElapsedTime().asSeconds();
if (clock.getElapsedTime().asSeconds() - prev >= 0.06)
{
@@ -420,9 +522,9 @@ int main(){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
if (collision(type, tilePos_x, tilePos_y, 'r', -1)) {
- printf("SHIT");
+ std::cout << "Collision\n";
}
- else {
+ else if (pieceDropped == FALSE) {
static float prev = clock.getElapsedTime().asSeconds();
if (clock.getElapsedTime().asSeconds() - prev >= 0.2)
{
@@ -436,9 +538,9 @@ int main(){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
if (collision(type, tilePos_x, tilePos_y, 'r', 1)) {
- printf("SHIT");
+ std::cout << "Collision\n";
}
- else {
+ else if (pieceDropped == FALSE) {
static float prev = clock.getElapsedTime().asSeconds();
if (clock.getElapsedTime().asSeconds() - prev >= 0.2)
{
@@ -447,28 +549,7 @@ int main(){
}
}
}
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
- bool blockInWay = FALSE;
- static float prev = clock.getElapsedTime().asSeconds();
- if (clock.getElapsedTime().asSeconds() - prev >= 1)
- {
- prev = clock.getElapsedTime().asSeconds();
- while (check <= 20 && blockInWay == FALSE) {
- if (collision(type, tilePos_x, tilePos_y + check, 'y', 0)) {
- tilePos_y = tilePos_y + check - 1;
- coordY = coordY + check - 1;
- check = 1;
- blockInWay = TRUE;
- }
- check += 1;
- if (blockInWay == FALSE){
- tilePos_y += 1;
- coordY +=1;
- check = 1;
- }
- }
- }
- }
+
// Draw Board
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 20; y++) {
@@ -478,6 +559,41 @@ int main(){
}
}
+ layout.setPosition(0, 0);
+ window.draw(layout);
+ window.draw(text);
+
+
+ // Draw Next Piece
+ for (int x = 0; x < 4; x++) {
+ for (int y = 0; y < 4; y++) {
+ if (tetromino[rand2][0][y][x]) {
+ positionX = 490 + x * 20;
+ positionY = 190 + y * 20;
+ tile.setPosition(sf::Vector2f(positionX, positionY));
+ tile.setTextureRect(sf::IntRect(rand2 * 30, 0, 20, 20));
+ window.draw(tile);
+
+ }
+ }
+ }
+ // Draw Holded Piece
+ if (holdPiece < 7) {
+ for (int x = 0; x < 4; x++) {
+ for (int y = 0; y < 4; y++) {
+ if (tetromino[holdPiece][0][y][x]) {
+ positionX = 40 + x * 20;
+ positionY = 190 + y * 20;
+ tile.setPosition(sf::Vector2f(positionX, positionY));
+ tile.setTextureRect(sf::IntRect(holdPiece * 30, 0, 20, 20));
+ window.draw(tile);
+
+ }
+
+ }
+ }
+ }
+
// Draw Tetrominos
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
@@ -495,3 +611,4 @@ int main(){
}
return 0;
}
+
diff --git a/sounds/clear.wav b/sounds/clear.wav
new file mode 100644
index 0000000..35a69bd
Binary files /dev/null and b/sounds/clear.wav differ
diff --git a/sounds/place.wav b/sounds/place.wav
new file mode 100644
index 0000000..716412b
Binary files /dev/null and b/sounds/place.wav differ