📄 fig15_12.c
字号:
/* Fig. 15.12: fig15_12.c
Displaying text on the screen */
#include <allegro.h>
/* symbolic constants for the ball抯 possible directions */
#define DOWN_RIGHT 0
#define UP_RIGHT 1
#define DOWN_LEFT 2
#define UP_LEFT 3
/* function prototypes */
void moveBall( void );
void respondToKeyboard( void );
void reverseVerticalDirection( void );
void reverseHorizontalDirection( void );
int ball_x; /* the ball抯 x-coordinate */
int ball_y; /* the ball抯 y-coordinate */
int barL_y; /* y-coordinate of the left paddle */
int barR_y; /* y-coordinate of the right paddle */
int scoreL; /* score of the left player */
int scoreR; /* score of the right player */
int direction; /* the ball抯 direction */
BITMAP *ball; /* pointer to ball's image bitmap */
BITMAP *bar; /* pointer to paddle's image bitmap */
BITMAP *buffer; /* pointer to the buffer */
SAMPLE *boing; /* pointer to sound file */
FONT *pongFont; /* pointer to font file */
int main( void )
{
/* first, set up Allegro and the graphics mode */
allegro_init(); /* initialize Allegro */
install_keyboard(); /* install the keyboard for Allegro to use */
install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, NULL );
set_color_depth( 16 ); /* set the color depth to 16-bit */
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0 ); /* set graphics mode */
ball = load_bitmap( "ball.bmp", NULL ); /* load the ball bitmap */
bar = load_bitmap( "bar.bmp", NULL); /* load the bar bitmap */
buffer = create_bitmap(SCREEN_W, SCREEN_H);/* create buffer */
boing = load_sample( "boing.wav" ); /* load the sound file */
pongFont = load_font( "pongfont.pcx", NULL, NULL ); /* load the font */
ball_x = SCREEN_W / 2; /* give the ball its initial x-coordinate */
ball_y = SCREEN_H / 2; /* give the ball its initial y-coordinate */
barL_y = SCREEN_H / 2; /* give left paddle its initial y-coordinate */
barR_y = SCREEN_H / 2; /* give right paddle its initial y-coordinate */
scoreL = 0; /* set left player抯 score to 0 */
scoreR = 0; /* set right player抯 score to 0 */
srand( time( NULL ) ); /* seed the random function ... */
direction = rand() % 4; /* and then make a random initial direction */
while ( !key[KEY_ESC] )/* until the escape key is pressed ... */
{
moveBall(); /* move the ball */
respondToKeyboard(); /* respond to keyboard input */
/* now, perform double buffering */
clear_to_color( buffer, makecol( 255, 255, 255 ) );
blit( ball, buffer, 0, 0, ball_x, ball_y, ball->w, ball->h );
blit( bar, buffer, 0, 0, 0, barL_y, bar->w, bar->h );
blit( bar, buffer, 0, 0, 620, barR_y, bar->w, bar->h );
/* draw text onto the buffer */
textprintf_ex( buffer, pongFont, 75, 0, makecol( 0, 0, 0 ),
-1, "Left Player Score: %d", scoreL );
textprintf_ex( buffer, pongFont, 400, 0, makecol( 0, 0, 0 ),
-1, "Right Player Score: %d", scoreR );
blit( buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h );
clear_bitmap( buffer );
} /* end while */
destroy_bitmap( ball ); /* destroy the ball bitmap */
destroy_bitmap( bar ); /* destroy the bar bitmap */
destroy_bitmap( buffer ); /* destroy the buffer bitmap */
destroy_sample( boing ); /* destroy the boing sound file */
destroy_font( pongFont ); /* destroy the font */
return 0;
} /* end function main */
END_OF_MAIN() /* don抰 forget this! */
void moveBall() /* moves the ball */
{
switch ( direction ) {
case DOWN_RIGHT:
++ball_x; /* move the ball to the right */
++ball_y; /* move the ball down */
break;
case UP_RIGHT:
++ball_x; /* move the ball to the right */
--ball_y; /* move the ball up */
break;
case DOWN_LEFT:
--ball_x; /* move the ball to the left */
++ball_y; /* move the ball down */
break;
case UP_LEFT:
--ball_x; /* move the ball to the left */
--ball_y; /* move the ball up */
break;
} /* end switch */
/* make sure the ball doesn抰 go off the screen */
/* if the ball is going off the top or bottom ... */
if ( ball_y <= 30 || ball_y >= 440 )
reverseVerticalDirection();
/* if the ball is going off the left or right ... */
if ( ball_x <= 0 || ball_x >= 600 )
reverseHorizontalDirection();
} /* end function moveBall */
void respondToKeyboard() /* responds to keyboard input */
{
if ( key[KEY_A] ) /* if A is being pressed... */
barL_y -= 3; /* ... move the left paddle up */
if ( key[KEY_Z] ) /* if Z is being pressed... */
barL_y += 3; /* ... move the left paddle down */
if ( key[KEY_UP] ) /* if the up arrow key is being pressed... */
barR_y -= 3; /* ... move the right paddle up */
if ( key[KEY_DOWN] ) /* if the down arrow key is being pressed... */
barR_y += 3; /* ... move the right paddle down */
/* make sure the paddles don抰 go offscreen */
if ( barL_y < 30 ) /* if left paddle is going off the top */
barL_y = 30;
else if ( barL_y > 380 ) /* if left paddle is going off the bottom */
barL_y = 380;
if ( barR_y < 30 ) /* if right paddle is going off the top */
barR_y = 30;
else if ( barR_y > 380 ) /* if right paddle is going off the bottom */
barR_y = 380;
} /* end function respondToKeyboard */
void reverseVerticalDirection() /* reverse the ball抯 up-down direction */
{
if ( ( direction % 2 ) == 0 ) /* "down" directions are even numbers */
++direction; /* make the ball start moving up */
else /* "up" directions are odd numbers */
--direction; /* make the ball start moving down */
play_sample( boing, 255, 128, 1000, 0 ); /* play "boing" sound once */
} /* end function reverseVerticalDirection */
void reverseHorizontalDirection() /* reverses the horizontal direction */
{
direction = ( direction + 2 ) % 4; /* reverse horizontal direction */
play_sample( boing, 255, 128, 1000, 0 ); /* play "boing" sound once */
} /* end function reverseHorizontalDirection */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -