1

i am working on a top down racing game, and currently i am trying to get the car to go on top of the racetrack. There are 2 cars, one is the computer's car (not finished) and the other is the user controlled car, however when i run the programme the user's car is behind the track, but still moves normally. I have tried a few different things, but the user's car never seems to get on top of the track. So i am mainly asking how to get the user's car on top of the track.

Thank you

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()
Y2J
  • 45
  • 5

2 Answers2

2

The problem lies in the for loop in which you blit the sprite images:

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

Here you blit the background and one of the sprites, then you blit the background again and the next sprite. So the background will be blit above the previous sprites in each iteration of the for loop.

Just blit the background once before the loop and then the sprites:

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

for sprite in all_sprites:
    screen.blit(sprite.image, sprite.rect.topleft+camera)
skrx
  • 19,980
  • 5
  • 34
  • 48
0

I think you should blit the racetrack before the cars.

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

put this before you blit the cars.

Arximede
  • 371
  • 1
  • 4
  • 17
  • Thank you for your response, however the user's car is still behind the racetrack. – Y2J Sep 14 '18 at 20:16
  • @Y2J is the code that you are running look like the one you posted here? another question: why are you using pygame.display.update() 3 times for each surface? – Arximede Sep 14 '18 at 20:23
  • Yes the bit i am posting is the same as the previous posts, i know that i probably shouldn't keep posting the same thing, but i keep hitting a wall. And i'm not sure why i use pygame.display.update() each time, am i not supposed to? (i am very new to python and i use examples and tutorials to put the code together) – Y2J Sep 14 '18 at 20:26
  • 2
    @Y2J I meant that the code that you run on your computer is the same code you posted? hitting the wall is good, just don't give up on learning! you shouldn't use pygame.display.update() every time you add a new picture. the game is like a flipbook, each frame is each page. so you want to update the display after you draw everything on it. – Arximede Sep 14 '18 at 20:30
  • also can you post the whole code, so I can try to run it? – Arximede Sep 14 '18 at 20:31
  • the code should run, it is only missing the music and an incomplete GUI. And thank you for your response. – Y2J Sep 14 '18 at 20:33
  • 1
    I tested the code and I found more problems than just picture. when I press 'w' 'a' 's' 'd' the whole background moves. you should want to make the background stationary. to fix that: screen.blit(background.image, background.rect.topleft+camera) remove the part where it says +camera, because in each frame you will be changing the position of the background. – Arximede Sep 14 '18 at 20:52
  • 1
    I like what you are doing with the intro and everything. good job on using tkinter. you should keep learning more. on youtube there is a lot of videos about pygame. – Arximede Sep 14 '18 at 20:56
  • Thank you! Yes i am using many videos and they are helpful. Thanks again for your help! – Y2J Sep 14 '18 at 21:05