Hi guys. This is my first post here Anyway, I'm working on a console tetris clone (I bet you get alot of people on here who do these little projects, lol), but I have ran into a touch of difficulty with one or two aspects of it. (This is my first game. I don't usually program games, hence why I'm having a few problems).
So far, I have just focused on functionality, rather than making it look nice... I will be doing that at a later stage. I have programmed it so that the w,s,a and d keys move just the one block around the 'allowed' area (obviously it wont work exactly like that when it is done, but I just wanna get something which can move about on the screen first). Below is my code atm. The problem I am having (among others), is that my block won't move Also, I'm not entirely sure how I will do the rotate part, so if anyone has any ideas, I would appreciate them. I'm programming in Dev-C++ in windows. I usually do most of my programming in Linux, but nm.
It is not as efficient as it really could be, but again, I will change that when I have something which works entirely first.
Code:
// INCLUDES //
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <conio.h>
// DEFINES //
#define WIN32_LEAN_AND_MEAN
#define WIDTH 50
#define HEIGHT 54
// Function Prototypes //
void game_menu(); // Menu function of the program
void game_init(); // Game init function
void game_grid(); // Function draws the tetris grid
void game_grid_clear(); // This function clears the interior of the grid
void game_previous_blocks();
void game_view_grid();
void game_keyboard(); // Take keyboard input and processing it
void game_translate(); // Performs the standard, one space down thing
void game_graphics_update(); // Update the graphics
void game_main(); // This is the main game loop function. This is where most of the work is done
void game_close(); // This function closes the application
void game_controls(); // This displays the controls for playing the game.
void game_draw_block();
int game_still_running(); // Tests if the program is still in 'run' mode
// Globally declared variables //
HANDLE hOutput;
int Option_number,i,j;
char grid[WIDTH][HEIGHT]; // This is the game array, and will record where the blocks are
char player_name;
int RUNNING;
using namespace std;
// Main Program //
int main(int argc, char *argv[])
{
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOutput,
FOREGROUND_GREEN|FOREGROUND_INTENSITY);
game_menu();
system("Pause");
return EXIT_SUCCESS;
}
void game_grid_clear()
{
COORD Position;
int i,j;
for(i=0; i<WIDTH; i++)
{
for(j=0; j<HEIGHT; j++)
{
Position.X = i;
Position.Y = j;
SetConsoleCursorPosition(hOutput, Position);
cout << " ";
}
}
}
void game_previous_blocks()
{
// This function draws all the previously dropped blocks
// It also gets rid of any full lines of blocks
}
void game_controls()
{
system("cls");
cout << "\n\t\tControls:\n\n";
cout << "\tUp Arrow : Rotate piece clockwise\n";
cout << "\tDown Arrow : Rotate piece anticlockwise\n";
cout << "\tLeft Arrow : Move piece to the left\n";
cout << "\tRight Arrow : Move piece to the right\n";
cout << "\tDelete : Make the piece drop quicker\n";
cout << "\n\t";
system("Pause");
game_menu();
}
void game_menu()
{
system("cls");
cout << "\n\t\tTetris Clone\n\n";
cout << "\tOptions:\n\t1. Play\n\t2. Controls\n\n\tOption Number>>";
cin >> Option_number;
if(Option_number == 1)
{
game_init();
}
else if(Option_number == 2)
{
game_controls();
}
}
void game_grid()
{
// Build game grid on the screen
int i;
COORD Position;
system("cls");
for(i=0; i<HEIGHT; i++)
{
Position.X = WIDTH;
Position.Y = i;
SetConsoleCursorPosition(hOutput,
Position);
cout << "|";
}
for(i=0; i<WIDTH; i++)
{
Position.X = i;
Position.Y = HEIGHT;
SetConsoleCursorPosition(hOutput,
Position);
cout << "_";
}
}
void game_init()
{
system("cls");
// Initial locally declared variables
char start;
int i, j;
COORD Position1, Position2;
// That the player's name
cout << "\n\t\tTetris Clone\n\n";
cout << "Please enter the player's name>>";
cin >> player_name;
// Draw the game grid
game_grid();
// Ask if the player is ready to begin
Position1.X = 0;
Position1.Y = 56;
Position2.X = 0;
Position2.Y = 57;
start = 'z';
while(start !='y')
{
for(i=0; i<WIDTH; i++)
{
Position2.X = i;
SetConsoleCursorPosition(hOutput, Position2);
cout << " ";
}
SetConsoleCursorPosition(hOutput, Position1);
cout << "Are you ready to start playing (y/n)?\n";
cout << ">>";
start = getch();
}
// Clear the question from the window
for(i=0; i<=WIDTH; i++)
{
for(j=56; j<=59; j++)
{
Position1.X = i;
Position1.Y = j;
SetConsoleCursorPosition(hOutput, Position1);
cout << " ";
}
}
game_main();
}
void game_keyboard()
{
if( (i-1)>0 ) {
switch(getch()) {
grid[i][j] = 0;
case 'a': i--; break;
grid[i][j] = 1;
}
}
else if( (i+1)<WIDTH ) {
switch(getch()) {
grid[i][j] = 0;
case 'd': i++; break;
grid[i][j] = 1;
}
}
else if( (j-1)>0 ) {
switch(getch()) {
grid[i][j] = 0;
case 'w': j--; break;
grid[i][j] = 1;
}
}
else if( (j+1)<HEIGHT ) {
switch(getch()) {
grid[i][j] = 0;
case 's': j++; break;
grid[i][j] = 1;
}
}
}
void game_translate()
{
}
void game_graphics_update()
{
for(i=0; i<WIDTH; i++)
{
for(j=0; j<HEIGHT; j++)
{
if( grid[i][j] == 1) {
cout << (unsigned char)219;
}
}
}
}
int game_still_running()
{
return 1;
}
void game_main()
{
// The first part of this code is just the beginning count down sequence
COORD Position;
RUNNING = 1;
int time;
time = 200; // Defines the time between frames
Position.X = 20;
Position.Y = 24;
int i,j;
for(i=3; i>=1; i--)
{
SetConsoleCursorPosition(hOutput, Position);
cout << "Starting in...\n\n\t\t\t " << i;
Sleep(1000);
}
game_grid_clear();
Position.X = 25;
Position.Y = 24;
SetConsoleCursorPosition(hOutput, Position);
cout << "GO!";
Sleep(1000);
// Wipe the screen for the beginning of the game
game_grid_clear();
// This is where the real game programming happens
// Initially set the grid array to be zeros. If a grid spot is filled, then
// 1 will be entered in the array
for(i=0; i<WIDTH; i++)
{
for(j=0; j<HEIGHT; j++)
{
grid[i][j] = 0;
}
}
// Make a block appear
grid[24][1] = 1;
// The main game loop
while(RUNNING == 1)
{
// Draw the block
game_draw_block();
// Sleep for time
Sleep(time);
// Consider keyboard input
game_keyboard();
// Move down by one grid space
game_translate();
// Update Graphics
game_grid_clear();
game_graphics_update();
// game_view_grid();
// Test if still running
RUNNING = game_still_running();
if(RUNNING == 0) {
game_close();
}
}
}
void game_draw_block()
{
COORD Position;
for(i=0; i<WIDTH; i++) {
for(j=0; j<HEIGHT; j++) {
if(grid[i][j] == 1) {
Position.X = i;
Position.Y = j;
SetConsoleCursorPosition(hOutput, Position);
cout << (unsigned char)219;
}
}
}
}
void game_view_grid()
{
}
void game_close()
{
}
P.S. Some of the functions in the program are either unfinished, or used only in the development stage (e.g. game_view_grid() function). These will be deleted later.