예제 #1
0
Outcome minimax( int player, Game game, vector<int> alphaBeta,int currLevel ) // notice that this is a call by value, so array gets copied!!!!
{

	//char c = 13;
	//if ( ! (minimaxCalls % 1000000 ) )
	//	cout << "minimax Calls = " << minimaxCalls/1000000 << " 000000" << c;
//	minimaxCalls++;

	int index = 1 - ((player*2)+2)/4;

	Move move = firstMove( game,player );
	Outcome Best( tryMove( player, move, game, alphaBeta,currLevel ), move ); // get a starting value on the first branch

	for ( move = nextMove( game, move ,player); move != noMovesLeft; move=nextMove( game, move ,player) )	{ 
//		If I am a max, being simulated by a min, who can already get
//      a score of beta, and the outcome from above is worse (for my parent), I might as well quit at this point.
//		Why? The grandparent will be totally uninterested in anything this node has to offer because it can pick
//		the previous node.
		
		alphaBeta[index] = player*__max( player*alphaBeta[index], player*Best.first );
		if ( alphaBeta[0] > alphaBeta[1] )  
		{
			Best.first = alphaBeta[index];
			break;
		}

		//add a check but ensure that we track the levels 
		int currentScore = tryMove( player, move, game, alphaBeta ,currLevel+1);
		if ( currentScore * player > Best.first * player ) 
		{ 
			Best = Outcome(currentScore, move);
		}
	}
	return Best;
}
//! [13]
void TetrixBoard::keyPressEvent(QKeyEvent *event)
{
    if (!isStarted || isPaused || curPiece.shape() == NoShape) {
	QFrame::keyPressEvent(event);
        return;
    }
//! [13]

//! [14]
    switch (event->key()) {
    case Qt::Key_Left:
        tryMove(curPiece, curX - 1, curY);
	break;
    case Qt::Key_Right:
        tryMove(curPiece, curX + 1, curY);
	break;
    case Qt::Key_Down:
        tryMove(curPiece.rotatedRight(), curX, curY);
	break;
    case Qt::Key_Up:
        tryMove(curPiece.rotatedLeft(), curX, curY);
	break;
    case Qt::Key_Space:
	dropDown();
	break;
    case Qt::Key_D:
	oneLineDown();
	break;
    default:
	QFrame::keyPressEvent(event);
    }
//! [14]
}
예제 #3
0
파일: main.cpp 프로젝트: inutard/CodeJam
void tryMove(char t){
    if(!ps[t]) return;
    if(t > 2 && c[top[t]].match(c[top[t-3]])){
        move(t, t-3);
        for(char z = t-3; z <= ct; ++z)
            if(top[z] != -1) tryMove(z);
    } else if(t && c[top[t]].match(c[top[t-1]])){
        move(t, t-1);
        for(char z = t-1; z <= ct; ++z)
            if(top[z] != -1) tryMove(z);
    }
}
예제 #4
0
/*
 * Listens to key press events from the graphical subsystem,
 * and handles the events appropriately:
 *   - '[n]' moves hero in direction [n],
 *           where n = 1, 2, 3, 4, 6, 7, 8, 9 on the numeric keypad
 *   - '5' on the numeric keypad makes the hero wait one turn
 *   - 'T' teleports hero
 */
void MainWindow::keyPressEvent(QKeyEvent *e)
{
    if (!gameOver) { // only process key presses while game is not over
        Hero hero = gameState.getHero();
        Point pt = hero.asPoint();
        bool actionTaken = false;
        bool waiting = false;

        if (e->modifiers() == Qt::KeypadModifier) {
            switch (e->key()) {
            case Qt::Key_1:
                actionTaken = tryMove(hero, Point{pt.x - 1, pt.y + 1});
                break;
            case Qt::Key_2:
                actionTaken = tryMove(hero, Point{pt.x, pt.y + 1});
                break;
            case Qt::Key_3:
                actionTaken = tryMove(hero, Point{pt.x + 1, pt.y + 1});
                break;
            case Qt::Key_4:
                actionTaken = tryMove(hero, Point{pt.x - 1, pt.y});
                break;
            case Qt::Key_6:
                actionTaken = tryMove(hero, Point{pt.x + 1, pt.y});
                break;
            case Qt::Key_7:
                actionTaken = tryMove(hero, Point{pt.x - 1, pt.y - 1});
                break;
            case Qt::Key_8:
                actionTaken = tryMove(hero, Point{pt.x, pt.y - 1});
                break;
            case Qt::Key_9:
                actionTaken = tryMove(hero, Point{pt.x + 1, pt.y - 1});
                break;
            case Qt::Key_5:
                actionTaken = true;
                waiting = true;
                break;
            default:
                QWidget::keyPressEvent(e);
            }
        } else {
            switch (e->key()) {
            case Qt::Key_T:
                gameState.teleportHero();
                actionTaken = true;
                break;
            default:
                QWidget::keyPressEvent(e);
            }
        }

        if (actionTaken) { // process results of viable move
            processMove(waiting);
        }
    } else {  // game is over - do not process key press
        QWidget::keyPressEvent(e);
    }
}
예제 #5
0
파일: main.cpp 프로젝트: inutard/CodeJam
int main(void){
    register char h;
    while((h = IN) != '#'){
        for(a = 0; a < 52; ++a) top[a] = -1, ps[a] = 0;
        for(a = ct = 0; a < 52; ++a, ++ct){
            while(isspace(h)) h = IN;
            c[a].v = h; c[a].s = IN;
            c[a].u = top[ct];
            top[ct] = a;
            ps[ct]++;
            tryMove(ct);
            h = IN;
        }
        printf("%d ", ct);
        fputs(rem[ct!=1], stdout);
        register char *out = ans, *s, k;
        for(x = 0; x < ct; ++x) {
            *out++ = ' '; s = stk; k = ps[x];
            while(k) *s++ = k % 10 + '0', k /= 10;
            while(s > stk) *out++ = *--s;
        }
        *out = 0;
        puts(ans);
    }
}
예제 #6
0
void Oubliette::keyPressEvent(QKeyEvent *ke)
{
    QPoint newPos = m_character.position();
    switch (ke->key()) {
    case Qt::Key_Up:
    case Qt::Key_K:
        newPos.ry() -= 1;
        break;
    case Qt::Key_Down:
    case Qt::Key_J:
        newPos.ry() += 1;
        break;
    case Qt::Key_Left:
    case Qt::Key_H:
        newPos.rx() -= 1;
        break;
    case Qt::Key_Right:
    case Qt::Key_L:
        newPos.rx() += 1;
        break;
    case Qt::Key_I:
        showInventory();
        break;
    default:
        QWidget::keyPressEvent(ke);
    }
    if (tryMove(newPos)) {
        QRect r(QPoint((newPos.x() - 8) * TileWidth, (newPos.y() - 8) * TileHeight),
                QSize(24 * TileWidth, 24 * TileHeight));
        update(r);
        emit characterMoved(visualCursorPos());
    }
}
예제 #7
0
bool ComplexTetris::oneLineDown(){
    bool moved = tryMove(curPiece(), curX, curY - 1);
    if (!moved){
        droppedPiece();
    }
    return moved;
}
예제 #8
0
/**
 * update the agent position in the environment. Apply simple physics (ie. obstacle collision detection and consequences).
 * if collision, translation and rotation speed are set to zero.
 * note: __recursiveIt is currently unused (maybe used for debug purpose, eg. checking for infinite loop.)
 */
void RobotAgent::move(int __recursiveIt) // the interface btw agent and world -- in more complex envt, this should be handled by the "world".
{
	// apply world dynamics onto this agent
	// compute real valued delta (convert to x/y delta coordinates)
	applyDynamics();
	tryMove();
	updateSensors();
}
예제 #9
0
void ComplexTetris::drop(){
    int newY = curY;
    while (newY > 0) {
        if (!tryMove(curPiece(), curX, newY - 1))
            break;
        --newY;
    }
    droppedPiece();
}
예제 #10
0
void ComplexTetris::newPiece(){
    game->setCurrentPiece(game->getNextPiece());
    game->setNextPiece(Tetronimo::getRandomPiece());
    curX = getStartColumn();
    curY = game->getHeight() - 1 + curPiece().minY();

    if (!tryMove(curPiece(), curX, curY)) {
        //game over
        gameOver = true;
    }
}
예제 #11
0
파일: PhysicsSystem.cpp 프로젝트: leod/game
void PhysicsSystem::moveOne(PhysicsComponent* physics, vec3 const& delta) {
    Ray ray = { physics->getPosition(), delta };

    vec3 n;
    auto intersection = rayMapIntersection(ray, map, nullptr, &n);

    if (!intersection || intersection.get() > 1) {
        // No collision
        physics->setPosition(physics->getPosition() + delta);
    } else {
        // Collision with a surface having normal vector n.
        // Decompose delta into a part parallel to n and a part
        // orthogonal to n
        vec3 p = glm::dot(delta, n) * n;
        vec3 o = delta - p;

        // Try moving in the directions of just the parts
        tryMove(map, physics, p);
        tryMove(map, physics, o);
    }
}
예제 #12
0
파일: gameui.cpp 프로젝트: xueerk/MazeGame
void GameUI::setClient(GameClient *client)
{
    m_client = client;

    connect(m_client, SIGNAL(newLog(QString)), this, SLOT(handleNewLog(QString)));
    connect(m_client, SIGNAL(newClientInfo(QString)), this, SLOT(handleNewClientInfo(QString)));
    connect(m_client, SIGNAL(newPrimaryServerInfo(QString)), this, SLOT(handleNewPrimaryServerInfo(QString)));
    connect(m_client, SIGNAL(newBackupServerInfo(QString)), this, SLOT(handleNewBackupServerInfo(QString)));
    connect(m_client, SIGNAL(initGameState(GameState*)), this, SLOT(handleInitGameState(GameState*)));
    connect(m_client, SIGNAL(gameOver()), this, SLOT(handleGameOver()));
    connect(m_chessBoard, SIGNAL(tryMove(QString)), m_client, SLOT(handleTryMove(QString)));
}
예제 #13
0
/**
 * Returns a list of all valid moves for \p pWho
 *
 * \param pMoves a vector where the list of moves will be appended
 * \param pWho the \ref ECell code (CELL_OWN or CELL_OTHER) of the
 * player making the move
 */
void GameState::findPossibleMoves(std::vector<GameState> &pStates) const
{
    pStates.clear();

    if (mLastMove.isEOG())
        return;

    if (mMovesUntilDraw <= 0)
    {
        pStates.push_back(GameState(*this, Move(Move::MOVE_DRAW)));
        return;
    }

    // Normal moves are forbidden if any jump is found
    bool lFound=false;
    int lPieces[cPlayerPieces];
    uint8_t lMoveBuffer[cPlayerPieces];
    std::vector<Move> lMoves;
    int lNumPieces=0;
    for (int i = 1; i <= cSquares; ++i)
    {
        // Is this a piece which belongs to the player making the move?
        if (at(i) & mNextPlayer)
        {
            bool lIsKing = at(i)&CELL_KING;

            if (tryJump(lMoves, cellToRow(i), cellToCol(i), lIsKing, lMoveBuffer))
                lFound=true;

            lPieces[lNumPieces++]=i;
        }
    }

    // Try normal moves if no jump was found
    if (!lFound)
    {
        for (int k = 0; k < lNumPieces; ++k)
        {
            int lCell = lPieces[k];
            bool lIsKing = at(lCell) & CELL_KING;
            tryMove(lMoves, lCell, lIsKing);
        }
    }

    // Convert moves to GameStates
    for (unsigned i = 0; i < lMoves.size(); ++i)
        pStates.push_back(GameState(*this, lMoves[i]));

    // Admit loss if no moves can be found
    if (pStates.size() == 0)
        pStates.push_back(GameState(*this, Move(mNextPlayer == CELL_WHITE ? Move::MOVE_RW : Move::Move::MOVE_WW)));
}
예제 #14
0
//! [19]
void TetrixBoard::dropDown()
{
    int dropHeight = 0;
    int newY = curY;
    while (newY > 0) {
        if (!tryMove(curPiece, curX, newY - 1))
            break;
        --newY;
        ++dropHeight;
    }
    pieceDropped(dropHeight);
//! [19] //! [20]
}
예제 #15
0
//重载keyPressEvent函数
void game_area_frame::keyPressEvent(QKeyEvent *event)
{
    if(!isStart)
    {
        QFrame::keyPressEvent(event);
        if(event->key() == Qt::Key_Space)
            this->start_game();
        return;
    }
    switch (event->key()) {     //需要添加头文件QtWidgets.h
    case Qt::Key_Left:
        tryMove(this->x - 30, this->y);
        break;
    case Qt::Key_Right:
        tryMove(this->x + 30, this->y);
        break;
    case Qt::Key_Up:
        //判断是否越界
        this->now_Te->rotate(this->x, this->y);
        //还要进行碰撞检测,不能旋转的话需要旋转三次变为原样
        if(!tryMove(this->x, this->y))
        {
            this->now_Te->rotate(this->x, this->y);
            this->now_Te->rotate(this->x, this->y);
            this->now_Te->rotate(this->x, this->y);
        }
        break;
    case Qt::Key_Down:
        this->tryMove(x, y+30);
        this->down();
        break;
    case Qt::Key_Space:
        this->pause_game();
        break;
    default:
        QFrame::keyPressEvent(event);
    }
}
예제 #16
0
//! [30]
void TetrixBoard::newPiece()
{
    curPiece = nextPiece;
    nextPiece.setRandomShape();
    showNextPiece();
    curX = BoardWidth / 2 + 1;
    curY = BoardHeight - 1 + curPiece.minY();

    if (!tryMove(curPiece, curX, curY)) {
	curPiece.setShape(NoShape);
        timer.stop();
        isStarted = false;
    }
//! [30] //! [31]
}
예제 #17
0
int tryMoveAdapter(struct board * b, char * s, int player)
{
	char x1 = s[0];
	int x1i = (int)x1 - 97;
	char y1 = s[1];
	int y1i = (int)y1 - 48-1;

	char x2 = s[3];
	int x2i = (int)x2 - 97;
	char y2 = s[4];
	int y2i = (int)y2 - 48-1;

	printf("%d,%d,%d,%d",x1i,y1i,x2i,y2i);
	int t = tryMove(b,x1i,y1i,x2i,y2i,player);
	return t;

}
예제 #18
0
void Gunner::tick () 
{
	onGround = false;
	tryMove(xa, ya);

	if ((onGround || sliding != 0) && xa * xa < 0.01) 
  {
		if (chargeTime++ >= CHARGE_DURATION) 
    {
			chargeTime = 0;
			double xd = (level->player->x + level->player->w / 2) - (x + w / 2);
			double yd = (level->player->y + level->player->h / 2) - (y + h / 2);
			double dd = sqrt(xd * xd + yd * yd);
			xd /= dd;
			yd /= dd;
			//SoundMeta::hit->play();
			level->add(new Bullet(this, x + 2, y + 2, xd, yd));
		}
	}
	xa *= level->FRICTION;
	ya *= level->FRICTION;
	ya += level->GRAVITY;
}
예제 #19
0
void Jabberwocky::tick ()
{
    slamTime++;
    if (temperature > 0)
    {
        temperature--;
        for (int i = 0; i < 1; i++)
        {
            if (MathUtils::random(MAX_TEMPERATURE) <= temperature)
            {
                double xd = (MathUtils::randomFloat(1) - MathUtils::randomFloat(1)) * 0.2;
                double yd = (MathUtils::randomFloat(1) - MathUtils::randomFloat(1)) * 0.2;
                level->add(new Spark(x + MathUtils::randomFloat(1) * w, y + MathUtils::randomFloat(1) * h, xa * 0.2 + xd, ya * 0.2 + yd));
            }
        }
    }
    tryMove(xa, ya);
    xa *= Level::FRICTION;
    ya *= Level::FRICTION;
    ya += Level::GRAVITY;

    std::vector<Entity*> entities = level->getEntities((int)x + 4, (int)y + 4, w - 8, h - 4);
    for (int i = 0; i < entities.size(); i++)
    {
        Entity* e = entities[i];
        Gunner* gunner = dynamic_cast<Gunner*>( e);
        if (gunner)
        {
            temperature += 10;
            if (temperature >= MAX_TEMPERATURE)
            {
                die();
            }
        }
        e->collideMonster(this);
    }
}
예제 #20
0
int main()
{
	init();
	
	enterMenu(0);

	int cloudScroll = 0;
	int loopSlower = 0;
	bool shootCoolDown = true;
	int reloadTimer = 0;
	
	//GAME LOOP
	while (true)
	{	
		//INPUT
		if((REG_KEYINPUT & KEY_RIGHT) == 0){
			tryMove(2,0,player.getWidth()-2,player.getHeight()/2,player);
			player.setRunning(true);
			player.setDir(true);
		}
		if((REG_KEYINPUT & KEY_LEFT) == 0){
			tryMove(-2,0,0,player.getHeight()/2,player);
			player.setRunning(true);
			player.setDir(false);
		}
		if((REG_KEYINPUT & KEY_UP) == 0 || (REG_KEYINPUT & KEY_B) == 0){
			if(player.getLanded()){
				player.jump();
				player.updateFrame();
			}
			player.setJumpHeight(player.getJumpHeight()+4);
		}
		if((REG_KEYINPUT & KEY_A) == 0){
			if(shootCoolDown){
				if(weapon == 8){
					if(player.getDir())tryMove(-2,0,0,player.getHeight()/2,player);
					else tryMove(2,0,player.getWidth()-2,player.getHeight()/2,player);
				}else if(weapon == 1){
					if(player.getDir())tryMove(-1,0,0,player.getHeight()/2,player);
					else tryMove(1,0,player.getWidth()-2,player.getHeight()/2,player);
				}
				shoot();
				shootCoolDown = false;
				reloadTimer = 0;
			}
		}
		
		//Player movement
		if(player.getJumping()){
			if(!tryMove(0,-3,player.getWidth()/2,0,player)){
				player.setJumping(false);
			}
		}else{
			if(!tryMove(0,2,player.getWidth()/2,player.getHeight(),player)){
				tryMove(0,1,player.getWidth()/2,player.getHeight(),player);
				player.setLanded(true);
				player.setJumping(false);
			}else{
				player.setLanded(false);
			}
		}

		//Monster Movement
		for(int i = 0; i < enemies.size(); i++){
			if(!enemies.at(i).isDead()){
				if(enemies.at(i).getDir()){
					if(!tryMove(1,0,enemies.at(i).getWidth(),(enemies.at(i).getHeight()/2)+1,enemies.at(i))){
						enemies.at(i).setDir(false);
					}
				}else{
					if(!tryMove(-1,0,0,(enemies.at(i).getHeight()/2)+1,enemies.at(i))){
						enemies.at(i).setDir(true);
					}
				}
				tryMove(0,2,enemies.at(i).getWidth()/2,enemies.at(i).getHeight(),enemies.at(i));
				ObjBuffer[i+3].attr0 &= ~(ATTR0_HIDE);
			}else{
				ObjBuffer[i+3].attr0 |= ATTR0_HIDE;
			}
		}
		
		//Bullet movement
		for(int i = 0; i < bullets.size(); i++){
			if(!bullets.at(i).isDead()){
			
				int yMove = 0;
				if(rand() % 3 == 0)yMove = bullets.at(i).getLift(); 
				
				if(bullets.at(i).getType() == 4){
				
					tryMove(0,1,bullets.at(i).getWidth(),bullets.at(i).getHeight()+4,bullets.at(i));
					
				}else if(bullets.at(i).getType() == 6){
					if(player.getDir()){
						ObjBuffer[i+24].attr1 &= ~(ATTR1_HFLIP);
						bullets.at(i).move(player.getX()+8, player.getY());
					}else{
						ObjBuffer[i+24].attr1 |= ATTR1_HFLIP;
						bullets.at(i).move(player.getX()-8, player.getY());
					}	
					if(bullets.at(i).charge()){
						bullets.at(i).setDead(true);
						int number = 0;
						if(player.getDir())number = (SCREEN_WIDTH - player.getX())/8;
						else number = (0 + player.getX())/8;
	
						for(int i = 0; i < number; i++){
							spawnBullet(7);
						}
					}
				}else if(bullets.at(i).getType() == 7){
					if(bullets.at(i).charge())bullets.at(i).setDead(true);
				}else{
					if(bullets.at(i).getDir()){
						if(!tryMove(2,yMove,bullets.at(i).getWidth(), bullets.at(i).getHeight(), bullets.at(i))){	
							if(bullets.at(i).getType() == 3 && !bullets.at(i).getBounce()){
								bullets.at(i).setBounce(true);
								bullets.at(i).setDir(false);
							}else{
								bullets.at(i).setDead(true);
								if(bullets.at(i).getType() == 0
								|| bullets.at(i).getType() == 4){
									explode(bullets.at(i).getX(), bullets.at(i).getY());
								}
							}
						}
					}else{ 
						if(!tryMove(-2,yMove,bullets.at(i).getWidth(), bullets.at(i).getHeight(), bullets.at(i))){
							if(bullets.at(i).getType() == 3 && !bullets.at(i).getBounce()){
								bullets.at(i).setBounce(true);
								bullets.at(i).setDir(true);
							}else{
								bullets.at(i).setDead(true);
								if(bullets.at(i).getType() == 0
								|| bullets.at(i).getType() == 4){
									explode(bullets.at(i).getX(), bullets.at(i).getY());
								}
							}
						}
					}
				}
				ObjBuffer[i+24].attr0 &= ~(ATTR0_HIDE);
			}else{
				ObjBuffer[i+24].attr0 |= ATTR0_HIDE;
			}
		}
		
		for(int i = 0; i < enemies.size(); i++){
			if(!enemies.at(i).isDead()){
				if(checkEntityCollision(player, enemies.at(i))){
					player.setDead(true);
				}
				
				for(int k = 0; k < bullets.size(); k++){
					if(!bullets.at(k).isDead()){
						if(checkEntityCollision(bullets.at(k), enemies.at(i))){
							enemies.at(i).hurt(bullets.at(k).getDamage());
							if(bullets.at(k).getType() != 3
							&& bullets.at(k).getType() != 6
							&& bullets.at(k).getType() != 7){
								bullets.at(k).setDead(true);
								if(bullets.at(k).getType() == 0
								|| bullets.at(k).getType() == 4){
									explode(bullets.at(k).getX(), bullets.at(k).getY());
								}
							}
							break;
						}
					}
				}
			}
		}
		
		if(checkEntityCollision(player, crate)){
			crate.setDead(true);
			weapon = crate.getWeapon();
			score++;
		}
		
		if(crate.isDead()){
			switch(rand() % 7){
				case 0:crate.move(80,32);
				break;
				case 1:crate.move(150,32);
				break;
				case 2:crate.move(30,64);
				break;
				case 3:crate.move(200,64);
				break;
				case 4:crate.move(115,104);
				break;
				case 5:crate.move(80,144);
				break;
				case 6:crate.move(150,144);
				break;
			}
			int newWep = 0;
			do newWep = rand() % 9;
			while(newWep == weapon || newWep == 6);
			crate.setWeapon(newWep);
			crate.setDead(false);
		}
		
		//General Slow things
		if(loopSlower % 7 == 0){
			cloudScroll++;
			REG_BG3HOFS = cloudScroll;
		
			SetTile(30, 14, 19, 16+rand()%5);
			SetTile(30, 15, 19, 16+rand()%5);
			
			player.updateFrame();
			
			for(int i = 0; i < enemies.size(); i++){
				if(!enemies.at(i).isDead()){
					enemies.at(i).updateFrame();
				}
			}
			ObjBuffer[2].attr2 = ATTR2_ID8(player.getFrame());
			for(int i = 0; i < enemies.size(); i++){
				ObjBuffer[i+3].attr2 = ATTR2_ID8(enemies.at(i).getFrame());
			}
			
		}
		
		reloadTimer++;
		if(weapon == 0 || weapon == 5 || weapon == 6 || weapon == 7){
			if(reloadTimer >= 60){
				shootCoolDown = true;
			}
		}else if(weapon == 2 || weapon == 4){
			if(reloadTimer >= 45){
				shootCoolDown = true;
			}
		}else if (weapon == 3){
			if(reloadTimer >= 20){
				shootCoolDown = true;
			}
		}else if(weapon ==  1){
			if(reloadTimer >= 10){
				shootCoolDown = true;
			}
		}else{
			if(reloadTimer >= 5){
				shootCoolDown = true;
			}
		}
		if(reloadTimer > 1000)reloadTimer = 60;
		
		//Monster spawner
		if(rand()%100 == 0){
			spawnMonster();	
		}
		
		//Player state updates
		if(player.getJumping())player.setState(2);
		else if(player.getRunning())player.setState(1);
		else player.setState(0);
		
		if(!player.getDir()){
			ObjBuffer[0].attr1 |= ATTR1_HFLIP;
			ObjBuffer[2].attr1 |= ATTR1_HFLIP;
		}else{
			ObjBuffer[0].attr1 &= ~(ATTR1_HFLIP);
			ObjBuffer[2].attr1 &= ~(ATTR1_HFLIP);
		}
		
		if(weapon == 8)ObjBuffer[0].attr0 |= ATTR0_SHAPE(1);
		else ObjBuffer[0].attr0 &= ~(ATTR0_SHAPE(1));
		
		ObjBuffer[0].attr2 = ATTR2_ID8(22+weapon);
			
		for(int i = 0; i < enemies.size(); i++){
			if(!enemies.at(i).isDead()){
				if(!enemies.at(i).getDir())ObjBuffer[i+3].attr1 |= ATTR1_HFLIP;
				else ObjBuffer[i+3].attr1 &= ~(ATTR1_HFLIP);
			}
		}
		
		player.update();
		
		if(player.isDead())enterMenu(2);
	
		//Render
		if(!player.getDir()){
			if(weapon != 8)SetObjectX(0, player.getX()-4);
			else{
				if(player.getX()-12 <= 0) SetObjectX(0, 0);
				else SetObjectX(0, player.getX()-12);
			}	
		}else{
			SetObjectX(0, player.getX()+4);
		}
		SetObjectY(0, player.getY()+1);
		
		SetObjectX(1, crate.getX());
		SetObjectY(1, crate.getY());
		
		SetObjectX(2, player.getX());
		SetObjectY(2, player.getY());
		
		for(int i = 0; i < enemies.size(); i++){
			if(!enemies.at(i).isDead()){
				SetObjectX(i+3, enemies.at(i).getX());
				SetObjectY(i+3, enemies.at(i).getY());
			}
		}
		
		for(int i = 0; i < bullets.size(); i++){
			if(!bullets.at(i).isDead()){
				SetObjectX(i+24, bullets.at(i).getX());
				SetObjectY(i+24, bullets.at(i).getY());
			}
		}

		drawText(112,10,toString(score));
		
		WaitVSync();
		UpdateObjects();
		clearText();
		loopSlower++;
	}

	return 0;

}
예제 #21
0
void RobotAgent::tryMove(int __recursiveIt) {
	// backup old position in case of collision
	this->xReal_old = _wm->_xReal;
	this->yReal_old = _wm->_yReal;

	// update x/y position
	double headingAngle = _wm->_agentAbsoluteOrientation * M_PI / 180;
	double sinHeading, cosHeading;
	sincos(headingAngle, &sinHeading, &cosHeading);
	_xDeltaReal = _wm->_agentAbsoluteLinearSpeed * cosHeading;
	_yDeltaReal = _wm->_agentAbsoluteLinearSpeed * sinHeading;

	_wm->_xReal += _xDeltaReal;
	_wm->_yReal += _yDeltaReal;
	setCoord((int) _wm->_xReal + 0.5, (int) _wm->_yReal + 0.5);

	if (isCollision()) {
		// roborobo assumes pixel-based resolution for solving collision. Ie. ignore sub-pixel translation values. (see _README.TXT for details)
		if (_wm->_agentAbsoluteLinearSpeed >= 1.0) {
			_wm->_agentAbsoluteLinearSpeed--;
		} else if (_wm->_agentAbsoluteLinearSpeed <= -1.0) {
			_wm->_agentAbsoluteLinearSpeed++;
		}
		if ((_wm->_agentAbsoluteLinearSpeed > -1.0) && (_wm->_agentAbsoluteLinearSpeed < 1.0)) {
			_wm->_agentAbsoluteLinearSpeed = 0;
		}

		_wm->_xReal = xReal_old;
		_wm->_yReal = yReal_old;

		// if zero: move is canceled
		if (!::isnan(_wm->_agentAbsoluteLinearSpeed) && _wm->_agentAbsoluteLinearSpeed != 0)
		{
			__recursiveIt++;
			tryMove(__recursiveIt);
		} else {
			// special case: agent cannot not move at all - restore original coordinate (remember, _x/yReal are global and modified during recursive call).
			_wm->_xReal = xReal_old;
			_wm->_yReal = yReal_old;
			setCoord((int) _wm->_xReal + 0.5, (int) _wm->_yReal + 0.5);
		}

		// update world model variables and internal data

		_wm->_agentAbsoluteLinearSpeed = 0;

		if (gLocomotionMode == 1) // consider obstacle friction or not for rotation?
		{
			_wm->_agentAbsoluteOrientation = _lastAgentAbsoluteOrientation;
			_wm->_actualRotationalVelocity = 0;
		} else
			_wm->_actualRotationalVelocity = _wm->_desiredRotationalVelocity;

		//_wm->_desiredTranslationalValue = _wm->_desiredRotationalVelocity = 0;
		_wm->_actualTranslationalValue = 0;

	} else {
		// actual rotational and translational values matches desired values
		_wm->_actualRotationalVelocity = _wm->_desiredRotationalVelocity;
		_wm->_actualTranslationalValue = _wm->_agentAbsoluteLinearSpeed; // (!) _wm->_desiredTranslationalValue is different as the "desired" translation speed may not be reached due to physical actuator limitations
	}
}
예제 #22
0
void MainWindow::timerEvent ( QTimerEvent * event ){

    // ----------- partie personnage
    if (gravity<0){
        personnage->setCurrentH(personnage->getCurrentH()+1);
        if(personnage->getCurrentH()>= personnage->getMaxH() || collisionTest(0,-1)){
            gravity = baseGravity;
            personnage->setCurrentH(0);
        }
        else{
            personnage->setCurrentS(6);
            personnage->immobile();
        }

    }
    else if(collisionTest(0,1)){
        if(gravity == 1)
            personnage->setCurrentS(4); //LANDING
        gravity = 0;
    }
    else {
        gravity = baseGravity;
        personnage->setCurrentS(3); // FALLING
        personnage->immobile();
    }

    int x = 0;
    if(gravity == 0 && controleur->getStateKeys(0))
        tryJump();

    if(controleur->getStateKeys(2)){
        if (personnage->tryDropBombe()){
            // ajouter un truc du style personnage->hasBonusBombe()
            ajouterBombe(personnage->getX()+personnage->getLargeur()/2,personnage->getY()+ personnage->getHauteur());
            controleur->setPressed(Qt::Key_Down,false);
        }
    }

    if(controleur->getStateKeys(1)){
        x +=- 1;
        personnage->courireG();
    }
    else if(controleur->getStateKeys(3)){
        x += 1;
        personnage->courireD();
    }

    if(x == 0 && gravity == 0 /*&& personnage->getCurrentS() != 3*/)
        personnage->immobile();

    // TODO ? asscocier les bombes au joueur, soit avec le trigger du Joueur, soir en ayant bombes[NumJ][bombes]
    if(/*personnage->hasBonusTrigger == trigger &&*/controleur->getStateKeys(4)){
        triggerAll();
        controleur->setPressed(Qt::Key_Space,false);
    }

    tryMove(0,gravity);
    tryMove(x,0);


    // ----------- partie bombes
    int tmpSizeB = bombes.size();
    for(int i = 0; i<tmpSizeB; i++){
        if(bombes[i]->isExploding()){
            explosion(bombes[i],0,0);
            explosion(bombes[i],0,1);
            explosion(bombes[i],0,-1);
            explosion(bombes[i],1,0);
            explosion(bombes[i],-1,0);


        }
        if(bombes[i]->hasExploded()){
            int tmpSizeE = bombes[i]->getExplosions().size();
            for(int j = 0; j< tmpSizeE; j++)
                scene->removeItem(bombes[i]->getExplosions().at(j));
            scene->removeItem((bombes[i])->getPicture());
            bombes.remove(i);
            tmpSizeB--;
            personnage->decrNbBombe();
        }
    }

}
예제 #23
0
void chessGame::tM2()
{
    tryMove(player2->getMove());
}
예제 #24
0
void chessGame::tM1()
{
    tryMove(player1->getMove());
}
예제 #25
0
void Player::tick (InputMeta* input) 
{
	breadSign = true; // onGround && input->buttons[InputMeta::UP] && !input->oldButtons[InputMeta::UP];
	if (noHurtTime > 0) noHurtTime--;
	double speed = 0.4;
	double aimAngle = -0.2;
	yAim = 0;
	if (input->buttons[InputMeta::UP]) 
  {
		aimAngle -= 0.8;
		yAim--;
	}
	if (input->buttons[InputMeta::DOWN]) 
  {
		aimAngle += 0.8;
		yAim++;
	}
	bool walk = false;
	if (input->buttons[InputMeta::LEFT]) 
  {
		walk = true;
		xa -= speed;
		dir = -1;
	}
	if (input->buttons[InputMeta::RIGHT]) 
  {
		walk = true;
		xa += speed;
		dir = 1;
	}
	if (walk)
		frame++;
	else
		frame = 0;
	if (input->buttons[InputMeta::JUMP] && !input->oldButtons[InputMeta::JUMP] && onGround) 
  {
		//SoundMeta::jump->play();
		ya -= 2 + fabs(xa) * 0.5;
	}

	tryMove(xa, ya);

	xa *= 0.7;
	if (ya < 0 && input->buttons[InputMeta::JUMP]) 
  {
		ya *= 0.992;
		ya += Level::GRAVITY * 0.5;
	} else {
		ya *= Level::FRICTION;
		ya += Level::GRAVITY;
	}

	bool shooting = false;
	if (gunLevel > 0 && input->buttons[InputMeta::SHOOT] && !input->oldButtons[InputMeta::SHOOT]) shooting = true;
	if (gunLevel > 1 && input->buttons[InputMeta::SHOOT] && (!input->oldButtons[InputMeta::SHOOT] || shootTime > 0)) 
  {
		shooting = shootTime++ % 3 == 0;
	} else {
		shootTime = 0;
	}
	if (shooting) 
  {
		double pow = 3;
		//SoundMeta::launch->play();

		double xx = x + w / 2.0 - 2.5 + dir * 7;
		double yy = y + h / 2.0 - 2.5 + yAim * 2;
		for (int i = 0; i < 4; i++) 
    {
			double xAim = MathUtils::cos(aimAngle + 0.2) * dir * pow;
			double yAim = MathUtils::sin(aimAngle + 0.2) * pow;
			double xxa = xa + xAim * 0.2;
			double yya = ya + yAim * 0.2;
			level->add(new Spark(xx, yy + (-2 + i) * 0.5, xxa, yya));
		}
		double xAim = MathUtils::cos(aimAngle) * dir * pow;
		double yAim = MathUtils::sin(aimAngle) * pow;
		double xxa = xa + xAim;
		double yya = ya + yAim;
		if (gunLevel == 2) 
    {
			xa -= xAim * 0.1;
			ya -= yAim * 0.1;
		}
		xx = x + w / 2.0 - 2.5;
		Stats::instance.shots++;
		level->add(new Gunner(xx, yy, xxa, yya));
	}

	if (y < 5) level->transition(0, -1);
	if (y > 240 - w + 10 - 5) level->transition(0, 1);
	if (x < 0 + 5) level->transition(-1, 0);
	if (x > 320 - h + 10 - 5) level->transition(1, 0);
}
예제 #26
0
//! [21]
void TetrixBoard::oneLineDown()
{
    if (!tryMove(curPiece, curX, curY - 1))
	pieceDropped(0);
}
예제 #27
0
bool ComplexTetris::moveRight(){
    return tryMove(curPiece(), curX + 1, curY);
}
예제 #28
0
bool ComplexTetris::moveLeft(){
    return tryMove(curPiece(), curX - 1, curY);
}
예제 #29
0
bool ComplexTetris::rotateCCW(){
    return tryMove(curPiece().rotatedLeft(), curX, curY);
}
예제 #30
0
//! [21]
void QrfeKeyBoard::oneLineDown()
{
    if (!tryMove(curPiece, curX, curY - 1))
	pieceDropped(0);
}