




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、.課 程 設 計 報 告 課程名稱:面向對象程序設計C+ 設計題目: 俄羅斯方塊 專 業:計算機科學與技術 姓 名: 學 號: 指導教師:李 曉 虹2016 年 1 月 10 日課程設計報告模塊內容:(1) 系統需求分析利用C+實現方塊方塊控制、顯示更新、游戲記分、游戲等級以及方塊控制,直接由玩家通過鍵盤控制,游戲區域的方塊部件根據玩家具體鍵盤的操作左右移動、旋轉、加速下落。(2) 總體設計屏幕中央有一個矩形“容器”,選擇“開始”菜單或“開始”工具按鈕后,俄羅斯方塊的部件隨機產生并在容器中自由下落。(3) 詳細設計1.游戲過程中,當在矩形“容器”中,出現一個游戲方塊時,必須在游戲方塊的預覽區域
2、中出現下一方塊,這樣利于游戲玩家控制游戲的策略。2.有七種標準俄羅斯方塊部件,通過各種判斷,實現游戲方塊并隨著鍵盤上的上鍵順時針旋轉90度,隨著下鍵加速下落,隨著左、右各鍵分別左、右移動。3.當部件下落達“容器”底部或已停止的部件時,停止下落;當同一行部件完整拼接上時,該行消失,其他行向下移動,在適當位置顯示當前累計分數。4.當部件總行數超過矩形容器的高度或沒有足夠的空間產生新的部件時,提示“游戲結束”信息并停止游戲運行。5.游戲計分原則:一次消去一行加個五角星,方塊下落的速度會越來越快。(4) 系統調試#include<windows.h>#include<time.h&g
3、t;#include<conio.h>#include<stdio.h>#include<stdlib.h>const int back = 30;/const,限定變量不允許被改變,back表示背景顏色const int speed = 0, 12, 9, 6, 3, 1;const int number = 0, 2, 3, 5, 7, 11, 15;const int col = 11, 15, 12, 12, 13, 13, 14;int rank = 1;int level = 0; /0表示既不是向右也不是向左,1表示向左,2表示向右int ki
4、nd;int pri_kind; / 7大類,顏色標記,調用colcol_kind;int revolve = 0; / 旋轉狀態標記struct int x; int y; current, before;struct/定義一個結構體 內容包括坐標,顏色,是否顯示方塊 int x; int y; int color; bool having;/布爾型變量 作邏輯判斷 運算結果為真或為假,有方塊為真,沒有方塊為假 board1525;struct int vary_x4; int vary_y4; vary = 0, 2, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1
5、, -2, -3, 0, 2, 2, 0, 0, 0, -1, -1, 0, -2, -2, -4, 0 , 0, -1, -1, 0, 0, 2, 2, 0, -1, -1, -2, 0, 2, 2, 4, 0, 0, -1, -1, 0, 0, -2, -2, 0, -1, -1, -2, 0, 0, 2, 4, 0, -1, 0, 0, 0, 0, 0, 2, 0, -1, -2, -2, 0, 0, -2, -4, 0, -1, -1, -1, 0, 2, 2, 2, 0, 0, -1, -2, 0, 2, 4, 4, 0, 0, 0, -1, 0, -2, -2, -2, 0, 0,
6、 -1, -2, 0, 0, 2, 4, 0, -1, -1, -1, 0, 0, 0, -2, 0, -1, -2, -2, 0 , 2, 4, 2, 0, 0, 0, -1, 0, -2, 0, 2, 0, -1, -1, -1, 0, 0, -2, 0, 0, -1, -1, -2, ;struct int sum; int connection_x5; int connection_y5; connection = 2, -2, 2, 0, 1, 1, 0, 0, 2, 2, -2, 0, 0, 2, -2, 2, 0, 0, 4, -2, 0, 4, -2, 0, 0, -1, 1,
7、 4, -4, 2, -2, 4, 0, 0, -1, 1, 4, -2, 2, 0, 0, 0, 0, 0, 0,;void gotoxy(int x, int y)/光標移動函數 COORD pos;/COORD是Windows API中定義的一種結構,表示一個字符在控制臺屏幕上的坐標 pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);/使光標 到(x,y)這個位置void color(int b)/顏色函數 HANDLE hConsole = GetStdHandle(
8、STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, b);void PrintGarphy()/畫界面函數,游戲界面是25*15的 color(1010);/邊框的顏色 int i, j; gotoxy(2, 2);/開始畫邊框 for(i = 0; i < 26; i+) printf("");/alt+43144 gotoxy(2, 28); for(i = 0; i < 26; i+) printf(""); for(i = 0; i < 26; i+) gotoxy(2,
9、2 + i); printf(""); gotoxy(34, 2 + i); if(i = 8 | i = 16) printf(""); else printf(""); gotoxy(52, 2 + i); printf(""); int t;FILE *fp = fopen("C:els.txt", "r");/最高分的取出 fscanf (fp, "%d", &t); fclose(fp); color(46);/最高分的顏色 gotoxy
10、(37, 12);/最高分的顯示位置 printf("最高紀錄: %d", t); color(46); gotoxy(37, 14); printf("當前得分: 0"); color(46); gotoxy(37, 16); printf("級數 "); color(39); gotoxy(36, 20); printf(" 操作 "); gotoxy(37, 22); printf(" 按空格 暫停"); gotoxy(37, 24); printf(" 按 ESC 退出&quo
11、t;); gotoxy(37, 26); printf(" 編寫人 沈志全"); color(back); for(i = 0; i < 25; i+)/做一個空白的游戲界面 gotoxy(4, 3 + i); for(j = 0; j < 15; j+) printf(" "); void GameOver()/游戲結束的函數 color(391); for(int i = 24; i >= 0; i-)/清屏 for(int j = 0; j < 15; j+) gotoxy(32 - j * 2, 3 + i); print
12、f(""); Sleep(10);/執行掛起一段時間 實現一種效果 color(back); for(int j = 0; j < 25; j+)/恢復起始的效果 for(int i = 0; i < 15; i+) boardij.having = 0; gotoxy(4 + 2 * i, j + 3); printf(" "); Sleep(10);/執行掛起一段時間 實現一種效果 color(34); gotoxy(42, 16); while(rank-) printf(" "); color(46); gotox
13、y(47, 14); printf(" ");void GetDepth(int *pdepth)/判斷是否達到最大高度 (*pdepth) = 25; for(int j = 0; j < 4; j+) int sum = 0; int n = (current.x + varykind.vary_xj - 4) / 2; int m = current.y + varykind.vary_yj - 3; if(m < 0) m = 0; for(+m; m < 25; m+) if(boardnm.having = 0 && n >
14、;= 0 && n < 15 && m >= 0 && m < 25) sum+; else break; if(sum < (*pdepth) (*pdepth) = sum; void CheckFull(int *pscore)/消行函數 int i, j; int sum = 0; int s = 0, 1, 3, 6, 10; for(i = 0; i < 25; i+) bool mark = 1; for(j = 0; j < 15; j+) if(boardji.having = 0) mark
15、 = 0; boardji.color = back; if(mark)/開始消行 sum+; for(int t = i; t > 0; t-) gotoxy(4, 3 + t); for(j = 0; j < 15; j+) if(boardjt-1.having)/消的那個上面是否有方塊 color(boardjt-1.color); printf("");/打出方塊 boardjt.color = boardjt-1.color;/換顏色 boardjt.having = 1;/格子下移 boardjt-1.having = 0;/消除上一個格子 els
16、e/沒有方塊 color(back);/把它賦予面板的顏色 printf(" ");/打出空格 boardjt.color = boardjt.color; boardjt.having = 0; boardjt-1.having = 0; (*pscore) += ssum;/接下來是速度變換 消行達到一定的行數就加速 color(46); gotoxy(47, 14); printf("%d", *pscore); if(*pscore >= 140) rank = 5; else if(*pscore >= 90) rank = 4;
17、else if(*pscore >= 50) rank = 3; else if(*pscore >= 20) rank = 2; else if(*pscore >= 1) rank = 1; color(44); gotoxy(42, 16); for(i = 0; i < rank && *pscore; i+) printf("");/顯示星級 int t; FILE *fp = fopen("C:els.txt", "r"); fscanf (fp, "%d", &
18、amp;t); fclose(fp); if(*pscore > t) fp = fopen("C:els.txt", "w"); fprintf (fp, "%d", *pscore); fclose(fp); color(43); gotoxy(47, 12); printf("%d", *pscore); fclose(fp);void PrintNext(int pkind)/顯示下一個是什么方塊 int i; int x = 43, y = 6; color(34); for(i = 0; i &l
19、t; 4; i+) gotoxy(x - 3, y + i - 2); printf(" "); pri_kind = pkind; if(pkind = 6) pkind = 15, x-; else if(pkind = 5) pkind = 11, x-; else if(pkind = 4) pkind = 7, x-; else if(pkind = 3) pkind = 5, x -= 2; else if(pkind = 2) pkind = 3, x += 2; else if(pkind = 1) pkind = 2, x-; else pkind = 0
20、, x -= 3; color(colpri_kind); for(i = 0; i < 4; i+) gotoxy(x + varypkind.vary_xi, y + varypkind.vary_yi); printf(""); void CheckBoundary()/檢查邊界線 int i, n, m; for(i = 0; i < 4; i+) m = current.x + varykind.vary_xi; if(m < 4 | m > 32) current.x += (m < 4 ? 2 : -2); i = -1; for
21、(i = 0; i < 4; i+) / 對四個方塊分別檢查 m = current.x + varykind.vary_xi; n = current.y + varykind.vary_yi; m = (m - 4) / 2; n -= 3; if(n < 0 | n > 24 | boardmn.having) if(level = 1) /左移 current.x += 2; /復原 else if(level = 2) /右移 current.x -= 2; /復原 level = 0; break; void LevelMove()/下落速度函數 CheckBou
22、ndary(); if(level = 0) return; int i; color(back); for(i = 0; i < 4; i+) if(current.y + varykind.vary_yi > 2) gotoxy(before.x + varykind.vary_xi, before.y + varykind.vary_yi); puts(" "); color(colpri_kind); for(i = 0; i < 4; i+) if(current.y + varykind.vary_yi > 2) gotoxy(curre
23、nt.x + varykind.vary_xi, current.y + varykind.vary_yi); puts(""); before.x = current.x;void Revolve(int *prev_count) /旋轉函數 int i; before.x = current.x; before.y = current.y; (*prev_count)+; (*prev_count) %= connectionpri_kind.sum; current.x = current.x + connectionpri_kind.connection_x *pr
24、ev_count; current.y = current.y + connectionpri_kind.connection_y *prev_count; CheckBoundary(); for(i = 0; i < 4; i+) int m = (current.x - 4 + varynumberpri_kind + *prev_count.vary_xi) / 2; int n = current.y + varynumberpri_kind + *prev_count.vary_yi - 3; if(boardmn.having | m > 14 | m < 0
25、| n > 24) current.x = current.x - connectionpri_kind.connection_x *prev_count; current.y = current.y - connectionpri_kind.connection_y *prev_count; revolve = 0; (*prev_count)-; before.x = current.x; before.y = current.y; return ; color(back); for(i = 0; i < 4; i+) if(before.y + varykind.vary_y
26、i > 2) gotoxy(before.x + varykind.vary_xi, before.y + varykind.vary_yi); puts(" "); kind = numberpri_kind + *prev_count; color(colpri_kind); for(i = 0; i < 4; i+) if(current.y + varykind.vary_yi > 2) gotoxy(current.x + varykind.vary_xi, current.y + varykind.vary_yi); puts("&q
27、uot;); Sleep(speedrank); before.x = current.x; before.y = current.y;void ChoiceDirection(int *prev_count) int t = 50 / rank; while(t-) if(_kbhit() char c = _getch(); switch(c) case 0x48:/ 上 revolve = 1; Revolve(&prev_count); break; case 0x50: /下 level = 0; return; case 0x4b: /左 before.x = curren
28、t.x; current.x -= 2; level = 1; LevelMove(); break; case 0x4d: /右 before.x = current.x; current.x += 2; level = 2; LevelMove(); break; case ' ': _getch(); break; case 27: color(back); exit(1); default: break; Sleep(speedrank); void StraightFall(int depth) int i; int rev_count = 0; before.x =
29、 current.x; before.y = current.y; while(GetDepth(&depth), depth-) ChoiceDirection(&rev_count); GetDepth(&depth); if(depth = 0) revolve = 0; before.x = current.x; before.y = current.y; break; color(back); for(i = 0; i < 4; i+) if(before.y + varykind.vary_yi > 2) gotoxy(before.x + va
30、rykind.vary_xi, before.y + varykind.vary_yi); puts(" "); current.y+; color(colpri_kind); for(i = 0; i < 4; i+) if(current.y + varykind.vary_yi > 2) gotoxy(current.x + varykind.vary_xi, current.y + varykind.vary_yi); puts(""); revolve = 0; before.x = current.x; before.y = cu
31、rrent.y; ChoiceDirection(&rev_count); if(level | revolve) level = 0; revolve = 0; ChoiceDirection(&rev_count); StraightFall(depth); /遞歸調用 int main() FILE *fp = fopen("c:123sa.c", "w"); srand(unsigned long)time(0); system("mode con cols=56 lines=30");/畫總界面大小 system("color 24"); int score = 0; fp = fopen("C:els.txt", "r");/打開最高分的文件 if(fp = NULL) fp = fopen("C:els.txt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
評論
0/150
提交評論