Exemple #1
0
void MainWindow::cellClicked(int index)
{
  if(!start && (isGameOver() || lcd->intValue() == 0))
  {
    if(soundaction->isChecked())
      clicksound->play();
    blink(index);
  }
  else
  {
    if(soundaction->isChecked())
      turnsound->play();

    //qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
    Cell* cell = board[index];
    toggleCell(cell);
    toggleCell(lCell(cell));
    toggleCell(rCell(cell));
    toggleCell(uCell(cell));
    toggleCell(ulCell(cell));
    toggleCell(urCell(cell));
    toggleCell(dCell(cell));
    toggleCell(dlCell(cell));
    toggleCell(drCell(cell));

    //qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents);

    if(soundaction->isChecked())
      connectsound->play();

    if (!start) lcd->display(lcd->intValue() - 1);
    cellsLcd->display(cellsCount());

    if(isGameOver() && !start)
    {
      if(soundaction->isChecked())
        winsound->play();
      blink(index);    
      if(lcd->intValue() <= highscores.at(2 * (skill + 1) * NumHighscores - 1).toInt())
        addHighscore(lcd->intValue());
                   QMessageBox::information(this,tr("Game Over"),tr("Congratulations! Score %1").arg(lcd->intValue()));
    }
  }
}
Exemple #2
0
void MainWindow::showHighscores()
{
  addHighscore(0);
}
Exemple #3
0
int gameloop(dataStore *data,SDL_Surface *screen) {
	createRandomField(data);
	clock_t innerStartTime, innerStopTime, startTime, stopTime, diffTime,mouseTime,mT,mousetimewait=500;
	int done=0,i=0,aVal,motionPath=0,runPath=0,drawPath=0,mouseDown=0,ownpath=0;
	position *lastmouse=NULL,*mouse_pos=NULL,*tmp=NULL,*lastpath=NULL;
	SDL_Event event;

	// MUSIC FADEOUT AND IN
	//Mix_HaltMusic();	 
		Mix_PlayMusic( data->ingamemusic, -1);
	
	
	GraphicUpdate(screen,data);
	while (!done) {
		
		/* Check for events */
		startTime = SDL_GetTicks();
		while ( SDL_PollEvent(&event) ) {
			innerStartTime = SDL_GetTicks();
			switch (event.type) {
				case SDL_QUIT:
					done = 1;
					quitSDL(data);
					break;
				case SDL_MOUSEMOTION:
					//Prüft ob sich die Maus aus dem 50x50 Feld bewegt hat
					mouse_pos=calloc(1,sizeof(struct position));
					SDL_GetMouseState(&mouse_pos->x,&mouse_pos->y);
					if(mouse_pos->x > 24 && 226 > mouse_pos->x && mouse_pos->y > 24 && 51 >  mouse_pos->y) {
						aStar(data,&(data->home));
						GraphicUpdate(screen,data);
						positionListDelete(data);
						break;
					} else if(mouse_pos->x > 574 && 776 > mouse_pos->x && mouse_pos->y > 24 && 51 > mouse_pos->y) {
						aStar(data,&(data->stash));
						GraphicUpdate(screen,data);
						positionListDelete(data);
						break;
					} else {
						mouse_pos=pixelToGrid(mouse_pos);
						if(lastmouse!=NULL && mouseDown) {
							if(lastmouse->x!=mouse_pos->x || lastmouse->y!=mouse_pos->y || ownpath==0) {
								tmp=calloc(1,sizeof(struct position));
								tmp->x=mouse_pos->x+data->horizontalScroll;
								tmp->y=mouse_pos->y+data->verticalScroll;
								if(data->player.anfang!=NULL&& data->hedgewood[tmp->y][tmp->x].aStarValue*data->hedgewood[tmp->y][tmp->x].visible>-1 && aStarManhatten(*lastpath,*tmp)==AVGASTAR) {
									lastpath=tmp;
									positionQListAdd(data,tmp);
									ownpath++;
									GraphicUpdate(screen,data);
								} else {
									if(tmp->x==data->player.p_pos.x && tmp->y==data->player.p_pos.y) {
										lastpath=tmp;
										positionQListAdd(data,tmp);
										ownpath++;
										GraphicUpdate(screen,data);
									} else if(lastmouse->x!=mouse_pos->x || lastmouse->y!=mouse_pos->y)printf("Die Startposition muss die Spielerposition sein\n");
								}
								//free(tmp);
							}
						} else if(lastmouse!=NULL && ownpath==0) {
							if(lastmouse->x!=mouse_pos->x || lastmouse->y!=mouse_pos->y)mouseTime=SDL_GetTicks(),drawPath=1;
							else {
								if(drawPath) {
									mT=SDL_GetTicks();
									if((mouseTime + mousetimewait)< mT) {
										motionPath=1;
									}
								}
							}
						} else if(ownpath==0) {
							mouseTime = SDL_GetTicks();
							drawPath=1;
							if((lastmouse=calloc(1,sizeof(position))) == NULL) {
								printf("Kein Speicherplatz vorhanden fuer position\n");
								return -1;
							}
						}
						memcpy(lastmouse,mouse_pos,sizeof(position));
					}
					free(mouse_pos);
					break;
				case SDL_MOUSEBUTTONDOWN:
					mouseDown=1;
					if(DEBUG)printf("MOUSE DOWN\n");
					break;
				case SDL_MOUSEBUTTONUP:
					if(ownpath>0) {
						runPath=1;
						mouseDown=0;
						break;
					}
					mouseDown=0;
					mouse_pos=calloc(1,sizeof(struct position));
					SDL_GetMouseState(&mouse_pos->x,&mouse_pos->y);
					printf("Cusor-Position x: %d y: %d\n",mouse_pos->x,mouse_pos->y);
					if(mouse_pos->x > 24 && 226 > mouse_pos->x && mouse_pos->y > 24 && 51 >  mouse_pos->y) {
						aStar(data,&(data->home));
						runPath=1;
						break;
					} else if(mouse_pos->x > 574 && 776 > mouse_pos->x && mouse_pos->y > 24 && 51 > mouse_pos->y) {
						aStar(data,&(data->stash));
						runPath=1;
						break;
					} else {
						mouse_pos=pixelToGrid(mouse_pos);
						if(DEBUG)printf("Cusor-Feld x: %d y: %d\n",mouse_pos->x,mouse_pos->y);
						if(lastmouse==NULL) {
							if((lastmouse=calloc(1,sizeof(position))) == NULL) {
								printf("Kein Speicherplatz vorhanden fuer position\n");
								return -1;
							}
						}
						memcpy(lastmouse,mouse_pos,sizeof(position));
						free(mouse_pos);
						mouse_pos=NULL;
						motionPath=1;
						runPath=1;
						break;
					}
					
				case SDL_KEYDOWN:
					switch( event.key.keysym.sym ) {
						case 	SDLK_m:
							
							if( Mix_PlayingMusic() == 0 )  
								Mix_PlayMusic( data->ingamemusic, -1);
							
							if( Mix_PausedMusic() == 1 )
								Mix_ResumeMusic(); 
							else Mix_PauseMusic(); 
							
							break;
						case SDLK_s:
							if (data->soundEnabled) {
								Mix_HaltChannel(-1);
							}
							data->soundEnabled=!data->soundEnabled;
							
							break;

						case SDLK_0:
							printf ("Music off\n");
							Mix_HaltMusic();
							Mix_HaltChannel(-1);
							data->soundEnabled=0;
							
							break;
							
							
						case SDLK_ESCAPE:
							done = ingameMenuStart(screen, data);
							if (!done)
								GraphicUpdate(screen, data);
							break;
						case SDLK_q:
							done = 1;
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
			innerStopTime = SDL_GetTicks();
			diffTime=(innerStopTime-innerStartTime);
			if (MS_FRAMETIME>diffTime)SDL_Delay(MS_FRAMETIME-diffTime);
		}
		mT=SDL_GetTicks();
		if((mouseTime + mousetimewait)< mT && lastmouse!=NULL && ownpath==0 &&runPath==0) {
			motionPath=1;
		}
		if((motionPath || runPath) && !done) {
			if(motionPath) {
				lastmouse->y+=data->verticalScroll;
				lastmouse->x+=data->horizontalScroll;
				aStar(data,lastmouse);
				if(DEBUG)printf("Player-Feld x: %d y: %d\n",data->player.p_pos.x,data->player.p_pos.y);
				GraphicUpdate(screen, data);
				free(lastmouse);
				lastmouse=NULL;
				motionPath=0;
				drawPath=0;
			}
			if(runPath) {
				i=1;
				tmp=data->player.anfang;
				/*sound*/
				if(data->soundEnabled&&data->player.cutSpeed>=2)
					Mix_PlayChannel(CHAINSAWCHANNEL1, data->chainpause, -1);
				while(tmp!=NULL&&i) {
					//Schleife um die Laufbewegung Abzubrechen
					while ( SDL_PollEvent(&event) ) {
						switch (event.type) {
							case SDL_MOUSEBUTTONUP:
								i=0;
								break;
							case SDL_KEYDOWN:
								switch( event.key.keysym.sym ) {
										
										
									case 	SDLK_m:
										printf ("Music on /Pause\n");				
										if( Mix_PlayingMusic() == 0 )  
											Mix_PlayMusic( data->ingamemusic, -1);			
										if( Mix_PausedMusic() == 1 )
											Mix_ResumeMusic(); 
										else Mix_PauseMusic();						
										break;
									case SDLK_s:
										if (data->soundEnabled) {
											Mix_HaltChannel(-1);
										}
										data->soundEnabled=!data->soundEnabled;

										break;
									case SDLK_0:
										printf ("Music off\n");
										Mix_HaltMusic();
										Mix_HaltChannel(-1);
										data->soundEnabled=0;
										break;
									case SDLK_ESCAPE:
										i=0;
										break;
									default:
										break;
								}
							default:
								break;
						}
					}
					tmp=positionListRead(data);
					if(tmp!=NULL) {
						if(DEBUG)printf("Position Stack x: %d y: %d\n",tmp->x,tmp->y);						
						if(headPositionUpdate(data,tmp,screen)) {
							aVal=data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].aStarValue;
							if(aVal>0) {
								data->player.currentEnergy-=aVal;
								if(data->player.currentEnergy<0) {
									printf("YOU ARE DEAD\nNEW GAME\n");
									done=1;
									i=0;
								} else {
									data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].type=6;
									data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].aStarValue=2;
									data->player.bp.currentVolume+=data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].currency;
									if(data->player.bp.currentVolume > data->player.bp.maxVolume) data->player.bp.currentVolume=data->player.bp.maxVolume;
									data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].currency=0;
								}
							}
							if(!done)GraphicUpdate(screen,data);
						} else positionListDelete(data);
					}
				}
				/*sound off*/
				Mix_HaltChannel(CHAINSAWCHANNEL1);

				runPath=0;
				ownpath=0;
			}
			positionListDelete(data);
		}
		stopTime = SDL_GetTicks();
		diffTime = (stopTime-startTime);
		if (MS_FRAMETIME>diffTime)SDL_Delay(MS_FRAMETIME-diffTime);
	}

	addHighscore(screen,data,calcHighscore(data));	
	return 0;
	
	
}
Exemple #4
0
int main(int argc, char* argv[])
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_Window *window = SDL_CreateWindow("Bloxz", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 480, 480.f/(320.f/480.f), SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
	SDL_GL_CreateContext(window);
	bool done = 0;
	init();
	
	int start = SDL_GetTicks();
	while (!done)
	{
		SDL_Event e;
		while (SDL_PollEvent(&e))
		{
			switch (e.type)
			{
			case SDL_QUIT:
				done = 1;
				break;
			case SDL_KEYDOWN:
				if (e.key.keysym.sym == SDLK_ESCAPE)
					done = 1;
				else if (menu == 2)
				{
					if (e.key.keysym.sym == SDLK_BACKSPACE)
					{
						name[--nameat] = 0;
					}
					if (e.key.keysym.sym == SDLK_RETURN)
					{
						menu = 1;
						playername = new char(strlen(name) + 2);
						strcpy(playername, name);
						addHighscore();
						newlevel();
					}
					else if (e.key.keysym.sym < 128 && e.key.keysym.sym>=32)
					{
						name[nameat++] = e.key.keysym.sym;
						name[nameat] = 0;
					}
				}
				break;
			case SDL_MOUSEBUTTONDOWN:
			{
				if (firing) break;;
				vec2 point(e.button.x, e.button.y);
				if (point.x<32 || point.x>320 - 32)
				{
					tx = -1;
					ty = (point.y - 30) / 32;
				}
				else if (point.y<62 || (point.y>316 && point.y < 390))
				{
					ty = -1;
					tx = (point.x) / 32;
				}
				else
				{
					tx = ty = -1;
				}
				if (tx == 0 || tx == 9)
					tx = -1;
				if (ty == 0 || ty == 9)
					ty = -1;
				tim = 0;
			}
				break;
			case SDL_MOUSEMOTION:
				if (e.motion.state)
				{
					if (firing) break;
					vec2 point(e.motion.x, e.motion.y);
					if (point.x<32 || point.x>320 - 32)
					{
						tx = -1;
						ty = (point.y - 30) / 32;
					}
					else if (point.y<62 || (point.y>316 && point.y < 390))
					{
						ty = -1;
						tx = (point.x) / 32;
					}
					else
					{
						tx = ty = -1;
					}
					if (tx == 0 || tx == 9)
						tx = -1;
					if (ty == 0 || ty == 9)
						ty = -1;
				}
				break;
			case SDL_MOUSEBUTTONUP:
			{
				if (firing) break;
				vec2 point(e.button.x, e.button.y);
				if (menu == 0)
				{
					if (point.x<32 && point.y>62 && point.y<316)
						fire(3, (point.y - 30) / 32, point.x);
					else if (point.x>32 * 9 && point.y > 62 && point.y<316)
						fire(1, (point.y - 30) / 32, point.x);
					if (point.y<62 && point.x>32 && point.x<316)
						fire(0, point.x / 32, point.y);
					else if (point.y>316 && point.x>32 && point.x < 32 * 9 && point.y<390)
						fire(2, point.x / 32, point.y);
					if (point.x<26 && point.y>454)
					{
						menu = 1;
					}
				}
				else if (menu == 1)
				{
					if (point.x>160 && gamemode != 0)
						menu = 0;
					else
					{
						if (point.y<330 && point.y>160)
						{
							menu = 0;
							if (point.y < 225)
								gamemode = 1;
							else if (point.y<280)
								gamemode = 2;
							else
								gamemode = 3;
							newlevel();
						}
						if (point.y>360 && point.y<405)
							menu = 3;
						if (point.y>415 && point.y<465)
						{
							menu = 5;
							hst = 0;
						}
					}
				}
				else if (menu == 2)
				{
					if (point.y>320)
					{
//						if (textbox == nil)
						menu = 1;
						playername = new char(strlen(name) + 2);
						strcpy(playername, name);
						addHighscore();
						newlevel();
					}
				}
				else if (menu == 3)
				{
					if (point.x > 16 && point.x<48 && point.y>445 && point.y < 477)
					{
						hst--;
						if (hst<0)
							hst = 2;
					}
					else if (point.x>272 && point.x<304 && point.y>445 && point.y<477)
					{
						hst++;
						if (hst>2)
							hst = 0;
					}
					else if (point.x > 128 && point.x<192 && point.y>445 && point.y < 477)
					{
						menu = 1;
					}
				}
				else if (menu == 4)//gameover
				{
					name[0] = 0;
					strcpy(name, playername);
					nameat = strlen(name);
					menu = 2;
				}
				else if (menu == 5)//high
				{
					if (point.x<26 && point.y>454)
					{
						menu = 1;
					}
					else
					{
						hst++;
						if (hst > 7)
							menu = 1;
					}
				}
				printf("%f,%f\n", point.x, point.y);
				tx = ty = -1;
			}
				break;
			}
		}
		update();
		SDL_GL_SwapWindow(window);
		int elapsed = (SDL_GetTicks() - start);
		SDL_Delay(max(0, 17 - elapsed));
		start = SDL_GetTicks();
	}
	saveHighscore();
	SDL_Quit();
	return 0;
}