如何让第二辆车说第二名?

我想做的是


所以我基本上想做的是每次一辆车越过终点 它说哪辆车有一个但我想做的是每次第一辆车获胜并且第二辆车越过终点线我想要它说哪辆车排在第二位和第三位等等


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


弑天下
浏览 71回答 2
2回答

哈士奇WWW

所以,你得到的代码有效,唯一的问题是当汽车与终点线相交时,你只在屏幕上显示文本因此,每帧都有一个字符串放在屏幕上,当汽车越过这条线时,将字符串更改为那辆车#put these outside the game loopfinish_text = ""font2 = pygame.font.SysFont("Papyrus", 45)players_finished = 0placings = ["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()笔记。还没有测试过

UYOU

我从未使用过 pygame,所以不知道是否有内置函数。但是,您可以轻松地制作一个计数器。#this is all in your while loopif playerY<50 or playerY_two<50 or playerY_three<50 or playerY_four<50:&nbsp; &nbsp; &nbsp; &nbsp; count+=1&nbsp; &nbsp; &nbsp; &nbsp; if count == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY first place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY=10000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_two<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_two first place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_two=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_three<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_three first place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_three=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_four<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_four first place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_four=1000000&nbsp; &nbsp; &nbsp; &nbsp; if count == 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY second place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY=10000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_two<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_two seoncd place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_two=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_three<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_three second place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_three=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_four<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_four second place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_four=1000000&nbsp; &nbsp; &nbsp; &nbsp; if count == 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY third place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY=10000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_two<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_two third place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_two=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_three<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_three third place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_three=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_four<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_four third place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_four=1000000&nbsp; &nbsp; &nbsp; &nbsp; if count == 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY 4th place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY=10000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_two<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_two 4th place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_two=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_three<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_three 4th place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_three=100000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerY_four<50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('playerY_four 4th place')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerY_four=1000000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp;你可以创建一个类/函数来将上面的全部内容写成 4 行到 5 行,但一般的想法是你想打印出谁越过终点线,但你只希望打印一次。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python