⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manix.c

📁 也是遗传算法的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-----------------------------------------------------------------------------+|									       || 				   ManIX				       ||									       |/             2001 some changes by Rudiger Koch to make it self contained and to adapt to Amygdala|	         	1989 Por Fernando J. G. Pereira .		       ||									       ||			   	 89-05-15				       ||			   	 89-11-05				       ||									       |+-----------------------------------------------------------------------------*/#include <stdio.h>#include <math.h>#include <unistd.h>#include <stdlib.h>#include <ctype.h>#include <string.h>#include <time.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include "screens.h"#include "pacman.h"#define N_DELAY 30000#define DOTSMELL 3.0#define GHOSTSMELL 50.0#define BFRUITSMELL 20.0#define CFRUITSMELL 15.0#define MAXFEELING  30#define BITMAP_HEIGHT	16#define BITMAP_WIDTH	16#define NAME_LENGHT	20#define MAX_LINES	22#define MAX_COL		40#define NIVEL_MAX	10#define NMAX_TABLE	10#define X_SCORE		8#define Y_SCORE 	24#define X_INICIAL	20#define Y_INICIAL	17#define X_ENTRADA	16#define Y_ENTRADA	11#define X_LENGHT	8#define Y_LENGHT	3/* How much less than completion do we need for a level */#define LEVEL_CHEAT 1200#define N_VIDAS		3#define VIDAS_POR_BONUS 8//#define SCORE_HUNT	500//#define SCORE_APPLE	400//#define SCORE_ORANGE	300#define SCORE_HUNT	0#define SCORE_APPLE	40#define SCORE_ORANGE	30#define TIMER_VALUE_1	80#define TIMER_VALUE_2	120#define TEC_INICIAIS	{ 'O', 'P', 'Q', 'A', 'H', 'C' }#define TEC_VI		{ 'H', 'L', 'K', 'J', 'A', 'Q' }#define AT( n ) ((n)<<4)#define LOW2UPPER(c) 	( (islower(c)) ? (toupper(c)) : (c) )#define RANDOM() (rand() % 256)    /*---------+| TypeDefs |+---------*/typedef struct Position{    int x, y ;}Position ;typedef char Ecra[MAX_LINES][MAX_COL] ;typedef enum { Left, Right, Up, Down } Direct ;struct _Player{    char name[NAME_LENGHT];    int  score,	 level,	 bonus,	 lives ;    Position CurrPos,	     ApagaPos ;    Direct manDir ;} Player ;typedef struct Keyboard{    char left,	 right,	 up,	 down,	 pause,	 stop ;} Keyboard ;struct _Ghost{    Position CurrPos,	     ApagaPos ;    Direct   OldMove ;} Ghosts[2*NIVEL_MAX+5] ;/*------------------+| Variaveis Globais |+------------------*/char LastOne, Ultima ;Keyboard TecCurr = TEC_INICIAIS ;Keyboard TecIni = TEC_INICIAIS ;struct _Player HighScores[NMAX_TABLE] ;Ecra ecra ;int total = 0 ;unsigned char Tecla ;int NivelInicial = 0 ;int NGhosts ;Direct Possibilidades[4] ;int timer = 0, armado = False, morte = False, NivelMudou = False ;int Score = 0 ;char string[80] ;Ecra cpPlayField;Senses feeling;/*----------------------------------+| Variaveis relacionas com XWindows |+----------------------------------*/Display *display ;int screen ;Window window ;Colormap cmap ;Font ch_font ;GC gc_yellow, gc_red, gc_green, gc_g_yellow, gc_blue, gc_orange, gc_cyan,   gc_gray, gc_brown, gc_pink, gc_violet, gc_magenta, gc_black, gc_white ;XSizeHints hints ;XColor	yellow, red, green, g_yellow, blue, orange, cyan,	gray, brown, pink, violet, magenta ;unsigned long black_pixel, white_pixel ;unsigned int depth ;Pixmap	bm_userup, bm_userdown, bm_userright, bm_userleft, bm_usereat;Pixmap	bm_ghost1, bm_ghost2, bm_ghost3 ;Pixmap	bm_space, bm_apple, bm_orange, bm_wall, bm_dot ;// Prototypesint Menu();void MudarNivel();void GerarMovimento();int LerTeclado();void initialize();void TopScores();void finalizacao();void PutGhost( int );void ImprimirGraficos();void ReadScreen( Ecra scr, int level );void GuiarXMan();void VerChoque();void IniciaJogo();void buscar_cores();void ImprimirEcra();void x_get_string( short x, short y, char* str, int len );void x_print_string(short x, short y, char* str, GC gc );void PAUSA();void Apagar();void put_object(short x, short y, Pixmap object );void criar_pixmaps();void criar_cursor();void criar_gcs();void resetFeeling();void initialize(){    char *disp_name = NULL;    Window root_window ;//    XColor exact ;    XEvent ev ;    srand( time( NULL ) ) ;    display = XOpenDisplay( disp_name ) ;    if( display == (Display *)NULL )    {	fprintf( stderr, "Cannot connect to server !\n" ) ;	exit( 1 ) ;    }    screen = DefaultScreen( display ) ;    root_window = DefaultRootWindow( display ) ;    buscar_cores() ;    ch_font = XLoadFont( display, "9x15" ) ;    hints.x = hints.y = 50 ;    hints.width = BITMAP_WIDTH * MAX_COL ;    hints.height = BITMAP_HEIGHT * ( MAX_LINES + 3 ) ;    hints.flags = PPosition | PSize ;    window = XCreateSimpleWindow( display, root_window, hints.x, hints.y,				  hints.width, hints.height, 2,				  yellow.pixel,  black_pixel) ;    XSetStandardProperties( display, window, "ManIX Game", "ManIX",			    None, NULL, 1, &hints ) ;    criar_gcs() ;    criar_cursor() ;    criar_pixmaps() ;    XSelectInput ( display, window, ExposureMask );    XMapRaised( display, window ) ;    do XNextEvent( display, &ev ) ;    while( ev.type != Expose ) ;    XSelectInput( display, window, KeyPressMask | ExposureMask |				   EnterWindowMask | LeaveWindowMask );    IniciaJogo();}void buscar_cores(){    XColor exact ;    depth = DefaultDepth( display, screen ) ;    cmap = DefaultColormap( display, screen ) ;    black_pixel = BlackPixel( display, screen ) ;    white_pixel = WhitePixel( display, screen ) ;    if( depth == 1 ) blue.pixel = red.pixel = green.pixel = cyan.pixel =		     magenta.pixel = orange.pixel = pink.pixel = brown.pixel =		     violet.pixel = g_yellow.pixel = gray.pixel =		     yellow.pixel = white_pixel;    else if( !XAllocNamedColor( display, cmap, "yellow", &exact, &yellow ) ||        !XAllocNamedColor( display, cmap, "blue", &exact, &blue ) ||        !XAllocNamedColor( display, cmap, "red", &exact, &red ) ||        !XAllocNamedColor( display, cmap, "green", &exact, &green ) ||        !XAllocNamedColor( display, cmap, "cyan", &exact, &cyan ) ||        !XAllocNamedColor( display, cmap, "magenta", &exact, &magenta ) ||        !XAllocNamedColor( display, cmap, "coral", &exact, &orange ) ||        !XAllocNamedColor( display, cmap, "pink", &exact, &pink ) ||        !XAllocNamedColor( display, cmap, "brown", &exact, &brown ) ||        !XAllocNamedColor( display, cmap, "violet", &exact, &violet ) ||        !XAllocNamedColor( display, cmap, "greenyellow", &exact, &g_yellow ) ||        !XAllocNamedColor( display, cmap, "gray", &exact, &gray ) )    {	fprintf( stderr, "Nao consigo 'allocar' todas as cores !\n" ) ;	exit( 1 ) ;    }}void criar_gcs(){    gc_yellow = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_yellow, yellow.pixel ) ;    XSetBackground( display, gc_yellow, black_pixel ) ;    XSetFont( display, gc_yellow, ch_font ) ;    gc_red = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_red, red.pixel ) ;    XSetBackground( display, gc_red, black_pixel ) ;    XSetFont( display, gc_red, ch_font ) ;    gc_green = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_green, green.pixel ) ;    XSetBackground( display, gc_green, black_pixel ) ;    XSetFont( display, gc_green, ch_font ) ;    gc_blue = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_blue, blue.pixel ) ;    XSetBackground( display, gc_blue, black_pixel ) ;    XSetFont( display, gc_blue, ch_font ) ;    gc_g_yellow = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_g_yellow, g_yellow.pixel ) ;    XSetBackground( display, gc_g_yellow, black_pixel ) ;    XSetFont( display, gc_g_yellow, ch_font ) ;    gc_cyan = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_cyan, cyan.pixel ) ;    XSetBackground( display, gc_cyan, black_pixel ) ;    XSetFont( display, gc_cyan, ch_font ) ;    gc_brown = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_brown, brown.pixel ) ;    XSetBackground( display, gc_brown, black_pixel ) ;    XSetFont( display, gc_brown, ch_font ) ;    gc_white = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_white, white_pixel ) ;    XSetBackground( display, gc_white, black_pixel ) ;    XSetGraphicsExposures( display, gc_white, 0 ) ;    XSetFont( display, gc_white, ch_font ) ;    gc_black = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_black, black_pixel ) ;    XSetBackground( display, gc_black, white_pixel ) ;    XSetFont( display, gc_black, ch_font ) ;    gc_gray = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_gray, gray.pixel ) ;    XSetBackground( display, gc_gray, black_pixel ) ;    XSetFont( display, gc_gray, ch_font ) ;    gc_magenta = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_magenta, magenta.pixel ) ;    XSetBackground( display, gc_magenta, black_pixel ) ;    XSetFont( display, gc_magenta, ch_font ) ;    gc_violet = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_violet, violet.pixel ) ;    XSetBackground( display, gc_violet, black_pixel ) ;    XSetFont( display, gc_violet, ch_font ) ;    gc_orange = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_orange, orange.pixel ) ;    XSetBackground( display, gc_orange, black_pixel ) ;    XSetFont( display, gc_orange, ch_font ) ;    gc_pink = XCreateGC( display, window, 0L, 0 ) ;    XSetForeground( display, gc_pink, pink.pixel ) ;    XSetBackground( display, gc_pink, black_pixel ) ;    XSetFont( display, gc_pink, ch_font ) ;}void criar_pixmaps(){#include "userup"#include "userdown"#include "userright"#include "userleft"#include "usereat"#include "wall"#include "dot"#include "orange"#include "apple"#include "ghost1"#include "ghost2"#include "ghost3"#include "space"    bm_userup = XCreatePixmapFromBitmapData( display, window, userup_bits,	userup_width, userup_height, yellow.pixel, black_pixel, depth );    bm_userdown = XCreatePixmapFromBitmapData( display, window, userdown_bits,	userdown_width, userdown_height, yellow.pixel, black_pixel, depth );    bm_userleft = XCreatePixmapFromBitmapData( display, window, userleft_bits,	userleft_width, userleft_height, yellow.pixel, black_pixel, depth );    bm_userright = XCreatePixmapFromBitmapData( display, window, userright_bits,	userright_width, userright_height, yellow.pixel, black_pixel, depth );    bm_usereat = XCreatePixmapFromBitmapData( display, window, usereat_bits,	usereat_width, usereat_height, yellow.pixel, black_pixel, depth );    bm_ghost1 = XCreatePixmapFromBitmapData( display, window, ghost1_bits,	ghost1_width, ghost1_height, red.pixel, black_pixel, depth );    bm_ghost2 = XCreatePixmapFromBitmapData( display, window, ghost2_bits,	ghost2_width, ghost2_height, cyan.pixel, black_pixel, depth );    bm_ghost3 = XCreatePixmapFromBitmapData( display, window, ghost3_bits,	ghost3_width, ghost3_height, green.pixel, black_pixel, depth );    bm_dot = XCreatePixmapFromBitmapData( display, window, dot_bits,	dot_width, dot_height, white_pixel, black_pixel, depth );    bm_space = XCreatePixmapFromBitmapData( display, window, space_bits,	space_width, space_height, white_pixel, black_pixel, depth );    bm_apple = XCreatePixmapFromBitmapData( display, window, apple_bits,	apple_width, apple_height, red.pixel, black_pixel, depth );    bm_orange = XCreatePixmapFromBitmapData( display, window, orange_bits,	orange_width, orange_height, orange.pixel, black_pixel, depth );    bm_wall = XCreatePixmapFromBitmapData( display, window, wall_bits,	wall_width, wall_height, gray.pixel, black_pixel, depth );}void criar_cursor(){#include "cursor"#include "cmask"    Pixmap curs_bmap, cmask_bmap ;    Cursor curs ;        curs_bmap = XCreateBitmapFromData(  display, window, cursor_bits,					cursor_width, cursor_height ) ;    cmask_bmap = XCreateBitmapFromData( display, window, cmask_bits,					cmask_width, cmask_height ) ;    curs = XCreatePixmapCursor(display, curs_bmap, cmask_bmap, &yellow, &brown,				cursor_x_hot, cursor_y_hot ) ;    XDefineCursor( display, window, curs ) ;}void put_object( x, y, object )short x, y ;Pixmap object ;{    XCopyArea( display, object, window, gc_white, 0, 0,		BITMAP_WIDTH, BITMAP_HEIGHT, AT( x ) , AT( y ) ) ;}void Apagar(){    XClearWindow( display, window ) ;    XSync( display, 1 ) ;}void PAUSA(){    Tecla = ' ';}void x_print_string( x, y, str, gc )short x, y ;char str[] ;GC gc ;{    int len ;    len = strlen( str ) ;    XDrawImageString( display, window, gc, AT( x ), AT( y ), str, len ) ;}void x_get_string( x, y, str, len )short x, y ;char *str ;int len ;{    int i = 0 ;    while( True )    {	str[i] = '\0' ;	sprintf( string, " %s_ ", str ) ;	x_print_string( x, y, string, gc_white ) ;	PAUSA() ;	switch( Tecla )	{	    case '\b' : if( i ) --i ;	    case '\0' : break ;	    case '\n' :	    case '\r' : return ;	    default   : if( Tecla < ' ' || Tecla > 127 ) break ;		        if( i > len - 2 ) break ;			str[i++] = Tecla ;	}    }}void finalizacao(){//    returnResults(Player.score);    XSync( display, 1 ) ;    XCloseDisplay( display ) ;    exit( 0 ) ;}void MudarNivel(){    do    {        Apagar();        sprintf( string, "Which Level ( 0..%d ) ?", NIVEL_MAX-1 );        x_print_string( 16, 10, string, gc_red ) ;        PAUSA() ;        NivelInicial = Tecla - '0';    }    while( NivelInicial < 0 || NivelInicial > NIVEL_MAX-1 );}void ReadScreen( scr, level )Ecra scr ;int level ;{ int i,j ; char *field;    field = playField[level];    total = 0 ;    for( i=0; i< MAX_LINES; ++i )    {    	for( j=0; j< MAX_COL; ++j )	    if( ( scr[i][j] = *field++) == '.' ) ++total ;	field++;    }    total *= 10 ;}int getLives(){    return Player.lives;}void initLevel(){        morte = False ;        NivelMudou = False ;        armado = False ;	      Apagar();	      sprintf( string, " LEVEL : %d ",Player.level );	      x_print_string( 18, 10, string, gc_red ) ;	      PAUSA();        ImprimirEcra() ;}int levelFinished(){    int retval;    retval = (morte || NivelMudou);    return retval;}void singleStep(int key){int x, y, k;float distance;    Tecla = key;		resetFeeling();    memcpy (cpPlayField, ecra, sizeof(Ecra));    for( k= 0 ; k < NGhosts ; ++k ) cpPlayField[Ghosts[k].CurrPos.y ][Ghosts[k].CurrPos.x] = 'g';    x = Player.CurrPos.x;    distance = 0;    while (cpPlayField[Player.CurrPos.y][x] != 'X'){        switch (cpPlayField[Player.CurrPos.y][x]) {            case '.':              feeling.rightGood += DOTSMELL / distance + 1;              if(feeling.rightGood > MAXFEELING)feeling.rightGood = MAXFEELING;              break;            case 'g':              feeling.rightBad += GHOSTSMELL / distance + 10;              if(feeling.rightBad > MAXFEELING)feeling.rightBad = MAXFEELING;              break;            case 'C':              feeling.rightGood += CFRUITSMELL / distance + 5;              if(feeling.rightGood > MAXFEELING)feeling.rightGood = MAXFEELING;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -