如何实施终点线?
How to Implement finish line?
我想做的是,一旦一辆车冲过终点线,我希望它说出哪辆车赢得了比赛,但我如何才能将其添加到我的游戏中?代码工作正常,只是想看看如何向它添加更多功能,例如信号或某种类型的通知,说明哪辆车先通过了终点线。
import pygame
pygame.init()
#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, [65, 70], [300, 70], 5)
font = pygame.font.SysFont("Papyrus", 45)
text = font.render("Finish line!", 1, (150, 50, 25))
screen.blit(text, (195 - (text.get_width() / 2), 15))
**Here is where the finish line code is at just want too add some type of notafication saying which car has crossed the finsh line first!**
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
# our functions
playerY += playerCar_position
playerY_two += playerCar_position_two
playerY_three += playerCar_position_three
player(playerX, playerY)
player_two(playerX_two, playerY_two)
player_three(playerX_three, playerY_three)
player_four(playerX_four, playerY_four)
# Refresh Screen
pygame.display.flip()
pygame.quit()
想象一下终点线实际上是一个矩形。鉴于终点线从 [65, 70]
到 [300, 70]
,这为我们提供了一个位于 (65,70) 的 235 像素宽的矩形。为了让事情更安全,我们可以使用更宽的矩形作为线条,以防汽车在更新中移动许多像素......只是这样它就不会在没有进入它的情况下“跳跃”线。
finish_line_rect = pygame.Rect( 65,70, 235,32 ) # but 32 pixels wide
然后每辆车移动的时候,可以用pygame.Rect.collidepoint()
和每辆车的x,y
看是否和finish_line_rect
重合。
例如:
# Move the Players' cars
playerY += playerCar_position
playerY_two += playerCar_position_two
playerY_three += playerCar_position_three
# Draw the Players' cars
player(playerX, playerY)
player_two(playerX_two, playerY_two)
player_three(playerX_three, playerY_three)
player_four(playerX_four, playerY_four)
# Did anyone cross the line?
if ( finish_line_rect.collidepoint( playerX, playerY ) ):
print( "Player (one) has crossed into finish rectangle" )
if ( finish_line_rect.collidepoint( playerX_two, playerY_two ) ):
print( "Player two has crossed into finish rectangle" )
# ETC.
首先,我认为您应该创建一个“汽车”class,以免重复太多代码并使其更易于使用。关于终点线,我会做以下事情:
#Create an array with all cars X value
CarPositions = [Car1_x, Car2_x, Car3_x, Car4_x]
#Define X value of Finishline
Finishline = 600
def CheckPosition(CarPositions, Finishline):
for i in range(len(CarPositions)):
if CarPositions[i] >= Finishline:
return CarPositions[i]
您应该按数字顺序排列 X 位置数组,即数组中汽车 1 在汽车 2 之前。这样for循环中的i代表第几辆车通过了。
我想做的是,一旦一辆车冲过终点线,我希望它说出哪辆车赢得了比赛,但我如何才能将其添加到我的游戏中?代码工作正常,只是想看看如何向它添加更多功能,例如信号或某种类型的通知,说明哪辆车先通过了终点线。
import pygame
pygame.init()
#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, [65, 70], [300, 70], 5)
font = pygame.font.SysFont("Papyrus", 45)
text = font.render("Finish line!", 1, (150, 50, 25))
screen.blit(text, (195 - (text.get_width() / 2), 15))
**Here is where the finish line code is at just want too add some type of notafication saying which car has crossed the finsh line first!**
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
# our functions
playerY += playerCar_position
playerY_two += playerCar_position_two
playerY_three += playerCar_position_three
player(playerX, playerY)
player_two(playerX_two, playerY_two)
player_three(playerX_three, playerY_three)
player_four(playerX_four, playerY_four)
# Refresh Screen
pygame.display.flip()
pygame.quit()
想象一下终点线实际上是一个矩形。鉴于终点线从 [65, 70]
到 [300, 70]
,这为我们提供了一个位于 (65,70) 的 235 像素宽的矩形。为了让事情更安全,我们可以使用更宽的矩形作为线条,以防汽车在更新中移动许多像素......只是这样它就不会在没有进入它的情况下“跳跃”线。
finish_line_rect = pygame.Rect( 65,70, 235,32 ) # but 32 pixels wide
然后每辆车移动的时候,可以用pygame.Rect.collidepoint()
和每辆车的x,y
看是否和finish_line_rect
重合。
例如:
# Move the Players' cars
playerY += playerCar_position
playerY_two += playerCar_position_two
playerY_three += playerCar_position_three
# Draw the Players' cars
player(playerX, playerY)
player_two(playerX_two, playerY_two)
player_three(playerX_three, playerY_three)
player_four(playerX_four, playerY_four)
# Did anyone cross the line?
if ( finish_line_rect.collidepoint( playerX, playerY ) ):
print( "Player (one) has crossed into finish rectangle" )
if ( finish_line_rect.collidepoint( playerX_two, playerY_two ) ):
print( "Player two has crossed into finish rectangle" )
# ETC.
首先,我认为您应该创建一个“汽车”class,以免重复太多代码并使其更易于使用。关于终点线,我会做以下事情:
#Create an array with all cars X value
CarPositions = [Car1_x, Car2_x, Car3_x, Car4_x]
#Define X value of Finishline
Finishline = 600
def CheckPosition(CarPositions, Finishline):
for i in range(len(CarPositions)):
if CarPositions[i] >= Finishline:
return CarPositions[i]
您应该按数字顺序排列 X 位置数组,即数组中汽车 1 在汽车 2 之前。这样for循环中的i代表第几辆车通过了。