1

how to make a program using trigonometric functions with the following output:

If the car hits the wall, the car will move backwards and change direction 45 degrees clockwise then continue moving forward and so on until the program is terminated.

I have made the program, but I feel inefficient and i didn't use the trigonometrics functions

import pygame, sys
from pygame.locals import *
pygame.init()

# Define some colors
white=(250,250,250);   whitdull=(247,247,247)


font1 = pygame.font.Font(None,150)
text1 = font1.render("Bom-bom  Car",True,white)

# preparing  the  screen
width = 1050 ; height = 600
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption('Bom-bom Car')
screen.fill(whitdull) 


click_sound = pygame.mixer.Sound("click.wav")

car= pygame.image.load("Car-2.jpeg").convert_alpha()
player_image = car

#initial value
b0X = 0; b0Y = 250; direction = 2
ct = 20
clock = pygame.time.Clock()

def base(cX,cY,player_image):
    screen.fill(whitdull)
    pygame.draw.rect(screen,whitdull,[0,0,1050,600],4)

    screen.blit(text1,[150,230])
    
    
    screen.blit(player_image, [cX,cY])

    
    pygame.display.flip()
    clock.tick(ct)

   
def carMovement(b0XX,b0YY,bdirection):
    sX = 5     
    sY = 5   
    
    while True:
        if bdirection == 2 :
            player_image = car
            pX = sX
            pY = 0
            b0XX = b0XX + pX
            b0YY = b0YY + pY
            if b0XX == 970:
                sX = -sX
            if pX == -5 and b0XX == 900:
                bdirection = 3  
        elif bdirection == 3:
            player_image = pygame.transform.rotate(car, -45)
            pX = -sX
            pY = sY
            b0XX = b0XX + pX
            b0YY = b0YY + pY
            if b0XX == 970:
                sX = -sX
                sY = -sY
            if pX == -5 and b0YY == 250:
                bdirection = 4
        base(b0XX,b0YY,player_image)
    return b0XX,b0YY


while True: 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            click_sound.play()
            b0X,b0Y = carMovement(b0X,b0Y,direction)
    base(b0X,b0Y,player_image)    

Output

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
HezTech
  • 29
  • 3
  • What was wrong with your previous question? [The car moves backward when it hits the window edge](https://stackoverflow.com/users/5577765/rabbid76?tab=following) – Rabbid76 Dec 07 '20 at 14:45
  • It doesn't go backwards when it hits – HezTech Dec 08 '20 at 02:24
  • I mean why are you asking the same question twice? This is against the guidelines of this website. You've to improve the question. What have you tried so far? Stack Overflow is not a code writing service. – Rabbid76 Dec 08 '20 at 07:30

0 Answers0