gpt4 book ai didi

c - 函数错误,在 'conflicting type'编译时出现 'C'

转载 作者:太空宇宙 更新时间:2023-11-04 06:17:05 24 4
gpt4 key购买 nike

我试图用 C 编译我的程序(使用 cygwin),但在尝试调用 hide_game() 和 show_game() 函数时,我不断收到“冲突类型”和“隐式声明”。如果有人可以帮助我解决这个问题,我将不胜感激!

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cab202_graphics.h>
#include <cab202_sprites.h>
#include <cab202_timers.h>
#include <curses.h>

bool helpPressed = false;
int was_visible[];
int score = 0;
int lives = 5;
sprite_id ship;
sprite_id ship;
#define S_WIDTH 13
#define S_HEIGHT 6
char * ship_image =
/**/ " ^ "
/**/ " / \\ "
/**/ " ^/ \\^ "
/**/ "( . Y . )"
/**/ "(BLACK LIVES)"
/**/ " ( MATTER ) ";

char * start_msg =
/**/ "----------------------------------------"
/**/ "- ******************** -"
/**/ "- The Diamonds of Doom -"
/**/ "- **************** -"
/**/ "- ******** -"
/**/ "----------------------------------------"
/**/ "- Controls -"
/**/ "- Quit : Q -"
/**/ "- Help : H -"
/**/ "- Move Left/Right : Arrow Keys -"
/**/ "- Shoot Bullet : Z X C -"
/**/ "----------------------------------------"
/**/ "- Press a key to play... -"
/**/ "----------------------------------------";

#define MAX_BD 10
sprite_id Bdiamond[MAX_BD];
#define BD_WIDTH 5
#define BD_HEIGHT 5
char * Bdiamond_image =
/**/ " 0 "
/**/ " 000 "
/**/ "00000"
/**/ " 000 "
/**/ " 0 ";

#define MAX_MD 20
sprite_id Mdiamond[MAX_MD];
#define MD_WIDTH 3
#define MD_HEIGHT 3
char * Mdiamond_image =
/**/ " 0 "
/**/ "000"
/**/ " 0 ";

#define MAX_LD 40
sprite_id diamond[MAX_LD];
#define LD_WIDTH 1
#define LD_HEIGHT 1
char * Ldiamond_image =
/**/ "0";

int bullet_index = 0;
#define MAX_BULLET 100
sprite_id bullet[MAX_BULLET];
#define BULLET_WIDTH 1
#define BULLET_HEIGHT 1
char * bullet_image =
/**/ "o";

char * msg_gameover =
/**/ "###########################"
/**/ "# #"
/**/ "# Game Over #"
/**/ "# Play again (y/n)? #"
/**/ "# #"
/**/ "###########################";

double ship_x, ship_y, l_bullet;

bool game_over = false;

void setup_ship(){
ship = sprite_create(
(screen_width() - S_WIDTH) / 2,
(screen_height() - S_HEIGHT) -3/2,
S_WIDTH,
S_HEIGHT,
ship_image);

sprite_draw (ship);
}

void setup_diamond() {

for (int i = 0; i < MAX_BD; i++){
Bdiamond[i] = sprite_create(
rand() % (screen_width()-BD_WIDTH),
3,
BD_WIDTH,
BD_HEIGHT,
Bdiamond_image );

sprite_turn_to (Bdiamond[i], 0.15, 0);
sprite_turn (Bdiamond[i], rand() % 360 );
sprite_draw (Bdiamond[i] );
}
}

void setup_bullet() {
for( int i = 0; i <=MAX_BULLET; i++){
bullet[i] = sprite_create(-1, -1, 1 , 1, bullet_image);
}
}

void wall_check(sprite_id bullet){
if( sprite_y(bullet) <= 3 && sprite_y(bullet) != -1){
sprite_move_to(bullet, -1, -1);
sprite_turn_to(bullet, 0, 0);

}
}

bool collision (sprite_id ship, sprite_id Bdiamond2) {
int lh = round(sprite_x(ship)), rh = lh + sprite_width(ship) -1;
int lz = round(sprite_x(Bdiamond2)), rz = lz + sprite_width(Bdiamond2) -1;
int th = round(sprite_y(ship)), bh = th + sprite_width(ship) -1;
int tz = round(sprite_y(Bdiamond2)), bz = tz + sprite_width(Bdiamond2) -1;

bool collided = true;

if ( bz < th ) collided = false;
else if ( bh < tz ) collided = false;
else if ( rz < lh ) collided = false;
else if ( rh < lz ) collided = false;

return collided;
}

void process_button() {
int key = get_char();

int xh = round(sprite_x(ship));
int yh = round(sprite_y(ship));

if ( ( 'a' == key || KEY_LEFT == key ) && xh > 1 ) {
sprite_move(ship, -1, 0);
}

else if ( ( 'd' == key || KEY_RIGHT == key ) && xh < screen_width() - sprite_width(ship) - 1 ) {
sprite_move(ship, +1, 0);
}
if ('x' == key){
sprite_move(bullet[bullet_index], xh + 7, yh);
sprite_turn_to(bullet[bullet_index], 0, -0.1);
bullet_index++;
}
if ('z' == key){
sprite_move(bullet[bullet_index], xh + 3, yh + 2);
sprite_turn_to(bullet[bullet_index], 0, -0.1);
bullet_index++;
}
if ('c' == key){
sprite_move(bullet[bullet_index], xh + 11, yh + 2);
sprite_turn_to(bullet[bullet_index], 0, -0.1);
bullet_index++;
}
if ('h' == key){
helpPressed = true;
hide_game();
sprite_draw(help_msg);
show_screen();
wait_char();
show_game();
}
}

void hide_game(){
for( int i= 0; i < MAX_BD; i++ ) {
if ( Bdiamond[i]->is_visible ) {
was_visible[i] = 1;
sprite_hide(Bdiamond[i]);
}
else {
was_visible[i] = 0;
}
}
sprite_hide(ship);
}

void show_game(){
for( int i= 0; i < MAX_BD; i++ ) {
if ( was_visible[i] == 1 ) {
sprite_show(Bdiamond[i]);
}
sprite_show(ship);
}

void process_diamond() {
for( int i= 0; i < MAX_BD; i++ ) {
if ( Bdiamond[i]->is_visible ) {
sprite_step( Bdiamond[i] );

if (collision (ship, Bdiamond[i] )) {
sprite_hide( Bdiamond[i] );
lives--;
}
}

int x_limit = screen_width(), y_limit = screen_height();
int x = round(sprite_x(Bdiamond[i]));
int y = round(sprite_y(Bdiamond[i]));
double dx = sprite_dx(Bdiamond[i]);
double dy = sprite_dy(Bdiamond[i]);
if ( (x <= 0) || (x >= screen_width() - sprite_width(Bdiamond[i])) ) dx = -dx;
if ( (y <= 2) || (y >= screen_height() - sprite_height(Bdiamond[i])) ) dy = -dy;
if ( dx != sprite_dx(Bdiamond[i]) || dy != sprite_dy(Bdiamond[i]) ) {
sprite_back(Bdiamond[i]);
sprite_turn_to(Bdiamond[i], dx, dy);
}
}
}

void process_bullet() {
for( int i = 0; i <= MAX_BULLET; i++){
sprite_step(bullet[i]);
wall_check(bullet[i]);

for(int j= 0; j< MAX_BD; j++)
{
if( sprite_visible(Bdiamond[j]) && collision(bullet[i], Bdiamond[j])) {
sprite_hide(Bdiamond[j]);
sprite_move_to(bullet[i], -1, -1);
sprite_turn_to(bullet[i], 0, 0);
score=score+1;
}
}

}

}


void draw_border(void) {
int left = 0, right = screen_width() - 1, top = 0, bottom = screen_height() - 1;
draw_line(left, top, right, top, '#');
draw_line(left, top + 2, right, top + 2, '#');
draw_line(left, bottom, right, bottom, '#');
draw_line(left, top, left, bottom, '#');
draw_line(right, top, right, bottom, '#');

int timer = 420;
draw_formatted(left + screen_width()/3, top + 1, "# Score: %d", score);
draw_formatted(left + 3, top + 1, "Lives: %d", lives);
draw_formatted(left + screen_width()*2/3, top + 1, "# Time: %d", timer);
//draw_double(10, 10, sprite_x(ship));
//draw_double(10, 11, sprite_y(ship));
//draw_int(10, 12, sprite_visible(ship));
}

void draw_game() {
clear_screen();
draw_border();
sprite_draw ( ship );

for (int i = 0; i < MAX_BD; i++ ) {
sprite_draw(Bdiamond[i] );
}

for( int i = 0; i <= MAX_BULLET; i++){
sprite_step(bullet[i]);
sprite_draw(bullet[i]);
}

}

void help() {
clear_screen();
int msg_height = 14;
int msg_width = 40;
sprite_id helpMsg = sprite_create(
(screen_width() - msg_width) / 2,
(screen_height() - msg_height) / 2,
msg_width, msg_height, start_msg);
sprite_draw(helpMsg);
show_screen();
wait_char();

}

int main () {
setup_screen();
help();
setup_ship();
setup_diamond();
setup_bullet();
show_screen();

while ( ! game_over && helpPressed == false;) {
process_button();
process_bullet();
process_diamond();
draw_game();
if ( bullet_index >= 100) {
bullet_index = 0;
}
show_screen();
timer_pause(10);
}
cleanup_screen;
}

最佳答案

这两个函数需要前向声明,编译器不知道hide_game() or show_game() 什么时候调用这些函数被调用。

process_button()前添加void hide_game();show_game();即可解决问题,通常添加这两个声明在文件的顶部。

关于c - 函数错误,在 'conflicting type'编译时出现 'C',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43259509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com