#include //fopen, fread, fclose, exit #include #include"justris.h" FILE *File; //Binary file for storing high scores void Check_Line(){ int Is_Line,z; //Flag to detect a line and a third counter for(y=GAMEBOARD_HIEGHT-1;y>=0;y--){ //Go through gameboard row by row Is_Line=1; //Default the line flag to true for(x=0;x0;z--) //Move all the lines above the complete line down one position for(x=0;x 7 return 1+random()%MAX_PIECES; //Randomly choose piece type between 1 and 7 } void Clear_GameBoard_Array(){ //This function clears off the gameboard for(x=0;xBlock[x][y]=0; //clear away blocks contents } void Collision_Detection(){ //Contains a little bit of the collision detection if((Piece.YPos+Piece.YSize)>GAMEBOARD_HIEGHT){ //If Piece hits the bottom, or goes through the bottom Piece.YPos=GAMEBOARD_HIEGHT-Piece.YSize; //Place place piece on the bottom Put_Block(); //Copy the piece array to the gameboard } for(x=0;x0) x--; //If the cursor is inside the textfield then decrease the cursor position Text_String[x]=' '; ch=0; }if(x>=17){ //If text exits textfield Text_String[x]=' '; x=17; //Put the cursor position inside the textfield }if(ch!=0){ play_sample((SAMPLE *)DataFile[KEYBOARD_SOUND].dat,255,128,1000,0); //Play the sound effect Text_String[x]=ch; //Make the string position equal the key enter x++; //increment the string position } Text_String[x+1]='\0'; textout(screen,Tech_Font,Text_String,1,40,96+(x*0.5)); //Display the string } //Place this highscore in the right order for(x=0;xHighScore[x].Points){ //If users score is greater than current highscore... for(y=MAX_HIGHSCORES-1;y>=x;y--){ //Move entries below it down one strcpy(HighScore[y].Name,HighScore[y-1].Name); HighScore[y].Points=HighScore[y-1].Points; } HighScore[x].Points=Stats.Points; //Put the users score in the current highscore position strcpy(HighScore[x].Name,Text_String); //Put the users name in the current highscore position break; } } } int Find_Piece_XSize(){ //This function returns the X-size of the piece for(x=MAX_PIECE_SIZE-1;x>=0;x--) for(y=0;y=0;y--) for(x=0;xHighScore[MAX_HIGHSCORES-1].Points){ //If the users points are greater than the lowest highscore... Enter_HighScore(); //Enter this user into the highscore table Draw_HighScores(); //Display the highscores } Stats.Level=1; //Starting level is 1 Stats.Lines=0; //Reset the varables to 0 Stats.Line_Ctr=0; Game_Timer=0; Clear_GameBoard_Array(); //Clear off the gameboard array //Make the next and current pieces Next_Piece.Kind=Choose_Random_Piece(); Piece.Kind=Next_Piece.Kind; Make_Piece(&Piece); Next_Piece.Kind=Choose_Random_Piece(); Make_Piece(&Next_Piece); } void Game_Stats(){ //This function takes care of all of the game stats such as current level, points and total lines if(Stats.Line_Ctr>=20){ //If the user gets 20 lines... Stats.Level++; //...Increase the level Stats.Line_Ctr=0; //and reset the line counter } Stats.Points=Stats.Lines*50; //make a line worth 50 points if(Stats.Level==10) Stats.Level=1; //When Stats.Level = ten, restart at Stats.Level one } void Init(){ allegro_init(); install_keyboard(); set_gfx_mode(GFX_AUTODETECT,640,480,0,0); //Set graphics mode to 640x480 detect_digi_driver(DIGI_AUTODETECT); install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL); //Install sound driver Virtual=create_bitmap(SCREEN_W,SCREEN_H); //Create virtual screen double buffer animation DataFile=load_datafile("justris.dat"); //Load the datafile containing all of the sounds, bitmaps, etc Tech_Font=(FONT *)DataFile[INTELL_FONT].dat; Palette=(PALLETE *)DataFile[GAME_PAL].dat; set_palette((RGB *)Palette); srandom(time(NULL)); //create a random seed File=fopen("hiscores.dat","rb"); //Open file for reading if(File){ //If the file exists, copy the saved highscores into the highscore structure fread(&HighScore,sizeof(struct HighScore_Struct),MAX_HIGHSCORES,File); //Read the file into the highscore structure fclose(File); //Close the file } else for(x=0;x<10;x++) //If the file doesn't exist, fill the highscores with some information HighScore[x].Points=(10-x)*1000; play_midi((MIDI *)DataFile[SONG_MIDI].dat,1); //Play the midi song in the background continuosly } void Input(){ //This function deals with the users input, and also does a bit of collision detection int CanMove=1; if((key[KEY_Q])||(key[KEY_ESC])) GameDone=1; //set the gamedone flag to 1 if user hits escape or Q if(key[KEY_R]) Game_Init(); //Restart the game if 'R' is pressed if(key[KEY_UP]) Rotate_Piece(); //If user hits the up arrow, rotate the piece clockwise if(key[KEY_H]) Draw_HighScores(); if(key[KEY_J]) //Move the gameboard left if(GAMEBOARD_XPOS>0) --GAMEBOARD_XPOS; if(key[KEY_L]) //Move the gameboard right if(GAMEBOARD_XPOS0) --GAMEBOARD_YPOS; if(key[KEY_K]) ///Move the gameboard down if(GAMEBOARD_YPOS=GAMEBOARD_WIDTH) CanMove=0; //Don't let the piece leave the boundries if(CanMove==1) Piece.XPos+=1; //If there isn't a block blocking the block, than move the block } if(key[KEY_LEFT]){ for(x=0;x=50-Stats.Level*5){ //If the timer is >= than it should be... Piece.YPos+=1; //...Than drop the piece, Game_Timer=0; //and reset the counter } clear_keybuf(); //Clearing the keyboard buffer seems to slow things down a bit, because then the user can't load the buffer with keys } void Make_Piece(struct Piece_Struct *Ptr){ //This function fills certain parts of the block structure with ones, making a piece Clear_Piece_Array(Ptr); switch(Ptr->Kind){ case 1: Ptr->Block[0][0]=Ptr->Block[0][1]=Ptr->Block[0][2]=Ptr->Block[0][3]=1;break; //Long shape case 2: Ptr->Block[0][0]=Ptr->Block[1][0]=Ptr->Block[0][1]=Ptr->Block[1][1]=1;break; //Box shape case 3: Ptr->Block[0][0]=Ptr->Block[0][1]=Ptr->Block[0][2]=Ptr->Block[1][2]=1;break; //L-Shape case 4: Ptr->Block[1][0]=Ptr->Block[1][1]=Ptr->Block[1][2]=Ptr->Block[0][2]=1;break; //Backwards L case 5: Ptr->Block[0][0]=Ptr->Block[1][0]=Ptr->Block[2][0]=Ptr->Block[1][1]=1;break; //T-shape case 6: Ptr->Block[1][0]=Ptr->Block[2][0]=Ptr->Block[0][1]=Ptr->Block[1][1]=1;break; //_/- shape case 7: Ptr->Block[0][0]=Ptr->Block[1][0]=Ptr->Block[1][1]=Ptr->Block[2][1]=1;break; //-\_ shape } Ptr->XSize=Find_Piece_XSize(); //Find the x size of the piece Ptr->YSize=Find_Piece_YSize(); //Find the y size of the piece Ptr->YPos=0; //Put the piece on the top part of the gameboard Ptr->XPos=4; //Place it kind of in the middle for(x=0;xGAMEBOARD_WIDTH)||(Piece.YPos+Piece.XSize>GAMEBOARD_HIEGHT)) return 0; //If rotation will put the piece out of bounds, exit the for(x=0;x