void Flys_AI(void) { // this function performs the AI for the flys for (int curr_fly=0; curr_fly<MAX_FLYS; curr_fly++) { // test if fly directional counter <= 0 if (--flys[curr_fly].varsI[0] <= 0) { // select a new random directional velocity flys[curr_fly].xv = -4 + rand()%9; flys[curr_fly].yv = -4 + rand()%9; // set time for motion to occur flys[curr_fly].varsI[0] = 2+rand()%8; } // end if // move the fly Move_BOB(&flys[curr_fly]); // animate the fly Animate_BOB(&flys[curr_fly]); // test if fly has left the fresh meat if (flys[curr_fly].x > MAX_X_FLY) { flys[curr_fly].xv=-flys[curr_fly].xv; flys[curr_fly].x = MAX_X_FLY; } // end if if (flys[curr_fly].x < MIN_X_FLY) { flys[curr_fly].xv=-flys[curr_fly].xv; flys[curr_fly].x = MIN_X_FLY; } // end if if (flys[curr_fly].y > MAX_Y_FLY) { flys[curr_fly].yv=-flys[curr_fly].yv; flys[curr_fly].y = MAX_Y_FLY; } // end if if (flys[curr_fly].y < MIN_Y_FLY) { flys[curr_fly].yv=-flys[curr_fly].yv; flys[curr_fly].y = MIN_Y_FLY; } // end if } // end for curr_fly } // end Flys_AI
int Game_Main(void *parms) { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! int index; // looping var int dx,dy; // general deltas used in collision detection static int player_moving = 0; // tracks player motion static PALETTEENTRY glow = {0,0,0,PC_NOCOLLAPSE}; // used to animation red border static int glow_count = 0, glow_dx = 5; // start the timing clock Start_Clock(); // clear the drawing surface DDraw_Fill_Surface(lpddsback, 0); // lock the back buffer DDraw_Lock_Back_Surface(); // draw the background reactor image Draw_Bitmap(&reactor, back_buffer, back_lpitch, 0); // unlock the back buffer DDraw_Unlock_Back_Surface(); // read keyboard and other devices here DInput_Read_Keyboard(); // reset motion flag player_moving = 0; // test direction of motion, this is a good example of testing the keyboard // although the code could be optimized this is more educational if (keyboard_state[DIK_RIGHT] && keyboard_state[DIK_UP]) { // move skelaton skelaton.x+=2; skelaton.y-=2; dx=2; dy=-2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_NEAST) Set_Animation_BOB(&skelaton,SKELATON_NEAST); } // end if else if (keyboard_state[DIK_LEFT] && keyboard_state[DIK_UP]) { // move skelaton skelaton.x-=2; skelaton.y-=2; dx=-2; dy=-2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_NWEST) Set_Animation_BOB(&skelaton,SKELATON_NWEST); } // end if else if (keyboard_state[DIK_LEFT] && keyboard_state[DIK_DOWN]) { // move skelaton skelaton.x-=2; skelaton.y+=2; dx=-2; dy=2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_SWEST) Set_Animation_BOB(&skelaton,SKELATON_SWEST); } // end if else if (keyboard_state[DIK_RIGHT] && keyboard_state[DIK_DOWN]) { // move skelaton skelaton.x+=2; skelaton.y+=2; dx=2; dy=2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_SEAST) Set_Animation_BOB(&skelaton,SKELATON_SEAST); } // end if else if (keyboard_state[DIK_RIGHT]) { // move skelaton skelaton.x+=2; dx=2; dy=0; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_EAST) Set_Animation_BOB(&skelaton,SKELATON_EAST); } // end if else if (keyboard_state[DIK_LEFT]) { // move skelaton skelaton.x-=2; dx=-2; dy=0; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_WEST) Set_Animation_BOB(&skelaton,SKELATON_WEST); } // end if else if (keyboard_state[DIK_UP]) { // move skelaton skelaton.y-=2; dx=0; dy=-2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_NORTH) Set_Animation_BOB(&skelaton,SKELATON_NORTH); } // end if else if (keyboard_state[DIK_DOWN]) { // move skelaton skelaton.y+=2; dx=0; dy=+2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_SOUTH) Set_Animation_BOB(&skelaton,SKELATON_SOUTH); } // end if // only animate if player is moving if (player_moving) { // animate skelaton Animate_BOB(&skelaton); // see if skelaton hit a wall // lock surface, so we can scan it DDraw_Lock_Back_Surface(); // call the color scanner with WALL_ANIMATION_COLOR, the color of the glowing wall // try to center the scan in the center of the object to make it // more realistic if (Color_Scan(skelaton.x+16, skelaton.y+16, skelaton.x+skelaton.width-16, skelaton.y+skelaton.height-16, WALL_ANIMATION_COLOR, WALL_ANIMATION_COLOR, back_buffer,back_lpitch)) { // back the skelaton up along its last trajectory skelaton.x-=dx; skelaton.y-=dy; } // end if // done, so unlock DDraw_Unlock_Back_Surface(); // check if skelaton is off screen if (skelaton.x < 0 || skelaton.x > (screen_width - skelaton.width)) skelaton.x-=dx; if (skelaton.y < 0 || skelaton.y > (screen_height - skelaton.height)) skelaton.y-=dy; } // end if // draw the skelaton Draw_BOB(&skelaton, lpddsback); // animate color glow.peGreen+=glow_dx; // test boundary if (glow.peGreen == 0 || glow.peGreen == 255) glow_dx = -glow_dx; Set_Palette_Entry(WALL_ANIMATION_COLOR, &glow); // draw some text Draw_Text_GDI("I STILL HAVE A BONE TO PICK!",0,screen_height - 32,WALL_ANIMATION_COLOR,lpddsback); Draw_Text_GDI("USE ARROW KEYS TO MOVE, <ESC> TO EXIT.",0,0,RGB(32,32,32),lpddsback); // flip the surfaces DDraw_Flip(); // sync to 30ish fps Wait_Clock(30); // check of user is trying to exit if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE]) { PostMessage(main_window_handle, WM_DESTROY,0,0); } // end if // return success return(1); } // end Game_Main
// animates the frames of each square void Block::Animate() { // animate for( int i=0; i< MAX_SQUARES_PER_BLOCK; ++i ) Animate_BOB( &(m_squares[i].m_bob) ); }
int Game_Main(void *parms, int num_parms) { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! int index; // looping var int dx,dy; // general deltas used in collision detection // check of user is trying to exit if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)) PostMessage(main_window_handle, WM_DESTROY,0,0); // start the timing clock Start_Clock(); // clear the drawing surface DDraw_Fill_Surface(lpddsback, 0); // get joystick data lpdijoy->Poll(); // this is needed for joysticks only lpdijoy->GetDeviceState(sizeof(DIJOYSTATE2), (LPVOID)&joy_state); // lock the back buffer DDraw_Lock_Back_Surface(); // draw the background reactor image Draw_Bitmap16(&playfield, back_buffer, back_lpitch, 0); // unlock the back buffer DDraw_Unlock_Back_Surface(); // is the player moving? blaster.x+=joy_state.lX; blaster.y+=joy_state.lY; // test bounds if (blaster.x > SCREEN_WIDTH-32) blaster.x = SCREEN_WIDTH-32; else if (blaster.x < 0) blaster.x = 0; if (blaster.y > SCREEN_HEIGHT-32) blaster.y = SCREEN_HEIGHT-32; else if (blaster.y < SCREEN_HEIGHT-128) blaster.y = SCREEN_HEIGHT-128; // is player firing? if (joy_state.rgbButtons[0]) Start_Missile(); // move and draw missle Move_Missile(); Draw_Missile(); // is it time to blink eyes if ((rand()%100)==50) Set_Animation_BOB(&blaster,0); // draw blaster Animate_BOB(&blaster); Draw_BOB16(&blaster,lpddsback); // draw some text Draw_Text_GDI("(16-Bit Version) Let's Rock!!!",0,0,RGB(255,255,255),lpddsback); // display joystick and buttons 0-7 sprintf(buffer,"Joystick Stats: X-Axis=%d, Y-Axis=%d, buttons(%d,%d,%d,%d,%d,%d,%d,%d)", joy_state.lX,joy_state.lY, joy_state.rgbButtons[0], joy_state.rgbButtons[1], joy_state.rgbButtons[2], joy_state.rgbButtons[3], joy_state.rgbButtons[4], joy_state.rgbButtons[5], joy_state.rgbButtons[6], joy_state.rgbButtons[7]); Draw_Text_GDI(buffer,0,SCREEN_HEIGHT-20,RGB(255,255,50),lpddsback); // print out name of joystick sprintf(buffer, "Joystick Name & Vendor: %s",joyname); Draw_Text_GDI(buffer,0,SCREEN_HEIGHT-40,RGB(255,255,50),lpddsback); // flip the surfaces DDraw_Flip(); // sync to 30 fps Wait_Clock(30); // return success return(1); } // end Game_Main
int Game_Main(void *parms) { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! int index; // looping var // start the timing clock Start_Clock(); // clear the drawing surface DDraw_Fill_Surface(lpddsback, 0); // lock back buffer and copy background into it DDraw_Lock_Back_Surface(); // draw background Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0); // unlock back surface DDraw_Unlock_Back_Surface(); // read keyboard DInput_Read_Keyboard(); // check the player controls // is the player turning right or left? if (keyboard_state[DIK_RIGHT]) { // there are 16 possible positions for the ship to point in if (++ship.varsI[0] >= 16) ship.varsI[0] = 0; } // end if else if (keyboard_state[DIK_LEFT]) { // there are 16 possible positions for the ship to point in if (--ship.varsI[0] < 0) ship.varsI[0] = 15; } // end if // now test for forward thrust if (keyboard_state[DIK_UP]) { // thrust ship in current direction float rad_angle = (float)ship.varsI[0]*(float)3.14159/(float)8; float xv = cos(rad_angle); float yv = sin(rad_angle); ship.varsF[0]+=xv; ship.varsF[1]+=yv; // animate the ship ship.curr_frame = ship.varsI[0]+16*(rand()%2); } // end if else // show non thrust version ship.curr_frame = ship.varsI[0]; // move ship ship.varsF[2]+=ship.varsF[0]; ship.varsF[3]+=ship.varsF[1]; // always apply friction in direction opposite current trajectory float fx = -ship.varsF[0]; float fy = -ship.varsF[1]; float length_f = sqrt(fx*fx+fy*fy); // normally we would avoid square root at all costs! // compute the frictional resitance if (fabs(length_f) > 0.1) { fx = FRICTION_FACTOR*fx/length_f; fy = FRICTION_FACTOR*fy/length_f; } // end if else fx=fy=0; // now apply friction to forward velocity ship.varsF[0]+=fx; ship.varsF[1]+=fy; //////////////////////////////////////////////////////////////////// // gravity calculation section // step 1: compute vector from black hole to ship, note that the centers // of each object are used float grav_x = (black_hole.x + black_hole.width/2) - (ship.x + ship.width/2); float grav_y = (black_hole.y + black_hole.height/2) - (ship.y + ship.height/2); float radius_squared = grav_x*grav_x + grav_y*grav_y; // equal to radius squared float length_grav = sqrt(radius_squared); // step 2: normalize the length of the vector to 1.0 grav_x = grav_x/length_grav; grav_y = grav_y/length_grav; // step 3: compute the gravity force float grav_force = (VIRTUAL_GRAVITY_CONSTANT) * (SHIP_MASS * BLACK_HOLE_MASS) / radius_squared; // step 4: apply gforce in the direction of grav_x, grav_y with the magnitude of grav_force ship.varsF[0]+=grav_x*grav_force; ship.varsF[1]+=grav_y*grav_force; //////////////////////////////////////////////////////////////////// // test if ship is off screen if (ship.varsF[2] > SCREEN_WIDTH) ship.varsF[2] = -ship.width; else if (ship.varsF[2] < -ship.width) ship.varsF[2] = SCREEN_WIDTH; if (ship.varsF[3] > SCREEN_HEIGHT) ship.varsF[3] = -ship.height; else if (ship.varsF[3] < -ship.height) ship.varsF[3] = SCREEN_HEIGHT; // test if velocity is insane if ( (ship.varsF[0]*ship.varsF[0] + ship.varsF[1]*ship.varsF[1]) > MAX_VEL) { // scale velocity down ship.varsF[0]*=.95; ship.varsF[1]*=.95; } // end if // animate the black hole Animate_BOB(&black_hole); // draw the black hole Draw_BOB(&black_hole, lpddsback); // copy floating point position to bob x,y ship.x = ship.varsF[2]; ship.y = ship.varsF[3]; // draw the ship Draw_BOB(&ship,lpddsback); // draw the title Draw_Text_GDI("GRAVITY MASS DEMO - Use Arrows to Control Ship.",10, 10,RGB(0,255,255), lpddsback); sprintf(buffer,"Friction: X=%f, Y=%f",fx, fy); Draw_Text_GDI(buffer,10,420,RGB(0,255,0), lpddsback); sprintf(buffer,"Velocity: X=%f, Y=%f",ship.varsF[0], ship.varsF[1]); Draw_Text_GDI(buffer,10,440,RGB(0,255,0), lpddsback); sprintf(buffer,"Gravity: X=%f, Y=%f",ship.varsF[2], ship.varsF[3]); Draw_Text_GDI(buffer,10,460,RGB(0,255,0), lpddsback); // flip the surfaces DDraw_Flip(); // sync to 30 fps = 1/30sec = 33 ms Wait_Clock(33); // check of user is trying to exit if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE]) { PostMessage(main_window_handle, WM_DESTROY,0,0); // stop all sounds DSound_Stop_All_Sounds(); // do a screen transition Screen_Transitions(SCREEN_DARKNESS,NULL,0); } // end if // return success return(1); } // end Game_Main
int Game_Main(void *parms, int num_parms) { // this is the workhorse of your game it will be called // continuously in real-time this is like main() in C // all the calls for you game go here! int index; // looping var int dx,dy; // general deltas used in collision detection static int player_moving = 0; // tracks player motion // check of user is trying to exit if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)) PostMessage(main_window_handle, WM_DESTROY,0,0); // start the timing clock Start_Clock(); // clear the drawing surface DDraw_Fill_Surface(lpddsback, 0); // lock the back buffer DDraw_Lock_Back_Surface(); // draw the background reactor image Draw_Bitmap16(&reactor, back_buffer, back_lpitch, 0); // unlock the back buffer DDraw_Unlock_Back_Surface(); // get player input // get the keyboard data lpdikey->GetDeviceState(256, (LPVOID)keyboard_state); // reset motion flag player_moving = 0; // test direction of motion, this is a good example of testing the keyboard // although the code could be optimized this is more educational if (keyboard_state[DIK_RIGHT] && keyboard_state[DIK_UP]) { // move skelaton skelaton.x+=2; skelaton.y-=2; dx=2; dy=-2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_NEAST) Set_Animation_BOB(&skelaton,SKELATON_NEAST); } // end if else if (keyboard_state[DIK_LEFT] && keyboard_state[DIK_UP]) { // move skelaton skelaton.x-=2; skelaton.y-=2; dx=-2; dy=-2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_NWEST) Set_Animation_BOB(&skelaton,SKELATON_NWEST); } // end if else if (keyboard_state[DIK_LEFT] && keyboard_state[DIK_DOWN]) { // move skelaton skelaton.x-=2; skelaton.y+=2; dx=-2; dy=2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_SWEST) Set_Animation_BOB(&skelaton,SKELATON_SWEST); } // end if else if (keyboard_state[DIK_RIGHT] && keyboard_state[DIK_DOWN]) { // move skelaton skelaton.x+=2; skelaton.y+=2; dx=2; dy=2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_SEAST) Set_Animation_BOB(&skelaton,SKELATON_SEAST); } // end if else if (keyboard_state[DIK_RIGHT]) { // move skelaton skelaton.x+=2; dx=2; dy=0; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_EAST) Set_Animation_BOB(&skelaton,SKELATON_EAST); } // end if else if (keyboard_state[DIK_LEFT]) { // move skelaton skelaton.x-=2; dx=-2; dy=0; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_WEST) Set_Animation_BOB(&skelaton,SKELATON_WEST); } // end if else if (keyboard_state[DIK_UP]) { // move skelaton skelaton.y-=2; dx=0; dy=-2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_NORTH) Set_Animation_BOB(&skelaton,SKELATON_NORTH); } // end if else if (keyboard_state[DIK_DOWN]) { // move skelaton skelaton.y+=2; dx=0; dy=+2; // set motion flag player_moving = 1; // check animation needs to change if (skelaton.curr_animation != SKELATON_SOUTH) Set_Animation_BOB(&skelaton,SKELATON_SOUTH); } // end if // only animate if player is moving if (player_moving) { // animate skelaton Animate_BOB(&skelaton); // see if skelaton hit a wall // lock surface, so we can scan it DDraw_Lock_Back_Surface(); // call the color scanner with WALL_COLOR the color of the walls // try to center the scan on the feet of the player // note since we are uin 16-bit mode, we need to scan the 16 bit value then compare // it against the 16-bit color code for the green pixel which has values RB(41,231,41) // but depending if this is a 5.5.5 or 5.6.5 the 16-bit value will be different, however // during ddraw_init RGB16Bit() was vectored (function pointer) to either 5.5.5 or 5.6.5 // depending on the actual surface mode, so it should all work out :) if (Color_Scan16(skelaton.x+16, skelaton.y+16, skelaton.x+skelaton.width-16, skelaton.y+skelaton.height-16, RGB16Bit(WALL_COLOR_R, WALL_COLOR_G, WALL_COLOR_B), RGB16Bit(WALL_COLOR_R, WALL_COLOR_G, WALL_COLOR_B), back_buffer,back_lpitch)) { // back the skelaton up along its last trajectory skelaton.x-=dx; skelaton.y-=dy; } // end if // done, so unlock DDraw_Unlock_Back_Surface(); // check if skelaton is off screen if (skelaton.x < 0 || skelaton.x > (screen_width - skelaton.width)) skelaton.x-=dx; if (skelaton.y < 0 || skelaton.y > (screen_height - skelaton.height)) skelaton.y-=dy; } // end if // draw the skelaton Draw_BOB16(&skelaton, lpddsback); // draw some text Draw_Text_GDI("I STILL HAVE A BONE TO PICK!",0,screen_height - 32,RGB(32,32,32),lpddsback); Draw_Text_GDI("(16-Bit Version) USE ARROW KEYS TO MOVE, <ESC> TO EXIT.",0,0,RGB(32,32,32),lpddsback); // flip the surfaces DDraw_Flip(); // sync to 30 fps Wait_Clock(30); // return success return(1); } // end Game_Main