wujr5 / c-and-cpp-language-learning

C和C++编程语言学习 - 2015级
67 stars 34 forks source link

软件:Optional Lab C Small Plane Game #31

Open ghostbody opened 8 years ago

ghostbody commented 8 years ago

Description

Now, we are going to do a small project named a small plane game.

Requirements

  1. Lookahead Self Learning is required in this project. The knowledge include struct and pointers in C. Especially for struct, you should know its advantage and usage. For pointer, you should know the deep thoughts.
  2. Debug method should be mastered such as breakpoint debug, binary debug or output debug.
  3. Good programming style should be mastered.

    Let's build a game!

We need to know what roles at the game and the rules as well. The question is what rules we need.

In this project, we are going to build the small plane game. We have the following roles:

  1. Plane, the main character in the game, and the player can control the plane. Note that we use 1 to mark plane in the game prototype. 2.Obstacles, the second main characters in the game. It can move as an AI, and when it hits the player, game over. Note that we use 2 to mark obstacles in game prototype.
  2. The map, the scene to play on. And the map display the objects in the map. Like the image, we can regard it as a map in the game. image

Actions:

  1. For player, it can move left, right, up and down if it's still in the map.
  2. For obstacles, it can move from right to left trying to hit the player.
  3. Update the game pane when something change.
  4. We need to draw the game pane when the map update.(print)

    Game Structure Code

#ifndef GAME_H
#define GAME_H

typedef struct point {
  int x;
  int y;
} point;

typedef enum objects {BLANK = 0, PLANE = 1, OBSTACLE = 2} OBJ;
typedef enum operations {UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3} OP;

#define GAME_ROW 2
#define GAME_COL 16

typedef struct gamePane {
  // the game pane, 0 for blank, 1 for plane and 2 for obstacle
  OBJ pane[GAME_ROW][GAME_COL];
  point cursor;
  int gameOver;
} gamePane;

typedef struct plane {
  point position;
} plane;

typedef struct obstacle {
  point position;
} obstacle;

// function list
void init(gamePane * thePane, plane * thePlane, obstacle * theObstacle1, obstacle * theObstacle2);
void setCursor(gamePane * thePane, const int x, const int y);
void setplane(gamePane * thePane, plane * plane);
void setobstacle(gamePane * thePane, obstacle * obstacle);
void removeOBJ(gamePane * thePane, const point p);
void gameOver();
void printPane(const gamePane thePane);
void movePlane(gamePane * thePane, plane * thePlane, OP op);
void moveObstacle(gamePane * thePane, obstacle * theObstacle);
#endif // GAME_H

Game Source Code

void init(gamePane * thePane, plane * thePlane, obstacle * theObstacle1, obstacle * theObstacle2) {
  thePlane->position.x = 0;
  thePlane->position.y = 0;
  theObstacle1->position.x = 0;
  theObstacle1->position.y = GAME_COL-1;
  theObstacle2->position.x = 1;
  theObstacle2->position.y = GAME_COL-3;

  int i, j;

  for(i = 0; i < GAME_ROW; i++) {
    for(j = 0; j < GAME_COL; j++) {
      thePane->pane[i][j] = BLANK;
    }
  }

  setplane(thePane, thePlane);
  setobstacle(thePane, theObstacle1);
  setobstacle(thePane, theObstacle2);
  thePane->gameOver = 0;
  thePane->cursor.x = 0;
  thePane->cursor.y = 0;
}

// basic operation for game pane
void setCursor(gamePane * thePane, const int x, const int y) {
  thePane->cursor.x = x;
  thePane->cursor.y = y;
}

void setplane(gamePane * thePane, plane * plane) {
  if(thePane->pane[plane->position.x][plane->position.y] == OBSTACLE) {
    thePane->gameOver = 1;
    gameOver();
    return;
  }
  thePane->pane[plane->position.x][plane->position.y] = PLANE;
}

void setobstacle(gamePane * thePane, obstacle * obstacle) {
  if(thePane->pane[obstacle->position.x][obstacle->position.y] == PLANE) {
    thePane->gameOver = 1;
    gameOver();
    return;
  }
  thePane->pane[obstacle->position.x][obstacle->position.y] = OBSTACLE;
}

void removeOBJ(gamePane * thePane, const point p) {
  thePane->pane[p.x][p.y] = BLANK;
}

void gameOver() {
  printf("GameOver\n");
}

void printPane(const gamePane thePane) {
  int i, j;
  for(i = 0; i < GAME_ROW; i++) {
    for(j = 0; j < GAME_COL; j++) {
      printf("%d", thePane.pane[i][j]);
    }
    printf("\n\n");
  }
}

// operation for plane
void movePlane(gamePane * thePane, plane * thePlane, OP op) {
  removeOBJ(thePane, thePlane->position);
  switch(op) {
  case 0 :
    if(thePlane->position.x > 0) {
      thePlane->position.x--;
    }
    break;
  case 1 :
    if(thePlane->position.x < GAME_ROW - 1) {
      thePlane->position.x++;
    }
    break;
  case 2:
    if(thePlane->position.y > 0) {
      thePlane->position.y--;
    }
    break;
  case 3:
    if(thePlane->position.y < GAME_COL - 1) {
      thePlane->position.y++;
    }
    break;
  }
  setplane(thePane, thePlane);
}

//operations for obstacle
void moveObstacle(gamePane * thePane, obstacle * theObstacle) {
  removeOBJ(thePane, theObstacle->position);
  if(theObstacle->position.y <= 0) {
    theObstacle->position.y = GAME_COL - 1;
  }

  theObstacle->position.y--;

  setobstacle(thePane, theObstacle);
}

Assignment 0

You do not need to write this part to your report.

  1. What's the meaning of the code, please write comments(注释) for each functions as well as the important expressions and statements. 2.The header declarations and source code of the game are provided. But we can not run the game without a game launch function(main function), please write a main function to run the game.

    Assignment 1

As you play the game, the interface of the game is very ugly. And also the game is not a game at all. Your job is to modify the game using your idea.

Some advice:

  1. Add bullet for the plane, which means that you can control the plane to hit the obstacles.
  2. Add score counter, and a list of score. And it can record the ranking of the score every you play it. You should use c file IO in this part.
  3. Large plane Scene, get a large Play Scene to the game.
  4. Muti-obstcales that the obstacles can spawn a bullet or any other idea.

    Grade

(55pts) Your personal runable game. (25pts) Your Document about your game elucidatio. And your algorithm to get to your idea game. (20pts) The Style of your source code. Besides google style check, you should have good indent in the source code and you are supposed to write good comments(充分的代码注释) for you game.

Notice that, you should upload you game project to your github repository and write the documentation using markdown and named it "Readme.md".

You will get bonus in the final grade.

Submit

just send me your github address.

Deadline

2015 12.15 18:00

Icenowy commented 8 years ago

这个作业是同时向计科和软件开放的?还有图形库不限?

---- Ye Jiaqi编写 ----

Description

Now, we are going to do a small project named a small plane game.

Requirements

  1. Lookahead Self Learning is required in this project. The knowledge include struct and pointers in C. Especially for struct, you should know its advantage and usage. For pointer, you should know the deep thoughts.
  2. Debug method should be mastered such as breakpoint debug, binary debug or output debug.
  3. Good programming style should be mastered.

Let's build a game!

We need to know what roles at the game and the rules as well. The question is what rules we need.

In this project, we are going to build the small plane game. We have the following roles:

  1. Plane, the main character in the game, and the player can control the plane. Note that we use 1 to mark plane in the game prototype. 2.Obstacles, the second main characters in the game. It can move as an AI, and when it hits the player, game over. Note that we use 2 to mark obstacles in game prototype.
  2. The map, the scene to play on. And the map display the objects in the map. Like the image, we can regard it as a map in the game. image

Game Structure Code

#ifndef GAME_H
#define GAME_H

typedef struct point {
  int x;
  int y;
} point;

typedef enum objects {BLANK = 0, PLANE = 1, OBSTACLE = 2} OBJ;
typedef enum operations {UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3} OP;

#define GAME_ROW 2
#define GAME_COL 16

typedef struct gamePane {
  // the game pane, 0 for blank, 1 for plane and 2 for obstacle
  OBJ pane[GAME_ROW][GAME_COL];
  point cursor;
  int gameOver;
} gamePane;

typedef struct plane {
  point position;
} plane;

typedef struct obstacle {
  point position;
} obstacle;

// function list
void init(gamePane * thePane, plane * thePlane, obstacle * theObstacle1, obstacle * theObstacle2);
void setCursor(gamePane * thePane, const int x, const int y);
void setplane(gamePane * thePane, plane * plane);
void setobstacle(gamePane * thePane, obstacle * obstacle);
void removeOBJ(gamePane * thePane, const point p);
void gameOver();
void printPane(const gamePane thePane);
void movePlane(gamePane * thePane, plane * thePlane, OP op);
void moveObstacle(gamePane * thePane, obstacle * theObstacle);
#endif // GAME_H

Game Source Code

void init(gamePane * thePane, plane * thePlane, obstacle * theObstacle1, obstacle * theObstacle2) {
  thePlane->position.x = 0;
  thePlane->position.y = 0;
  theObstacle1->position.x = 0;
  theObstacle1->position.y = GAME_COL-1;
  theObstacle2->position.x = 1;
  theObstacle2->position.y = GAME_COL-3;

  int i, j;

  for(i = 0; i < GAME_ROW; i++) {
    for(j = 0; j < GAME_COL; j++) {
      thePane->pane[i][j] = BLANK;
    }
  }

  setplane(thePane, thePlane);
  setobstacle(thePane, theObstacle1);
  setobstacle(thePane, theObstacle2);
  thePane->gameOver = 0;
  thePane->cursor.x = 0;
  thePane->cursor.y = 0;
}

// basic operation for game pane
void setCursor(gamePane * thePane, const int x, const int y) {
  thePane->cursor.x = x;
  thePane->cursor.y = y;
}

void setplane(gamePane * thePane, plane * plane) {
  if(thePane->pane[plane->position.x][plane->position.y] == OBSTACLE) {
    thePane->gameOver = 1;
    gameOver();
    return;
  }
  thePane->pane[plane->position.x][plane->position.y] = PLANE;
}

void setobstacle(gamePane * thePane, obstacle * obstacle) {
  if(thePane->pane[obstacle->position.x][obstacle->position.y] == PLANE) {
    thePane->gameOver = 1;
    gameOver();
    return;
  }
  thePane->pane[obstacle->position.x][obstacle->position.y] = OBSTACLE;
}

void removeOBJ(gamePane * thePane, const point p) {
  thePane->pane[p.x][p.y] = BLANK;
}

void gameOver() {
  printf("GameOver\n");
}

void printPane(const gamePane thePane) {
  int i, j;
  for(i = 0; i < GAME_ROW; i++) {
    for(j = 0; j < GAME_COL; j++) {
      printf("%d", thePane.pane[i][j]);
    }
    printf("\n\n");
  }
}

// operation for plane
void movePlane(gamePane * thePane, plane * thePlane, OP op) {
  removeOBJ(thePane, thePlane->position);
  switch(op) {
  case 0 :
    if(thePlane->position.x > 0) {
      thePlane->position.x--;
    }
    break;
  case 1 :
    if(thePlane->position.x < GAME_ROW - 1) {
      thePlane->position.x++;
    }
    break;
  case 2:
    if(thePlane->position.y > 0) {
      thePlane->position.y--;
    }
    break;
  case 3:
    if(thePlane->position.y < GAME_COL - 1) {
      thePlane->position.y++;
    }
    break;
  }
  setplane(thePane, thePlane);
}

//operations for obstacle
void moveObstacle(gamePane * thePane, obstacle * theObstacle) {
  removeOBJ(thePane, theObstacle->position);
  if(theObstacle->position.y <= 0) {
    theObstacle->position.y = GAME_COL - 1;
  }

  theObstacle->position.y--;

  setobstacle(thePane, theObstacle);
}

Assignment 0

You do not need to write this part to your report. Please use markdown to write your report.

  1. What's the meaning of the code, please write comments(注释) for each functions as well as the important expressions and statements. 2.The header declarations and source code of the game are provided. But we can not run the game without a game launch function(main function), please write a main function to run the game.

Assignment 1

As you play the game, the interface of the game is very ugly. And also the game is not a game at all. Your job is to modify the game using your idea.

Some advice:

  1. Add bullet for the plane, which means that you can control the plane to hit the obstacles.
  2. Add score counter, and a list of score. And it can record the ranking of the score every you plane it. You should c file IO in this part.
  3. Large plane Scene, get a large Play Scene to the game.
  4. Muti-obstcales that the obstacles can spawn a bullet or any other idea.

Grade

(55pts) Your personal runable game. (25pts) Your Document about your game elucidatio. And your algorithm to get to your idea game. (20pts) The Style of your source code. Besides google style check, you should have good indent in the source code and you are supposed to write good comments(充分的代码注释) for you game.

Notice that, you should upload you game project to your github repository and write the documentation using markdown and named it "Readme.md".

You will get bonus in the final grade.

Submit

just send me your github address.

Deadline

2015 12.15 18:00


Reply to this email directly or view it on GitHub: https://github.com/wujr5/c-and-cpp-language-learning/issues/31

ghostbody commented 8 years ago

@Icenowy 对软件班的,计科班的还没出。

Icenowy commented 8 years ago

哦。属于加分作业?

---- Ye Jiaqi编写 ----

@Icenowy 对软件班的,计科班的还没出。


Reply to this email directly or view it on GitHub: https://github.com/wujr5/c-and-cpp-language-learning/issues/31#issuecomment-159147620

ghostbody commented 8 years ago

@Icenowy 是