C語言實現的2048小遊戲
給大一新生寫的一個小遊戲。
缺點:函式名稱和功能略微不對應,函式功能寫得比較亂,時間記錄有誤差,可擴充套件性弱。
優點:通過幾個配置陣列,將單位方塊移動的函式縮短到30行以內。
#include <stdio.h> #include <windows.h> #include <stdlib.h> #include <time.h> #include <conio.h> //座標常量 const int squareSize = 4; const int locationX[squareSize] = { 4, 6, 8, 10 }; const int locationY[squareSize] = { 2, 8, 14, 20 }; const int locationTime[2] = { 13, 12}; const int directionS[2][3] = { {1, 4, 1}, {2, -1, -1} }; //左 右 上 下 //0 1 2 3 const int directionXY[4][2] = { {0,-1}, {0, 1}, {-1, 0}, {1, 0} }; //矩陣內容 int square[squareSize][squareSize]; //時間相關全域性變數 int t_hour = 0; int t_min = 0; int t_sec = 0; int t_ms = 0; //得分相關全域性變數 int score = 0; //步數相關全域性變數 int step = 0; //存活全域性變數 int alive = 1; //函式宣告 void gotoxy(int x, int y); void sleep(int n); void gotoxy(int x, int y); void move(int direction); void play(); int judge(int x, int y, int direction); void printSquare(); void init(); void randSquare(); void printScore(); //列印當前時間 void printTime() { gotoxy(locationTime[0], locationTime[1]); printf("%02d:%02d:%02d", t_hour, t_min, t_sec); } //睡眠函式 不是很準確 推薦用系統時間換算 void sleep(int n) { Sleep(n); t_ms += n; if (t_ms >= 1000) { t_ms -= 1000; t_sec += 1; } if (t_sec >= 60) { t_sec -= 60; t_min += 1; } if (t_min >= 60) { t_sec -= 60; t_hour += 1; } gotoxy(locationTime[0], locationTime[1]); printTime(); } //游標移動函式 void gotoxy(int x, int y) { COORD pos; pos.X = y; pos.Y = x; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } //左 右 上 下 //0 1 2 3 //遊戲單元 移動函式 void move(int direction) { //const int directionS[2][3] = { {1, 4, 1}, {2, -1, -1} }; //向左移動 i = directionS[0][0]; i != directionS[0][1] ; i += directionS[0][2]; //向右移動 i = directionS[1][0]; i != directionS[1][1] ; i += directionS[1][2]; //向上移動 i = 1; i != 4 ; i++ //向下移動 i = 2; i != -1; i-- int mydirection = direction % 2; for(int k = 0;k < 3;k++) { for (int i = directionS[mydirection][0]; i != directionS[mydirection][1]; i += directionS[mydirection][2]) { for (int j = 0; j < 4; j++) { if (direction / 2 == 0) { //橫向移動 if (square[j][i] != 0) { judge(j, i, direction); //getch(); } } else { //縱向移動 if (square[i][j] != 0) { judge(i, j, direction); //getch(); } } } } sleep(80); } //生成一個新的方塊 randSquare(); step += 1; printScore(); } int judge(int x, int y, int direction) { int toX = x + directionXY[direction][0]; int toY = y + directionXY[direction][1]; if (square[toX][toY] == 0 || square[toX][toY] == square[x][y]) { if(square[toX][toY] != 0) { score += square[x][y] * square[x][y]; } square[toX][toY] += square[x][y]; square[x][y] = 0; gotoxy(locationX[toX],locationY[toY]); printf("%4d", square[toX][toY]); gotoxy(locationX[x],locationY[y]); printf("%4d", square[x][y]); return 1; } else { return 0; } } //遊戲函式 //左 右 上 下 //0 1 2 3 void play() { char ch; for (;alive;) { ch = 'o'; if (kbhit()) { ch = getch() | 32; } switch (ch) { case 'w': //向上移動 move(2); break; case 's'://向下移動 move(3); break; case 'a'://向左移動 move(0); break; case 'd'://向右移動 move(1); break; default://不進行移動 break; } sleep(20); gotoxy(15, 20); } } void printScore() { gotoxy(6, 28); printf("%10d", step); gotoxy(10, 28); printf("%10d", score); } //列印棋盤 void printSquare() { printf("┏━━━━━━━━━━━━━━━━━━┓\n"); printf("┃ 2048 小遊戲 ┃\n"); printf("┗━━━━━━━━━━━━━━━━━━┛\n"); printf("┏━━┳━━┳━━┳━━┓┏━━━━━┓\n"); printf("┃ 0┃ 0┃ 0┃ 0┃┃ 步數 ┃\n"); printf("┣━━╋━━╋━━╋━━┫┣━━━━━┫\n"); printf("┃ 0┃ 0┃ 0┃ 0┃┃ 0┃\n"); printf("┣━━╋━━╋━━╋━━┫┣━━━━━┫\n"); printf("┃ 0┃ 0┃ 0┃ 0┃┃ 得分 ┃\n"); printf("┣━━╋━━╋━━╋━━┫┣━━━━━┫\n"); printf("┃ 0┃ 0┃ 0┃ 0┃┃ 0┃\n"); printf("┗━━┻━━┻━━┻━━┛┗━━━━━┛\n"); printf("┏━━━━━━━━━━━┓┏━━━━━┓\n"); printf("┃ 時長:00:00:00 ┃┃ 徐絡絡 ┃\n"); printf("┗━━━━━━━━━━━┛┗━━━━━┛\n"); } //初始化函式 void init() { srand(time(NULL)); printSquare(); //時間相關全域性變數 t_hour = 0; t_min = 0; t_sec = 0; t_ms = 0; //得分相關全域性變數 score = 0; //步數相關全域性變數 step = 0; for (int i = 0; i < squareSize; i++) { for(int j = 0;j < squareSize;j++) { square[i][j] = 0; } } //隨機生成一個2或者4 randSquare(); } //隨機生成一個方塊 void randSquare() { int x = rand() % 4; int y = rand() % 4; int number = rand() % 2 + 1; int legal = 0; number *= 2; if (square[x][y] != 0) { for (int i = 0; i < 4 && !legal; i++) { for (int j = 0; j < 4 && !legal; j++) { if (square[i][j] == 0) { gotoxy(locationX[i], locationY[j]); printf("%4d", number); square[i][j] = number; legal = 1; } } } } else { gotoxy(locationX[x], locationY[y]); printf("%4d", number); square[x][y] = number; legal = 1; } if (legal == 0) { alive = 0; } gotoxy(15, 20); } int main() { //先初始化 init(); //開始遊戲 play(); //遊戲結束 游標移動至遊戲介面最下方 gotoxy(15, 20); //暫停 printf("遊戲結束!"); getch(); return 0; }
相關推薦
C語言實現經典小遊戲貪吃蛇
純c語言寫的小遊戲,本人才疏學淺,程式碼有很多不足,僅供給初學者參考。 實現功能: ↑ ↓ ← →分別用來控制上下左右四個方向 空格暫停 esc退出遊戲 f1加速, f2 減速 蛇的長度越長,速度越快(速度有上限) 食物顏色隨機
C語言實現一條龍小遊戲
一條龍小遊戲 一、程式碼構思 全部程式碼分為三個檔案:game.h, game.c, text.c(game.h用來申明所有用到的函式,game.c用來寫實現遊戲的程式碼,text.c用來除錯程式碼) 第一步將棋盤初始化為空格;第二步列印棋盤;第三步設計由玩家先走(在棋盤中放入X),然後電腦
使用C語言實現隨機小遊戲原始碼
#include"stdio.h" #include"stdlib.h" #include"windows.h" int print() { printf("\n\n+++++++++++++你會看見的數字和運算子+++++++++++++\n"); &
C語言實現掃雷小遊戲
本文將從一行行程式碼中詳解掃雷小遊戲,對每一個模組都使用詳細的註釋,使這個掃雷小遊戲簡單易懂。 首先,簡單分析掃雷的玩法,掃雷就是在一個棋盤中佈置適當數量的雷數玩家通過對雷陣的排查,來找出雷的位置。如果玩家選擇的座標周圍無雷將自動展開這片區域,若有雷會顯示雷數。
C語言介面實現2048小遊戲
這是我在大一第二學期(兩年前)為了參加比賽,自學後寫的一個Dome,拿出來和大家分享一下,程式碼為兩年前的程式碼,未改動,優化以及各式可能很一般,請見諒。 #include "stdio.h" #include "stdlib.h" #include "time.h" #d
C語言在linux終端下實現2048小遊戲:第二版
原來我轉載過一個機遇ncurses的2048,今天無聊自己手寫了一個,看下我的目錄結構: $ tree ../2048/ ../2048/ ├── 2048.c ├── 2048.h └── main.c 0 directories, 3 files 2048.h
C語言實現2048遊戲
文章目錄列印介面生成隨機數是否獲勝移動遊戲程式碼: 2048遊戲在很長一段時間內是一款很火的小遊戲。 遊戲要求: 最開始兩個隨機數 每次生成一個隨機數2或者4,生成4的概率是1/10 鍵盤上下左右鍵控制遊戲的走向 當出現2048數字時,玩家獲勝 列印介面
C++實現2048小遊戲(控制檯版的)
無聊,在公司寫了個2048小遊戲的程式,聊以自娛。(事實是我手機壞了,沒得玩)。 很簡單,直接上程式碼了。 #include <iostream> #include <windows.h> #include <ctime> using
C#實現2048小遊戲
要實現這個簡單的小遊戲主要在於實現一個方向移動 數字的移動及合併該如何處理 然後其它方向的邏輯是相同的 我做的這個基本功能實現了 主要分為三個類 Box.cs格子類(一些格子裡儲存的資料,行下標,列下標,是否合併過的開關。。) Grid.cs網格類(主要演算法在裡
C++實現2048小遊戲
程式碼如下: #define _CRT_SECURE_NO_WARNINGS//去掉編譯器內部擴增問題#include<stdio.h>#include<stdlib.h>#include<math.h>#include<graphics.h>//需要下載圖形庫
基於C語言實現的掃雷遊戲
函式實現的功能: 1.佈置雷:用rand函式生成隨機座標再雷陣裡隨機佈雷 2.排雷:踩到雷炸死,不是雷,統計周圍一圈有⼏個雷,並記錄資訊放到另一個數組 實現原理: 1.雷的資訊儲存到一個二維陣列mine裡 2.排出來的雷存到另一個二維陣列show裡
用C語言實現最小二乘法演算法
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
用C語言實現三子棋遊戲
game.h #ifndef __GAME_H__ #define __GAME_H__ #define _CRT_SECURE_NO_WARNINGS #include <string.h> #include <s
JQuery初體驗-JavaScript實現2048小遊戲PC端
目錄 效果圖 遊戲頁面 遊戲頁面樣式 遊戲基礎邏輯 遊戲動畫邏輯 遊戲主邏輯 遊戲互動邏輯 效果圖: 遊戲頁面: <!DOCTYPE html> <html lang="en"> <head> <
matlab和C語言實現最小二乘法
參考:https://blog.csdn.net/zengxiantao1994/article/details/70210662 Matlab程式碼: N = 8; x = [1 2 3 4 5 6 7 8 ]; y = [67 84 102 120 137 1
C程式碼實現掃雷小遊戲
分析 同三子棋小遊戲一樣,掃雷小遊戲也分為如下思路: 標頭檔案 #ifndef __GAME_H__ #define __GAME_H__ #include <stdlib.h> #include <time.h> #include <s
C語言實現猜數字遊戲 全乾貨!
知識點:隨機數的生成: srand((unsigned)time(NULL)): srand函式是隨機數發生器的初始化函式。原型:void srand(unsigned seed); 用法:它初始化隨機種子,會提供一個種子,這個種子會對應一個隨機數,如果使用相同的種子後面的ran
用C++語言實現貪吃蛇遊戲
寫在前面 用C++語言寫遊戲再適合不過了,當然不是因為用它寫起來簡單,(相反那並不簡單),但是其效能絕對是其他語言沒法比的。所以這裡我會用C++實現一個貪吃蛇的遊戲。當然我可能有意隱瞞了你,因為我們不僅僅是用C++純語言來幹這件事,那會很彆扭,因為我們需要影象
C語言實現三子棋遊戲
先直接上程式碼: #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> //2.實現三子棋遊戲。 #include<Windows.h> //Sleep
java實現2048小遊戲
學習Java基礎有一段時間了,一直想做個小的桌面程式練下手,最近自己有點時間用Java寫了一個2048的桌面程式,和大家分享一下!!! 遊戲效果展示: 1、設計思想 AppFrame.java遊戲的啟動類,只調用了一個MainFrame的構造方法 MainFrame.ja