Search code examples
pythonpygamepycharm2d-games

How to get the second car to say second place?


What am trying to is

So what I’m basically trying to do is every time a car crosses the finish It says which car has one but what I’m trying to do is every time the first car wins and a second car crosses the finish line I would like it to say which car came in second place in and in third place and so on

import pygame, random

pygame.init()
# music
music = pygame.mixer.music.load(“image/Led Zeppelin - Rock And Roll (Alternate Mix) (Official Music Video).mp3”)
pygame.mixer.music.play(-1)

#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))



# 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!", 1, (150, 50, 25))
    screen.blit(text, (195 - (text.get_width() / 2), 15))



    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]:
            playerCar_position = -0.1
        if keys[pygame.K_q]:
            playerCar_position = 0.1
        if keys[pygame.K_2]:
            playerCar_position_two = -0.1
        if keys[pygame.K_w]:
            playerCar_position_two = 0.1
        if keys[pygame.K_3]:
            playerCar_position_three = -0.1
        if keys[pygame.K_e]:
            playerCar_position_three = 0.1
        if keys[pygame.K_4]:
            playerCar_position_four = -0.1
        if keys[pygame.K_r]:
            playerCar_position_four = 0.1

        # 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)

** here is where i been trying to get the code to work the way I want it to but I haven’t had any luck yet please help.**

    # Did anyone cross the line?
    if (finish_line_rect.collidepoint(playerX, playerY)):
        font2 = pygame.font.SysFont("Papyrus", 45)
        text2 = font2.render("Red Car Won!!", 5, (255, 99, 7))
        screen.blit(text2, (135 - (text2.get_width() / 5), 95))
        pygame.display.flip()
        print("Player (one) has crossed into finish line!")


    elif (finish_line_rect.collidepoint(playerX_two , playerY_two)):
        print("Player one has crossed into finish line first other car lost!")
        font2 = pygame.font.SysFont("Papyrus", 45)
        text2 = font2.render("Green Car Lost!!", 5,  (34, 139, 34))
        screen.blit(text2, (135 - (text2.get_width() / 5), 95))
        pygame.display.flip()

    elif (finish_line_rect.collidepoint(playerX , playerY)):
        print("Player two has crossed into finish line first other car lost!")
        font2 = pygame.font.SysFont("Papyrus", 45)
        text2 = font2.render("Red Car Lost!!", 5, (255, 99, 7))
        screen.blit(text2, (135 - (text2.get_width() / 5), 95))
        pygame.display.flip()

    elif (finish_line_rect.collidepoint(playerX_two, playerY_two)):
        print("Player two has crossed into finish line first other car lost!")
        font2 = pygame.font.SysFont("Papyrus", 45)
        text2 = font2.render("Green Car Won!!", 5, (255, 99, 7))
        screen.blit(text2, (135 - (text2.get_width() / 5), 95))
        pygame.display.flip()


    # Refresh Screen
    pygame.display.flip()








pygame.quit()

Solution

  • So, the code you have got works, the only problem is you are only blitting the text on the screen when the car is intersecting with the finish line

    So have a string that gets put on the screen every frame and when a car crosses the line, change the string to that car

    #put these outside the game loop
    finish_text = ""
    font2 = pygame.font.SysFont("Papyrus", 45)
    players_finished = 0
    placings = ["1st", "2nd", "3rd", "4th"]
    
    
    while running: #game loop
    
    ...
    
         # Did anyone cross the line?
        if (finish_line_rect.collidepoint(playerX, playerY)):
            if finish_text[:9] != "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[:9] != "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 , playerY)):
            if finish_text[:9] != "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_two, playerY_two)):
            if finish_text[:9] != "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 finish_text != "": #test to see if there is anything to put on screen
            text2 = font2.render(finish_text, 5, (255, 99, 7))
            screen.blit(text2, (135 - (text2.get_width() / 5), 95))
        pygame.display.flip()
    
    
    

    Note. haven't tested it