void BulletLayer::addBullet(float dt) { if (m_isPowerUp) { fireBullet(BULLET_LEFT); fireBullet(BULLET_RIGHT); } else { fireBullet(BULLET_CENTER); } }
void Spaceship::handleInput(unsigned char key) { switch(key) { case 'w': m_currentSpeed++; break; case 's': m_currentSpeed--; break; case 'a': m_Angle -= ANGLE_OF_ROTATION; break; case 'd': m_Angle += ANGLE_OF_ROTATION; break; case ' ': fireBullet(); break; case 'r': if(!m_isAlive){ revive(); } break; } }
// 自动发射子弹 void Enemy::updateFireBullet() { log("Enemy::updateFireBullet"); Point actorPos = getActorPos(); Point currentPos = getPosition(); Point dist = actorPos - currentPos; if (fabs(dist.y) < 12) { fireBullet(); } }
void Snake::handleInput() { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) fireBullet(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) direction_ = Direction::Up; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) direction_ = Direction::Down; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) direction_ = Direction::Left; else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) direction_ = Direction::Right; }
//prototype int main(void){ bool done = false; bool redraw = true; const int FPS = 60; bool isGameOver = false; //object variables spaceShip ship; Bullet bullets[NUM_BULLETS]; Comet comets[NUM_COMETS]; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *eventQueue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font18 = NULL; //check if allegro is initialised <allegro_native_dialog.h> if(!al_init()){ return -1; } display = al_create_display(WIDTH,HEIGHT); //check if display was created if(!display){ return -1; } al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); eventQueue = al_create_event_queue(); timer= al_create_timer(1.0/FPS); srand(time(NULL)); // gives the rand() some value initShip(ship); initBullet(bullets, NUM_BULLETS); initComet(comets, NUM_COMETS); font18 = al_load_font("arial.ttf",18,0); al_register_event_source(eventQueue, al_get_keyboard_event_source()); al_register_event_source(eventQueue, al_get_timer_event_source(timer)); al_register_event_source(eventQueue, al_get_display_event_source(display)); //event os display resize closing, moving al_start_timer(timer); while(!done){ ALLEGRO_EVENT ev; al_wait_for_event(eventQueue, &ev); if (ev.type==ALLEGRO_EVENT_TIMER){ redraw=true; if(keys[UP]){ moveShipUp(ship); } if(keys[DOWN]){ moveShipDown(ship); } if(keys[LEFT]){ moveShipLeft(ship); } if(keys[RIGHT]){ moveShipRight(ship); } if(!isGameOver){ updateBullet(bullets,NUM_BULLETS); startComet(comets, NUM_COMETS); updateComet(comets, NUM_COMETS); collideBullet(bullets,NUM_BULLETS, comets, NUM_BULLETS, ship); collideComet(comets, NUM_COMETS, ship); if (ship.lives <= 0){ isGameOver = true; } } } // when clicling on red X icon to close the game else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ done=true; } else if (ev.type==ALLEGRO_EVENT_KEY_DOWN){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done=true; break; case ALLEGRO_KEY_UP: keys[UP]=true; break; case ALLEGRO_KEY_DOWN: keys[DOWN]=true; break; case ALLEGRO_KEY_LEFT: keys[LEFT]=true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT]=true; break; case ALLEGRO_KEY_SPACE: keys[SPACE]=true; fireBullet(bullets, NUM_BULLETS, ship); break; } } else if (ev.type==ALLEGRO_EVENT_KEY_UP){ switch(ev.keyboard.keycode){ //make sure when exiting the program exit with esc button case ALLEGRO_KEY_ESCAPE: done=true; break; case ALLEGRO_KEY_UP: keys[UP]=false; break; case ALLEGRO_KEY_DOWN: keys[DOWN]=false; break; case ALLEGRO_KEY_LEFT: keys[LEFT]=false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT]=false; break; case ALLEGRO_KEY_SPACE: keys[SPACE]=false; break; } } if (redraw && al_is_event_queue_empty(eventQueue)){ // I only draw the screen if there no more event redraw=false; if(!isGameOver){ drawShip(ship); drawBullet(bullets,NUM_BULLETS); drawComet(comets, NUM_COMETS); al_draw_textf(font18, al_map_rgb(255,0,255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score); } else{ al_draw_textf(font18, al_map_rgb(0,255,255),WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over, Pal. Final Score: %i", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); //that prevents the filled rectangle draw imga just like snake style } //make sure when exiting the program exit with esc button // when clicling on red X icon to close the game } //al_rest(5.0); al_destroy_display(display); //hadouken return 0; }
int main(int argc, char *argv[]) { int i; float r = 0; Space *space; Entity *cube1,*cube2; char bGameLoopRunning = 1; Vec3D cameraPosition = {-10,3,0}; Vec3D cameraRotation = {180,0,180}; SDL_Event e; Obj *bgobj; Sprite *bgtext; //my variables Ship *playerShip; int specMode; init_logger("gametest3d.log"); if (graphics3d_init(1024,768,1,"gametest3d",33) != 0) { return -1; } model_init(); obj_init(); entity_init(255); initShips(); bgobj = obj_load("models/mountainvillage.obj"); bgtext = LoadSprite("models/mountain_text.png",1024,1024); //cube1 = newCube(vec3d(0,0,0),"Cubert"); //cube2 = newCube(vec3d(10,0,0),"Hobbes"); //cube1->body.velocity.x = 0.1; space = space_new(); space_set_steps(space,100); space_add_body(space,&cube1->body); space_add_body(space,&cube2->body); //my variables specMode = 0; numShips = 0; shipVel = 0; shipRot = 0; turretRot = 0; gunElev = 0; realTurrRot = 0; playerShip = spawnShip(space, vec3d(-10,0,0), 1); while (bGameLoopRunning) { updateAllShipPos(); for (i = 0; i < 100;i++) { space_do_step(space); } updateAllShipComp(); applyGrav(); if(specMode == 0) { cameraPosition.x = playerShip->hull->body.position.x; cameraPosition.y = (playerShip->hull->body.position.y + 5); cameraPosition.z = playerShip->hull->body.position.z; } while ( SDL_PollEvent(&e) ) { if (e.type == SDL_QUIT) { bGameLoopRunning = 0; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_ESCAPE) { bGameLoopRunning = 0; } else if (e.key.keysym.sym == SDLK_SPACE) { cameraPosition.z++; } else if (e.key.keysym.sym == SDLK_z) { cameraPosition.z--; } else if (e.key.keysym.sym == SDLK_w) { vec3d_add( cameraPosition, cameraPosition, vec3d( -sin(cameraRotation.z * DEGTORAD), cos(cameraRotation.z * DEGTORAD), 0 )); } else if (e.key.keysym.sym == SDLK_s) { vec3d_add( cameraPosition, cameraPosition, vec3d( sin(cameraRotation.z * DEGTORAD), -cos(cameraRotation.z * DEGTORAD), 0 )); } else if (e.key.keysym.sym == SDLK_d) { vec3d_add( cameraPosition, cameraPosition, vec3d( cos(cameraRotation.z * DEGTORAD), sin(cameraRotation.z * DEGTORAD), 0 )); } else if (e.key.keysym.sym == SDLK_a) { vec3d_add( cameraPosition, cameraPosition, vec3d( -cos(cameraRotation.z * DEGTORAD), -sin(cameraRotation.z * DEGTORAD), 0 )); } else if (e.key.keysym.sym == SDLK_LEFT) { cameraRotation.y -= 1; } else if (e.key.keysym.sym == SDLK_RIGHT) { cameraRotation.y += 1; } else if (e.key.keysym.sym == SDLK_UP) { cameraRotation.x += 1; } else if (e.key.keysym.sym == SDLK_DOWN) { cameraRotation.x -= 1; } else if (e.key.keysym.sym == SDLK_y && shipVel < 0.4) { shipVel += 0.1; } else if (e.key.keysym.sym == SDLK_h && shipVel > 0.05) { shipVel -= 0.1; } else if (e.key.keysym.sym == SDLK_j) { shipRot += 0.5; if(shipRot >= 360){shipRot -= 360;} } else if (e.key.keysym.sym == SDLK_g) { shipRot -= 0.5; if(shipRot < 0){shipRot += 360;} } else if (e.key.keysym.sym == SDLK_m && turretRot < 135) { turretRot += 1; } else if (e.key.keysym.sym == SDLK_b && turretRot > -135) { turretRot -= 1; } else if (e.key.keysym.sym == SDLK_o && gunElev < 50) { gunElev += 0.5; } else if (e.key.keysym.sym == SDLK_l && gunElev > -5) { gunElev -= 0.5; } else if (e.key.keysym.sym == SDLK_q) { if(specMode == 0){specMode = 1;} else {specMode = 0;} } else if (e.key.keysym.sym == SDLK_p) { fireBullet(space, playerShip->gun->body.position, realTurrRot, gunElev, 0.5, -1); } else if (e.key.keysym.sym == SDLK_x) { fireBullet(space, playerShip->hull->body.position, shipRot, 0, 0, -2); } else if (e.key.keysym.sym == SDLK_1) { startLevel(space, 1); } else if (e.key.keysym.sym == SDLK_2) { startLevel(space, 2); } else if (e.key.keysym.sym == SDLK_3) { startLevel(space, 3); } } } graphics3d_frame_begin(); glPushMatrix(); set_camera( cameraPosition, cameraRotation); entity_draw_all(); //updateAllShipModels(); obj_draw( bgobj, vec3d(0,0,2), vec3d(90,90,0), vec3d(5,5,5), vec4d(1,1,1,1), bgtext ); if (r > 360)r -= 360; glPopMatrix(); /* drawing code above here! */ graphics3d_next_frame(); } return 0; }
//active mouse function void myMoose(int button, int state, int mooseX, int mooseY){ if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) fireBullet(); //else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP) }
//prototype int main(void){ bool done = false; bool redraw = true; const int FPS = 60; //object variables spaceShip ship; Bullet bullets[5]; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *eventQueue = NULL; ALLEGRO_TIMER *timer = NULL; //check if allegro is initialised <allegro_native_dialog.h> if(!al_init()){ return -1; } display = al_create_display(WIDTH,HEIGHT); //check if display was created if(!display){ return -1; } al_init_primitives_addon(); al_install_keyboard(); //to capture keyboard events eventQueue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); initShip(ship); initBullet(bullets,NUM_BULLETS); //register the keyboard to eventQueue al_register_event_source(eventQueue,al_get_keyboard_event_source()); al_register_event_source(eventQueue,al_get_timer_event_source(timer)); al_register_event_source(eventQueue,al_get_display_event_source(display)); al_start_timer(timer); while(!done){ ALLEGRO_EVENT ev; al_wait_for_event(eventQueue,&ev); if(ev.type == ALLEGRO_EVENT_TIMER){ redraw=true; if(keys[UP]){ moveShipUp(ship); } if(keys[DOWN]){ moveShipDown(ship); } if(keys[LEFT]){ moveShipLeft(ship); } if(keys[RIGHT]){ moveShipRight(ship); } updateBullet(bullets,NUM_BULLETS); } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ done=true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; fireBullet(bullets, NUM_BULLETS, ship); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } if(redraw && al_is_event_queue_empty(eventQueue)){ redraw = false; drawShip(ship); //without updating the bullets they will be only create a static spot in the screen, limited to n number of bullets drawBullet(bullets,NUM_BULLETS); // when clicling on red X icon to close the game al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); //that prevents the filled rectangle draw imga just like snake style } //make sure when exiting the program exit with esc button } //al_rest(5.0); al_destroy_display(display); //hadouken return 0; }
void CArmySprite::enemyUpdate(float fEscapeTime) { switch(m_eCurState) { case AS_DEFAULT: break; case AS_MOVE: //if (getPosition().y < -getContentSize().height || getPosition().y > mScreenSize.height - BANNER_HEIGHT + getContentSize().height) //{ // m_eCurState = BS_HIDE; //} m_fFireCoolTime += fEscapeTime; if (m_fFireCoolTime > 2.0f) { fireBullet(fEscapeTime); } break; case AS_EXPLODE: break; case AS_HIDE: if(getPosition().y < -getContentSize().height * 0.5f) { getParent()->removeChild(this, true); } break; } if (m_eCurState < AS_EXPLODE && m_sCurData.mKind < AK_BUNKER01) { CCNode *m_pTarget = (CCNode*)(getParent()->getChildByTag(PALYER_TYPE)); float o = m_pTarget->getPosition().x - getPosition().x; float a = m_pTarget->getPosition().y - getPosition().y; float at = (float) CC_RADIANS_TO_DEGREES( atanf( o/a) ); if( a < 0 ) { if( o < 0 ) at = 180 + fabs(at); else at = 180 - fabs(at); } m_pTurretSprite->setRotation(180 + at); } if (getPosition().y > -getContentSize().height * 0.5f) { setPosition(ccpAdd(getPosition(),ccpMult(ccp(0.0f, -1.0f), m_sCurData.mMoveSpeed * fEscapeTime))); } else { m_eCurState = AS_HIDE; } if (!m_bIsNormal) { m_fFlashTime += fEscapeTime; if (m_fFlashTime > 0.1f) { m_fFlashTime = 0.0f; CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(m_pTurretSprite); pRGBAProtocol->setColor(color); m_bIsNormal = true; } } }