C语言课程设计俄罗斯方块源代码_第1页
C语言课程设计俄罗斯方块源代码_第2页
C语言课程设计俄罗斯方块源代码_第3页
C语言课程设计俄罗斯方块源代码_第4页
C语言课程设计俄罗斯方块源代码_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 新建新建 h 头文件 将头文件 将 头文件头文件 代码粘贴至其中 代码粘贴至其中 2 新建新建 c 源文件 将源文件 将 源代码源代码 代码粘贴到其中 代码粘贴到其中 3 新建空白工程 将头文件和源代码新建空白工程 将头文件和源代码 添加进去 调试使用 添加进去 调试使用 头文件头文件 1 自定义枚举类型 定义 7 种形态的游戏方块 typedef enum tetris shape ZShape 0 SShape LineShape TShape SquareShape LShape MirroredLShape shape 2 函数声明 1 操作方块函数 int maxX 取得当前方块的最大 x 坐标 int minX 取得当前方块的最小 x 坐标 void turn left 当前方块逆时针旋转 90 度 void turn right int out of table void transform int leftable int rightable int downable void move left void move right 2 操作游戏桌面的函数 int add to table void remove full 3 控制游戏函数 void new game void run game void next shape int random int seed 4 绘图函数 void paint void draw table 5 其他功能函数 void key down WPARAM wParam void resize void initialize void finalize 6 回调函数 用来处理 Windows 消息 LRESULT CALLBACK WndProc HWND UINT WPARAM LPARAM 源代码源代码 1 文件包含 include include include include tetris h 2 常量定义 define APP NAME TETRIS define APP TITLE Tetris Game define GAMEOVER GAME OVER define SHAPE COUNT 7 define BLOCK COUNT 4 define MAX SPEED 5 define COLUMS 10 define ROWS 20 define RED RGB 255 0 0 define YELLOW RGB 255 255 0 define GRAY RGB 128 128 128 define BLACK RGB 0 0 0 define WHITE RGB 255 255 255 define STONE RGB 192 192 192 define CHARS IN LINE 14 define SCORE SCORE 4d 3 全局变量定义 1 char score char CHARS IN LINE 0 2 char press enter Press Enter key 3 帮助提示信息 char help press space or up key to transform shape Press left or right key to mover shape Press down key to speed up Press enter key to pause game Enjoy it 0 4 枚举游戏的状态 enum game state game start game run game pause game over state game start 5 定义方块的颜色 COLORREF shape color RGB 255 0 0 RGB 0 255 0 RGB 0 0 255 RGB 255 255 0 RGB 0 255 255 RGB 255 0 255 RGB 255 255 255 6 方块的 7 中类型 int shape coordinate SHAPE COUNT BLOCK COUNT 2 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 1 0 2 1 0 0 0 1 0 0 1 0 0 1 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 0 1 0 0 0 1 7 得分 int score 0 8 下一个方块 shape next 0 9 当前方块 shape current 0 10 当前方块的每一部分坐标 int current coordinate 4 2 0 11 游戏桌面 int table ROWS COLUMS 0 12 当前方块的 x 坐标 int shapex 0 13 当前方块的 y 坐标 int shapey 0 14 方块下移速度 int speed 0 15 每一帧开始时间 clock t start 0 16 每一帧结束时间 clock t finish 0 17 windows 绘图用变量 HWND gameWND HBITMAP memBM HBITMAP memBMOld HDC memDC RECT clientRC HBRUSH blackBrush HBRUSH stoneBrush HBRUSH shapeBrush SHAPE COUNT HPEN grayPen HFONT bigFont HFONT smallFont 4 主要处理函数 1 取最大坐标 int maxX int i 0 int x current coordinate i 0 int m x for i 1 i BLOCK COUNT i x current coordinate i 0 if m x m x return m 2 取最小坐标 int minX int i 0 int x current coordinate i 0 int m x for i 1 ix m x return m 3 逆时针转动方块 void turn left int i 0 int x y for i 0 i 4 i x current coordinate i 0 y current coordinate i 1 current coordinate i 0 y current coordinate i 1 x 4 顺时针旋转方块 void turn right int i 0 int x y for i 0 i 4 i x current coordinate i 0 y current coordinate i 1 current coordinate i 0 y current coordinate i 1 x 5 检查方块是否越界 int out of table int i 0 int x y for i 0 i 4 i x shapex current coordinate i 0 y shapey current coordinate i 1 if x COLUMS 1 y ROWS 1 return 1 if table y x return 1 return 0 6 旋转方块 void transform if current SquareShape return turn right if out of table turn left 7 判断方块是否向左移动 int leftable int i 0 int x y for i 0 i 4 i x shapex current coordinate i 0 y shapey current coordinate i 1 if x 0 table y x 1 1 return 0 return 1 8 判断方块是否向右移动 int rightable int i 0 int x y for i 0 i COLUMS 1 table y x 1 1 return 0 return 1 9 判断方块是否向下移动 int downable int i 0 int x y for i 0 i ROWS 1 table y 1 x 1 return 0 return 1 10 向左移动当前方块 void move left if leftable shapex 11 向右移动当前方块 void move right if rightable shapex 12 向下移动当前方块 void move down if downable shapey else if add to table remove full next shape else state game over 13 将当前方块固定到桌面上 int add to table int i 0 int x y for i 0 i 4 i x shapex current coordinate i 0 y shapey current coordinate i 1 if y0 i c 0 for j 0 j MAX SPEED speed 100 move down start clock InvalidateRect gameWND NULL TRUE 17 操作当前方块 void next shape current next memcpy current coordinate shape coordinate next sizeof int BLOCK COUNT 2 shapex COLUMS maxX current minX current 2 shapey 0 next random SHAPE COUNT 18 取随机数 int random int seed if seed 0 return 0 srand unsigned time NULL return rand seed 19 绘图 void paint PAINTSTRUCT ps HDC hdc draw table hdc BeginPaint gameWND BitBlt hdc clientRC left clientRC top clientRC right clientRC bottom memDC 0 0 SRCCOPY EndPaint gameWND 20 绘制游戏桌面 void draw table HBRUSH hBrushOld HPEN hPenOld HFONT hFontOld RECT rc int x0 y0 w int x y i j char str w clientRC bottom ROWS 2 x0 y0 w FillRect memDC 如果游戏是开始或结束状态 if state game start state game over memcpy rc bottom rc bottom 2 hFontOld SelectObject memDC bigFont SetBkColor memDC BLACK 如果游戏是开始状态 用黄色字显示游戏开始画面 if state game start str APP TITLE SetTextColor memDC YELLOW 如果游戏是结束状态 用红色字显示 GAME OVER else str GAMEOVER SetTextColor memDC RED DrawText memDC str strlen str SelectObject memDC hFontOld hFontOld SelectObject memDC smallFont rc top rc bottom rc bottom rc bottom 2 if state game over SetTextColor memDC YELLOW sprintf score char SCORE score DrawText memDC score char strlen score char SetTextColor memDC STONE DrawText memDC press enter strlen press enter SelectObject memDC hFontOld return 桌面上残留的方块 hBrushOld SelectObject memDC stoneBrush for i 0 i ROWS i for j 0 j COLUMS j if table i j 1 x x0 j w y y0 i w Rectangle memDC x y x w 1 y w 1 SelectObject memDC hBrushOld 画当前的方块 hBrushOld SelectObject memDC shapeBrush current for i 0 i 4 i x x0 current coordinate i 0 shapex w y y0 current coordinate i 1 shapey w if x x0 y y0 continue Rectangle memDC x y x w 1 y w 1 SelectObject memDC hBrushOld 画桌面上的表格线 hPenOld SelectObject memDC grayPen for i 0 i ROWS i MoveToEx memDC x0 y0 i w NULL LineTo memDC x0 COLUMS w y0 i w for i 0 i COLUMS i MoveToEx memDC x0 i w y0 NULL LineTo memDC x0 i w y0 ROWS w SelectObject memDC hPenOld 画玩家得分 x0 x0 COLUMS w 3 w y0 y0 w hFontOld SelectObject memDC smallFont SetTextColor memDC YELLOW sprintf score char SCORE score TextOut memDC x0 y0 score char strlen score char 画下一个方块 y0 w SetTextColor memDC STONE TextOut memDC x0 y0 NEXT 4 x0 w y0 2 w hBrushOld SelectObject memDC shapeBrush next for i 0 i 4 i x x0 shape coordinate next i 0 w y y0 shape coordinate next i 1 w Rectangle memDC x y x w 1 y w 1 SelectObject memDC hBrushOld 打印帮助信息 x0 COLUMS 2 w y0 4 w SetTextColor memDC GRAY i 0 while help i TextOut memDC x0 y0 help i strlen help i y0 w i SelectObject memDC hFontOld 21 处理按键 void key down WPARAM wParam 如果游戏不是运行状态 按下回车键 if state game run if wParam VK RETURN switch state case game start next shape state game run break case game pause state game run break case game over new game next shape state game run break 如果游戏状态是运行 else switch wParam case VK SPACE case VK UP transform break case VK LEFT move left break case VK RIGHT move right break case VK DOWN move down break case VK RETURN state game pause break InvalidateRect gameWND NULL TRUE 22 改变窗口大小 void resize HDC hdc LOGFONT lf hdc GetDC gameWND GetClientRect gameWND SelectObject memDC memBMOld DeleteObject memBM memBM CreateCompatibleBitmap hdc clientRC right clientRC bottom memBMOld SelectObject memDC memBM DeleteObject bigFont memset lf lfWidth clientRC right clientRC left CHARS IN LINE lf lfHeight clientRC bottom clientRC top 4 lf lfItalic 1 lf lfWeight FW BOLD bigFont CreateFontIndirect DeleteObject smallFont lf lfHeight clientRC bottom ROWS 2 lf lfWidth lf lfHeight 2 lf lfItalic 0 lf lfWeight FW NORMAL smallFont CreateFontIndirect ReleaseDC gameWND hdc 23 处理消息 LRESULT CALLBACK WndProc HWND hwnd UINT message WPARAM wParam LPARAM lParam switch message case WM SIZE resize return 0 case WM ERASEBKGND return 0 case WM PAINT paint return 0 case WM KEYDOWN key down wParam return 0 case WM DESTROY PostQuitMessage 0 return 0 其他消息用 Windows 默认的消息处理函数处理 return DefWindowProc hwnd message wParam lParam 24 初始化 void initialize LOGFONT lf HDC hdc int i hdc GetDC gameWND GetClientRect gameWND memDC CreateCompatibleDC hdc memBM CreateCompatibleBitmap hdc clientRC right clientRC bottom memBMOld SelectObject memDC memBM blackBrush CreateSolidBrush BLACK stoneBrush CreateSolidBrush STONE 创建每个方块对应颜色的画笔 for i 0 i SHAPE COUNT i shapeBrush i CreateSolidBrush shape color i grayPen CreatePen PS SOLID 1 GRAY memset 创建一个大字体 lf lfWidth clientRC right clientRC left CHARS IN LINE lf lfHeight clientRC bottom clientRC top 4 lf lfItalic 0 lf lfWeight FW NORMAL smallFont CreateFontIndirect ReleaseDC gameWND hdc 25 释放资源 void finalize int i 0 DeleteObject blackBrush DeleteObject stoneBrush for i 0 i SHAPE COUNT i DeleteObject shapeBrush i DeleteObject g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论