c语言小游戏代码大全
在C语言中,你可以实现各种小游戏。
1. 猜数字游戏
c#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guess, number, attempts = 0;
// 生成随机数
srand(time(NULL));
number = rand() % 100 + 1; // 生成1到100的随机数
printf("欢迎来到猜数字游戏!\n");
printf("猜一个1到100之间的数字。\n");
do {
printf("请输入你的猜测: ");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("太大了!\n");
} else if (guess < number) {
printf("太小了!\n");
} else {
printf("恭喜你,猜对了!你用了%d次尝试。\n", attempts);
}
} while (guess != number);
return 0;
}
2. 井字棋游戏
c#include <stdio.h>
// 定义常量
#define SIZE 3
// 初始化游戏板
void initializeBoard(char board[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = ' ';
}
}
}
// 打印游戏板
void printBoard(char board[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c", board[i][j]);
if (j < SIZE - 1) {
printf(" | ");
}
}
printf("\n");
if (i < SIZE - 1) {
printf("---------\n");
}
}
printf("\n");
}
// 检查是否有玩家获胜
int checkWin(char board[SIZE][SIZE], char player) {
// 检查行和列
for (int i = 0; i < SIZE; i++) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return 1; // 有玩家获胜
}
}
// 检查对角线
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return 1; // 有玩家获胜
}
return 0; // 没有玩家获胜
}
int main() {
char board[SIZE][SIZE];
int row, col;
char currentPlayer = 'X';
int moves = 0;
initializeBoard(board);
do {
printBoard(board);
// 获取玩家输入
printf("玩家 %c,请输入行号和列号:", currentPlayer);
scanf("%d %d", &row, &col);
// 检查输入的合法性
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || board[row][col] != ' ') {
printf("无效的输入,请重新输入。\n");
continue;
}
// 更新游戏板
board[row][col] = currentPlayer;
moves++;
// 检查是否有玩家获胜
if (checkWin(board, currentPlayer)) {
printBoard(board);
printf("玩家 %c 胜利!\n", currentPlayer);
break;
}
// 切换玩家
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
// 检查是否平局
if (moves == SIZE * SIZE) {
printBoard(board);
printf("游戏平局!\n");
break;
}
} while (1);
return 0;
}
这两个例子只是C语言小游戏的开始,你可以根据自己的兴趣和需求进行修改和扩展。希望这些代码能够帮助你入门C语言游戏编程!
3. 俄罗斯方块游戏
c#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 10
#define HEIGHT 20
int board[WIDTH][HEIGHT] = {0};
struct Point {
int x, y;
};
struct Block {
struct Point points[4];
};
// 初始化游戏板
void initializeBoard() {
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
board[i][j] = 0;
}
}
}
// 显示游戏板
void displayBoard() {
system("cls"); // 清屏
for (int j = 0; j < HEIGHT; j++) {
for (int i = 0; i < WIDTH; i++) {
if (board[i][j] == 0) {
printf(" .");
} else {
printf(" *");
}
}
printf("\n");
}
}
// 在游戏板上绘制方块
void drawBlock(struct Block block, int value) {
for (int i = 0; i < 4; i++) {
int x = block.points[i].x;
int y = block.points[i].y;
board[x][y] = value;
}
}
// 检查方块是否可以移动
int canMove(struct Block block, int offsetX, int offsetY) {
for (int i = 0; i < 4; i++) {
int x = block.points[i].x + offsetX;
int y = block.points[i].y + offsetY;
// 检查是否越界或与其他方块重叠
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT || board[x][y] != 0) {
return 0;
}
}
return 1;
}
// 移动方块
void moveBlock(struct Block *block, int offsetX, int offsetY) {
for (int i = 0; i < 4; i++) {
block->points[i].x += offsetX;
block->points[i].y += offsetY;
}
}
// 旋转方块
void rotateBlock(struct Block *block) {
int centerX = block->points[1].x;
int centerY = block->points[1].y;
for (int i = 0; i < 4; i++) {
int x = block->points[i].x;
int y = block->points[i].y;
// 使用旋转矩阵进行旋转
int newX = centerX + centerY - y;
int newY = centerY - centerX + x;
block->points[i].x = newX;
block->points[i].y = newY;
}
}
// 主游戏循环
void gameLoop() {
struct Block currentBlock;
int gameover = 0;
int score = 0;
while (!gameover) {
// 生成一个新的方块
currentBlock.points[0].x = WIDTH / 2 - 1;
currentBlock.points[0].y = 0;
currentBlock.points[1].x = WIDTH / 2;
currentBlock.points[1].y = 0;
currentBlock.points[2].x = WIDTH / 2 - 1;
currentBlock.points[2].y = 1;
currentBlock.points[3].x = WIDTH / 2;
currentBlock.points[3].y = 1;
// 游戏循环中的每一帧
while (1) {
displayBoard();
drawBlock(currentBlock, 1);
// 检查用户输入
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'a':
if (canMove(currentBlock, -1, 0)) {
moveBlock(¤tBlock, -1, 0);
}
break;
case 'd':
if (canMove(currentBlock, 1, 0)) {
moveBlock(¤tBlock, 1, 0);
}
break;
case 's':
if (canMove(currentBlock, 0, 1)) {
moveBlock(¤tBlock, 0, 1);
}
break;
case 'w':
struct Block rotatedBlock = currentBlock;
rotateBlock(&rotatedBlock);
if (canMove(rotatedBlock, 0, 0)) {
currentBlock = rotatedBlock;
}
break;
case 'q':
gameover = 1;
break;
}
}
// 检查方块是否能向下移动
if (canMove(currentBlock, 0, 1)) {
moveBlock(¤tBlock, 0, 1);
} else {
// 将方块固定在游戏板上
drawBlock(currentBlock, 1);
// 检查是否有行被填满
for (int y = HEIGHT - 1; y >= 0; y--) {
int rowFull = 1;
for (int x = 0; x < WIDTH; x++) {
if (board[x][y] == 0) {
rowFull = 0;
break;
}
}
// 如果一行被填满,消除该行并增加分数
if (rowFull) {
for (int i = y; i > 0; i--) {
for (int x = 0; x < WIDTH; x++) {
board[x][i] = board[x][i - 1];
}
}
score += 10;
}
}
// 生成新的方块
break;
}
Sleep(100); // 控制游戏速度
}
}
}
int main() {
initializeBoard();
gameLoop();
return 0;
}
这是一个简单的俄罗斯方块游戏的实现。通过“a”、“d”、“s”和“w”键控制方块的左移、右移、下移和旋转,按“q”键退出游戏。游戏循环中的每一帧显示当前游戏板状态,并在方块无法下移时固定在游戏板上。当一行被填满时,该行消失,玩家得分增加。游戏通过方向键和空格键等输入进行交互。