Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
shpodg committed Oct 9, 2019
1 parent 67a2cb0 commit ff98e0b
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.0)
project(console-tetris)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
add_executable(console-tetris ConsoleCanvas.cpp main.cpp)
95 changes: 95 additions & 0 deletions ConsoleCanvas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <iostream>
#include <conio.h>
#include <windows.h>
#include "ConsoleCanvas.h"

ConsoleCanvas::ConsoleCanvas() {
_width = 55, _height = 26, width = (_width - 7) / 2, height = _height - 2;
int i, j;
for (i = 0; i < _width; i++)
for (j = 0; j < _height; j++)
board[j][i] = ' ';
for (i = 0; i < _height; i++) {
_put("", _width - 7, i);
board[i][_width - 5] = '\n';
_put("\t\t", _width - 4, i);
_put("", _width - 2, i);
}
for (j = 0; j < _width - 7; j += 2) {
_put("", j, 0);
_put("", j, _height - 1);
}
_put("", _width - 7, 0);
_put("", _width - 2, _height - 2);
_put("", _width - 7, _height - 1);
board[_height - 1][_width - 2] = '\0';
}

void ConsoleCanvas::cls() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++)
_put(" ", i * 2, j + 1);
}
}

void ConsoleCanvas::put(const string &s, int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height)return;
board[y + 1][x * 2] = s[0];
board[y + 1][x * 2 + 1] = s[1];
}

void ConsoleCanvas::_put(const string &s, int x, int y) {
board[y][x] = s[0];
board[y][x + 1] = s[1];
}

char ConsoleCanvas::get(int x, int y) {
return board[y + 1][x * 2 + 1];
}

void ConsoleCanvas::note(const string &s, int wait) {
system("color 02");
cls();
text(s, -1, _height / 2);
show();
Sleep(wait);
while (kbhit()) getch();
}

void ConsoleCanvas::show() {
system("cls");

std::cout << "\n\t\t" << *board;
}

void ConsoleCanvas::text(const string &s, int x, int y) {
int len = strLen(s);
if (x == -1) x = (_width - len - 7) / 2;
if (x > 0 && y >= 0) {
if (len % 2 == 1) len--; //处理成偶数
for (int i = 0; i < len; i++)
board[y][i + x] = s[i];
} else {
x = 2, y = 5;
int n = 0, _n = 0;
while (n < len) {
(s[n] >> 7 == -1) && _n++;
if (s[n] == '\n') {
n++, x = 2, y += 2;
continue;
}
board[y][x++] = s[n];
if (x >= _width - 7) {
if (_n % 2) board[y][x - 1] = ' ', _n--, n--;
x = 2, y += 2;
}
n++;
if (!(_n % 2))show(), Sleep(15);
}
}
}

int ConsoleCanvas::strLen(const string &s) {
return s.length();
}

33 changes: 33 additions & 0 deletions ConsoleCanvas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#ifndef _BACKGROUND_H_
#define _BACKGROUND_H_
using namespace std;
class ConsoleCanvas {
public:
static int strLen(const string& s);

char board[26][55];
int width,height;

ConsoleCanvas();

void show();

void cls();

void text(const string& s, int x = -1, int y = 0);

void put(const string& s, int x, int y);

void _put(const string& s, int x, int y);

char get(int x, int y);

void note(const string& s, int wait = 2000);

private:
int _width,_height;

};

#endif //_BACKGROUND_H_
Binary file added docs/gameover.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/main.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/playing.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ff98e0b

Please sign in to comment.