程序在 7 分钟后被杀死(c 语言使用 curses.h)

问题描述 投票:0回答:0

我的程序运行了 7 分钟,但随后我的电脑崩溃了,我有时会收到“被杀死”的消息,我知道我的内存溢出了,但我不知道在哪里……我不知道如何调试,所以请随意尝试。提前谢谢你,我真的被困住了,因为这是一个学校项目......



Game.c :

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include "../headers/game.h"

#include "../headers/structs.h"

#include "../headers/print.h"

void init_poke(pokemon* pokenull, pokemon* charmander, pokemon* bulbasaur, pokemon* squirtle){

    

    //  PAS FAIT, SYSTEME À RÉALISER ENCORE

    //pokenull

    pokenull->atk=0;

    pokenull->def=0;

    pokenull->dodge=0;

    pokenull->pv=0;

    pokenull->lvl=0;

    pokenull->spetaux=0;

    pokenull->catchrate=0;

    pokenull->art=0;

    sprintf(pokenull->basicatk,"none");

    sprintf(pokenull->speatk,"none");

    sprintf(pokenull->name,"no-pokemon");

    //charmander -

    charmander->atk=10.0;

    charmander->def=30.0/100;

    charmander->dodge=18;

    charmander->pv=35.0;

    charmander->lvl=1;

    charmander->spetaux=30;

    charmander->catchrate=50;

    charmander->art=4;

    sprintf(charmander->basicatk,"fire-punch");

    sprintf(charmander->speatk,"flamethrower");

    sprintf(charmander->name,"charmander");

    

    //bulbasaur -

    bulbasaur->atk=10.0;

    bulbasaur->def=45.0/100;

    bulbasaur->dodge=10;

    bulbasaur->pv=40.0;

    bulbasaur->lvl=1;

    bulbasaur->spetaux=22;

    bulbasaur->catchrate=50;

    bulbasaur->art=1;

    sprintf(bulbasaur->basicatk,"bullet-seed");

    sprintf(bulbasaur->speatk,"solar-beam");

    sprintf(bulbasaur->name,"bulbasaur");

    //squirtle -

    squirtle->atk=10.0;

    squirtle->def=35.0/100;

    squirtle->dodge=15;

    squirtle->pv=35.0;

    squirtle->lvl=1;

    squirtle->spetaux=21;

    squirtle->catchrate=50;

    squirtle->art=7;

    sprintf(squirtle->basicatk,"water-gun");

    sprintf(squirtle->speatk,"hydropump");

    sprintf(squirtle->name,"squirtle");

}

void create_newplayer(trainer* newplayer){

    WINDOW* chatwin=newwin(LINES-1,COLS-1,0,0);

    WINDOW* write=subwin(chatwin,5,50,30,55);

    nodelay(stdscr,FALSE);

    echo();

    curs_set(1);

    box(chatwin,0,0);

    box(write,0,0);

    print_newtrainer(chatwin);

    wrefresh(chatwin);

    wmove(write,2,22);

    wscanw(write,"%s",newplayer->name);

    newplayer->lvl=1;

    newplayer->money=200;

    curs_set(0);

    nodelay(stdscr,TRUE);

    noecho();

    delwin(chatwin);

    delwin(write);

}

void get_firstpoke(trainer* player){

    int finish=0,ch=ERR;

    int x=50,y=60;

    while (finish==0)

    {

        WINDOW* pokewin=newwin(LINES-1,COLS-1,0,0);

        print_get_firstpoke(pokewin,x,y);   

        wrefresh(pokewin);

        ch=getch();

        switch (ch)

        {

        case 'd':

        case KEY_RIGHT:

            if (y==60)

            {

                y=115;

            }

            else if (y==115)

            {

                y=180;

            }

            break;

        case 'q':

        case KEY_LEFT :

            if (y==115)

            {

                y=60;

            }

            else if (y==180)

            {

                y=115;

            }

            break;

        case 'e':

        case '\n':

        case '\r':

            switch (y)

            {

            case 60:

                //trainer->poke1=bulbasaur

                finish=1;

                break;

            case 115:

                //trainer->poke1=charmander

                finish=1;

                break;

            case 180:

                //trainer->poke1=squirtle

                finish=1;

                break;

            

            default:

                break;

            }

            break;

        

        default:

            break;

        }  

        delwin(pokewin);

    } 

}

void menu(int* exit){

    int chmenu=ERR,menuexit=0;

    int x=13,y=90;

    

    while(chmenu!='m' && menuexit==0)

        {            

            WINDOW* winmenu=newwin(LINES/1.5,COLS/1.5,LINES/6,COLS/6);

            print_menu(winmenu,x,y);

            box(winmenu,0,0);

            wrefresh(winmenu);  

            

            chmenu=getch();                                         

            switch (chmenu)

            {

                case KEY_UP:

                case 'z':

                    if (x==13)

                    {

                        //nothing happens

                    }

                    else if (x==22)

                    {

                        x=13;

                    }

                    else if (x==31)

                    {

                        x=22;

                    }

                    break;

                case KEY_DOWN:

                case 's':

                    if (x==31)

                    {

                        //nothing happens

                    }

                    else if (x==22)

                    {

                        x=31;

                    }

                    else if (x==13)

                    {

                        x=22;

                    }

                    break;

                case KEY_ENTER:

                case 'e':

                case '\r':

                case '\n':

                    switch (x)

                    {

                    case 31:

                        menuexit=1;

                        *exit=1;

                        break;

                    case 22:

                        chargement();

                        break;

                    case 13:

                        break;

                    }

                    break;

                default:

                    break;

            } 

            usleep(16667);

            delwin(winmenu);

        }

}

void inventory(){

    int finish=0,ch=ERR;

    int x=12,y=17;

    while (finish==0)

    {

        WINDOW* sac = newwin(LINES/1.5,COLS/1.5,(LINES/6)+5,(COLS/6)+1);

        WINDOW* bag_array=newwin(5,COLS/1.5,(LINES/6),COLS/6+1);

        box(sac,0,0);

        mvwprintw(bag_array,1,12," ___   _   ___ ");

        mvwprintw(bag_array,1+1,12,"| _ ) /_\\ / __|");

        mvwprintw(bag_array,1+2,12,"| _ \\/ _ \\ (_ |");

        mvwprintw(bag_array,1+3,12,"|___/_/ \\_\\___|");

                                  

        ch=getch();

        print_inventory(sac,x,y);        

        wrefresh(sac);

        wrefresh(bag_array);

        switch (ch)

        {

        case 'i':

            finish=1;

            break;

        case 'q' :

        case KEY_LEFT :

            if (y!=17)

            {

                y-=40;

            }

            break;

        case 'd' :

        case KEY_RIGHT :

            if (y!=137)

            {

                y+=40;

            }

            break;

        case 'z' :

        case KEY_UP :

            if (x==39)

            {

                x=26;

            }

            else if (x==26)

            {

                x=12;

            }

            break;

        case 's' :

        case KEY_DOWN :

            if (x==12)

            {

                x=26;

            }

            else if (x==26)

            {

                x=39;

            }

            break;

        

        default:

            break;

        }

        delwin(sac);

        delwin(bag_array);

        

    }

}

void main_menu(trainer* player,int* exit,int* x, int* y){

    WINDOW* win=newwin(LINES-1,COLS-1,0,0);

    int ch=ERR;

    

    box(win,0,0);

    ch=getch();

    print_main_menu(win,*x,*y);

    wrefresh(win);

    switch (ch)

    {

    case 'z':

    case KEY_UP:

        if (*x==38)

        {

            *x=28;

        }

        

        break;

    case 's':

    case KEY_DOWN:

        if (*x==28)

        {

            *x=38;

        }

        break; 

    case 'e' :

    case '\n' :

    case '\r' :

        switch (*x)

        {

        case 28:

            create_newplayer(player);

            get_firstpoke(player);

            *exit=1;

            chargement();

            break;

        case 38:

            *exit=1;

            chargement();

            break;

        

        default:

            break;

        }

        break;  

    

    default:

        break;

    }

    usleep(16667);

    delwin(win);

}

void game(int* exit,int* l,int* c){

    int ch=ERR;int i;int j;

    WINDOW* map = newwin(170,500, 0, 0);

    WINDOW* fenetre_backup= newwin(170, 500, 0, 0);

    WINDOW* cadre= subwin(map,111, 266, 29,116);      //cadre = map réelle : origine sur map (29;116), dimensions (111;268)

    WINDOW* cam=subwin(map,LINES-1,COLS-1,*l,*c);

    box(cadre,0,0);                                   //repérage : x_map = x_cadre + 29      y_map = y_cadre + 116

    box(fenetre_backup,0,0);

    fenetre_backup=dupwin(map);

    mvwin(cam,0,0);

    print_player(cam);

    create_map(map); 

    ch=getch();

    switch (ch)

    {

        case KEY_UP:

        case 'z':

            if (*l!=0)  //physic bordermap

            {

                *l=*l-1;

            }

            if (*c<=36 && *l==54)   //physic pokeshop

            {

                *l=*l+1;

            }

            if (*c<=36 && *l==14)   //physic home

            {

                *l=*l+1;

            }

            if (*c>=220 && *l==14)   //physic lab

            {

                *l=*l+1;

            }

            if (*c>=220 && *l==54)   //physic house 3

            {

                *l=*l+1;

            }

            if (*c>=220 && *l==79)   //physic house 4

            {

                *l=*l+1;

            }

            if (*c>=246 && *l==23)   //physic "OUTDSIDE"

            {

                *l=*l+1;

            }

            break;

        case KEY_DOWN:

        case 's':

            if (*l!=103)    //physic bordermap

            {

                *l=*l+1;

            }

            if (*c<=36 && *l==39)   //physic pokeshop

            {

                *l=*l-1;

            }

            if (*c<=36 && *l==87)   //physic house 2

            {

                *l=*l-1;

            }

            if (*c>=220 && *l==37)   //physic house 3

            {

                *l=*l-1;

            }

            if (*c>=220 && *l==62)   //physic house 4

            {

                *l=*l-1;

            }

            if (*c>=220 && *l==87)   //physic house 5

            {

                *l=*l-1;

            }

            if (*c>=246 && *l==16)   //physic "OUTDSIDE"

            {

                *l=*l-1;

            }

            

            

            break;

        case KEY_RIGHT:

        case 'd':

            if (*c!=256)    //physic bordermap

            {

                *c=*c+2;

            }

            if (*c==220 && *l<=14)  //physic lab

            {

                *c=*c-2;

            }

            if (*c==220 && *l>=37 && *l<=54)  //physic house 3

            {

                *c=*c-2;

            }

            if (*c==220 && *l>=62 && *l<=79)  //physic house 4

            {

                *c=*c-2;

            }

            if (*c==220 && *l>=87)  //physic house 5

            {

                *c=*c-2;

            }

            if (*c==246 && *l>=16 && *l<=23)  //physic "OUTSIDE"

            {

                *c=*c-2;

            }

            

            

            break;

        case KEY_LEFT:

        case 'q':

            if (*c!=0)  //physic bordermap

            {

                *c=*c-2;

            }

            

            if (*c==36 && *l>=39 && *l<=54) //physic pokeshop

            {

                *c=*c+2;

            }

            if (*c==36 && *l<=14) //physic home

            {

                *c=*c+2;

            }

            

            if (*c==36 && *l>=87) //physic house 2

            {

                *c=*c+2;

            }

            

            break;

        case 'm':

            menu(exit);

            break;

        case 'i':

            inventory();

            break;

        case 'p':

            *exit=1;

                break;

        default:

            break;

    }                 

    wrefresh(cam);       

    map=dupwin(fenetre_backup);

    usleep(16667);

    delwin(cam);

    delwin(map);

    delwin(cadre);

    delwin(fenetre_backup);

}


-------------------------

Main.c:



#include <curses.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include "../headers/game.h"

#include "../headers/structs.h"

#include "../headers/print.h"

int main(){

    initscr();

    cbreak();

    noecho();

    start_color();

    nodelay(stdscr,TRUE);

    keypad(stdscr,TRUE);

    curs_set(0);

    if(has_colors()==FALSE){

        printf("Your terminal does not support colors");

        exit(1);

    }

    if(can_change_color()==FALSE){

        printf("Your terminal does not support color changing");

        exit(1);

    }

    //set fullscreen if not already set

    if (LINES== 59 || LINES<63 && COLS<236)

    {

        system("xdotool key FN+F11"); //sudo apt install xdotool

    }  

    int exit=0,exitmenu=0;

    int l=52,c=130; //position caméra

    int x=28,y=8; //position curseur menu principal

    trainer player;

    pokemon pokenull, charmander, squirtle, bulbasaur;

    while (exit==0)

    {

        while (exitmenu==0)

        {

            main_menu(&player,&exitmenu,&x,&y);

        }

        game(&exit,&l,&c);

    }

    

    endwin();

    return 0;

}


------------------

我只放了 3 个代码中的 2 个,但最后一个只是印刷品。

c crash game-development ncurses curses
© www.soinside.com 2019 - 2024. All rights reserved.