【问题标题】:How would I make a car move the direction it's pointing (After use of pygame.translation.rotate)我如何让汽车朝着它指向的方向移动(使用 pygame.translation.rotate 之后)
【发布时间】:2014-02-27 00:49:09
【问题描述】:

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

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。

【问题讨论】:

    标签: python pygame


    【解决方案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)
    

    【讨论】:

    • 所以我运行它并得到这个语法错误... Traceback(最近一次调用最后一次):文件“C:\Python33\WhatEver.py”,第 40 行,在 thing2. scroll(dx, dy) TypeError: integer argument expected, got float
    • 啊,是的,您必须将 dx 和 dy 转换为整数。 thing2.scroll(int(dx), int(dy))
    • 那么现在我该如何让它朝着那个方向前进呢?我不使用数学库,所以我不知道你做了什么。
    • 对不起。如果不了解三角学,就很难解释 cos 和 sin 是如何工作的。不过,我编辑了我的帖子,试图更清楚地知道你应该做什么。
    • 好吧,我还是想不通。如果您获取我的原始代码并按照我需要它的方式发布它会有所帮助。
    【解决方案2】:

    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)
    

    【讨论】:

    • 谢谢你的作品。如果你能发布一些东西来告诉我这是如何工作的,那就太好了。 dx = math.cos(math.radians(degree)) dy = math.sin(math.radians(degree))
    • 我发现了这个 - 我没有读过它,但它是带有三角形、角度、sin 和 cos、弧度和度数、速度的图像。也许它可以帮助你。 raywenderlich.com/35866/…
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2021-10-28
    • 1970-01-01
    • 2013-04-18
    相关资源
    最近更新 更多