c++ 小游戏编程源代码
时间: 2023-12-04 11:04:13 浏览: 55
很高兴回答您的问题。以下是两个C++小游戏的源代码:
1. 俄罗斯方块游戏源代码
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
const int WIDTH = 10;
const int HEIGHT = 20;
const int BLOCK_SIZE = 4;
const int SHAPE_TYPES = 7;
const int SHAPES[SHAPE_TYPES][BLOCK_SIZE][BLOCK_SIZE] = {
{
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 3, 3, 0},
{3, 3, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{4, 4, 0, 0},
{0, 4, 4, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 5, 0, 0},
{5, 5, 5, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 6, 0, 0},
{0, 6, 6, 0},
{0, 6, 0, 0}
},
{
{0, 0, 0, 0},
{0, 7, 0, 0},
{0, 7, 7, 0},
{0, 0, 7, 0}
}
};
int board[HEIGHT][WIDTH] = {0};
void draw_block(int x, int y, int shape_type, int angle, int value) {
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
if (SHAPES[shape_type][i][j] == value) {
int px = x + j;
int py = y + i;
if (px >= 0 && px < WIDTH && py >= 0 && py < HEIGHT) {
board[py][px] = angle + 1;
}
}
}
}
}
void draw_board() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
cout << ".";
} else {
cout << board[i][j];
}
}
cout << endl;
}
}
bool check_collision(int x, int y, int shape_type, int angle) {
for (int i = 0; i < BLOCK_SIZE; i++) {
for (int j = 0; j < BLOCK_SIZE; j++) {
if (SHAPES[shape_type][i][j] != 0) {
int px = x + j;
int py = y + i;
if (px < 0 || px >= WIDTH || py >= HEIGHT) {
return true;
}
if (py >= 0 && board[py][px] != 0) {
return true;
}
}
}
}
return false;
}
void clear_board() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
board[i][j] = 0;
}
}
}
void remove_full_lines() {
int k = HEIGHT - 1;
for (int i = HEIGHT - 1; i >= 0; i--) {
bool full = true;
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
full = false;
break;
}
}
if (!full) {
for (int j = 0; j < WIDTH; j++) {
board[k][j] = board[i][j];
}
k--;
}
}
for (int i = k; i >= 0; i--) {
for (int j = 0; j < WIDTH; j++) {
board[i][j] = 0;
}
}
}
int main() {
srand(time(NULL));
int x = WIDTH / 2 - BLOCK_SIZE / 2;
int y = 0;
int shape_type = rand() % SHAPE_TYPES;
int angle = 0;
int score = 0;
bool game_over = false;
while (!game_over) {
if (check_collision(x, y + 1, shape_type, angle)) {
draw_block(x, y, shape_type, angle, 1);
remove_full_lines();
clear_board();
x = WIDTH / 2 - BLOCK_SIZE / 2;
y = 0;
shape_type = rand() % SHAPE_TYPES;
angle = 0;
if (check_collision(x, y, shape_type, angle)) {
game_over = true;
}
} else {
y++;
}
if (!game_over) {
draw_board();
if (_kbhit()) {
int key = _getch();
switch (key) {
case 'a':
if (!check_collision(x - 1, y, shape_type, angle)) {
x--;
}
break;
case 'd':
if (!check_collision(x + 1, y, shape_type, angle)) {
x++;
}
break;
case 'w':
if (!check_collision(x, y, shape_type, (angle + 1) % 4)) {
angle = (angle + 1) % 4;
}
break;
case 's':
while (!check_collision(x, y + 1, shape_type, angle)) {
y++;
}
break;
}
}
draw_block(x, y, shape_type, angle, 1);
Sleep(100);
}
}
cout << "Game over! Score: " << score << endl;
return 0;
}
```
2. 贪吃蛇游戏源代码
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int WIDTH = 20;
const int HEIGHT = 20;
const int MAX_LENGTH = WIDTH * HEIGHT;
const int DX[4] = {0, 0, -1, 1};
const int DY[4] = {-1, 1, 0, 0};
int board[HEIGHT][WIDTH] = {0};
int snake_x[MAX_LENGTH] = {0};
int snake_y[MAX_LENGTH] = {0};
int snake_length = 3;
int snake_direction = 3;
int food_x = 0;
int food_y = 0;
void draw_board() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (board[i][j] == 0) {
cout << ".";
} else if (board[i][j] == 1) {
cout << "#";
} else if (board[i][j] == 2) {
cout << "o";
} else if (board[i][j] == 3) {
cout << "x";
}
}
cout << endl;
}
}
void generate_food() {
while (true) {
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
if (board[y][x] == 0) {
food_x = x;
food_y = y;
board[y][x] = 2;
break;
}
}
}
void move_snake() {
int new_x = snake_x[0] + DX[snake_direction];
int new_y = snake_y[0] + DY[snake_direction];
if (new_x < 0 || new_x >= WIDTH || new_y < 0 || new_y >= HEIGHT) {
snake_length = 0;
return;
}
if (board[new_y][new_x] == 1) {
snake_length = 0;
return;
}
if (board[new_y][new_x] == 2) {
snake_length++;
if (snake_length > MAX_LENGTH) {
snake_length = MAX_LENGTH;
}
generate_food();
} else {
board[snake_y[snake_length - 1]][snake_x[snake_length - 1]] = 0;
}
for (int i = snake_length - 1; i > 0; i--) {
snake_x[i] = snake_x[i - 1];
snake_y[i] = snake_y[i - 1];
}
snake_x[0] = new_x;
snake_y[0] = new_y;
for (int i = 0; i < snake_length; i++) {
board[snake_y[i]][snake_x[i]] = 1;
}
}
int main() {
srand(time(NULL));
snake_x[0] = WIDTH / 2;
snake_y[0] = HEIGHT / 2;
board[snake_y[0]][snake_x[0]] = 1;
generate_food();
while (snake_length > 0) {
if (_kbhit()) {
int key = _getch();
if (key == 'w' && snake_direction != 1) {
snake_direction = 0;
} else if (key == 's' && snake_direction != 0) {
snake_direction = 1;
} else if (key == 'a' && snake_direction != 3) {
snake_direction = 2;
} else if (key == 'd' && snake_direction != 2) {
snake_direction = 3;
}
}
move_snake();
draw_board();
Sleep(100);
}
cout << "Game over!" << endl;
return 0;
}
```