21xrx.com
2024-11-22 06:27:38 Friday
登录
文章检索 我的文章 写文章
DevC++游戏代码大全
2023-07-06 03:04:19 深夜i     --     --
DevC++ 游戏代码 大全 编程 开发工具

DevC++是一款非常不错的编程软件,许多程序员都喜欢使用它来进行游戏开发。如果你想学习如何利用DevC++编写游戏,下面是一些非常有用的DevC++游戏代码,让你开始编写自己的游戏吧!

1. 俄罗斯方块

俄罗斯方块是一款简单而又有趣的游戏,它是由七种不同的形状的方块组成的,玩家需要移动和旋转这些方块,使它们落到最底部,从而消除整行方块。下面是俄罗斯方块DevC++游戏代码:

#include

#include

#include

#include

#include

#define Width 10

#define Height 20

#define Scale 20

int field[Height][Width];

int block[4][4];

int blockType;

int posX, posY;

int oldPosX, oldPosY;

void InitGame();

void DrawGame();

void UpdateGame();

void MoveBlock(int, int);

bool CheckCollision(int, int);

bool CheckLineCompleted();

void RemoveLine();

int main(){

  InitGame();

  while(true){

    DrawGame();

    UpdateGame();

    Sleep(100);

  }

}

void InitGame(){

  for(int y=0; y

    for(int x=0; x

      if(y==Height-1 || x==0 || x==Width-1){

        field[y][x] = 1;

      }else{

        field[y][x] = 0;

      }

    }

  }

  srand(time(NULL));

  blockType = rand()%7;

  for(int y=0; y<4; y++){

    for(int x=0; x<4; x++){

      if((block[blockType][y][x]) == '#'){

        field[y][x+Width/2] = 2;

      }

    }

  }

  posX = Width/2;

  posY = 0;

}

void DrawGame(){

  system("cls");

  for(int y=0; y

    for(int x=0; x

      if(field[y][x]==0){

        printf(".");

      }else if(field[y][x]==1){

        printf("#");

      }else{

        printf("%c", block[blockType][y-posY][x-posX]);

      }

    }

    printf("\n");

  }

}

void UpdateGame(){

  if(kbhit()){

    switch(getch()){

      case 'a': MoveBlock(-1, 0);

           break;

      case 'd': MoveBlock(1, 0);

           break;

      case 's': MoveBlock(0, 1);

           break;

      case 'w':

           oldPosX = posX;

           oldPosY = posY;

           blockType = (blockType+1)%7;

           for(int y=0; y<4; y++){

             for(int x=0; x<4; x++){

               if((block[blockType][y][x]) == '#'){

                 field[y][x+posX] = 0;

               }

             }

           }

           if(CheckCollision(0, 0)){

             posX = oldPosX;

             posY = oldPosY;

             blockType = (blockType+6)%7;

             for(int y=0; y<4; y++){

               for(int x=0; x<4; x++){

                 if((block[blockType][y][x]) == '#'){

                   field[y][x+posX] = 2;

                 }

               }

             }

           }

           break;

    }

  }

  MoveBlock(0, 1);

}

void MoveBlock(int dx, int dy){

  if(CheckCollision(dx, dy)){

    oldPosX = posX;

    oldPosY = posY;

    posX += dx;

    posY += dy;

    for(int y=0; y<4; y++){

      for(int x=0; x<4; x++){

        if((block[blockType][y][x]) == '#'){

          field[y+posY][x+posX] = 2;

        }

      }

    }

    if(CheckLineCompleted()){

      RemoveLine();

    }

    blockType = rand()%7;

    for(int y=0; y<4; y++){

      for(int x=0; x<4; x++){

        if((block[blockType][y][x]) == '#'){

          field[y][x+Width/2] = 2;

        }

      }

    }

    posX = Width/2;

    posY = 0;

  }

}

bool CheckCollision(int dx, int dy){

  for(int y=0; y<4; y++){

    for(int x=0; x<4; x++){

      if((block[blockType][y][x]) == '#'){

        if(field[y+posY+dy][x+posX+dx] != 0){

          return false;

        }

      }

    }

  }

  return true;

}

bool CheckLineCompleted(){

  for(int y=0; y

    int count = 0;

    for(int x=1; x

      if(field[y][x]==2){

        count++;

      }

    }

    if(count==(Width-2)){

      return true;

    }

  }

  return false;

}

void RemoveLine(){

  for(int y=0; y

    if(field[y][1]==2){

      for(int x=1; x

        field[y][x] = 0;

      }

      for(int i=y-1; i>=0; i--){

        for(int x=1; x

          field[i+1][x] = field[i][x];

        }

      }

    }

  }

}

2. 2048游戏

2048游戏是一款非常欢乐的数字消除游戏,它的玩法非常简单,但挑战性却很高。下面是2048游戏DevC++代码:

#include

#include

#include

#include

#include

using namespace std;

#define Width 4

#define Height 4

#define CellWidth 5

#define CellHeight 3

int board[Height][Width];

vector >emptyCells;

void InitGame();

void DrawGame();

void UpdateGame();

int GetRandom(int, int);

int main(){

  InitGame();

  while(true){

    DrawGame();

    UpdateGame();

    Sleep(100);

  }

}

void InitGame(){

  for(int y=0; y

    for(int x=0; x

      board[y][x] = 0;

      emptyCells.push_back(make_pair(y, x));

    }

  }

  board[GetRandom(0, Height-1)][GetRandom(0, Width-1)] = 2;

}

void DrawGame(){

  system("cls");

  for(int y=0; y

    for(int i=0; i

      for(int x=0; x

        if(i==1){

          printf("%c  ", board[y][x]==0 ? '.' : '0'+board[y][x]);

        }else{

          printf("   ");

        }

      }

      printf("\n");

    }

  }

}

void UpdateGame(){

  if(kbhit()){

    switch(getch()){

      case 'a':{

        int moved = 0;

        for(int y=0; y

          int last = 0;

          for(int x=1; x

            if(board[y][x]==0){

              continue;

            }

            if(board[y][last]==0 || board[y][x]==board[y][last]){

              board[y][last] += board[y][x];

              board[y][x] = 0;

              moved = 1;

              last += 1;

            }else{

              last += 1;

              board[y][last] = board[y][x];

              if(last!=x){

                board[y][x] = 0;

                moved = 1;

              }

            }

          }

        }

        if(moved){

          emptyCells.clear();

          for(int y=0; y

            for(int x=0; x

              if(board[y][x]==0){

                emptyCells.push_back(make_pair(y, x));

              }

            }

          }

          int index = GetRandom(0, emptyCells.size()-1);

          int value = GetRandom(0, 9);

          value = (value==0) ? 4 : 2;

          board[emptyCells[index].first][emptyCells[index].second] = value;

        }

        break;

      }

      case 'd':{

        int moved = 0;

        for(int y=0; y

          int last = Width-1;

          for(int x=Width-2; x>=0; x--){

            if(board[y][x]==0){

              continue;

            }

            if(board[y][last]==0 || board[y][x]==board[y][last]){

              board[y][last] += board[y][x];

              board[y][x] = 0;

              moved = 1;

              last -= 1;

            }else{

              last -= 1;

              board[y][last] = board[y][x];

              if(last!=x){

                board[y][x] = 0;

                moved = 1;

              }

            }

          }

        }

        if(moved){

          emptyCells.clear();

          for(int y=0; y

            for(int x=0; x

              if(board[y][x]==0){

                emptyCells.push_back(make_pair(y, x));

              }

            }

          }

          int index = GetRandom(0, emptyCells.size()-1);

          int value = GetRandom(0, 9);

          value = (value==0) ? 4 : 2;

          board[emptyCells[index].first][emptyCells[index].second] = value;

        }

        break;

      }

      case 'w':{

        int moved = 0;

        for(int x=0; x

          int last = 0;

          for(int y=1; y

            if(board[y][x]==0){

              continue;

            }

            if(board[last][x]==0 || board[last][x]==board[y][x]){

              board[last][x] += board[y][x];

              board[y][x] = 0;

              moved = 1;

              last += 1;

            }else{

              last += 1;

              board[last][x] = board[y][x];

              if(last!=y){

                board[y][x] = 0;

                moved = 1;

              }

            }

          }

        }

        if(moved){

          emptyCells.clear();

          for(int y=0; y

            for(int x=0; x

              if(board[y][x]==0){

                emptyCells.push_back(make_pair(y, x));

              }

            }

          }

          int index = GetRandom(0, emptyCells.size()-1);

          int value = GetRandom(0, 9);

          value = (value==0) ? 4 : 2;

          board[emptyCells[index].first][emptyCells[index].second] = value;

        }

        break;

      }

      case 's':{

        int moved = 0;

        for(int x=0; x

          int last = Height-1;

          for(int y=Height-2; y>=0; y--){

            if(board[y][x]==0){

              continue;

            }

            if(board[last][x]==0 || board[last][x]==board[y][x]){

              board[last][x] += board[y][x];

              board[y][x] = 0;

              moved = 1;

              last -= 1;

            }else{

              last -= 1;

              board[last][x] = board[y][x];

              if(last!=y){

                board[y][x] = 0;

                moved = 1;

              }

            }

          }

        }

        if(moved){

          emptyCells.clear();

          for(int y=0; y

            for(int x=0; x

              if(board[y][x]==0){

                emptyCells.push_back(make_pair(y, x));

              }

            }

          }

          int index = GetRandom(0, emptyCells.size()-1);

          int value = GetRandom(0, 9);

          value = (value==0) ? 4 : 2;

          board[emptyCells[index].first][emptyCells[index].second] = value;

        }

        break;

      }

    }

  }

}

int GetRandom(int min, int max){

  return rand()%((max-min)+1)+min;

}

无论你是对俄罗斯方块还是2048游戏感兴趣,在以上给出的DevC++游戏代码中,你都能找到动手写不同游戏所需的技能和知识。这样的编程练习不仅有益于增强你的技能,提高你的编程能力,也将为你未来的游戏开发实践奠定坚实的基础。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复