如何让汽车移动它指向的方向(使用pygame.translation.rotate之后)

时间:2014-02-27 00:49:09

标签: python pygame

好的,所以我正在制作赛车游戏的测试... 我希望汽车朝着它指向的方向移动。 这是我的代码。

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)

就像我说的那样,我想知道如何将汽车朝着它指向的方向移动。 我试着想办法,但我什么都想不到。所以没有什么可以纠正的。 (如果有任何问题我会编辑我的问题来回答。)请确认你在回答之前知道pygame。

2 个答案:

答案 0 :(得分:1)

您需要使用三角法计算您想要在x和y方向上移动多少,以便汽车最终朝着正确的方向移动。要计算这个,你可以这样做:

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

请注意,您还需要在代码开头的某处初始化位置变量,如下所示:

position = (100, 100)

然后,您必须更改代码的blit行,以便在位置变量处绘制,而不是始终在(100,100)绘制:

screen.blit(thing2, position)

编辑:工作示例

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)

答案 1 :(得分:0)

kabb回答的工作示例

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)