Part 16: Game over screen

16_gameoverscreen

After all the cannons have disappeared, need to display a game over screen.
How do we switch to a different screen when the game is over? Very simple! We draw a black/pink screen and draw text on it?


Post the pygame.display.update(), type in the code to show the game over screen.

GAMEOVER = "Game Over"
    FONTNAME = 'freesansbold.ttf'
    GAMEOVERTXTCOLOR = (150, 150, 150)
    CENTERSCREENPOS = (250, 250)
    if(len(wall_group.sprites()) == 0):
        print("game over")
        SCREEN.fill(BLACK)
        font = pygame.font.Font(FONTNAME, 30)
        text_surface = font.render(GAMEOVER, True, GAMEOVERTXTCOLOR)
        text_rect = text_surface.get_rect()
        text_rect.center = CENTERSCREENPOS
        SCREEN.blit(text_surface, text_rect)
        pygame.display.update()
        pygame.time.wait(5000)
        pygame.quit()
        sys.exit()

To write a text on to an empty SCREEN, we follow the steps below:

  • Fill the SCREEN black
  • Create a font object.
    font = pygame.font.Font(FONTNAME, 30)
  • Create a surface for the text. Pass the text to display and its colour. The second argument is the “antialias”, if “True”, the characters will have smooth edges.
    text_surface = font.render(GAMEOVER, True, GAMEOVERTXTCOLOR)
  • Get the rectangle from the surface and center it in the middle of the SCREEN.
    text_rect = text_surface.get_rect()
            text_rect.center = CENTERSCREENPOS
  • Blit it on the SCREEN by passing the surface and the rectangle.
    SCREEN.blit(text_surface, text_rect)
  • Update the display, wait for 5 seconds (5000ms), quit pygame and exit the SCREEN.
    pygame.display.update()
            pygame.time.wait(5000)
            pygame.quit()
            sys.exit()

Entire code:

#Import statements are to enable the code to use the functions from the library
import pygame
import sys
import os
import random


#instructions to windows to center the game window in the center of
#the screen, which it might ignore
os.environ["SDL_VIDEO_CENTERED"] = "1"

#initialize pygame
pygame.init()

#Right way
SCREENWIDTH = 500
SCREENHEIGHT = 500
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = pygame.display.set_mode(SCREENSIZE)

#caption for the game
pygame.display.set_caption("My first game in pygame")

ANGLE = 90
BLACK = (0,0,0)

#The rocket - the actor/sprite that moves with arrows/WASD
class Rocket(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        #Horizontal
        #self.vertical = 1
    def draw_updatescreen(self):
        #Do it once
        #SCREEN.fill(BLACK)
        blitrocketrect = SCREEN.blit(self.image, (self.rect.x, self.rect.y))
        #pygame.display.update()
    def rotate(self, angle):
        self.image = pygame.transform.rotate(self.image, angle)
    def update(self, x, y):
        self.rect.x =  x
        self.rect.y =  y

#This group has only one sprite
X = 40
Y = 40
SPRITENAME = 'rocket.png'
rocket = Rocket(SPRITENAME, X, Y)

#uncomment - for drawing with sprite group
rocket_group = pygame.sprite.Group()
rocket_group.add(rocket)

#Do it once
#rocket.draw_updatescreen()
#rocket_group.draw(SCREEN)


ZEROINTENSITY = 0
MAXINTENSITY = 255
RADIUS = 20

#Getting the surface of the drawn circle and finding collisions is difficult
#hence will have to change it to an image
class Wall(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        pygame.sprite.Sprite.__init__(self)
        #Not required - tried for collision
        #screensurf = pygame.Surface([SCREENWIDTH, SCREENHEIGHT])

        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
                

NOOFWALLS = 4
XMIN = 50
XMAX = SCREENWIDTH-100
YMIN = 50
YMAX = SCREENHEIGHT-100

WALLNAME = 'football.png'

wall_group=pygame.sprite.Group()
for i in range(0, NOOFWALLS):
    wall = Wall(WALLNAME, random.randrange(XMIN, XMAX, 40), random.randrange(YMIN,YMAX, 20))
    wall_group.add(wall)

#Do it once
#pygame.display.update()
#wall_group.draw(SCREEN)

while True:
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if (events.type == pygame.MOUSEBUTTONDOWN):
            rocket.rotate(ANGLE)
            #Method 1
            #rocket.draw_updatescreen()
            #Method 2 - commented out
            rocket_group.draw(SCREEN)
            
    #Inside while, a single tab space from while
    user_input = pygame.key.get_pressed()
    if(user_input[pygame.K_UP]):
        Y=Y-1
        if(YSCREENHEIGHT):Y=0
    elif(user_input[pygame.K_LEFT]):
        X=X-1
        if(XSCREENWIDTH):X=0

    SCREEN.fill(BLACK)
    wall_group.draw(SCREEN)
    
    rocket.update(X,Y)
    rocket.draw_updatescreen()
    #Method 2
    #rocket_group.draw(SCREEN)
    
    #sprite collide is for sprites in a group
    for circle in wall_group:
       if(rocket.rect.colliderect(circle.rect)):
            print("collision  :  " + str(id(circle)))
            wall_group.remove(circle)
            wall_group.draw(SCREEN)
            
    pygame.display.update()

    GAMEOVER = "Game Over"
    FONTNAME = 'freesansbold.ttf'
    GAMEOVERTXTCOLOR = (150, 150, 150)
    CENTERSCREENPOS = (250, 250)
    if(len(wall_group.sprites()) == 0):
        print("game over")
        SCREEN.fill(BLACK)
        font = pygame.font.Font(FONTNAME, 30)
        text_surface = font.render(GAMEOVER, True, GAMEOVERTXTCOLOR)
        text_rect = text_surface.get_rect()
        text_rect.center = CENTERSCREENPOS
        SCREEN.blit(text_surface, text_rect)
        pygame.display.update()
        pygame.time.wait(5000)
        pygame.quit()
        sys.exit()

 

Next challenge: Save the score in a text file on the hard disk.
Let’s go!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s