Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <time.h>
- #include <sstream>
- using namespace std;
- const int FIELD_WIDTH = 10;
- const int FIELD_HEIGHT = 24;
- const int FIELD_HIDDEN = 4;
- const int SQUARE_SIZE = 30;
- const int WINDOW_WIDTH = 440;
- const int WINDOW_HEIGHT = 600;
- const int COLORS = 4;
- const int BLOCKS = 5;
- char map[FIELD_HEIGHT][FIELD_WIDTH];
- int nextBlock[4][4];
- int currentBlock[4][4];
- int currentX, currentY;
- int score = 0;
- const int colors[COLORS + 1][3] = {
- { 220, 220, 220 },
- { 220, 0, 0 },
- { 0, 150, 0 },
- { 0, 0, 220 },
- { 175, 175, 0 }
- };
- const bool blocks[BLOCKS][4][4] = {
- {
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 }
- },
- {
- { 1, 0, 0, 0 },
- { 1, 0, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 }
- },
- {
- { 0, 1, 0, 0 },
- { 0, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 }
- },
- {
- { 0, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 1, 0, 0, 0 },
- { 0, 0, 0, 0 }
- },
- {
- { 1, 1, 0, 0 },
- { 1, 1, 0, 0 },
- { 0, 0, 0, 0 },
- { 0, 0, 0, 0 }
- },
- };
- void init(HWND);
- void update(HWND);
- void draw(HDC);
- void moveDown(HWND);
- void rotateCurrent();
- void moveLeft();
- void moveRight();
- void drawRect(HDC hdc, int x, int y, int width, int height, int r, int g, int b) {
- RECT rect = { x, y, x + width, y + height};
- HBRUSH brush = CreateSolidBrush(RGB(r, g, b));
- FillRect(hdc, &rect, brush);
- DeleteObject(brush);
- }
- void redraw(HWND hWnd) {
- RECT rect;
- GetClientRect(hWnd, &rect);
- InvalidateRect(hWnd, &rect, true);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
- PAINTSTRUCT ps;
- HDC hdc;
- HDC backBufferDC;
- HBITMAP backBuffer;
- HBITMAP realBackBuffer;
- int savedDC;
- switch (message) {
- case WM_ERASEBKGND:
- return 1;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- backBufferDC = CreateCompatibleDC(hdc);
- backBuffer = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
- SelectObject(backBufferDC, backBuffer);
- draw(backBufferDC);
- BitBlt(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, backBufferDC, 0, 0, SRCCOPY);
- SelectObject(backBufferDC, backBuffer);
- DeleteObject(backBuffer);
- DeleteDC(backBufferDC);
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- if (wParam == 37) {
- moveLeft();
- redraw(hWnd);
- } else if (wParam == 39) {
- moveRight();
- redraw(hWnd);
- } else if (wParam == 40) {
- moveDown(hWnd);
- redraw(hWnd);
- } else if (wParam == 32) {
- rotateCurrent();
- redraw(hWnd);
- }
- break;
- case WM_TIMER:
- update(hWnd);
- redraw(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- }
- //int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nCmdShow) {
- int main() {
- WNDCLASSEX wcex = { 0 };
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = (HINSTANCE)GetModuleHandle(NULL);
- wcex.hIcon = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = "snakesnake";
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
- if (!RegisterClassEx(&wcex)) {
- return 1;
- }
- RECT client = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
- AdjustWindowRectEx(&client, WS_OVERLAPPEDWINDOW, false, NULL);
- HWND hWnd = CreateWindowEx(NULL, "snakesnake", "Snake", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, -client.left + client.right, -client.top + client.bottom, NULL, NULL, wcex.hInstance, NULL);
- if (!hWnd) {
- return 1;
- }
- ShowWindow(hWnd, SW_SHOW);
- UpdateWindow(hWnd);
- init(hWnd);
- MSG msg;
- while (1) {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
- if (msg.message == WM_QUIT) break;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return msg.wParam;
- }
- void cleanNext() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- nextBlock[i][j] = 0;
- }
- }
- }
- void generateNextBlock() {
- int nextBlockID = rand() % BLOCKS;
- char nextColor = rand() % COLORS + 1;
- cleanNext();
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- nextBlock[i][j] = blocks[nextBlockID][i][j] ? nextColor : 0;
- }
- }
- }
- void setCurrentBlock() {
- currentX = FIELD_WIDTH / 2 - 1;
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- currentBlock[i][j] = nextBlock[i][j];
- }
- }
- int size = 0;
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 4; j++)
- if (currentBlock[i][j])
- size = max(max(size, i), j);
- currentY = 3 - size;
- }
- void placeCurrentBlock(HWND hWnd) {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (currentBlock[i][j]) {
- map[currentY + i][currentX + j] = currentBlock[i][j];
- if (currentY + i < FIELD_HIDDEN) {
- init(hWnd);
- return;
- }
- }
- }
- }
- }
- void init(HWND hWnd) {
- srand(time(NULL));
- KillTimer(hWnd, 3001);
- SetTimer(hWnd, 3001, 260 - (int)sqrt(score / 100.0), NULL);
- score = 0;
- for (int i = 0; i < FIELD_HEIGHT; i++) {
- for (int j = 0; j < FIELD_WIDTH; j++) {
- map[i][j] = 0;
- }
- }
- generateNextBlock();
- setCurrentBlock();
- generateNextBlock();
- }
- void moveDown(HWND hWnd) {
- currentY++;
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (currentBlock[i][j]) {
- if (currentY + i == FIELD_HEIGHT || map[currentY + i][currentX + j]) {
- currentY--;
- placeCurrentBlock(hWnd);
- setCurrentBlock();
- generateNextBlock();
- return;
- }
- }
- }
- }
- }
- void moveLeft() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (currentBlock[i][j]) {
- if (currentX - 1 + j < 0 || map[currentY + i][currentX - 1 + j]) {
- return;
- }
- }
- }
- }
- currentX--;
- }
- void moveRight() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (currentBlock[i][j]) {
- if (currentX + 1 + j >= FIELD_WIDTH || map[currentY + i][currentX + 1 + j]) {
- return;
- }
- }
- }
- }
- currentX++;
- }
- void rotateCurrent() {
- int size = 0;
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 4; j++)
- if (currentBlock[i][j])
- size = max(max(size, i), j);
- char newBlock[4][4];
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 4; j++)
- newBlock[i][j] = 0;
- for (int i = 0; i <= size; i++)
- for (int j = 0; j <= size; j++) {
- newBlock[i][j] = currentBlock[size - j][i];
- if (newBlock[i][j]) {
- if (currentX + j >= FIELD_WIDTH || currentX + j < 0 || currentY >= FIELD_HEIGHT || map[currentY + i][currentX + j]) {
- return;
- }
- }
- }
- for (int i = 0; i <= size; i++)
- for (int j = 0; j <= size; j++)
- currentBlock[i][j] = newBlock[i][j];
- }
- void checkForLine(HWND hWnd) {
- for (int i = FIELD_HIDDEN; i < FIELD_HEIGHT; i++) {
- for (int j = 0; j < FIELD_WIDTH; j++) {
- if (map[i][j] == 0) {
- goto away;
- }
- }
- for (int j = i; j > 0; j--) {
- for (int k = 0; k < FIELD_WIDTH; k++) {
- map[j][k] = map[j - 1][k];
- map[j - 1][k] = 0;
- }
- }
- score += 100;
- KillTimer(hWnd, 3001);
- SetTimer(hWnd, 3001, 260 - (int)sqrt(score / 50.0), NULL);
- away: continue;
- }
- }
- void update(HWND hWnd) {
- moveDown(hWnd);
- checkForLine(hWnd);
- }
- void draw(HDC hdc) {
- drawRect(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 150, 150, 150);
- for (int i = FIELD_HIDDEN; i < FIELD_HEIGHT; i++) {
- for (int j = 0; j < FIELD_WIDTH; j++) {
- drawRect(hdc, j * SQUARE_SIZE + 1, (i - FIELD_HIDDEN) * SQUARE_SIZE + 1, SQUARE_SIZE - 2, SQUARE_SIZE - 2, colors[map[i][j]][0], colors[map[i][j]][1], colors[map[i][j]][2]);
- }
- }
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- drawRect(hdc, FIELD_WIDTH * SQUARE_SIZE + 10 + j * SQUARE_SIZE + 1, (i + 1) * SQUARE_SIZE + 1, SQUARE_SIZE - 2, SQUARE_SIZE - 2, colors[nextBlock[i][j]][0], colors[nextBlock[i][j]][1], colors[nextBlock[i][j]][2]);
- }
- }
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (currentY - FIELD_HIDDEN + i < 0) continue;
- if (currentBlock[i][j] == 0) continue;
- drawRect(hdc, (currentX + j) * SQUARE_SIZE + 1, (currentY - FIELD_HIDDEN + i) * SQUARE_SIZE + 1, SQUARE_SIZE - 2, SQUARE_SIZE - 2, colors[currentBlock[i][j]][0], colors[currentBlock[i][j]][1], colors[currentBlock[i][j]][2]);
- }
- }
- HFONT font = CreateFont(-20, 0, 0, 0, 0, ANSI_CHARSET, 0U, 0U, 0U, 0U, 0U, 0U, 0U, TEXT("Arial"));
- HGDIOBJ obj = SelectObject(hdc, font);
- SetTextColor(hdc, 0);
- SetBkMode(hdc, TRANSPARENT);
- RECT rect = { FIELD_WIDTH * SQUARE_SIZE + 10, 5 * SQUARE_SIZE + 10, FIELD_WIDTH * SQUARE_SIZE + 10 + WINDOW_WIDTH - (FIELD_WIDTH * SQUARE_SIZE + 10), WINDOW_HEIGHT };
- ostringstream o;
- o << "SCORE: " << score;
- string str = "";
- str += o.str();
- DrawText(hdc, str.c_str(), str.length(), &rect, 0);
- //RECT rect1 = { FIELD_WIDTH * SQUARE_SIZE + 10, 5 * SQUARE_SIZE + 35, FIELD_WIDTH * SQUARE_SIZE + 10 + WINDOW_WIDTH - (FIELD_WIDTH * SQUARE_SIZE + 10), WINDOW_HEIGHT };
- //str = "";
- //str += (int)sqrt(score / 50.0);
- //DrawText(hdc, str.c_str(), str.length(), &rect1, 0);
- SelectObject(hdc, obj);
- DeleteObject(font);
- }
Advertisement
Add Comment
Please, Sign In to add comment