chrome小恐龙c语言代码
时间: 2023-07-25 10:26:17 浏览: 819
chrome小恐龙游戏图片资源
以下是 Chrome 小恐龙游戏的 C 语言实现,可以在控制台中运行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 50
#define HEIGHT 10
int score = 0;
int speed = 100;
void clrscr() {
system("cls");
}
void gotoxy(int x, int y) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void draw(int x, int y) {
gotoxy(x, y);
printf("*");
}
void erase(int x, int y) {
gotoxy(x, y);
printf(" ");
}
void print_score() {
gotoxy(WIDTH + 5, 1);
printf("Score: %d", score);
}
void print_speed() {
gotoxy(WIDTH + 5, 2);
printf("Speed: %d", speed);
}
int main() {
int dino_x = 10;
int dino_y = HEIGHT - 2;
int obs_x = WIDTH;
int obs_y = dino_y;
int jump = 0;
clrscr();
while (1) {
// draw dino
draw(dino_x, dino_y);
// draw obstacle
erase(obs_x, obs_y);
if (obs_x == 1) {
obs_x = WIDTH;
score += 10;
if (score % 50 == 0 && speed > 10) {
speed -= 10;
}
} else {
draw(obs_x, obs_y);
obs_x--;
}
// print score and speed
print_score();
print_speed();
// check collision
if (dino_x == obs_x && dino_y == obs_y) {
break;
}
// jump
if (_kbhit()) {
char c = _getch();
if (c == ' ') {
jump = 1;
}
}
if (jump == 1) {
dino_y--;
} else {
if (dino_y < HEIGHT - 2) {
dino_y++;
}
}
if (jump == 1 && dino_y == HEIGHT - 5) {
jump = 0;
}
// delay
Sleep(speed);
}
clrscr();
printf("Game Over! Score: %d", score);
return 0;
}
```
该代码实现了一个简单的控制台小游戏,玩家需要控制小恐龙躲避障碍物,直到碰到障碍物游戏结束。玩家可以通过空格键使小恐龙跳跃,得分会随着时间的推移而增加,游戏难度也会随之增加。
阅读全文