来一个c语言写的游戏代码
时间: 2023-04-03 13:02:43 浏览: 64
以下是一个简单的 C 语言游戏代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guess, num, tries = 0;
srand(time(0));
num = rand() % 100 + 1;
printf("猜数字游戏!\n");
do {
printf("请输入一个 1 到 100 之间的整数:");
scanf("%d", &guess);
tries++;
if (guess > num) {
printf("太大了!\n");
} else if (guess < num) {
printf("太小了!\n");
} else {
printf("恭喜你,猜对了!你用了 %d 次机会。\n", tries);
}
} while (guess != num);
return 0;
}
相关问题
C语言 写一个游戏代码
好的,我可以帮你讲解一下 C 语言写游戏的基本流程。
首先,游戏需要一个界面来展示游戏的内容,你可以通过 C 语言的图形库来实现游戏界面的绘制,比如使用 SDL 或者 OpenGL 等库。
其次,游戏需要一个游戏循环,来不断刷新游戏界面并响应用户输入。在游戏循环中,你可以使用 C 语言的控制结构和函数调用来完成游戏逻辑的处理和绘制。
最后,你需要定义游戏的规则和设定游戏的目标,让玩家通过游戏规则和目标来获得游戏乐趣和挑战。
以下是一个简单的 C 语言写游戏的示例代码,供你参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define BALL_SIZE 10
#define PADDLE_WIDTH 80
#define PADDLE_HEIGHT 10
#define PADDLE_SPEED 5
int main(int argc, char* argv[]) {
// 初始化 SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
// 创建游戏窗口
SDL_Window* window = SDL_CreateWindow("Pong Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Failed to create window: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
// 创建游戏渲染器
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
fprintf(stderr, "Failed to create renderer: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
// 设置游戏界面颜色
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// 创建球和球拍
SDL_Rect ball = {SCREEN_WIDTH / 2 - BALL_SIZE / 2, SCREEN_HEIGHT / 2 - BALL_SIZE / 2,
BALL_SIZE, BALL_SIZE};
SDL_Rect paddle = {SCREEN_WIDTH / 2 - PADDLE_WIDTH / 2, SCREEN_HEIGHT - PADDLE_HEIGHT - 10,
PADDLE_WIDTH, PADDLE_HEIGHT};
// 设置球的初始速度和方向
srand(time(NULL));
int ballSpeedX = (rand() % 2 == 0 ? 5 : -5);
int ballSpeedY = -5;
// 进入游戏循环
bool quit = false;
while (!quit) {
// 处理输入事件
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
} else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_LEFT:
paddle.x -= PADDLE_SPEED;
break;
case SDLK_RIGHT:
paddle.x += PADDLE_SPEED;
break;
}
}
}
// 更新游戏状态
ball.x += ballSpeedX;
ball.y += ballSpeedY;
if (ball.x < 0 || ball.x > SCREEN_WIDTH - BALL_SIZE) {
ballSpeedX = -ballSpeedX;
}
if (ball.y < 0) {
ballSpeedY = -ballSpeedY;
} else if (ball.y > SCREEN_HEIGHT - BALL_SIZE) {
quit = true;
}
if (SDL_HasIntersection(&ball, &paddle)) {
ballSpeedY = -ballSpeedY;
}
// 渲染游戏界面
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &paddle);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &ball);
SDL_RenderPresent(renderer);
}
// 清理游戏资源
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
```
这段代码实现的是一个简单的“乒乓球”游戏,使用 SDL 实现游戏窗口和界面的绘制,使用 SDL_Event 处理用户输入事件,而游戏逻辑和渲染则通过控制 ball 和 paddle 的位置来实现。
c语言写一个贪吃蛇游戏代码
以下是一个基本的C语言贪吃蛇游戏代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define WIDTH 30 // 游戏界面宽度
#define HEIGHT 20 // 游戏界面高度
int snake[WIDTH * HEIGHT][2]; // 蛇身
int food[2]; // 食物
int score = 0; // 得分
int direction = 2; // 蛇头方向,初始向右
int speed = 200; // 蛇移动速度,单位毫秒
void gotoxy(int x, int y) {
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void draw() {
system("cls");
for (int i = 0; i <= WIDTH + 1; i++) {
gotoxy(i, 0);
printf("#");
gotoxy(i, HEIGHT + 1);
printf("#");
}
for (int i = 0; i <= HEIGHT + 1; i++) {
gotoxy(0, i);
printf("#");
gotoxy(WIDTH + 1, i);
printf("#");
}
for (int i = 1; i <= score; i++) {
gotoxy(snake[i][0], snake[i][1]);
printf("*");
}
gotoxy(food[0], food[1]);
printf("@");
gotoxy(WIDTH + 3, 2);
printf("Score: %d", score);
}
void init() {
srand(time(NULL));
snake[1][0] = 5;
snake[1][1] = 5;
snake[2][0] = 4;
snake[2][1] = 5;
food[0] = rand() % (WIDTH - 2) + 2;
food[1] = rand() % (HEIGHT - 2) + 2;
draw();
}
void move() {
for (int i = score; i >= 2; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
switch (direction) {
case 1: // 上
snake[1][1]--;
break;
case 2: // 右
snake[1][0]++;
break;
case 3: // 下
snake[1][1]++;
break;
case 4: // 左
snake[1][0]--;
break;
}
if (snake[1][0] == food[0] && snake[1][1] == food[1]) {
score++;
food[0] = rand() % (WIDTH - 2) + 2;
food[1] = rand() % (HEIGHT - 2) + 2;
}
}
int main() {
init();
while (1) {
if (_kbhit()) {
int ch = _getch();
if (ch == 224) {
ch = _getch();
if ((ch == 72 && direction != 3) || (ch == 80 && direction != 1)
|| (ch == 77 && direction != 4) || (ch == 75 && direction != 2)) {
direction = ch == 72 ? 1 : (ch == 80 ? 3 : (ch == 77 ? 2 : 4));
}
} else if (ch == ' ') {
while (_getch() != ' ');
} else {
break;
}
}
move();
draw();
Sleep(speed);
}
return 0;
}
```
这里使用了Windows API函数`gotoxy`实现控制光标位置输出字符,同时使用了`_kbhit`和`_getch`函数实现键盘输入检测和读取。游戏的核心逻辑在`move`函数中实现,包括蛇的移动、食物的生成和得分的更新。