void TFishka::init(int _x, int _y, TColor _c, int __gamer)




{

int temp;

x=_x;

y=_y;

temp=0;

if (mas[x][y]==0) {color[x][y]=_c; mas[x][y]=1; temp++;}

if (mas[x+1][y]==0) {color[x+1][y]=_c; mas[x+1][y]=1; temp++;}

if (mas[x-1][y]==0) {color[x-1][y]=_c; mas[x-1][y]=1; temp++;}

if (mas[x][y+1]==0) {color[x][y+1]=_c; mas[x][y+1]=1; temp++;}

if (mas[x][y-1]==0) {color[x][y-1]=_c; mas[x][y-1]=1; temp++;}

if (mas[x+1][y-1]==0) {color[x+1][y-1]=_c; mas[x+1][y-1]=1; temp++;}

if (mas[x+1][y+1]==0) {color[x+1][y+1]=_c; mas[x+1][y+1]=1; temp++;}

if (mas[x-1][y-1]==0) {color[x-1][y-1]=_c; mas[x-1][y-1]=1; temp++;}

if (mas[x-1][y+1]==0) {color[x-1][y+1]=_c; mas[x-1][y+1]=1; temp++;}

 

switch(__gamer)

{

case 1: count_1+=temp;break;

case 2: count_2+=temp;break;

}

}

 

 

Результат работы программы:

1) Главное окно после запуска видим зеленый ящик. Появляються фигуры рандомно, игрок должен упраявлять фигурой с помощтю клавиатуры интерфейс включен. На форме отображаеться появление следующей фигуры и счет.

Рис. 4 Главное окно после запуска

 

 

Заключение

В ходе курсовой работы мною были изучены некоторые аспекты объектно-ориентированного программирования на языке С++ (организация данных в структуре класс, обеспечение доступа к ним, наследование классов, переопределение функций). При оформлении курсовой работы был получены навыки оформления программной документации, а также большой практический опыт работы в C++, MicrosoftWordforWindows, VisualStudio 2015 (хотя освоение этих программных продуктов не было целью курсовой работы, данные навыки нельзя считать бесполезными). Теоретические сведения были закреплены практическими занятиями.

 

 

Список использованной литературы

1. «Профессиональное программирование на С++» М. Шлее – СПб.: БХВ-Петербург, 2010. 896 с.

2. «С++: руководство для начинающих» Г. Шилдт – М.: Издательский дом «Вильямс», 2005. 672 с.

3. «программирование на С++» Ж. Бланшет, М. Саммерфилд – М.: КУДИЦ-ПРЕСС, 2008. 736 с.

4. «Язык программирования С++. Лекции и упражнения» С. Прата – М.: ООО «И.Д. Вильямс», 2012. 1248 с.

5.Конспект лекций по ООП

6. Страустрап Б. Введение в язык С++ Ресурсы электронной библиотеки InfoCity (https://www.inforcity.kiev.ua), 1995.

7. Бочков С. О., Субботин Д. М. Язык программирования Си для персонального компьютера. М.: “Диалог”, Радио и связь, 1990.

8. Справочная система языка C++.

 

 

Приложение 1

(tetris.cpp)

#include "Tetris.h"

#include "Console.h"

#include <ctime>

 

Matrix::Matrix():

m_matix{0}

{

}

 

bool Matrix::IsFull() const

{

for (int j = 0; j < Size; j++)

if (m_matix[0][j] == 1)

return true;

 

return false;

}

 

bool Matrix::CaneMoveLeft(const Figure &figure) const

{

WORD fgr = figure.GetFigure();

COORD coord = figure.GetXY();

 

for (inti = 0; i<Figure::Size; i++) {

for (int j = 0; j <Figure::Size; j++) {

if ((fgr& 0x8000)!= 0)

if ((coord.X + j == 0) || (m_matix[coord.Y + i][coord.X + j - 1] == 1))

return false;

 

fgr<<= 1;

}

}

 

return true;

}

 

bool Matrix::CaneMoveRight(const Figure &figure) const

{

WORD fgr = figure.GetFigure();

COORD coord = figure.GetXY();

 

for (inti = 0; i<Figure::Size; i++) {

for (int j = 0; j <Figure::Size; j++) {

if ((fgr& 0x8000)!= 0)

if ((coord.X + j == Size - 1) || (m_matix[coord.Y + i][coord.X + j + 1] == 1))

return false;

 

fgr<<= 1;

}

}

 

return true;

}

 

bool Matrix::CaneRotate(const Figure &figure) const

{

WORD fgr = figure.GetFigure();

COORD coord = figure.GetXY();

 

for (inti = 0; i<Figure::Size; i++) {

for (int j = 0; j <Figure::Size; j++) {

if ((fgr& 0x8000)!= 0)

if ((m_matix[coord.Y + i][coord.X + j] == 1) || (coord.X + j < 0 || coord.X + j >= Size) || (coord.Y + i>= Size))

return false;

 

fgr<<= 1;

}

}

 

return true;

}

 

bool Matrix::IsDown(const Figure &figure) const

{

WORD fgr = figure.GetFigure();

COORD coord = figure.GetXY();

 

for (inti = 0; i<Figure::Size; i++) {

for (int j = 0; j <Figure::Size; j++) {

if ((fgr& 0x8000)!= 0)

if ((coord.Y + i == Size) || (m_matix[coord.Y + i][coord.X + j] == 1))

return true;

 

fgr<<= 1;

}

}

 

return false;

}

 

void Matrix::AddFigure(const Figure &figure)

{

WORD fgr = figure.GetFigure();

COORD coord = figure.GetXY();

 

for (inti = 0; i<Figure::Size; i++) {

for (int j = 0; j <Figure::Size; j++) {

if ((fgr& 0x8000)!= 0)

m_matix[coord.Y + i - 1][coord.X + j] = 1;

 

fgr<<= 1;

}

}

}

 

intMatrix::GetFullRow() const

{

for (inti = 0; i< Size; i++) {

int counter = 0;

for (int j = 0; j < Size; j++) {

if (m_matix[i][j] == 1) {

counter++;

if (counter == Size)

return i;

}

}

}

 

return -1;

}

 

void Matrix::DeleteRow(int row)

{

for (inti = row; i>= 1; i--) {

for (int j = 0; j < Size; j++) {

m_matix[i][j] = m_matix[i - 1][j];

m_matix[i - 1][j] = 0;

}

}

}

 

void Matrix::DebugDraw(int x, int y) const

{

for (inti = 0; i< Size; i++) {

for (int j = 0; j < Size; j++) {

if (m_matix[i][j] == 1)

console::Write("1", x + j, y + i);

else

console::Write("0", x + j, y + i);

}

}

}

 

Figure::Figure():

m_type(FIGURE_TYPE_I),

m_rotate(FIGURE_ROTATE_0),

m_coord{0, 0},

m_drawCoord{0, 0}

{

srand(time(nullptr));

}

 

const WORD Figure::Figures[FigureCount][FigureRotateCount] = {

{0xF000, 0x8888, 0xF000, 0x8888}, // I

{0x8E00, 0xC880, 0xE200, 0x2260}, // J

{0x2E00, 0x88C0, 0xE800, 0x6220}, // L

{0xCC00, 0xCC00, 0xCC00, 0xCC00}, // O

{0x6C00, 0x8C40, 0x6C00, 0x8C40}, // S

{0x4E00, 0x8C80, 0xE40, 0x4C40}, // T

{0xC600, 0x4C80, 0xC600, 0x4C80} // Z

};

 

const WORD Figure::FigureColors[FigureCount] = {

BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY,

BACKGROUND_BLUE | BACKGROUND_INTENSITY,

BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY,

BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY,

BACKGROUND_GREEN | BACKGROUND_INTENSITY,

BACKGROUND_BLUE | BACKGROUND_RED,

BACKGROUND_RED | BACKGROUND_INTENSITY

};

 

void Figure::SetRandomType()

{

m_type = (FigureType)(rand() % FigureRotateCount);

}

 

void Figure::Draw(bool black) const

{

WORD figure = Figures[m_type][m_rotate];

WORD color = (black?0:FigureColors[m_type]);

 

for (inti = 0; i< Size; i++) {

for (int j = 0; j < Size; j++) {

if ((figure & 0x8000)!= 0)

console::Write(" ", m_drawCoord.X + m_coord.X + j, m_drawCoord.Y + m_coord.Y + i, color);

 

figure <<= 1;

}

}

}

 

void Figure::Rotate()

{

FigureRotate rotate = m_rotate;

 

if (rotate!= FIGURE_ROTATE_270)

rotate = (FigureRotate)((int)rotate + 1);

else

rotate = FIGURE_ROTATE_0;

 

m_rotate = rotate;

}

 

void Figure::operator=(const Figure &figure)

{

m_type = figure.m_type;

m_rotate = figure.m_rotate;

m_coord = figure.m_coord;

m_drawCoord = figure.m_drawCoord;

}

 

TetrisGame::TetrisGame()

{

console::Init();

console::HideCursor();

}

 

void TetrisGame::Play()

{

Matrix matix;

Figure nextFigure;

Figure currentFigure;

Figure tempFigure;

bool figureExist = false;

bool changed = false;

bool superSpeed = false;

int speed = 20;

int counter = 0;

int score = 0;

constint SCORE_TABLE_SIZE = 15;

intscoreTable[SCORE_TABLE_SIZE] = {500, 1000, 1700, 2500, 3500, 5000, 8000, 12000, 18000, 25000, 34000, 42000, 55000, 75000, 100000};

 

console::ClearScreen();

 

console::DrawHLine(30, 2, 20);

console::DrawHLine(30, 23, 20);

console::DrawVLine(29, 2, 22);

console::DrawVLine(50, 2, 22);

 

console::Write("Next:", 55, 4);

 

console::Write("Score: ", score, 55, 10);

 

nextFigure.SetRandomType();

 

while (true) {

if (!figureExist) {

currentFigure = nextFigure;

 

nextFigure.Draw(true);

nextFigure.SetRandomType();

nextFigure.SetDrawXY(61, 4);

nextFigure.Draw();

 

currentFigure.SetDrawXY(30, 3);

currentFigure.SetXY(8, 0);

 

tempFigure = currentFigure;

 

figureExist = true;

superSpeed = false;

}

 

if (changed) {

tempFigure.Draw(true);

tempFigure = currentFigure;

currentFigure.Draw();

}

 

if (superSpeed || counter == speed) {

tempFigure.Draw(true);

tempFigure = currentFigure;

currentFigure.Draw();

 

currentFigure.MoveDown();

 

counter = 0;

changed = true;

}

counter++;

 

if (changed) {

changed = false;

 

if (matix.IsDown(currentFigure)) {

matix.AddFigure(currentFigure);

 

figureExist = false;

 

if (matix.IsFull()) {

console::ClearScreen();

console::Write("Game Over!", 32, 10);

console::Pause();

return;

}

 

intdelFullRows = 0;

 

while (true) {

intfullRow = matix.GetFullRow();

if (fullRow == -1)

break;

 

matix.DeleteRow(fullRow);

 

delFullRows++;

 

for (inti = fullRow - 1; i>= 1; i--)

console::MoveDown(30, 3 + i, 20);

}

 

if (delFullRows> 0) {

score += delFullRows * 100;

console::Write("Score: ", score, 55, 10);

 

for (inti = 0; i< SCORE_TABLE_SIZE; i++)

if (scoreTable[i]!= 0 && score >= scoreTable[i]) {

speed--;

 

scoreTable[i] = 0;

 

break;

}

}

}

}

 

WORD key = console::GetKeyDown();

 

if (key == VK_LEFT &&matix.CaneMoveLeft(currentFigure)) {

currentFigure.MoveLeft();

changed = true;

} else if (key == VK_RIGHT &&matix.CaneMoveRight(currentFigure)) {

currentFigure.MoveRight();

changed = true;

} else if (key == VK_UP) {

changed = true;

FigureRotaterotete = currentFigure.GetRotate();

currentFigure.Rotate();

if (!matix.CaneRotate(currentFigure)) {

currentFigure.SetRotate(rotete);

changed = false;

}

} else if (key == VK_DOWN)

superSpeed = true;

 

Sleep(20);

}

}

 

Приложение 2

(main.cpp)

#include "Tetris.h"

#include "Console.h"

 

intmain()

{

console::SetTitle("Tetris");

 

TetrisGametetrisGame;

tetrisGame.Play();

 

return 0;

}

 

Приложение 3

(Файл Tetris.h)

#ifndef TETRIS_H

#define TETRIS_H

#include <windows.h>

class Figure;

class Matrix {

public:

Matrix();

enum {

Size = 20

};

bool IsFull() const;

bool CaneMoveLeft(const Figure &figure) const;

bool CaneMoveRight(const Figure &figure) const;

bool CaneRotate(const Figure &figure) const;

bool IsDown(const Figure &figure) const;

void AddFigure(const Figure &figure);

intGetFullRow() const;

void DeleteRow(int row);

voidDebugDraw(int x, int y) const;

private:

char m_matix[Size][Size];

};

enumFigureRotate {

FIGURE_ROTATE_0 = 0,

FIGURE_ROTATE_90,

FIGURE_ROTATE_180,

FIGURE_ROTATE_270

};

class Figure {

public:

Figure();

enum {

Size = 4

};

void SetRandomType();

void SetDrawXY(int x, int y) { m_drawCoord.X = x; m_drawCoord.Y = y; };

void Draw(bool black = false) const;

void MoveDown() { m_coord.Y++; };

void MoveLeft() { m_coord.X--; };

void MoveRight() { m_coord.X++; };

 

void Rotate();

void SetRotate(FigureRotate rotate) { m_rotate = rotate; };

FigureRotateGetRotate() const { return m_rotate; }

WORD GetFigure() const { return Figures[m_type][m_rotate]; };

void SetXY(int x, int y) { m_coord.X = x; m_coord.Y = y; };

COORD GetXY() const { return m_coord; };

void operator=(const Figure &figure);

private:

enumFigureType {

FIGURE_TYPE_I = 0,

FIGURE_TYPE_J,

FIGURE_TYPE_L,

FIGURE_TYPE_O,

FIGURE_TYPE_S,

FIGURE_TYPE_T,

FIGURE_TYPE_Z,

};

enum {

FigureCount = 7,

FigureRotateCount = 4

};

static const WORD Figures[FigureCount][FigureRotateCount];

static constWORD FigureColors[FigureCount];

FigureTypem_type;

FigureRotatem_rotate;

COORD m_coord;

COORD m_drawCoord;

};

class TetrisGame {

public:

TetrisGame();

void Play();

};

#endif

Приложение 4

(Файл Console.cpp)

#include "Console.h"

namespace console {

static HANDLE g_hStdout = nullptr;

static HANDLE g_hStdinp = nullptr;

void Init()

{

g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

g_hStdinp = GetStdHandle(STD_INPUT_HANDLE);

}

 

HANDLE GetStdoutHandle()

{

return g_hStdout;

}

 

void SetTitle(const char *title)

{

SetConsoleTitle(title);

}

 

void GotoXY(int x, int y)

{

COORD coord;

coord.X = x;

coord.Y = y;

 

SetConsoleCursorPosition(g_hStdout, coord);

}

 

void ClearScreen()

{

CONSOLE_SCREEN_BUFFER_INFO csbi;

GetConsoleScreenBufferInfo(g_hStdout, &csbi);

 

COORD coord = {0, 0};

DWORD size = csbi.dwSize.X * csbi.dwSize.Y;

DWORD nw;

FillConsoleOutputCharacter(g_hStdout, ' ', size, coord, &nw);

FillConsoleOutputAttribute(g_hStdout, csbi.wAttributes, size, coord, &nw);

 

GotoXY(0, 0);

}

 

void HideCursor()

{

CONSOLE_CURSOR_INFO cursorInfo;

cursorInfo.dwSize = 1;

cursorInfo.bVisible = FALSE;

 

SetConsoleCursorInfo(g_hStdout, &cursorInfo);

}

 

void Write(const char *text, int x, int y, WORD attributes)

{

DWORD length = lstrlen(text);

COORD coord;

DWORD nw;

 

coord.X = x;

coord.Y = y;

 

WriteConsoleOutputCharacter(g_hStdout, text, length, coord, &nw);

FillConsoleOutputAttribute(g_hStdout, attributes, length, coord, &nw);

}

 

void Write(const char *text, int value, int x, int y)

{

Write(text, x, y);

 

char buffer[32];

DWORD length = lstrlen(text);

 

wsprintf(buffer, "%d", value);

Write(buffer, length + x, y);

}

 

void DrawHLine(int x, int y, int length, WORD attributes)

{

for (inti = 0; i< length; i++) {

Write(" ", x, y, attributes);

x++;

}

}

 

void DrawVLine(int x, int y, int length, WORD attributes)

{

for (inti = 0; i< length; i++) {

Write(" ", x, y, attributes);

y++;

}

}

 

void MoveDown(int x, int y, int length)

{

char chr;

WORD attribute;

DWORD n;

COORD coord1;

COORD coord2;

 

coord1.X = x;

coord1.Y = y;

 

coord2 = coord1;

 

coord2.Y++;

 

for (inti = 0; i< length; i++) {

ReadConsoleOutputCharacter(g_hStdout, &chr, 1, coord1, &n);

ReadConsoleOutputAttribute(g_hStdout, &attribute, 1, coord1, &n);

coord1.X++;

WriteConsoleOutputCharacter(g_hStdout, &chr, 1, coord2, &n);

WriteConsoleOutputAttribute(g_hStdout, &attribute, 1, coord2, &n);

coord2.X++;

}

 

DrawHLine(x, y, length, 0);

}

 

WORD GetKeyDown()

{

INPUT_RECORD input;

DWORD nr;

WORD key = 0;

 

PeekConsoleInput(g_hStdinp, &input, 1, &nr);

if (nr> 0) {

if(input.EventType == KEY_EVENT)

if(input.Event.KeyEvent.bKeyDown)

key = input.Event.KeyEvent.wVirtualKeyCode;

 

FlushConsoleInputBuffer(g_hStdinp);

}

 

return key;

}

 

void Pause()

{

while (GetKeyDown() == 0)

Sleep(100);

}

};

Приложение 5

(Файл Console.h)

#ifndef CONSOLE_H

#define CONSOLE_H

#include <windows.h>

namespace console {

void Init();

HANDLE GetStdoutHandle();

void SetTitle(const char *title);

void GotoXY(int x, int y);

void ClearScreen();

void HideCursor();

void Write(const char *text, int x, int y, WORD attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

void Write(const char *text, int value, int x, int y);

void DrawHLine(int x, int y, int length, WORD attributes = BACKGROUND_RED | BACKGROUND_GREEN);

void DrawVLine(int x, int y, int length, WORD attributes = BACKGROUND_RED | BACKGROUND_GREEN);

void MoveDown(int x, int y, int length);

WORD GetKeyDown();

void Pause();

};

#endif

 

 



Поделиться:




Поиск по сайту

©2015-2024 poisk-ru.ru
Все права принадлежать их авторам. Данный сайт не претендует на авторства, а предоставляет бесплатное использование.
Дата создания страницы: 2018-03-19 Нарушение авторских прав и Нарушение персональных данных


Поиск по сайту: