예제 #1
0
파일: snake.cpp 프로젝트: opieproject/opie
void Snake::createSnake()
{
   snakeparts = new QCanvasPixmapArray();
   QString s0 = Opie::Core::OResource::findPixmap("snake/s0001");
   s0.replace(QRegExp("0001"),"%1");
   snakeparts->readPixmaps(s0, 15);

   grow = 0;
   last = Key_Right;

   QCanvasSprite* head = new QCanvasSprite(snakeparts, canvas );
   head->setFrame(7);
   snakelist.insert(0, head);
   head->show();
   head->move(34, 16);

   QCanvasSprite* body = new QCanvasSprite(snakeparts, canvas );
   body->setFrame(6);
   snakelist.append( body );
   body->show();
   body->move(18, 16);

   QCanvasSprite* end = new QCanvasSprite(snakeparts, canvas );
   end->setFrame(11);
   snakelist.append( end );
   end->show();
   end->move(2, 16);

   currentdir = right;
   speed = 250;
   autoMoveTimer->start(speed);
   moveSnake();
}
예제 #2
0
파일: snake.cpp 프로젝트: juanAngel/Snake
int runSnake(int /*interval*/,snakeContext* context) {
    moveSnake(context->snake,context->display,context->applet);

    renderer::sendUpdateWindows();

    return context->snake->delay;
}
예제 #3
0
  void loop() {
    Color segmentColor;
    frameCount++;

    if (!deathFrame) {
      moveSnake();
    } else {
      if (frameCount - deathFrame > 100) {
        setup();
      }
    }

    cube.background(black);
    for(auto it = snake.begin(); it != snake.end(); ++it) {
      if (deathFrame && frameCount % 16 < 8) {
        segmentColor = Color(255, 255, 255);
      } else if (deathFrame) {
        segmentColor = Color(255, 0, 0);
      } else {
        segmentColor = Color((it->x+1)*255/8, (it->y+1)*255/8, (it->z+1)*255/8);
      }
      cube.setVoxel(it->x, it->y, it->z, segmentColor);
    }

    if (!deathFrame) {
      for(auto it = treats.begin(); it != treats.end(); ++it) {
        cube.setVoxel(it->x, it->y, it->z, Color(150, 255, 0));
      }
    }
    cube.show();
  }
예제 #4
0
int main()
{
	printf("\t\t\t\t****Controls****\n\n");
	printf("\tW = Up | A = Left | S = Down | D = Right\n\n");
	printf("\t\t\t\t****KNOWN ISSUES****\n\n");
	printf("\t As the main functionality has been added already (which was the intent)\n");
	printf("\t I didn\'t care to develop it further.\n\n");
	printf("\t (1) Game crashes when snake reaches the edge, no boundary \n\t limit has been defined for the snake to crawl.\n");
	printf("\n\t (2) No score/level system.\n");
	printf("\n\t (3) Food generates around a specific region only, Tweak the\n \t rand() values to get more random locations.\n");
	printf("\n\t\t\t\t\t\tGaurav Butola");
	printf("\n\t\t\t\t\t\[email protected]");
	printf("\n\n\t\t\t\tPress any key to play");

	
	getch();
	printf("%c[2J",0x1B); /*Erase the screen*/
	
	/*Show the snake when game starts*/
	gotoxy(headPositionX, headPositionY);
	putchar(snakeHead);
	
	int i;
	for(i=1; i<=bodyLength; i++) {
		bodyPositionX[i] =  headPositionX-i;
		bodyPositionY[i] = headPositionY; /*Not moving in Y axis*/
		gotoxy(bodyPositionX[i], bodyPositionY[i]);
		putchar(snakeBody);
	}	
	
	/*********Start game loop here*******/
	
	while(1) {
		changemode(1);
	    if(!foodEaten) {
            generateFood();
            foodEaten = 1;
	    }
		if(kbhit()) {
			direction = getch();
			moveSnake();
		}
		else
			moveSnake();
	}
	return 0;
}
예제 #5
0
파일: snake.cpp 프로젝트: opieproject/opie
Snake::Snake(QCanvas* c)
{
   canvas = c;
   score = 0;
   snakelist.setAutoDelete(true);
   autoMoveTimer = new QTimer(this);
   connect( autoMoveTimer, SIGNAL(timeout()), this, SLOT(moveSnake()) );
   createSnake();
}
예제 #6
0
파일: snake.cpp 프로젝트: Camelek/qtmoko
Snake::Snake(QGraphicsScene* c)
    : scene(c)
    , score(0)
{
    autoMoveTimer = new QTimer(this);
    autoMoveTimer->setSingleShot(false);
    connect( autoMoveTimer, SIGNAL(timeout()), this, SLOT(moveSnake()) );
    reset();
}
예제 #7
0
void EventManager::keyBotAction()
{
  if (_snake.getDir() == TOP)
    return;
  _snake.setDir(BOTTOM);
  if (moveSnake())
    _timer.start();
  else
    _timer.stop();
}
예제 #8
0
void EventManager::keyRightAction()
{
  if (_snake.getDir() == LEFT)
    return;
  _snake.setDir(RIGHT);
  if (moveSnake())
    _timer.start();
  else
    _timer.stop();
}
예제 #9
0
void UpdateController::update() {
	snakeMutex.lock();
    for (auto snake : snakes) {
        moveSnake(*snake);
    }
	snakeMutex.unlock();
    if (coins.size() < NUMBER_OF_COINS) {
        while (coins.size() < NUMBER_OF_COINS) addCoin();
    }
}
예제 #10
0
파일: game_object.cpp 프로젝트: nhees/snake
void SnakeObject::update(){
	
	// Since we want a "choppy" motion, only update the snake every 10 frames
	frameCount++;

	if( frameCount >= 10 ){
		frameCount = 0;
		xvel = incomingVelocity.x;
		yvel = incomingVelocity.y;
		moveSnake();
	}
}
예제 #11
0
EventManager::EventManager(QWidget &parent, GameManager &engine, uint speed) :
  QWidget(&parent, 0),
  _snake(engine.getSnake()),
  _land(engine.getLand()),
  _engine(engine)
{
  setFocusPolicy(Qt::StrongFocus);
  connect(&_timer, SIGNAL(timeout()), this, SLOT(moveSnake()));
  _timer.start(100 / speed);
  resetDir();
  _left = false;
}
예제 #12
0
파일: snake.cpp 프로젝트: Vinashu/snake
int main(void){
  	setlocale(LC_ALL, "Portuguese");    
	iniciaJogo();
	c = 0;
	tecla = ' ';
	while(tecla != 'x'){
		desenhaTela();
		debug(c++);
		delay();
		lerTecla();	
		moveSnake();					
		clear();
	}
	return(0);	
}
예제 #13
0
파일: Snake.c 프로젝트: fanatiique/Snake
int	main(int argc, char *argv[]) 
{
    int **grille;
	int column;
	int line;
    int nbrCells = 0;
    int dir=2;
    grille = setArray();
    setWindow(grille, &dir);

    for (line = 0; line < 42; line++)
    {
        for (column = 0; column < 62; column++)
        {
            if (grille[line][column] == 1)
            {
                printf("Snake %d => grille[%d][%d]\n", column - 25, line, column);
            } else if (grille[line][column] == 3) 
            {
        	   printf("Cell %d => grille[%d][%d]\n", nbrCells, line, column);
               nbrCells++;
            }
        }
    }
    unsigned long suivant=Microsecondes()+33333;
    unsigned long move=Microsecondes()+250000;
    double head=21.35;
    double tail=21.26;
    int fin=0;
    while(fin==0)
    {   
        if ((Microsecondes()>move) || (ToucheEnAttente()==1))
        {
            fin=moveSnake(grille,&dir,&head, &tail);
            move=Microsecondes()+250000;
        }
        if (Microsecondes()>suivant)
        {
            refresh(grille, &dir);
            suivant=Microsecondes()+33333;    
        }
        
    }
    
	return EXIT_SUCCESS;
}
예제 #14
0
void doGame(void)
{
	isPause = 0;
	while (!isOver)
	{
		if (!isPause)
    {
		moveSnake();
		if (!isFood)            
			drawFood();
    }
    delayMS(ms - snakeLength*2);
    if (jkHasKey()) ///异步检测按键
    {
		switch (jkGetKey())
		{
			case JK_UP:
				if (snakeDir != DIR_DOWN)
				snakeDir = DIR_UP;
			break;
			case JK_DOWN:
				if (snakeDir != DIR_UP)
				snakeDir = DIR_DOWN;
			break;
			case JK_LEFT:
				if (snakeDir != DIR_RIGHT)
				snakeDir = DIR_LEFT;
			break;
			case JK_RIGHT:
				if (snakeDir != DIR_LEFT)
				snakeDir = DIR_RIGHT;
			break;
			case JK_ESC: //游戏结束
				isOver = !isOver;
			break;
			case JK_SPACE: //暂停
				isPause = !isPause;
				break;
			default:
			break;
      }
    }
  }
}
예제 #15
0
파일: main.cpp 프로젝트: ahmedlogic/LEDcube
int main() {
    
    sysClock.reset();
    sysClock.start();
    my2c.frequency(400000);

    b_xpos.fall(&getSnakeInput);
    b_xpos.mode(PullUp);
    b_xneg.fall(&getSnakeInput);
    b_xneg.mode(PullUp);
    b_ypos.fall(&getSnakeInput);
    b_ypos.mode(PullUp);
    b_yneg.fall(&getSnakeInput);
    b_yneg.mode(PullUp);
    b_zpos.fall(&getSnakeInput);
    b_zpos.mode(PullUp);
    b_zneg.fall(&getSnakeInput);
    b_zneg.mode(PullUp);
    wait(0.2);
        
    initializeSnake(&Snake); // sets ticker to SPEED
    
    if(LLDEBUG) printListLL(&Snake);
    while(1) {

        myCube.lightCube(CUBE_DELAY);
        if(snakeFlag) {
            snakeFlag = 0;
            moveSnake();
        }
    }
    
    //freeListLL(&Snake);
    //printf("$ Program complete ... how did we get here?\r\n");

}
예제 #16
0
파일: Snake.c 프로젝트: Sergio0694/Snake
void newGame(int** grid, int* score, int* backToMenu, int gameMode, int delay) {
	int eaten_flag, skip_sleep = 0;
	int backup_move = KEY_RIGHT, move = KEY_RIGHT;
	while (1) {
		eaten_flag = 0;
		if (!skip_sleep) {
			if (move == KEY_RIGHT || move == KEY_LEFT) {
				Sleep(delay - 20);
			}
			else {
				Sleep(delay);
			}
		}
		else {
			skip_sleep = 0;
		}
		if (_kbhit()) {
			move = playerMove();
		}
		if (move == EXIT) {
			while (1) {
				clear_screen();
				pauseGraphics();
				if (mainMenu(0)) {
					break;
				}
				else {
					*backToMenu = 1;
					return;
				}
			}
			move = backup_move;
			restoreInGameGraphics(grid, *score);
			continue;
		}
		if (!checkValidMove(move) || move == INVALID_MOVE) {
			move = backup_move;
			skip_sleep = 1;
			continue;
		}
		backup_move = move;
		if (gameOverCheck(move, gameMode)) {
			drawGameOver();
			Sleep(1000);
			if (*score > record) {
				record = *score;
			}
			break;
		}
		assignSnake2Matrix(grid, 0);
		moveSnake(move, gameMode);
		eaten_flag = checkIfEaten(grid);
		if (eaten_flag) {
			fattenSnake();
		}
		assignSnake2Matrix(grid, 1);
		if (eaten_flag) {
			addFood(grid);
			updateSnakeGraphics(grid, 1);
			*score += 1;
			updateScore(*score);
		}
		else {
			updateSnakeGraphics(grid, 0);
		}
	}
}
예제 #17
0
//主函数
int main()
{ clock_t start,finish;
   int isPause = 1;
   int chance=0;
   float RewardTime=0;
   int z;
   
   setCursorVisible(0);
   setConsoleTitle("贪吃蛇--D_Y。 2010.1.11");        //标题
   ch=menu();
   ch2=menuDouble();               
   if(ch2=='1')
    z=0;
   if(ch2=='2')
    z=1;
   initSnake(SNAKE_MIN_LEN,z);
   drawMap();
   drawSnake(z);
   //PlaySound("d:\\泡泡堂轻松欢快音乐音效WAV.wav",NULL, SND_ASYNC|SND_NODEFAULT|SND_LOOP );   
   switch(ch)                 //不同的难度对应不同的奖励食物消失时间
   {
   case '1':
       RewardTime=2500;
   case '2':
    RewardTime=3000;
   case '3':
    RewardTime=3500;
   }
   while (!isOver[0]&&!isOver[1])             //当两条蛇任意一条死亡则游戏结束
   {   
    srand((uint32)time(NULL));
      if (!isPause)                           
      {
         moveSnake(z);
         if (!isFood)
            drawFood();
    while(((point[1]+point[0])%4==0)&&((point[1]+point[0])>3)&&p==1)        
                                                          //规定在得分大于三分时每吃掉四个常规食物产生一个奖励食物
    { 
     if (!isReward)
    drawReward();
       start=clock();                                        //画出食物,记录此刻时间
      break;  
    }

    finish=start+RewardTime ;                                //结束时间为开始时间加规定的奖励食物出现时间
            if((isReward==1)&&(finish<=clock()))
    {
             drawBlock(myReward.x, myReward.y, BS_SPACE); //超出规定时间则将奖励食物用背景方框覆盖
    myReward.x=-1;
    myReward.y=-1;
    isReward=0;                                           
    }
    if(((point[1]+point[0])%3==0)&&((point[1]+point[0])>2)&&q==0)                         
    {
    
      clearBar();
    
                    drawBar();
    }
           
    setTextColor(myColors[4]);                                    //规定出输出分数等信息
    gotoTextPos(MAP_BASE_X*2 -14 , MAP_BASE_Y + 2);
    printf("Player 1");
   gotoTextPos(MAP_BASE_X*2 -14 , MAP_BASE_Y + 4);
   
            printf("Score: %d.", point[0]);
    if(z==1)
    {
   gotoTextPos(MAP_BASE_X*2 + MAP_WIDTH*2+4 , MAP_BASE_Y + 2);
            printf("Player 2");
   gotoTextPos(MAP_BASE_X*2 + MAP_WIDTH*2+4 , MAP_BASE_Y + 4);
            printf("Score: %d.", point[1]);
    }                                                          
   
    
      }
      DELAY(ch); //赋予游戏速度
   delayMS(80- snakeLength[0]*2); //游戏速度随着蛇身增长而增加
      if (jkHasKey()) //键盘控制
      {
         switch (jkGetKey())
         {
      
            case JK_UP:
               if (snakeDir[0] != DIR_DOWN)
                  snakeDir[0] = DIR_UP;
      break;
    case 'w': 
               if (snakeDir[1] != DIR_DOWN)
                  snakeDir[1] = DIR_UP;
               
               break;
  
            case JK_DOWN:
               if (snakeDir[0] != DIR_UP)
                  snakeDir[0] = DIR_DOWN;
      break;
      case 's': 
      if (snakeDir[1] != DIR_UP)
                  snakeDir[1] = DIR_DOWN;
               break;
  
            case JK_LEFT:
               if (snakeDir[0] != DIR_RIGHT)
                  snakeDir[0] = DIR_LEFT;
      break;
      case 'a': 
       if (snakeDir[1] != DIR_RIGHT)
                  snakeDir[1] = DIR_LEFT;
               break;
  
            case JK_RIGHT:
               if (snakeDir[0] != DIR_LEFT)
                  snakeDir[0] = DIR_RIGHT;
      case 'd': 
        if (snakeDir[1] != DIR_LEFT)
                  snakeDir[1] = DIR_RIGHT;
               break;
            case JK_ENTER:
            case JK_SPACE:
               isPause = !isPause;
               break;
      case JK_ESC:
       isOver[0]=1;
       break;
            default:
               break;
         }

      }
   }
   PlaySound("d:\\超级马里奥兄弟 死掉音.wav",NULL, SND_ASYNC|SND_NODEFAULT );
   gotoTextPos(MAP_BASE_X + MAP_WIDTH - 7, MAP_BASE_Y + MAP_HEIGHT + 1);
   if(isOver[0]==1&&z==0)
printf("Game Over!!");
   if(isOver[0]==1&&z==1)
printf("Player 2 win!!");   
   if(isOver[1]==1)
   printf("Player 1 win!!");//游戏结束,在规定处显示分数
   getch();
   return 0;
}