void Hero::atack() { /* 主人公の攻撃(当たり判定) */ if ( state == H_ATACK ) { /* チャージの長さにより 攻撃力、攻撃範囲、攻撃距離を設定 */ int power, angle, range, level; setAtackStatus( power, angle, range, level ); chargeCounter = 0; hitCount = 0; for ( auto ite = enemies.begin(); ite != enemies.end(); /* iteは進めない */ ) { Vector ePos = (*ite)->getPosition(); double eRad = (*ite)->getRadius(); if ( collisionDetect( ePos, angle, range ) ) { (*ite)->takeDamage( power ); hitCount ++; if ( (*ite)->getHP() <= 0 ) { ite = enemies.erase( ite ); } else { ite++; } } else { ite++; } } if ( level == 1 ) { if ( 0 < hitCount ) soundMap["atack"]->play(); else { soundMap["swing"]->play(); drawSwing = FRAME_SPAN_DRAW; } } if ( level == 2 ) { GameScene::addOnomatope( "dogo", Vector( 0, 0.35, 1.3 ), FRAME_SPAN_DRAW, Color( 0, 0, 1 ) ); soundMap["atack2"]->play(); } } /* チャージ中から攻撃中に遷移 */ if ( state == H_CHARGE ) { soundMap["charge"]->stop(); setState( H_ATACK ); } }
void BattleScene::gameController(float dt) {//to drive the main logic GameMaster::getInstance()->update(dt); collisionDetect(dt); solveAttacks(dt); moveCamera(dt); }
int contactPhi(RigidBodyManipulator* r, SupportStateElement& supp, void *map_ptr, VectorXd &phi, double terrain_height) { std::unique_ptr<RigidBody>& b = r->bodies[supp.body_idx]; int nc = supp.contact_pt_inds.size(); phi.resize(nc); if (nc<1) return nc; Vector3d contact_pos,pos,posB,normal; Vector4d tmp; int i=0; for (std::set<int>::iterator pt_iter=supp.contact_pt_inds.begin(); pt_iter!=supp.contact_pt_inds.end(); pt_iter++) { if (*pt_iter<0 || *pt_iter>=b->contact_pts.cols()) mexErrMsgIdAndTxt("DRC:ControlUtil:BadInput","requesting contact pt %d but body only has %d pts",*pt_iter,b->contact_pts.cols()); tmp = b->contact_pts.col(*pt_iter); r->forwardKin(supp.body_idx,tmp,0,contact_pos); collisionDetect(map_ptr,contact_pos,pos,NULL,terrain_height); pos -= contact_pos; // now -rel_pos in matlab version phi(i) = pos.norm(); if (pos.dot(normal)>0) phi(i)=-phi(i); i++; } return nc; }
void moveZombies(){ int i; for(i=1;i<MAX_ENTITIES;i++){ float maxZombieSpeed=.10; allEntities[i].prevYpos = allEntities[i].curYpos; allEntities[i].prevXpos = allEntities[i].curXpos; if(allEntities[i].isAlive){ if(((float)(clock() - allEntities[i].lastMoveClock)/CLOCKS_PER_SEC) > allEntities[i].speed ){ getNextPos(allEntities[i].curYpos,allEntities[i].curXpos,allEntities[0].curYpos,allEntities[0].curXpos); struct CollisionEvent collisionEvent = collisionDetect(nextPos.y,nextPos.x); if(collisionEvent.collidedWithBoundary || collisionEvent.collidedWithUnPassableChar || collisionEvent.collidedWithItem || collisionEvent.collidedWithEntity){ if(collisionEvent.collidedWithEntity){ if(collisionEvent.collidedWithEntity->type,"zombie"){ } } } else{ allEntities[i].curYpos = nextPos.y; allEntities[i].curXpos = nextPos.x; } allEntities[i].lastMoveClock=clock(); } } } }
int contactConstraintsBV(RigidBodyManipulator *r, int nc, std::vector<double> support_mus, std::vector<SupportStateElement,Eigen::aligned_allocator<SupportStateElement>>& supp, void *map_ptr, MatrixXd &B, MatrixXd &JB, MatrixXd &Jp, VectorXd &Jpdotv, MatrixXd &normals, double terrain_height) { int j, k=0, nq = r->num_positions; B.resize(3,nc*2*m_surface_tangents); JB.resize(nq,nc*2*m_surface_tangents); Jp.resize(3*nc,nq); Jpdotv.resize(3*nc); normals.resize(3, nc); Vector3d contact_pos,pos,normal; MatrixXd J(3,nq); Matrix<double,3,m_surface_tangents> d; for (std::vector<SupportStateElement,Eigen::aligned_allocator<SupportStateElement>>::iterator iter = supp.begin(); iter!=supp.end(); iter++) { double mu = support_mus[iter - supp.begin()]; double norm = sqrt(1+mu*mu); // because normals and ds are orthogonal, the norm has a simple form if (nc>0) { for (auto pt_iter=iter->contact_pts.begin(); pt_iter!=iter->contact_pts.end(); pt_iter++) { auto contact_pos_gradientvar = r->forwardKin(*pt_iter, iter->body_idx, 0, 0, 1); contact_pos = contact_pos_gradientvar.value(); J = contact_pos_gradientvar.gradient().value(); if (iter->use_support_surface) { normal = iter->support_surface.head(3); } else { collisionDetect(map_ptr,contact_pos,pos,&normal,terrain_height); } surfaceTangents(normal,d); for (j=0; j<m_surface_tangents; j++) { B.col(2*k*m_surface_tangents+j) = (normal + mu*d.col(j)) / norm; B.col((2*k+1)*m_surface_tangents+j) = (normal - mu*d.col(j)) / norm; JB.col(2 * k * m_surface_tangents + j) = J.transpose() * B.col(2 * k * m_surface_tangents + j); JB.col((2 * k + 1) * m_surface_tangents + j) = J.transpose() * B.col((2*k+1)*m_surface_tangents+j); } // store away kin sols into Jp and Jpdotv // NOTE: I'm cheating and using a slightly different ordering of J and Jdot here Jp.block(3*k,0,3,nq) = J; Vector3d pt = (*pt_iter).head(3); auto Jpdotv_grad = r->forwardJacDotTimesV(pt,iter->body_idx,0,0,0); Jpdotv.block(3*k,0,3,1) = Jpdotv_grad.value(); normals.col(k) = normal; k++; } } } return k; }
void LayeredHairMesh::simulate() { auto state = simPts; state.insert(state.end(), simVel.begin(), simVel.end()); externalUpdate(state); internalUpdate(state); collisionDetect(state); int compSize = simPts.size(); simPts.clear(); simPts.insert(simPts.end(), state.begin(), state.begin() + compSize); simVel.clear(); simVel.insert(simVel.end(), state.begin() + compSize, state.end()); }
void goLeft (void) { unsigned char t; for (t=0; t<2; t++) { if ((y2pos==4) && (y2point==7)) {} else { unsigned char i,j; char m, n; for (j=0; j<60; j++) { for (i=0; i<5; i++) { if (i==0) { m = (0B10000000)&(blockFall[4][j]); m = m>>7; } n = (0B10000000)&(blockFall[i][j]); n = n>>7; blockFall[i][j] = (blockFall[i][j])<<1; blockFall[i][j] |= m; m = n; } } collisionDetect(); if(collision) { collision =0; slideRight(); } else { y1point +=1; y2point +=1; if (y1point >7) { y1pos +=1; y1point =0; } if (y2point >7) { y2pos +=1; y2point =0; } } } }
int contactConstraintsBV(RigidBodyManipulator *r, int nc, double mu, std::vector<SupportStateElement>& supp, void *map_ptr, MatrixXd &B, MatrixXd &JB, MatrixXd &Jp, MatrixXd &Jpdot, MatrixXd &normals, double terrain_height) { int j, k=0, nq = r->num_dof; B.resize(3,nc*2*m_surface_tangents); JB.resize(nq,nc*2*m_surface_tangents); Jp.resize(3*nc,nq); Jpdot.resize(3*nc,nq); normals.resize(3, nc); Vector3d contact_pos,pos,posB,normal; Vector4d tmp; MatrixXd J(3,nq); Matrix<double,3,m_surface_tangents> d; double norm = sqrt(1+mu*mu); // because normals and ds are orthogonal, the norm has a simple form for (std::vector<SupportStateElement>::iterator iter = supp.begin(); iter!=supp.end(); iter++) { std::unique_ptr<RigidBody>& b = r->bodies[iter->body_idx]; if (nc>0) { for (std::set<int>::iterator pt_iter=iter->contact_pt_inds.begin(); pt_iter!=iter->contact_pt_inds.end(); pt_iter++) { if (*pt_iter<0 || *pt_iter>=b->contact_pts.cols()) mexErrMsgIdAndTxt("DRC:ControlUtil:BadInput","requesting contact pt %d but body only has %d pts",*pt_iter,b->contact_pts.cols()); tmp = b->contact_pts.col(*pt_iter); r->forwardKin(iter->body_idx,tmp,0,contact_pos); r->forwardJac(iter->body_idx,tmp,0,J); collisionDetect(map_ptr,contact_pos,pos,&normal,terrain_height); surfaceTangents(normal,d); for (j=0; j<m_surface_tangents; j++) { B.col(2*k*m_surface_tangents+j) = (normal + mu*d.col(j)) / norm; B.col((2*k+1)*m_surface_tangents+j) = (normal - mu*d.col(j)) / norm; JB.col(2*k*m_surface_tangents+j) = J.transpose()*B.col(2*k*m_surface_tangents+j); JB.col((2*k+1)*m_surface_tangents+j) = J.transpose()*B.col((2*k+1)*m_surface_tangents+j); } // store away kin sols into Jp and Jpdot // NOTE: I'm cheating and using a slightly different ordering of J and Jdot here Jp.block(3*k,0,3,nq) = J; r->forwardJacDot(iter->body_idx,tmp,0,J); Jpdot.block(3*k,0,3,nq) = J; normals.col(k) = normal; k++; } } } return k; }
Mode PlayMode::frame() { if(!paused) { background->draw(); if(!handleInput()) return MENU; if(!crashed) { moveField(); background->update(); updatePlayer(); generateWalls(); generateObstacles(); generateItems(); } updatePlayerTail(); particles->draw(0); particles->draw(2); drawStuff(); particles->draw(4); drawPlayer(); particles->draw(1); floating->draw(); collisionDetect(); obstacleCounter(); drawScorePanel(); if(crashed && !gameOverExplosionTime--) { globalStore->seconds = playtime / 60; globalStore->score = score; globalStore->obstacles = passed; globalStore->stars = collected; storage->read(); storage->insert(score); return GAMEOVER; } particles->update(); floating->update(); } else { drawPauseScreen(); if(!handleInput()) return MENU; } return PLAY; }
/* 引数stepの状態での攻撃を行う*/ void Hero::atack( int level ) { int power, angle, range; //setAtackStatus( power, angle, range, level ); setAtackStatus( power, angle, range ); hitCount = 0; for ( auto ite = enemies.begin(); ite != enemies.end(); /* iteは進めない */ ) { Vector ePos = (*ite)->getPosition(); double eRad = (*ite)->getRadius(); if ( collisionDetect( ePos, angle, range ) ) { (*ite)->takeDamage( power ); hitCount ++; if ( (*ite)->getHP() <= 0 ) { ite = enemies.erase( ite ); } else { ite++; } } else { ite++; } } if ( justAtack ) { //GameScene::addOnomatope( "dogo", Vector( 0, 0.35, 1.3 ), FRAME_SPAN_DRAW, Color( 0, 0, 1 ) ); soundMap["atack"]->play(); } else { if ( 0 < hitCount ) soundMap["atack"]->play(); else { soundMap["swing"]->play(); if ( level == 0 ) GameScene::addScript( "h_swing", Point2D( 0.7, 0.5 ), H_FRAME_SPAN_ATACK, Color( 0, 1, 1 ), 0.4, 20 ); else GameScene::addScript( "h_swing", Point2D( 0.1, 0.6 ), H_FRAME_SPAN_ATACK, Color( 0, 1, 1 ), 0.4, -20 ); } } }
int contactConstraintsBV(RigidBodyManipulator *r, int nc, double mu, std::vector<SupportStateElement>& supp, void *map_ptr, MatrixXd &B, MatrixXd &JB, MatrixXd &Jp, MatrixXd &Jpdot, MatrixXd &normals, double terrain_height) { int j, k=0, nq = r->num_positions; B.resize(3,nc*2*m_surface_tangents); JB.resize(nq,nc*2*m_surface_tangents); Jp.resize(3*nc,nq); Jpdot.resize(3*nc,nq); normals.resize(3, nc); Vector3d contact_pos,pos,posB,normal; MatrixXd J(3,nq); Matrix<double,3,m_surface_tangents> d; double norm = sqrt(1+mu*mu); // because normals and ds are orthogonal, the norm has a simple form for (std::vector<SupportStateElement>::iterator iter = supp.begin(); iter!=supp.end(); iter++) { if (nc>0) { for (std::vector<Vector4d,aligned_allocator<Vector4d>>::iterator pt_iter=iter->contact_pts.begin(); pt_iter!=iter->contact_pts.end(); pt_iter++) { r->forwardKin(iter->body_idx,*pt_iter,0,contact_pos); r->forwardJac(iter->body_idx,*pt_iter,0,J); collisionDetect(map_ptr,contact_pos,pos,&normal,terrain_height); surfaceTangents(normal,d); for (j=0; j<m_surface_tangents; j++) { B.col(2*k*m_surface_tangents+j) = (normal + mu*d.col(j)) / norm; B.col((2*k+1)*m_surface_tangents+j) = (normal - mu*d.col(j)) / norm; JB.col(2*k*m_surface_tangents+j) = J.transpose()*B.col(2*k*m_surface_tangents+j); JB.col((2*k+1)*m_surface_tangents+j) = J.transpose()*B.col((2*k+1)*m_surface_tangents+j); } // store away kin sols into Jp and Jpdot // NOTE: I'm cheating and using a slightly different ordering of J and Jdot here Jp.block(3*k,0,3,nq) = J; r->forwardJacDot(iter->body_idx,*pt_iter,0,J); Jpdot.block(3*k,0,3,nq) = J; normals.col(k) = normal; k++; } } } return k; }
int contactConstraints(RigidBodyManipulator *r, int nc, std::vector<SupportStateElement,Eigen::aligned_allocator<SupportStateElement>>& supp, void *map_ptr, MatrixXd &n, MatrixXd &D, MatrixXd &Jp, MatrixXd &Jpdot,double terrain_height) { int j, k=0, nq = r->num_positions; n.resize(nc,nq); D.resize(nq,nc*2*m_surface_tangents); Jp.resize(3*nc,nq); Jpdot.resize(3*nc,nq); Vector3d contact_pos,pos,normal; MatrixXd J(3,nq); Matrix<double,3,m_surface_tangents> d; for (std::vector<SupportStateElement,Eigen::aligned_allocator<SupportStateElement>>::iterator iter = supp.begin(); iter!=supp.end(); iter++) { if (nc>0) { for (std::vector<Vector4d,aligned_allocator<Vector4d>>::iterator pt_iter=iter->contact_pts.begin(); pt_iter!=iter->contact_pts.end(); pt_iter++) { r->forwardKin(iter->body_idx,*pt_iter,0,contact_pos); r->forwardJac(iter->body_idx,*pt_iter,0,J); collisionDetect(map_ptr,contact_pos,pos,&normal,terrain_height); surfaceTangents(normal,d); n.row(k) = normal.transpose()*J; for (j=0; j<m_surface_tangents; j++) { D.col(2*k*m_surface_tangents+j) = J.transpose()*d.col(j); D.col((2*k+1)*m_surface_tangents+j) = -D.col(2*k*m_surface_tangents+j); } // store away kin sols into Jp and Jpdot // NOTE: I'm cheating and using a slightly different ordering of J and Jdot here Jp.block(3*k,0,3,nq) = J; r->forwardJacDot(iter->body_idx,*pt_iter,0,J); Jpdot.block(3*k,0,3,nq) = J; k++; } } } return k; }
int contactPhi(RigidBodyManipulator* r, SupportStateElement& supp, void *map_ptr, VectorXd &phi, double terrain_height) { int nc = static_cast<int>(supp.contact_pts.size()); phi.resize(nc); if (nc<1) return nc; Vector3d contact_pos,pos,normal; int i=0; for (std::vector<Vector4d,aligned_allocator<Vector4d>>::iterator pt_iter=supp.contact_pts.begin(); pt_iter!=supp.contact_pts.end(); pt_iter++) { r->forwardKin(supp.body_idx,*pt_iter,0,contact_pos); collisionDetect(map_ptr,contact_pos,pos,&normal,terrain_height); pos -= contact_pos; // now -rel_pos in matlab version phi(i) = pos.norm(); if (pos.dot(normal)>0) phi(i)=-phi(i); i++; } return nc; }
struct CollisionEvent collisionDetect(int objYpos, int objXpos){ int i; for(i=1; i<MAX_ENTITIES; i++){ if(allEntities[i].isAlive){ if(objYpos==allEntities[i].curYpos && objXpos==allEntities[i].curXpos || (objYpos==allEntities[i].prevYpos && objXpos==allEntities[i].prevXpos && mvwinch(viewportWin,objYpos,objXpos)==allEntities[i].symbol)){ struct CollisionEvent collisionEvent = {.collidedWithEntity=&allEntities[i]}; return collisionEvent; } } } for(i=0; i<MAX_ITEMS; i++){ if(allItems[i].isAlive){ if(objYpos==allItems[i].curYpos && objXpos==allItems[i].curXpos){ struct CollisionEvent collisionEvent = {.collidedWithItem=&allItems[i]}; return collisionEvent; } } } for(i=0; i<strlen(unpassableChars); i++){ if (mvwinch(viewportWin,objYpos,objXpos)==unpassableChars[i]){ struct CollisionEvent collisionEvent = {.collidedWithUnPassableChar=1}; return collisionEvent; } } if((objYpos < 1 || objYpos > viewportWinRows-2) || (objXpos < 1 || objXpos > viewportWinCols-2)){ struct CollisionEvent collisionEvent = {.collidedWithBoundary=1}; return collisionEvent; } struct CollisionEvent collisionEvent = {.collidedWithItem=0,.collidedWithEntity=0,.collidedWithUnPassableChar=0,.collidedWithBoundary=0}; return collisionEvent; } void checkProjectileCollision(struct Projectile *projectileRef ){ struct CollisionEvent collisionEvent = collisionDetect(projectileRef->curYpos, projectileRef->curXpos); if(collisionEvent.collidedWithEntity){ if(!strcmp(collisionEvent.collidedWithEntity->type,"zombie")){ projectileRef->isAlive=0; collisionEvent.collidedWithEntity->isAlive=0; allEntities[0].killCount++; allEntities[0].hitCount++; if (allEntities[0].killCount != 0 && allEntities[0].killCount%5==0){ itemModifier++; if(zombieModifier>5){ zombieModifier-=5; } } } } else if(collisionEvent.collidedWithUnPassableChar==1 || collisionEvent.collidedWithBoundary==1){ projectileRef->isAlive=0; } } void checkPlayerCollision(){ struct CollisionEvent collisionEvent = collisionDetect(allEntities[0].curYpos, allEntities[0].curXpos); if(collisionEvent.collidedWithEntity){ if(!strcmp(collisionEvent.collidedWithEntity->type,"zombie")){ allEntities[0].isAlive=0; updateViewportWin(); sleep(1); drawGameOverWin(); } } if(collisionEvent.collidedWithItem){ if(!strcmp(collisionEvent.collidedWithItem->type,"Pistol")){ if(allEntities[0].weapons[1].isAlive){ addMagazine(&allEntities[0].weapons[1]); collisionEvent.collidedWithItem->isAlive=0; } else if(!allEntities[0].weapons[1].isAlive){ allEntities[0].weapons[1].isAlive=1; allEntities[0].weapons[1].curCapacity=allEntities[0].weapons[1].maxCapacity; if(allEntities[0].curWeapon==0){ allEntities[0].curWeapon=1; } collisionEvent.collidedWithItem->isAlive=0; } } if(!strcmp(collisionEvent.collidedWithItem->type,"Shotgun")){ if(allEntities[0].weapons[2].isAlive){ addMagazine(&allEntities[0].weapons[2]); collisionEvent.collidedWithItem->isAlive=0; } else if(!allEntities[0].weapons[2].isAlive){ allEntities[0].weapons[2].isAlive=1; allEntities[0].weapons[2].curCapacity=allEntities[0].weapons[2].maxCapacity; if(allEntities[0].curWeapon==0){ allEntities[0].curWeapon=2; } collisionEvent.collidedWithItem->isAlive=0; } } } }
void gameLoop(){ while(1) { secsElapsed = (float)(clock() - startTime)/CLOCKS_PER_SEC; moveProjectiles(); updateViewportWin(); if((float)(clock() - timerStart)/CLOCKS_PER_SEC > 1){ tryRandomEvent(); timerStart=clock(); } if((int)secsElapsed%10){ zombieModifier++; } moveZombies(); updateViewportWin(); updateInfoWin(); doupdate(); int ch; ch = getch(); switch(ch){ case 'w': case 'W':{ struct CollisionEvent collisionEvent = collisionDetect(allEntities[0].curYpos-1,allEntities[0].curXpos); if(!collisionEvent.collidedWithUnPassableChar && !collisionEvent.collidedWithBoundary){ allEntities[0].prevXpos=allEntities[0].curXpos; allEntities[0].prevYpos=allEntities[0].curYpos; allEntities[0].curYpos--; }; break; } case 's': case 'S':{ struct CollisionEvent collisionEvent = collisionDetect(allEntities[0].curYpos+1,allEntities[0].curXpos); if(!collisionEvent.collidedWithUnPassableChar && !collisionEvent.collidedWithBoundary){ allEntities[0].prevXpos=allEntities[0].curXpos; allEntities[0].prevYpos=allEntities[0].curYpos; allEntities[0].curYpos++; }; break; } case 'a': case 'A':{ struct CollisionEvent collisionEvent = collisionDetect(allEntities[0].curYpos,allEntities[0].curXpos-1); if(!collisionEvent.collidedWithUnPassableChar && !collisionEvent.collidedWithBoundary){ allEntities[0].prevYpos=allEntities[0].curYpos; allEntities[0].prevXpos=allEntities[0].curXpos; allEntities[0].curXpos--; }; break; } case 'd': case 'D':{ struct CollisionEvent collisionEvent = collisionDetect(allEntities[0].curYpos,allEntities[0].curXpos+1); if(!collisionEvent.collidedWithUnPassableChar && !collisionEvent.collidedWithBoundary){ allEntities[0].prevYpos=allEntities[0].curYpos; allEntities[0].prevXpos=allEntities[0].curXpos; allEntities[0].curXpos++; }; break; } case KEY_UP:{ if(allEntities[0].weapons[allEntities[0].curWeapon].curCapacity>0 || allEntities[0].curWeapon == 0){ fireWeapon(&allEntities[0].weapons[allEntities[0].curWeapon], "up"); } break; } case KEY_LEFT:{ if(allEntities[0].weapons[allEntities[0].curWeapon].curCapacity>0 || allEntities[0].curWeapon == 0){ fireWeapon(&allEntities[0].weapons[allEntities[0].curWeapon], "left"); } break; } case KEY_DOWN:{ if(allEntities[0].weapons[allEntities[0].curWeapon].curCapacity>0 || allEntities[0].curWeapon == 0){ fireWeapon(&allEntities[0].weapons[allEntities[0].curWeapon], "down"); } break; } case KEY_RIGHT:{ if(allEntities[0].weapons[allEntities[0].curWeapon].curCapacity>0 || allEntities[0].curWeapon == 0){ fireWeapon(&allEntities[0].weapons[allEntities[0].curWeapon], "right"); } break; } case 'r': case 'R':{ if(allEntities[0].weapons[allEntities[0].curWeapon].curMags > 0 && allEntities[0].weapons[allEntities[0].curWeapon].curCapacity < allEntities[0].weapons[allEntities[0].curWeapon].maxCapacity ){ reload(&allEntities[0].weapons[allEntities[0].curWeapon]); } break; } case '0':{ allEntities[0].curWeapon=0; break; } case '1':{ if(allEntities[0].weapons[1].isAlive){ allEntities[0].curWeapon=1; } break; } case '2':{ if(allEntities[0].weapons[2].isAlive){ allEntities[0].curWeapon=2; } break; } case '3':{ if(allEntities[0].weapons[3].isAlive){ allEntities[0].curWeapon=3; } break; } case '4':{ if(allEntities[0].weapons[4].isAlive){ allEntities[0].curWeapon=4; } break; } default: allEntities[0].prevYpos=allEntities[0].curYpos; allEntities[0].prevXpos=allEntities[0].curXpos; break; } if(allEntities[0].weapons[allEntities[0].curWeapon].curCapacity==0 && allEntities[0].weapons[allEntities[0].curWeapon].curMags==0){ allEntities[0].curWeapon=0; } updateViewportWin(); checkPlayerCollision(); updateViewportWin(); } }
//main int main() { REG_DISPCNT = MODE_3 | BG2_ENABLE; enum GBAState state = START; enum GBAState prevState = state; while(1) { waitForVblank(); switch(state) { case START: drawImage3(0,0,SPLASH_WIDTH,SPLASH_HEIGHT,splash); prevState = START; state = NODRAW; break; case NODRAW: if (prevState == START) { if (KEY_DOWN_NOW(BUTTON_START)) { state = GAME; } if (KEY_DOWN_NOW(BUTTON_A)) { state = HELP; } } if (prevState == HELP) { if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } } if (prevState == GAMEOVER) { if (KEY_DOWN_NOW(BUTTON_SELECT)) { state = START; } } break; case GAME: drawImage3(0,0,GAME2_WIDTH,GAME2_HEIGHT,game2); char stuff[4] = "000\0"; stuff[0] = 48 + (clearedRows/100)%10; stuff[1] = 48 + (clearedRows/10)%10; stuff[2] = 48 + clearedRows%10; drawImagePartial(25, 180, 20, 30, GAME2_WIDTH, game2); drawString(25,180,stuff, WHITE); block curr = randomBlock(); block next = randomBlock(); drawBlock(curr); int button = 0; while(1) { delay(20); if (KEY_DOWN_NOW(BUTTON_SELECT)) { prevState = GAME; state = START; break; } block old = curr; tick++; if (collisionDetectBottom(curr) == 1) { rowCheck(curr); curr = next; next = randomBlock(); if(collisionDetect(curr) == 1) { prevState = GAME; state = GAMEOVER; break; } } else { if(KEY_DOWN_NOW(BUTTON_LEFT) && collisionDetectLeft(curr) == 0){ curr.x--; } else if(KEY_DOWN_NOW(BUTTON_RIGHT) && collisionDetectRight(curr) == 0){ curr.x++; } else if(KEY_DOWN_NOW(BUTTON_DOWN) && collisionDetectBottom(curr) == 0){ curr.y--; } else if(button == 0 && KEY_DOWN_NOW(BUTTON_UP) && rotationDetect(curr) == 0){ button = 1; rotateBlock(&curr); } else if (tick > 20) { tick = 0; curr.y--; } else if (!KEY_DOWN_NOW(BUTTON_DOWN)){ //drawString(25,70,"stuff stuff", WHITE); button = 0; } waitForVblank(); eraseBlock(old); drawBlock(curr); } } clearBoard(); drawRect(4,4,70,10, BLACK); break; case GAMEOVER: drawImage3(0,0,END_WIDTH,END_HEIGHT,end); prevState = GAMEOVER; state = NODRAW; break; case HELP: drawImage3(0,0,HELP_WIDTH,HELP_HEIGHT,help); prevState = HELP; state = NODRAW; break; } } return 0; }
void playBlock(void) { unsigned char a=0; collisionDetect(); if(collision > 0) { arrayOr(); lcdWrite(); gameOver(); } else { goDown(); arrayOr(); collisionDetect(); bottomReachDetect(); if( (collision+bottom) == 0) { a = (PINC&0B00001111); if (a == 0B00001110) { goLeft(); } if (a == 0B00001101) { goRight(); } if (a == 0B00000111) { blockTurn(); } if (a== 0B00001011) { _delay_ms(50); } else { switch(level) { case 1: _delay_ms(500); break; case 2: _delay_ms(350); break; case 3: _delay_ms(250); break; case 4: _delay_ms(125); break; case 5: _delay_ms(75); break; } } } if (collision==0) { lcdWrite(); } } }
int main(void) { gamestate game; SDL_Window* window = NULL; game.renderer = NULL; SDL_Init(SDL_INIT_VIDEO); time_t t; srand(time(&t)); window = SDL_CreateWindow( "Game Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0 ); if (window == NULL) { printf("Window initialization failed: %s\n", SDL_GetError()); return 1; } game.renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); TTF_Init(); if (game.renderer == NULL) { printf("Rendering failed: %s\n", SDL_GetError()); return 1; } loadGame(&game); int done = 0; while (!done) { processEvents(window, &done, &game); process(&game); collisionDetect(&game); doRender(&game); } SDL_DestroyTexture(game.spook[0]); SDL_DestroyTexture(game.spook[1]); SDL_DestroyTexture(game.spook[2]); SDL_DestroyTexture(game.tile); if (game.label != NULL) { SDL_DestroyTexture(game.label); } TTF_CloseFont(game.font); SDL_DestroyWindow(window); SDL_DestroyRenderer(game.renderer); TTF_Quit(); SDL_Quit(); return 0; }
void level::globalCollisions() { collisionDetect(fireList,enemyList); playerShip->collectFireUpgrades(fireUpgradeList); playerShip->collectLifeUpgrades(lifeUpgradeList); }
int main() { Start: // 게임 재시작을 위한 레이블 /* 재시작했을 경우를 대비해 게임과 관련된 변수들을 여기서 초기화 */ i = 0; restart = 0; // 재시작 여부 저장할 변수 초기화 dir = 0; // 오른쪽으로 이동하도록 초기화 cnt = 0; // 비동기 조작을 위한 변수 초기화 item_cnt = 0; // 먹이 먹은 수 초기화 score = 0; // 점수 초기화 length = 1; // 지렁이 길이 초기화 (처음 길이: 1) x[0] = 3; // 지렁이 초기 x좌표 초기화 y[0] = 3; // 지렁이 초기 y좌표 초기화 for (i = 1 ; i < 100 ; i++) // 길이가 1이고 배열의 크기가 100이므로 나머지 1~99 배열값들을 0으로 초기화 { x[i] = 0; } /* 테두리 장식 */ gotoxy(0,0); printf("+-------------------------------------------------+\n"); // 맨 윗줄 장식 for (i = 1 ; i <= 22 ; i++) { printf("| |\n"); } printf("+-------------------------------------------------+\n"); // 맨 아랫줄 장식 /* 오른쪽 부분에 게임 이름 소개 */ gotoxy(52,0); printf("+-----------------------+\n"); for (i = 1 ; i <= 3 ; i++) { gotoxy(52,i); printf("| |\n"); } gotoxy(52,4); printf("+-----------------------+\n"); gotoxy(56,2); printf("지렁이 게임 : 9조"); /* 오른쪽 부분에 점수(초기값 0)을 출력 */ gotoxy(57,7); printf("Score : %d ", score); /* 오른쪽 부분에 지렁이 길이를 출력 */ gotoxy(56,9); printf("Length : %d ", length); /* 먹이의 좌표를 랜덤으로 지정 */ item_x = (rand() % 49) + 1; // x좌표 출력 범위: 1 ~ 49 item_y = (rand() % 22) + 1; // y좌표 출력 범위: 1 ~ 22 /* 초기 위치에 지렁이를 출력 */ for (i = 0 ; i < length ; i++) { gotoxy(x[i],y[i]); printf("*"); } /* 초기 위치에 먹이를 출력 */ gotoxy(item_x,item_y); printf("@"); while (1) { if (_kbhit()) // 키보드 값을 입력받는 경우에만 실행 { key = _getch(); // 입력받은 키값을 key에 저장 if (key == 'w') // w를 입력받으면 위쪽 { if (dir != 3) // 아래쪽으로 움직이지 않도록 dir = 2; } else if (key == 's') // s를 입력받으면 아래쪽 { if (dir != 2) // 위쪽으로 움직이지 않도록 dir = 3; } else if (key == 'a') // a를 입력받으면 왼쪽 { if (dir != 0) // 오른쪽으로 움직이지 않도록 dir = 1; } else if (key == 'd') // d를 입력받으면 오른쪽 { if (dir != 1) // 왼쪽으로 움직이지 않도록 dir = 0; } } /* 벽이나 자기 몸에 부딪히면 종료 */ if (collisionDetect() == 1) break; /* 먹이에 부딪혔을 때 먹이 수 카운트, 점수 올리기 */ if (collisionDetect() == 2) { /* 새로운 먹이의 좌표를 지정 */ item_x = (rand() % 49) + 1; // x좌표 출력 범위: 1 ~ 49 item_y = (rand() % 22) + 1; // y좌표 출력 범위: 1 ~ 22 /* 새로운 먹이의 위치에 새로운 먹이를 출력 */ gotoxy(item_x, item_y); printf("@"); item_cnt++; // 먹이 먹은 수를 증가 score = score + (length * 10); // 점수를 올림 gotoxy(57,7); printf("Score : %d", score); // 증가한 점수를 출력 /* 먹이 먹었을 때 지렁이가 향하는 방향에 따라 새 지렁이가 생성되는 위치를 달리 함 */ if (dir == 0) // 오른쪽을 향할 때 { new_x = x[length-1] + 1; new_y = y[length-1]; } else if (dir == 1) // 왼쪽을 향할 때 { new_x = x[length-1] - 1; new_y = y[length-1]; } else if (dir == 2) // 위쪽을 향할 때 { new_x = x[length-1]; new_y = y[length-1] - 1; } else if (dir == 3) // 아래쪽을 향할 때 { new_x = x[length-1]; new_y = y[length-1] + 1; } /* 새로운 머리를 출력 */ gotoxy(new_x,new_y); printf("*"); length++; // 길이를 증가 /* 새 좌표를 이식 */ x[length-1] = new_x; y[length-1] = new_y; gotoxy(56,9); printf("Length : %d", length); // 증가한 길이를 출력 } while ((cnt++) % speed == 0) // 비동기 조작 (큰 while문 반복시마다 cnt가 증가) { is_collide = collisionDetect(); if (is_collide == 1) // is_collide가 1일 때 빠져나옴 { break; } else { /* 꼬리 지우기 */ gotoxy(x[0], y[0]); printf(" "); /* 몸통 움직이기 */ i = 1; while (i < length) // 원래 몸통 좌표값을 하나씩 밀기 { x[i-1] = x[i]; y[i-1] = y[i]; i++; } /* 새 머리 이동 */ if (dir == 0) x[length-1]++; // 오른쪽 이동 else if (dir == 1) x[length-1]--; // 왼쪽 이동 else if (dir == 2) y[length-1]--; // 위쪽 이동 else if (dir == 3) y[length-1]++; // 아래쪽 이동 /* 새 머리 그리기 */ gotoxy(x[length-1], y[length-1]); printf("*"); } Sleep(1000/(5+item_cnt)); // 먹이 먹은 수에 따라 지렁이 속도 조절 } } /* 테두리에 닿아서 while문 빠져나오면 게임 오버 문구 출력 */ gotoxy(58,12); printf("< Game Over >"); // 게임 오버 출력 /* 점수에 따른 사용자의 수준 출력 */ if (score >= 0 && score <= 550) // 0 ~ 550: 하수 { gotoxy(56,14); printf("당신은 하수입니다."); } else if (score > 550 && score <= 1200) // 551 ~ 1200: 중수 { gotoxy(56,14); printf("당신은 중수입니다."); } else if (score > 1200 && score <= 2100) // 1201 ~ 2100: 고수 { gotoxy(56,14); printf("당신은 고수입니다!"); } else if (score > 2100) // 2101 ~ : 지존 { gotoxy(56,14); printf("당신은 지존입니다!!"); } Continue: /* 계속할 건지 여부 묻고 키 입력 받음 */ gotoxy(55,16); printf("Continue? (Y/N) : "); restart = getche(); // 키값을 하나 입력받으면 입력받은 문자 출력 후 바로 다음 줄 실행 if (restart == 'y' || restart == 'Y') // 'y'나 'Y'를 입력받았을 때 { /* 오른쪽 항목을 모두 지움 */ for (i = 0 ; i <= 23 ; i++) { gotoxy(51,i); printf(" "); } goto Start; // 게임 시작 부분으로 복귀 } else if (restart == 'n' || restart == 'N') // 'n'이나 'N'을 입력받았을 때 { /* 키 잘못 입력한 데에 대한 경고 메시지가 있었다면 지움 */ gotoxy(53,18); printf(" "); /* 5초 후에 종료 */ for (i=0 ; i<=4 ; i++) { gotoxy(55,16); printf("%d초 후에 종료됩니다.", 5-i); // 몇 초 남았는지 출력 Sleep(1000); // 1초간 휴식 } } else // 위의 두 경우에 해당하지 않는 문자를 입력받았을 때 { /* 잘못 입력시 잘못 입력한 문자를 가림 */ gotoxy(73,16); printf(" "); gotoxy(53,18); printf("* Y,y,N,n 중 하나 입력 *"); goto Continue; // 문구를 다시 출력 } return 0; }