-2

I am trying to get a text to say "Game Over" for 5 seconds once a car reaches the finish line.

import pygame, random
from time import sleep

pygame.init()
# music/sounds
CarSound = pygame.mixer.Sound("image/CAR+Peels+Out.wav")
CarSound_two = pygame.mixer.Sound("image/racing01.wav")
CarSound_three = pygame.mixer.Sound("image/RACECAR.wav")
CarSound_four = pygame.mixer.Sound("image/formula+1.wav")
music = pygame.mixer.music.load("image/Led Zeppelin - Rock And Roll (Alternate Mix) (Official Music Video).mp3")
pygame.mixer.music.play(-1)
bg = pygame.image.load('image/Crowds.png')

#Setting up our colors that we are going to use
GREEN = (20, 255, 140)
GREY = (210, 210, 210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
BLACKWHITE =(96, 96, 96)

SCREENWIDTH = 400
SCREENHEIGHT = 500

size = (SCREENWIDTH, SCREENHEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
Icon = pygame.image.load("image/redca_iconr.png")
pygame.display.set_icon((Icon))
# This will be a list that will contain all the sprites we intend to use in our game.
#all_sprites_list = pygame.sprite.Group()

#player
playerIMG = pygame.image.load("image/red_racecar.png")
playerX = 250
playerY = 450
playerCar_position = 0

#player2
playerIMG_two = pygame.image.load("image/greencar.png")
playerX_two = 150
playerY_two = 450
playerCar_position_two = 0

#player3
playerIMG_three = pygame.image.load("image/Orangecar.png")
playerX_three = 50
playerY_three = 450
playerCar_position_three = 0

#player4
playerIMG_four = pygame.image.load("image/yellow_car.png")
playerX_four = 200
playerY_four = 450
playerCar_position_four = 0

#Putting cars to the screen
def player(x, y):
    screen.blit(playerIMG, (x, y))

def player_two(x, y):
    screen.blit(playerIMG_two, (x, y))

def player_three(x, y):
    screen.blit(playerIMG_three, (x, y))

def player_four(x, y):
    screen.blit(playerIMG_four, (x, y))

finish_text = ""
font2 = pygame.font.SysFont("Papyrus", 65)
players_finished = 0
placings = ["1st", "2nd", "3rd", "4th"]

def text_objects(text, font):
    textSurface = font.render(text, True, RED)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largText =pygame.font.Font("Mulish-Regular.ttf", 15)
    TextSurf, TextRect = text_objects(text, largText)
    TextRect.center = ((SCREENWIDTH / 1), (SCREENHEIGHT / 1))
    screen.blit(TextSurf, TextRect)

screen.blit(bg, (0, 0))

pygame.display.flip()

**Here is the function on where am trying to show the text temporary on the screen**

def Game_over():
    if (players_finished):
        clock.tick(1)
        pygame.time.delay(5000)
        font = pygame.font.SysFont("Impact", 25)
        text = font.render("Game over!", 4, (0, 66, 37))
        screen.blit(text, (185 - (text.get_width() / 2), 120))
        pygame.display.flip()

# Main game loop
run = True
clock = pygame.time.Clock()
#TIP - lots of our actions take place in our while loop cause we want the function/program to run consistently
while run:
    # Drawing on Screen
    screen.fill(GREEN)
    # Draw The Road
    pygame.draw.rect(screen, GREY, [40, 0, 300, 500])
    # Draw Line painting on the road
    pygame.draw.line(screen, WHITE, [185, 0], [185, 500], 5)
    #Finish line
    pygame.draw.rect(screen, BLACKWHITE, [50, 50, 280, 40])
    pygame.draw.line(screen, WHITE, [50, 70], [330, 70], 5)
    font = pygame.font.SysFont("Impact", 35)
    text = font.render("Finish line!", 4, (150, 50, 25))
    screen.blit(text, (180 - (text.get_width() / 2), -8))


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            # Number of frames per secong e.g. 60
            clock.tick(60)

        keys = pygame.key.get_pressed()

        if keys[pygame.K_1]:
            CarSound.play()
            playerCar_position = -0.1
        if keys[pygame.K_q]:
            playerCar_position = 0.2
        if keys[pygame.K_2]:
            CarSound_two.play()
            playerCar_position_two = -0.1
        if keys[pygame.K_w]:
            playerCar_position_two = 0.2
        if keys[pygame.K_3]:
            CarSound_three.play()
            playerCar_position_three = -0.1
        if keys[pygame.K_e]:
            playerCar_position_three = 0.2
        if keys[pygame.K_4]:
            CarSound_four.play()
            playerCar_position_four = -0.1
        if keys[pygame.K_r]:
            playerCar_position_four = 0.2

        # our functions
    playerY += playerCar_position
    playerY_two += playerCar_position_two
    playerY_three += playerCar_position_three
    playerY_four += playerCar_position_four

    player(playerX, playerY)
    player_two(playerX_two, playerY_two)
    player_three(playerX_three, playerY_three)
    player_four(playerX_four, playerY_four)

    finish_line_rect = pygame.Rect(50, 70, 235, 32)
    # Did anyone cross the line?
    if (finish_line_rect.collidepoint(playerX, playerY)):
        if finish_text[:8] != "Player 1":  # so it doesnt do this every frame the car is intersecting
         finish_text = "Player 1 is " + placings[players_finished]
         players_finished += 1
         print("Player (one) has crossed into finish line!")


    elif (finish_line_rect.collidepoint(playerX_two, playerY_two)):
        if finish_text[:8] != "Player 2":
            print("Player one has crossed into finish line first other car lost!")
            finish_text = "Player 2 is " + placings[players_finished]
            players_finished += 1


    elif (finish_line_rect.collidepoint(playerX_three, playerY_three)):
        if finish_text[:8] != "Player 3":
            print("Player two has crossed into finish line first other car lost!")
            finish_text = "Player 3 is " + placings[players_finished]
            players_finished += 1

    elif (finish_line_rect.collidepoint(playerX_four, playerY_four)):
         if finish_text[:8] != "Player 4":
          print("Player two has crossed into finish line first other car lost!")
          finish_text = "Player 4 is " + placings[players_finished]
          players_finished += 1

    if (players_finished and finish_text):
        font = pygame.font.SysFont("Impact", 25)
        text = font.render(finish_text, 4, (0, 66, 37))
        screen.blit(text, (185 - (text.get_width() / 2), 90))
        Game_over()
        pygame.display.update()
    #print("Player two has crossed into finish line first other car lost!")
            #finish_text = "Player 4 is " + placings[players_finished]
    pygame.display.flip()
pygame.quit()
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Luis Perez
  • 13
  • 4
  • Welcome to stack! Have you tried this? https://stackoverflow.com/questions/56502113/how-to-show-text-for-5-seconds-then-disappear-and-display-buttons – Surya Narayanan Aug 03 '20 at 18:42
  • What exactly is the problem with the code you have shown? Please create a [mre]. – mkrieger1 Aug 03 '20 at 18:47
  • There is no error just trying to see how I could get my text "Game Over" to appear on the screen for 5 secs once cars reach the finish line just kinda stuck at the moment @mkrieger1 – Luis Perez Aug 03 '20 at 18:49

1 Answers1

0

add a variable at the start of your code with the name tick_var and if you want a fixed tickrate remove it from the parameter of the function if not change your code and att the tickrate as a paramter of the function when used.

def Game_over(tickrate):
    global tick_var
    tick_var +=1
    if(tick_var>5*tickrate):
        if (players_finished):
            clock.tick(tickrate)
            font = pygame.font.SysFont("Impact", 25)
            text = font.render("Game over!", 4, (0, 66, 37))
            screen.blit(text, (185 - (text.get_width() / 2), 120))
            pygame.display.flip()
Anass ABEA
  • 479
  • 5
  • 14
  • So when I try this the screen ends up stopping once a car reaches the finish line the music still keeps on playing though. @AnassAbea – Luis Perez Aug 03 '20 at 18:56
  • if you want to stop the music add this command `pygame.mixer.music.stop()` – Anass ABEA Aug 03 '20 at 18:57
  • No, it's good that the music still playing I was just letting you know that the code I tried wasn't working but the music still was sorry for the confusion. – Luis Perez Aug 03 '20 at 19:01
  • @LuisPerez so you mean this code doesn't display the message ? or doesn't meet your requirements? please explain more what you need to be done – Anass ABEA Aug 03 '20 at 19:14
  • So it's not what am looking for when I try your code and run it the cars start off normal but once they reach the finish line the whole screen freezes and the only thing working is just the music playing in the background what I want is to display the "Game Over" for 5 seconds once a car crosses the finish line @Anass ABEA – Luis Perez Aug 03 '20 at 19:27