commit b0c29d4a24959b4ac2a0e758b20f0e028b68713f Author: Namesq <2728290997@qq.com> Date: Thu Apr 23 18:26:00 2026 +0800 调整到vscode兼容 初始化项目 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4eaad64 --- /dev/null +++ b/.gitignore @@ -0,0 +1,49 @@ +# Build outputs +/.vscode-build/ +/build/ +/bin/ +/obj/ +/out/ +/dist/ + +# Executables and libraries +*.exe +*.dll +*.lib +*.a +*.so +*.dylib + +# Object and intermediate files +*.o +*.obj +*.pch +*.pdb +*.ilk +*.idb +*.exp +*.res + +# Logs and runtime dumps +*.log +*.tmp +*.temp +*.stackdump + +# IDE and editor user files +.idea/ +*.suo +*.user +*.userossc +*.vcxproj.user +*.code-workspace + +# VS Code: keep shared project settings, ignore local history/cache +.vscode/*.log +.vscode/*.tmp +.vscode/.ropeproject/ + +# OS files +.DS_Store +Thumbs.db +Desktop.ini diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..0ff9cc1 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "version": 4, + "configurations": [ + { + "name": "MinGW", + "compilerPath": "g++.exe", + "intelliSenseMode": "windows-gcc-x64", + "cppStandard": "c++17", + "includePath": [ + "${workspaceFolder}\\src" + ], + "defines": [ + "UNICODE", + "_UNICODE", + "_WINDOWS" + ] + } + ] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ba6ab1d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug Tetris MinGW", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}\\.vscode-build\\mingw\\Tetris.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}\\src", + "environment": [], + "externalConsole": true, + "MIMode": "gdb", + "miDebuggerPath": "gdb.exe", + "preLaunchTask": "build Tetris MinGW" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..79079dc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "C_Cpp.default.compilerPath": "g++.exe", + "C_Cpp.default.cppStandard": "c++17", + "C_Cpp.default.intelliSenseMode": "windows-gcc-x64", + "files.associations": { + "*.rc": "cpp", + "tchar.h": "cpp", + "windows.h": "cpp" + } +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..b35f273 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,37 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build Tetris MinGW", + "type": "shell", + "command": "powershell", + "args": [ + "-NoProfile", + "-ExecutionPolicy", + "Bypass", + "-File", + "${workspaceFolder}\\build-mingw.ps1" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": "$gcc" + }, + { + "label": "run Tetris MinGW", + "type": "shell", + "command": "powershell", + "args": [ + "-NoProfile", + "-ExecutionPolicy", + "Bypass", + "-File", + "${workspaceFolder}\\build-mingw.ps1", + "-Run" + ], + "group": "test", + "problemMatcher": "$gcc" + } + ] +} diff --git a/VSCode运行说明.md b/VSCode运行说明.md new file mode 100644 index 0000000..521ef2e --- /dev/null +++ b/VSCode运行说明.md @@ -0,0 +1,13 @@ +# VS Code 运行说明 + +1. 用 VS Code 打开本文件所在文件夹: `C:\Users\27282\Desktop\src_学生版` +2. 安装 VS Code 扩展: `C/C++`,发布者是 `Microsoft` +3. 按 `Ctrl+Shift+B` 编译 +4. 按 `F5` 调试运行,或在命令面板执行 `Tasks: Run Task` -> `run Tetris MinGW` + +说明: + +- 默认配置优先使用系统 `PATH` 中的 `g++.exe` / `gdb.exe`,也兼容 `C:\mingw64\bin\g++.exe`。 +- 源码目录是 `src`,资源目录是 `assets`。 +- MinGW 构建输出文件在 `.vscode-build\mingw\Tetris.exe`。 +- 由于原项目的 `Tetris.rc` 是 UTF-16,MinGW 暂不编译资源文件;这只影响图标/菜单资源,不影响源码编译运行。 diff --git a/assets/Tetris.ico b/assets/Tetris.ico new file mode 100644 index 0000000..d551aa3 Binary files /dev/null and b/assets/Tetris.ico differ diff --git a/assets/background.bmp b/assets/background.bmp new file mode 100644 index 0000000..716c17a Binary files /dev/null and b/assets/background.bmp differ diff --git a/assets/background.wav b/assets/background.wav new file mode 100644 index 0000000..6c7588a Binary files /dev/null and b/assets/background.wav differ diff --git a/assets/small.ico b/assets/small.ico new file mode 100644 index 0000000..d551aa3 Binary files /dev/null and b/assets/small.ico differ diff --git a/build-mingw.ps1 b/build-mingw.ps1 new file mode 100644 index 0000000..886eba3 --- /dev/null +++ b/build-mingw.ps1 @@ -0,0 +1,66 @@ +param( + [switch]$Run +) + +$ErrorActionPreference = "Stop" + +$Root = Split-Path -Parent $MyInvocation.MyCommand.Path +$ProjectDir = Join-Path $Root "src" +$BuildDir = Join-Path $Root ".vscode-build\mingw" +$ExePath = Join-Path $BuildDir "Tetris.exe" + +$GxxCandidates = @( + "g++.exe", + "C:\mingw64\bin\g++.exe" +) + +$Gxx = $null +foreach ($Candidate in $GxxCandidates) { + try { + $Resolved = Get-Command $Candidate -ErrorAction Stop + $Gxx = $Resolved.Source + break + } catch { + if (Test-Path $Candidate) { + $Gxx = $Candidate + break + } + } +} + +if (-not $Gxx) { + throw "g++.exe not found. Add MinGW to PATH or install it at C:\mingw64\bin\g++.exe." +} + +New-Item -ItemType Directory -Force -Path $BuildDir | Out-Null + +$Sources = @( + (Join-Path $ProjectDir "stdafx.cpp"), + (Join-Path $ProjectDir "Tetris.cpp"), + (Join-Path $ProjectDir "TetrisLogic.cpp"), + (Join-Path $ProjectDir "TetrisRender.cpp") +) + +& $Gxx ` + -std=c++17 ` + -g ` + -O0 ` + -municode ` + -mwindows ` + -DUNICODE ` + -D_UNICODE ` + -D_WINDOWS ` + -I $ProjectDir ` + @Sources ` + -lwinmm ` + -lgdi32 ` + -luser32 ` + -o $ExePath + +if ($LASTEXITCODE -ne 0) { + exit $LASTEXITCODE +} + +if ($Run) { + Start-Process -FilePath $ExePath -WorkingDirectory $ProjectDir +} diff --git a/src/Tetris.cpp b/src/Tetris.cpp new file mode 100644 index 0000000..538f195 --- /dev/null +++ b/src/Tetris.cpp @@ -0,0 +1,57 @@ +// Tetris.cpp : 程序入口、窗口初始化与消息处理 + +#include "stdafx.h" +#include "Tetris.h" + +#define MAX_LOADSTRING 100 + +HINSTANCE hInst; +TCHAR szTitle[MAX_LOADSTRING]; +TCHAR szWindowClass[MAX_LOADSTRING]; + +ATOM MyRegisterClass(HINSTANCE hInstance); +BOOL InitInstance(HINSTANCE, int); +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); + +int APIENTRY _tWinMain(HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPTSTR lpCmdLine, + int nCmdShow) +{ + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine); + // TODO(作业1): 补全 Win32 程序入口函数。 + return 0; +} + +ATOM MyRegisterClass(HINSTANCE hInstance) +{ + // TODO(作业2): 注册窗口类。 + UNREFERENCED_PARAMETER(hInstance); + return 0; +} + +BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) +{ + // TODO(作业3): 创建并显示主窗口。 + UNREFERENCED_PARAMETER(hInstance); + UNREFERENCED_PARAMETER(nCmdShow); + return FALSE; +} + +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + // TODO(作业4): 补全主窗口消息处理函数。 + return DefWindowProc(hWnd, message, wParam, lParam); +} + +INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) +{ + UNREFERENCED_PARAMETER(lParam); + // TODO(作业5): 补全“关于”对话框的消息处理。 + UNREFERENCED_PARAMETER(hDlg); + UNREFERENCED_PARAMETER(message); + UNREFERENCED_PARAMETER(wParam); + return (INT_PTR)FALSE; +} diff --git a/src/Tetris.h b/src/Tetris.h new file mode 100644 index 0000000..2a157b9 --- /dev/null +++ b/src/Tetris.h @@ -0,0 +1,52 @@ +#pragma once + +#include "resource.h" +#include "stdafx.h" +#include + +#pragma comment(lib, "winmm.lib") + +constexpr int GRID = 30; +constexpr int nGameWidth = 10; +constexpr int nGameHeight = 20; +constexpr int nWidth = 16; +constexpr int nHeight = 22; + +// 定义一个点,用来表示方块的位置 +struct Point +{ + int x; + int y; +}; + +extern int nType; // 下一方块类型 +extern int type; // 当前方块类型 +extern int state; // 当前方块状态 +extern int tScore; // 当前得分 +extern bool gameOverFlag; // 游戏已经结束 +extern bool suspendFlag; // 暂停游戏 +extern bool targetFlag; // 启用瞄准器 +extern int workRegion[20][10]; // 工作区数据,0 表示该位置没有被占用 +extern Point point; // 当前方块的当前位置 +extern Point target; // 当前方块目标位置,用于瞄准器 +extern int bricks[7][4][4][4]; +extern COLORREF BrickColor[7]; + +// 游戏逻辑相关函数 +bool CanMoveDown(); +bool CanMoveLeft(); +bool CanMoveRight(); +void MoveDown(); +void MoveLeft(); +void MoveRight(); +void Rotate(); +void DropDown(); +void Fixing(); +void DeleteOneLine(int number); +void DeleteLines(); +bool GameOver(); +void ComputeTarget(); +void Restart(); + +// 绘图函数 +void TDrawScreen(HDC hdc, HWND hWnd); diff --git a/src/Tetris.rc b/src/Tetris.rc new file mode 100644 index 0000000..80e9053 Binary files /dev/null and b/src/Tetris.rc differ diff --git a/src/TetrisLogic.cpp b/src/TetrisLogic.cpp new file mode 100644 index 0000000..a227cac --- /dev/null +++ b/src/TetrisLogic.cpp @@ -0,0 +1,148 @@ +#include "stdafx.h" +#include "Tetris.h" + +int nType = 0; +int type = 0; +int state = 0; +int tScore = 0; +bool gameOverFlag = false; +bool suspendFlag = false; +bool targetFlag = false; +int workRegion[20][10] = { 0 }; +Point point = { 0, 0 }; +Point target = { 0, 0 }; + +int bricks[7][4][4][4] = +{ + { + {{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}}, + {{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}}, + {{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}}, + {{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}} + }, + { + {{0, 0, 0, 0}, {0, 0, 2, 0}, {0, 2, 2, 2}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {0, 0, 2, 0}, {0, 0, 2, 2}, {0, 0, 2, 0}}, + {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 2, 2, 2}, {0, 0, 2, 0}}, + {{0, 0, 0, 0}, {0, 0, 2, 0}, {0, 2, 2, 0}, {0, 0, 2, 0}} + }, + { + {{0, 3, 0, 0}, {0, 3, 0, 0}, {0, 3, 3, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {3, 3, 3, 0}, {3, 0, 0, 0}, {0, 0, 0, 0}}, + {{3, 3, 0, 0}, {0, 3, 0, 0}, {0, 3, 0, 0}, {0, 0, 0, 0}}, + {{0, 0, 3, 0}, {3, 3, 3, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} + }, + { + {{0, 0, 4, 0}, {0, 0, 4, 0}, {0, 4, 4, 0}, {0, 0, 0, 0}}, + {{0, 4, 0, 0}, {0, 4, 4, 4}, {0, 0, 0, 0}, {0, 0, 0, 0}}, + {{0, 0, 4, 4}, {0, 0, 4, 0}, {0, 0, 4, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {0, 4, 4, 4}, {0, 0, 0, 4}, {0, 0, 0, 0}} + }, + { + {{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {0, 5, 5, 0}, {0, 5, 5, 0}, {0, 0, 0, 0}} + }, + { + {{0, 6, 0, 0}, {0, 6, 6, 0}, {0, 0, 6, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {0, 6, 6, 0}, {6, 6, 0, 0}, {0, 0, 0, 0}}, + {{6, 0, 0, 0}, {6, 6, 0, 0}, {0, 6, 0, 0}, {0, 0, 0, 0}}, + {{0, 6, 6, 0}, {6, 6, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} + }, + { + {{0, 0, 7, 0}, {0, 7, 7, 0}, {0, 7, 0, 0}, {0, 0, 0, 0}}, + {{0, 7, 7, 0}, {0, 0, 7, 7}, {0, 0, 0, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 7}, {0, 0, 7, 7}, {0, 0, 7, 0}, {0, 0, 0, 0}}, + {{0, 0, 0, 0}, {0, 7, 7, 0}, {0, 0, 7, 7}, {0, 0, 0, 0}} + } +}; + +COLORREF BrickColor[7] = +{ + RGB(255, 0, 0), + RGB(0, 255, 0), + RGB(0, 0, 255), + RGB(0, 255, 255), + RGB(255, 150, 0), + RGB(255, 255, 0), + RGB(168, 0, 168) +}; + +bool CanMoveDown() +{ + // TODO(作业6): 判断当前方块是否可以继续下落。 + return true; +} + +bool CanMoveLeft() +{ + // TODO(作业7): 判断当前方块是否可以向左移动。 + return true; +} + +bool CanMoveRight() +{ + // TODO(作业8): 判断当前方块是否可以向右移动。 + return true; +} + +void MoveDown() +{ + // TODO(作业9): 方块下移一格。 + point.y = point.y; +} + +void MoveLeft() +{ + // TODO(作业10): 方块左移一格。 + point.x = point.x; +} + +void MoveRight() +{ + // TODO(作业11): 方块右移一格。 + point.x = point.x; +} + +void Rotate() +{ + // TODO(作业12): 旋转当前方块。 +} + +void DropDown() +{ + // TODO(作业13): 实现“一键到底”。 +} + +void Fixing() +{ + // TODO(作业14): 将当前方块固定到工作区,并生成下一个方块。 +} + +void DeleteOneLine(int number) +{ + // TODO(作业15): 删除指定行,并让其上方所有行整体下移。 + UNREFERENCED_PARAMETER(number); +} + +void DeleteLines() +{ + // TODO(作业16): 完成消行与计分逻辑。 +} + +bool GameOver() +{ + // TODO(作业17): 判断游戏是否结束。 + return false; +} + +void ComputeTarget() +{ + // TODO(作业18): 计算瞄准器的预测落点。 +} + +void Restart() +{ + // TODO(作业19): 重置游戏状态。 +} diff --git a/src/TetrisRender.cpp b/src/TetrisRender.cpp new file mode 100644 index 0000000..163acf6 --- /dev/null +++ b/src/TetrisRender.cpp @@ -0,0 +1,9 @@ +#include "stdafx.h" +#include "Tetris.h" + +void TDrawScreen(HDC hdc, HWND hWnd) +{ + // TODO(作业20): 完整绘制游戏界面。 + UNREFERENCED_PARAMETER(hdc); + UNREFERENCED_PARAMETER(hWnd); +} diff --git a/src/resource.h b/src/resource.h new file mode 100644 index 0000000..6eef328 --- /dev/null +++ b/src/resource.h @@ -0,0 +1,32 @@ +#pragma once + +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ 生成的包含文件。 +// 供 Tetris.rc 使用 +// + +#define IDS_APP_TITLE 103 + +#define IDR_MAINFRAME 128 +#define IDD_TETRIS_DIALOG 102 +#define IDD_ABOUTBOX 103 +#define IDM_ABOUT 104 +#define IDM_EXIT 105 +#define IDI_TETRIS 107 +#define IDI_SMALL 108 +#define IDC_TETRIS 109 +#define IDC_MYICON 2 + +#ifndef IDC_STATIC +#define IDC_STATIC -1 +#endif + +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NO_MFC 130 +#define _APS_NEXT_RESOURCE_VALUE 129 +#define _APS_NEXT_COMMAND_VALUE 32771 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 110 +#endif +#endif diff --git a/src/stdafx.cpp b/src/stdafx.cpp new file mode 100644 index 0000000..25ea8d8 --- /dev/null +++ b/src/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : 只包括标准包含文件的源文件 +// Tetris.pch 将作为预编译头 +// stdafx.obj 将包含预编译类型信息 + +#include "stdafx.h" + +// TODO: 在 STDAFX.H 中 +// 引用任何所需的附加头文件,而不是在此文件中引用 diff --git a/src/stdafx.h b/src/stdafx.h new file mode 100644 index 0000000..d85f9f5 --- /dev/null +++ b/src/stdafx.h @@ -0,0 +1,22 @@ +// stdafx.h : 标准系统包含文件的包含文件, +// 或是经常使用但不常更改的 +// 特定于项目的包含文件 +// + +#pragma once + +#include "targetver.h" + +#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息 +// Windows 头文件: +#include + +// C 运行时头文件 +#include +#include +#include +#include +#include + + +// TODO: 在此处引用程序需要的其他头文件 diff --git a/src/targetver.h b/src/targetver.h new file mode 100644 index 0000000..6347f6d --- /dev/null +++ b/src/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。 + +// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将 +// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。 + +#include