Moon-Buggy的代码
时间: 2024-01-13 19:02:43 浏览: 73
以下是 Moon-Buggy 的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <curses.h>
#include <time.h>
#define VERSION "1.0"
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define WIDTH 64
#define HEIGHT 16
#define PLAYER_POS_X 4
#define PLAYER_POS_Y (HEIGHT-3)
#define DISTANCE_PER_FRAME 8
#define OBSTACLE_PERIOD 8
#define JUMP_DURATION 12
#define JUMP_HEIGHT 3
#define GRAVITY 1
#define SPRITE_GAP 2
static char player_up[] = {'O',0,'-','^',0,'|',0,0,'|',0,'^',0,'-',0,'O',0};
static char player_down[] = {'O',0,'-','v',0,'|',0,0,'|',0,'v',0,'-',0,'O',0};
static char obstacle1[] = {'*',' ',' ',0,'*','*',0,'*',' ',0,'*','*',0,'*',' ',0,'*',0};
static char obstacle2[] = {'*',0,0,'*',' ',0,'*','*',0,'*','*',0,'*',' ',0,'*',0,0};
static char *obstacles[] = {obstacle1, obstacle2};
static int sprite_widths[] = {16, 17};
static int obstacle_probabilities[] = {60, 40};
static int obstacle_y[] = {HEIGHT-3, HEIGHT-4};
static char *title[] = {
"",
" Moon Buggy",
"",
"",
"",
" Press SPACE to start",
" Press Q to quit",
"",
" Version " VERSION,
"",
"",
"",
"",
"",
"",
NULL
};
static char *gameover[] = {
"",
"",
"",
" GAME OVER",
"",
"",
"",
" Press SPACE to restart",
" Press Q to quit",
"",
"",
"",
"",
"",
"",
NULL
};
static char *score_prefix[] = {
"SCORE: ",
NULL
};
static char score[16];
static int obstacle_pos = WIDTH-1;
static int obstacle_type = 0;
static int obstacle_speed = 1;
static int obstacle_visible = 0;
static int player_pos_y = PLAYER_POS_Y;
static int player_speed_y = 0;
static int player_jump_remaining = 0;
static int distance = 0;
static int score_length;
static int frame_count = 0;
static int frame_rate = 20;
static int frame_period = 1000/frame_rate;
static int screen_width = WIDTH+1;
static int screen_height = HEIGHT+4;
static WINDOW *win;
static void draw_sprite(int x, int y, char *sprite, int width)
{
int i, j;
for (i = 0; i < width; i++) {
if (sprite[i]) {
mvwaddch(win, y, x+i, sprite[i]);
}
}
}
static void draw_title()
{
int i;
int y = (screen_height - sizeof(title)/sizeof(title[0])) / 2;
for (i = 0; title[i]; i++) {
mvwaddstr(win, y+i, (screen_width-strlen(title[i]))/2, title[i]);
}
}
static void draw_gameover()
{
int i;
int y = (screen_height - sizeof(gameover)/sizeof(gameover[0])) / 2;
for (i = 0; gameover[i]; i++) {
mvwaddstr(win, y+i, (screen_width-strlen(gameover[i]))/2, gameover[i]);
}
}
static void draw_score()
{
sprintf(score, "%d", distance);
mvwaddstr(win, 0, screen_width-score_length-1, score_prefix[0]);
wattron(win, A_REVERSE);
mvwaddstr(win, 0, screen_width-score_length-1+strlen(score_prefix[0]), score);
wattroff(win, A_REVERSE);
}
static void draw_player()
{
if (player_jump_remaining) {
draw_sprite(PLAYER_POS_X, player_pos_y, player_up, sprite_widths[0]);
} else {
draw_sprite(PLAYER_POS_X, player_pos_y, player_down, sprite_widths[1]);
}
}
static void draw_obstacle()
{
if (obstacle_visible) {
draw_sprite(obstacle_pos, obstacle_y[obstacle_type], obstacles[obstacle_type], sprite_widths[obstacle_type]);
}
}
static void draw()
{
werase(win);
draw_score();
draw_player();
draw_obstacle();
wrefresh(win);
}
static void update_player()
{
if (player_jump_remaining) {
player_pos_y -= player_speed_y;
player_speed_y -= GRAVITY;
if (player_pos_y >= PLAYER_POS_Y) {
player_jump_remaining = 0;
player_pos_y = PLAYER_POS_Y;
player_speed_y = 0;
}
}
}
static void update_obstacle()
{
if (++frame_count % OBSTACLE_PERIOD == 0) {
if (!obstacle_visible) {
obstacle_visible = 1;
obstacle_type = rand() % 2;
obstacle_pos = WIDTH - sprite_widths[obstacle_type];
obstacle_speed = 1 + rand() % 3;
}
}
if (obstacle_visible) {
obstacle_pos -= obstacle_speed;
if (obstacle_pos < -sprite_widths[obstacle_type]) {
obstacle_visible = 0;
}
}
}
static void update_distance()
{
distance += DISTANCE_PER_FRAME;
score_length = strlen(score_prefix[0]) + strlen(score);
}
static void handle_input()
{
int c = getch();
if (c == ' ') {
if (player_pos_y == PLAYER_POS_Y) {
player_jump_remaining = JUMP_DURATION;
player_speed_y = JUMP_HEIGHT;
} else if (!obstacle_visible) {
distance = 0;
}
} else if (c == 'q') {
endwin();
exit(0);
}
}
static void init()
{
initscr();
cbreak();
noecho();
curs_set(0);
nodelay(stdscr, TRUE);
win = newwin(screen_height, screen_width, 0, 0);
srand(time(NULL));
}
static void cleanup()
{
delwin(win);
endwin();
}
static void game_loop()
{
for (;;) {
update_player();
update_obstacle();
update_distance();
draw();
handle_input();
if (player_pos_y == PLAYER_POS_Y && obstacle_visible && obstacle_type == 0 &&
obstacle_pos == PLAYER_POS_X && obstacle_y[0] == PLAYER_POS_Y) {
draw_gameover();
wrefresh(win);
while (getch() != ' ') {
/* wait for space */
}
distance = 0;
obstacle_visible = 0;
player_pos_y = PLAYER_POS_Y;
player_speed_y = 0;
player_jump_remaining = 0;
frame_count = 0;
}
napms(frame_period);
}
}
int main(int argc, char *argv[])
{
init();
draw_title();
wrefresh(win);
while (getch() != ' ') {
/* wait for space */
}
game_loop();
cleanup();
return 0;
}
```
这个代码是一个使用 curses 库的 Moon-Buggy 游戏。游戏中,玩家需要控制一辆月球车,躲避障碍物并尽可能地前进,直到撞上障碍物或按下 Q 键退出游戏。
阅读全文