Larry McMuffin
Larry McMuffin

Reputation: 227

How would I make a car move the direction it's pointing (After use of pygame.translation.rotate)

Okay so I'm making a test for making a racing game... And I want the car to move in the direction it's pointing. Here is my code.

import pygame, sys
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
degree = 0
WHITE = 250,250,250
rect2 = pygame.rect = (100,100,50,50)
WINDOWWIDTH = 1200
WINDOWHEIGHT = 750
thing = pygame.image.load('car.png')
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Teh test')
left = False
right = False
while True:
    rect2 = pygame.rect = (100,100,50,50)
    if right == True:
        degree -= 2
    if left == True:
        degree += 2
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == ord('a'):
                left = True
            if event.key == ord('d'):
                right = True
        if event.type == KEYUP:
            if event.key == ord('a'):
                left = False
            if event.key == ord('d'):
                right = False
    pygame.draw.rect(screen,WHITE,rect2)
    screen.fill((40, 40, 40))
    thing2 = pygame.transform.rotate(thing,degree)
    screen.blit(thing2,(100,100))
    pygame.display.update()
    mainClock.tick(60)

So like I said I want to know how to move the car in the direction it's pointing. I tried to think of a way but I couldn't think of anything. So there isn't really anything to correct. (If there are any questions I'll edit my question to answer it.) Please make sure you know pygame before answering.

Upvotes: 2

Views: 5281

Answers (2)

kabb
kabb

Reputation: 2502

You'll want to use trigonometry to calculate how much you want to move in the x and y directions, so that the car ends up moving in the correct direction. To calculate this, you can do this:

dx = math.cos(math.radians(degree))
dy = math.sin(math.radians(degree))
position = (position[0] + dx * SPEED, position[1] - dy * SPEED)

Note that you'll also need to initialize a position variable somewhere at the start of your code like this:

position = (100, 100)

Then, you have to change the blit line of code so you draw at the position variable, instead of at (100, 100) all of the time:

screen.blit(thing2, position)

Edit: Working example

import pygame, sys
from pygame.locals import *
import math
pygame.init()
mainClock = pygame.time.Clock()
degree = 0
WHITE = 250,250,250
rect2 = pygame.rect = (100,100,50,50)
WINDOWWIDTH = 1200
WINDOWHEIGHT = 750
thing = pygame.image.load('car.png')
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Teh test')
left = False
right = False
position = (100, 100)
while True:
    rect2 = pygame.rect = (100,100,50,50)
    if right == True:
        degree -= 2
    if left == True:
        degree += 2
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == ord('a'):
                left = True
            if event.key == ord('d'):
                right = True
        if event.type == KEYUP:
            if event.key == ord('a'):
                left = False
            if event.key == ord('d'):
                right = False
    pygame.draw.rect(screen,WHITE,rect2)
    screen.fill((40, 40, 40))
    thing2 = pygame.transform.rotate(thing,degree)
    dx = math.cos(math.radians(degree))
    dy = math.sin(math.radians(degree))
    position = (position[0] + dx, position[1] - dy)
    screen.blit(thing2, position)
    pygame.display.update()
    mainClock.tick(60)

Upvotes: 3

furas
furas

Reputation: 142889

Working example to kabb answer

import pygame, sys
from pygame.locals import *

import math # math library

pygame.init()
mainClock = pygame.time.Clock()
degree = 0
WHITE = 250,250,250
rect2 = pygame.rect = (100,100,50,50)
WINDOWWIDTH = 1200
WINDOWHEIGHT = 750
thing = pygame.image.load('car.png')
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Teh test')
left = False
right = False


forward = False
backward = False

thing2_x = 100
thing2_y = 100
speed = 20


while True:
    rect2 = pygame.rect = (100,100,50,50)

    if right: # don't need == True
        degree -= 2
        while degree < 0:
            degree += 360
    elif left: # don't need == True
        degree += 2
        while degree > 359:
            degree -= 360

    dx = math.cos(math.radians(degree))
    dy = math.sin(math.radians(degree))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            elif event.key == K_a: # use constants K_a
                left = True
            elif event.key == K_d: # use constants K_d
                right = True
            elif event.key == K_w: # use constants K_w
                forward = True
            elif event.key == K_s: # use constants K_s
                backward = True

        if event.type == KEYUP:
            if event.key == K_a: # use constants K_a
                left = False
            elif event.key == K_d: # use constants K_d
                right = False
            elif event.key == K_w: # use constants K_w
                forward = False
            elif event.key == K_s: # use constants K_s
                backward = False

    if forward:
        thing2_y -= int(speed * dx)
        thing2_x -= int(speed * dy)
    elif backward:
        thing2_y += int(speed * dx)
        thing2_x += int(speed * dy)

    pygame.draw.rect(screen,WHITE,rect2)
    screen.fill((40, 40, 40))

    thing2 = pygame.transform.rotate(thing,degree)
    screen.blit(thing2,(thing2_x,thing2_y))

    pygame.display.update()
    mainClock.tick(60)

Upvotes: 1

Related Questions