We will implement a very simple scoring system that simply gives the player 10 points each time they clear a row. We will later be saving this score and displaying the best score for the player as well. This means we need to define score = 0 and last_score = max_score() at the top of our main() function. Then we need to make a slight modification to our clear_rows() function so that it returns how many rows were cleared. Then when we are calling clear_rows() from within the main() function we will simply do score += clear_rows() * 10 so that it adds the returned value to our score.
score=0last_score=max_score()# will will create this function later
defclear_rows(gird,locked):...returninc# add this to the bottom of the function
We are going to be displaying the current and best score from within the draw_window() function. This means we must somehow pass the scores there. To do this we will add two parameters to draw_window() and we will add two arguments to our call statement.
The draw_window() function should now look like this.
defdraw_window(surface,grid,score=0,last_score=0):surface.fill((0,0,0))pygame.font.init()font=pygame.font.SysFont('comicsans',60)label=font.render('Tetris',1,(255,255,255))surface.blit(label,(top_left_x+play_width/2-(label.get_width()/2),30))# current scorefont=pygame.font.SysFont('comicsans',30)label=font.render('Score: '+str(score),1,(255,255,255))sx=top_left_x+play_width+50sy=top_left_y+play_height/2-100surface.blit(label,(sx+20,sy+160))# last scorelabel=font.render('High Score: '+last_score,1,(255,255,255))sx=top_left_x-200sy=top_left_y+200surface.blit(label,(sx+20,sy+160))foriinrange(len(grid)):forjinrange(len(grid[i])):pygame.draw.rect(surface,grid[i][j],(top_left_x+j*block_size,top_left_y+i*block_size,block_size,block_size),0)pygame.draw.rect(surface,(255,0,0),(top_left_x,top_left_y,play_width,play_height),5)draw_grid(surface,grid)
Now when we call draw_window() we must add the two new arguments.
draw_window(win,grid,score,last_score)# Modify this from within the main() function so that# it has score and last_score as arguments
This function is going to aid us in displaying text to the middle of the screen and will used for when we create a main menu and end screen for our game.
Right now when we loose the game we simply quit the program. However, we want to first tell the user they lost and then prompt them to play again. Inside our main() function inside the while loop we will add some lines into the block of code below.
defmain():whilerun:...ifcheck_lost(locked_positions):draw_text_middle(win,"YOU LOST!",80,(255,255,255))pygame.display.update()pygame.time.delay(1500)run=Falseupdate_score(score)# We will create this function later
Creating a Main Menu
We are now going to create a main menu so that when the game is first launched the user can press a button to start the game. This main menu will also show up when the user looses the game allowing them to play again.
defmain_menu(win):# *run=Truewhilerun:win.fill((0,0,0))draw_text_middle(win,'Press Any Key To Play',60,(255,255,255))pygame.display.update()foreventinpygame.event.get():ifevent.type==pygame.QUIT:run=Falseifevent.type==pygame.KEYDOWN:main(win)pygame.display.quit()
Now instead of calling main() to start the game we will call main_menu().
To save the users best score we are going to update a text file each time the game is played. We will do this using two functions update_file() and max_score.
*IMPORTANT* Please create a new text file called scores.txt and type “0” on the first line. CLICK SAVE OR SAVE AS. If you do not do this the program will crash.