如何让汽车上赛道

How to get car on top of the racetrack

我正在开发一款自上而下的赛车游戏,目前我正试图让汽车驶上赛道。有2辆小车,一辆是电脑的小车(未完成),另一辆是用户控制的小车,但是当我运行程序中用户的小车在轨道后面,但仍然正常移动。我已经尝试了几种不同的方法,但用户的车似乎从来没有上过赛道。所以我主要是问怎么让用户的车上轨道。

谢谢

import math
import random
from tkinter import *

import pygame

class Buttons:
#window
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        self.quitButton = Button(frame, text="Race!", fg="red", command=frame.quit)
        self.quitButton.pack(side=LEFT)

root = Tk()
b = Buttons(root)
root.mainloop()

pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()
screen = pygame.display.set_mode((1280, 800))
rect = screen.get_rect()
clock = pygame.time.Clock()

#music
pygame.mixer.music.load("Noteblock.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)

WHITE = pygame.Color('white')
# Load images globally and reuse them in your program.
# Also use the `.convert()` or `.convert_alpha()` methods after
# loading the images to improve the performance.

VEHICLE1 = pygame.Surface((40, 70), pygame.SRCALPHA)
VEHICLE1.fill((130, 180, 20))
#blitting car onto 'rectangle car'
VEHICLE1 = pygame.image.load("YellowLambo.png")
screen.blit(VEHICLE1,(0,0))
pygame.display.update()

VEHICLE2 = pygame.Surface((40, 70), pygame.SRCALPHA)
VEHICLE2.fill((200, 120, 20))
#blitting computer's car
VEHICLE2 = pygame.image.load("RedLambo.png")
screen.blit(VEHICLE2,(0,0))
pygame.display.update()

BACKGROUND = pygame.Surface((1280, 800))
BACKGROUND.fill((127, 69, 2))
BACKGROUND = pygame.image.load("track1.png").convert()
screen.blit(BACKGROUND,(0,0))
pygame.display.update()




class Entity(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)


class VehicleSprite(Entity):
    MAX_FORWARD_SPEED = 10
    MAX_REVERSE_SPEED = 2
    ACCELERATION = 0.05
    TURN_SPEED = 0.000000000001


    def __init__(self, image, position):
        Entity.__init__(self)
        self.src_image = image
        self.image = image
        self.rect = self.image.get_rect(center=position)
        self.position = pygame.math.Vector2(position)
        self.velocity = pygame.math.Vector2(0, 0)
        self.speed = self.direction = 0
        self.k_left = self.k_right = self.k_down = self.k_up = 0

    def update(self, time):
        # SIMULATION
        self.speed += self.k_up + self.k_down
        # To clamp the speed.
        self.speed = max(-self.MAX_REVERSE_SPEED,
                         min(self.speed, self.MAX_FORWARD_SPEED))

        # Degrees sprite is facing (direction)
        self.direction += (self.k_right + self.k_left)
        rad = math.radians(self.direction)
        self.velocity.x = -self.speed*math.sin(rad)
        self.velocity.y = -self.speed*math.cos(rad)
        self.position += self.velocity
        self.image = pygame.transform.rotate(self.src_image, self.direction)
        self.rect = self.image.get_rect(center=self.position)


class Background(pygame.sprite.Sprite):

    def __init__(self, image, location):
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect(topleft=location)


def game_loop():
    bike = VehicleSprite(VEHICLE1, rect.center)
    pygame.sprite.LayeredUpdates.move_to_front
    ball = VehicleSprite(VEHICLE2, rect.center)

    bike_group = pygame.sprite.Group(bike)
    ball_group = pygame.sprite.Group(ball)
    all_sprites = pygame.sprite.Group(bike_group, ball_group)
    background = Background(BACKGROUND, [0, 0])

    camera = pygame.math.Vector2(0, 0)
    done = False

    while not done:
        time = clock.tick(60)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.KEYDOWN:
                # Bike Input (Player 1)
                if event.key == pygame.K_d:
                    bike.k_right = -5
                elif event.key == pygame.K_a:
                    bike.k_left = 5
                elif event.key == pygame.K_w:
                    bike.k_up = 2
                elif event.key == pygame.K_s:
                    bike.k_down = -2

                elif event.key == pygame.K_ESCAPE:
                    done = True
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_d:
                    bike.k_right = 0
                elif event.key == pygame.K_a:
                    bike.k_left = 0
                elif event.key == pygame.K_w:
                    bike.k_up = 0
                elif event.key == pygame.K_s:
                    bike.k_down = 0


        camera -= bike.velocity

        #screen.blit(background.image, background.rect)
        all_sprites.update(time)

        screen.fill(WHITE)



        for sprite in all_sprites:
            screen.blit(background.image, background.rect.topleft+camera)
            screen.blit(sprite.image, sprite.rect.topleft+camera)


        pygame.display.flip()


game_loop()
pygame.quit()

我认为你应该在赛车之前先让跑道消失。

BACKGROUND = pygame.Surface((1280, 800))
BACKGROUND.fill((127, 69, 2))
BACKGROUND = pygame.image.load("track1.png").convert()
screen.blit(BACKGROUND,(0,0))

把这个放在你的车之前。

问题出在 for 循环中,您在其中 blit 精灵图像:

for sprite in all_sprites:
    screen.blit(background.image, background.rect.topleft+camera)
    screen.blit(sprite.image, sprite.rect.topleft+camera)

在这里你 blit 背景和一个精灵,然后你再次 blit 背景和下一个精灵。因此,在 for 循环的每次迭代中,背景将被 blit 到前面的精灵上方。

在循环之前只 blit 一次背景,然后是精灵:

screen.fill(WHITE)
screen.blit(background.image, background.rect.topleft+camera)

for sprite in all_sprites:
    screen.blit(sprite.image, sprite.rect.topleft+camera)