2
votes

I am trying to make my own own game. Before, the car image would move really well but then and put my loop in a "def" thing so that the game could restart when the car crash in the wall and when you win. Now, everything works only the game doesn't seem to update to the screen because the car won't move. The game still seems to work behind the screen because when I make the car crash without seeing it move it plays the crash segment. PyGame doesn't say day is an error. this is kind of new to me and I really don,t understand what the problem. here is part of my code:

#Setting and variables
display_width = 1570
display_height = 450
car_width = 98
car_height = 66
clock = pygame.time.Clock()
wn = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('My own game')
finish_line = pygame.image.load('myOwnFinishreal.png')

carImg = pygame.image.load('myOwnRGame.png')
carY = 192
carX = 10
Xchange = 0
Ychange = 0
Xfin = 1480
Yfin = 0
carY = 192
carX = 10

#racecar
def car(x,y):
    wn.blit(carImg, (carX, carY))

#finish line
def finish():
    wn.blit(finish_line, (Xfin, Yfin))
#Crashing
def textObjects(text, font):
    textSurface = font.render(text, True, red)
    return textSurface, textSurface.get_rect()

def displayMessage(text):
    textFont1 = pygame.font.Font('freesansbold.ttf', 32)
    textSurf, textRect = textObjects(text, textFont1)
    textRect.center = ((display_width/2),(display_height/2))
    while True:
        wn.blit(textSurf, textRect)

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_x:
                    pygame.quit()
                if event.key == pygame.K_SPACE:
                    gameLoop()
        pygame.display.update()

def crash():
    displayMessage('You crashed!Press X to quit, _SPACE_ to restart!')
def win():
    displayMessage('Bravo! You are the best car runner! Press X to quit _SPACE_ to restart.')

#Game loop
def gameLoop():
    carY = 192
    carX = 10
    Xchange = 0
    Ychange = 0

    alive = True
    losing = True
    while alive and losing:
        carX += Xchange
        carY += Ychange
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_x:
                    pygame.quit()
                if event.key == pygame.K_UP:
                    Xchange = 0
                    Ychange = 0
                    Xchange = 2.5

                elif event.key == pygame.K_LEFT:
                    Xchange = 0
                    Ychange = 0
                    time.sleep(0.85)
                    Ychange = -3

                elif event.key == pygame.K_RIGHT:
                    Xchange = 0
                    Ychange = 0
                    time.sleep(0.85)
                    Ychange = 3
                elif event.key == pygame.K_DOWN:
                    Xchange = 0
                    Ychange = 0
                    time.sleep(0.85)
                    Xchange = -3
        if carY <= -15 or carY >= display_height - 15:
            Xchange = 0
            Ychange = 0
            crash()

        if carX >= display_width:
            Xchange = 0
            Ychange = 0
            win()
        if carX <= 0:
            carX = 10

            #350 250 120 30

        wn.fill(grey)
        finish()
        carX += Xchange
        carY += Ychange
        car(carX, carY)
        pygame.display.update()
        clock.tick(60)

    pygame.quit()
gameLoop()

Looking forward for your help and bid thanks!

1
The "def" thing is called function. You're approach won't work at all, because the main application loop is invoked recursively. - Rabbid76

1 Answers

3
votes

You have to use the arguments x and y rather than carX and carY in the function car:

def car(x,y):
    wn.blit(carImg, (x, y))