Giter Site home page Giter Site logo

ascii_tetris's Introduction

ASCII TETRIS GAME

Download with git clone https://github.com/GPotoshin/ascii_tetris. Run make to compile that project. You might need to download ncurses library on your machine.

How my code is organized

Data

Each figure is an array of four pointers to arrays of 8 numbers, which are allocated on stack.

typedef char figure[8];
const figure O[4] = {
	{0, 0, 0, 1, 1, 0, 1, 1},
	{0, 0, 0, 1, 1, 0, 1, 1},
	{0, 0, 0, 1, 1, 0, 1, 1},
	{0, 0, 0, 1, 1, 0, 1, 1}
};

Those octates numbers are divided in pairs of relevant coordinates. Arrays are placed in order of rotation, so it can be easily done by adding or subtracting 1 in the ring of rests of 4.

Tetris

The position is stored in global variable pos, rotation in rot, and figure *tetris is a pointer to the current figure displayed on the screen.

Program structure

int main () {
	if (init())
		goto _bailout;
	refresh();
	getch(); // wait to start
	mvprintw (1, 51, "press 'x' to exit        ");
	mvaddfig (pos.y, pos.x, tetris[rot], 'c');
	// set game not to wait for input
	nodelay (stdscr, TRUE); 
	
	while (!quite) {
		run();
	}

_bailout:
	// set back to default settings
	move (0, 0);
	nodelay(stdscr, FALSE);
	curs_set (1);
	endwin();
	system ("stty sane");
	return 0;
}

Despite that goto constructions are considered a bad habit, their main idea was not to use them instead of if, else, for and while etc. In C we do not have exceptions and goto can easily blend into code to satisfy that need.

Functions

char mod (char a, char b)

Unfortunately % is not the same thing as taking rest of division

void mvaddfig (int y, int x, figure fig, char ch)

I'm trying to keep the same style of naming functions as in ncurses library. It draws figure at a right spot

void removefig (int y, int x, figure fig)

Removes figure while keeping background filling

figure *genfig ()

Returns random figure

bool checkpos (int y, int x, figure fig)

Checks whether the figure can be placed there

int init ()

It draws board and texts, sets up ncurses

int getpoints (char linecount)

Your level based on number of cleaned lines

void end ()

Draws end logo

void run ()

Loop function with events, button checks and gameplay

ascii_tetris's People

Contributors

gpotoshin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.