0

I am trying to make a racing game for my final project(High School). but I am having problems rotating the car. It loads and draws the image properly but I haven't been able to rotate the image.The program is much longer but here is the parts that are involved:

from tkinter import *
from time import *
from math import *
from random import * 
from PIL import *

global screenWidth, screenHeight

screenWidth = 1200
screenHeight = 800

root = Tk()
s = Canvas(root, width=screenWidth, height=screenHeight, background= "gray")


def setGameStartingValues():

    carLength = 60
    carWidth = 28
    carAngle = 0
    lapCount = 0

    speed = 0
    speedX = 0
    speedY = 0

    directionX = ""
    directionY = ""

    imageCar = PhotoImage(file = "C:\\Users\\Antonio\\Desktop\\Python Programs\\GameFinal\\car.png")

    colisionX = False
    colisionY = False

    carRadius = sqrt((carLength/2)**2 + (carWidth/2)**2)
    carAngleFixed = asin((carWidth/2)/carRadius)

    barriers, finishLine = createCirquit()

    drawnBarriers = []
    for i in range(len(barriers)):

        drawnBarriers.append(None)

    return carAngle, carRadius, carAngleFixed, barriers, speed, drawnBarriers, speedY, speedX, lapCount, finishLine, colisionX, colisionY, imageCar, directionX, directionY
        

def drawCar(carAngle, carRadius, carAngleFixed):

    global screenWidth, screenHeight, correctCarImage

    backLeftAngle = ((2*pi-carAngleFixed+carAngle)%(2*pi))
    backRightAngle = ((carAngle+carAngleFixed)%(2*pi))
    frontRightAngle = ((pi-carAngleFixed+carAngle)%(2*pi))
    frontLeftAngle = ((pi+carAngle+carAngleFixed)%(2*pi))

    frontRightX = carRadius*cos(frontRightAngle) + screenWidth/2
    frontRightY = carRadius*sin(frontRightAngle) + screenHeight/2
    frontLeftX = carRadius*cos(frontLeftAngle) + screenWidth/2
    frontLeftY = carRadius*sin(frontLeftAngle) + screenHeight/2
    backRightX = carRadius*cos(backRightAngle) + screenWidth/2
    backRightY = carRadius*sin(backRightAngle) + screenHeight/2
    backLeftX= carRadius*cos(backLeftAngle) + screenWidth/2
    backLeftY = carRadius*sin(backLeftAngle) + screenHeight/2
    
    car = s.create_image(600, 400, anchor = NW, image = correctCarImage)

    return car, frontRightX, frontRightY, frontLeftX, frontLeftY, backRightX, backRightY, backLeftX, backLeftY
    

def sKeyPressed(event):

    global speed, screenUsed

    if screenUsed == "Game Screen":
        if speed > -6:
            speed = speed - 2

def wKeyPressed(event):

    global speed, screenUsed

    if screenUsed == "Game Screen":
        if speed < 30:
            speed = speed + 2

def aKeyPressed(event):

    global carAngle, screenUsed, imageCar, correctCarImage

    if screenUsed == "Game Screen":

        carAngle = carAngle - radians(15)
        correctCarImage = imageCar.rotate(degrees(carAngle))

def dKeyPressed(event):

    global carAngle, screenUsed, correctCarImage, imageCar

    if screenUsed == "Game Screen":

        carAngle = carAngle + radians(15)
        correctCarImage = imageCar.rotate(degrees(carAngle))

The full code doesn't fit in the question

Tontonio3
  • 11
  • 1
  • I think there is another question regarding this issue. See here: https://stackoverflow.com/questions/46245682/how-can-i-rotate-an-image-loaded-by-the-photoimage-class-on-a-canvas-without – dvr Jun 22 '22 at 23:02
  • We don't want your full code, just a runnable [mre]. – martineau Jun 22 '22 at 23:19
  • Do you have any errors or logs? What is supposed to call the rotate function? – Kerry Hatcher Jun 23 '22 at 05:37

0 Answers0