汽车游戏在pygame中无法正常工作

时间:2016-10-28 11:47:22

标签: python pygame

image = https://www.dropbox.com/s/nx6yzx8ddhu36m7/car.png?dl=0

当我旋转(按左或右键)而我加速(按下键)时,我的车以奇怪的方式移动。

加速

也有问题 速度并没有增加我预期的方式。我认为速度应该随着时间的推移而增加,但它并没有......

任何人都可以通过尝试代码来帮助我吗?

谢谢

这是我的代码:

import pygame,math
pygame.init()
display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
car_image = pygame.image.load('car.png')
role_model = pygame.image.load('role_model.png')
clock = pygame.time.Clock()
FPS = 30
screen = pygame.display.set_mode([display_width,display_height])
car_width = 76
car_height = 154

def rotate(image, rect, angle):
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = rot_image.get_rect(center=rect.center)
    return rot_image,rot_rect

def carRotationPos(angle):
    x=1*math.cos(math.radians(angle-90))
    y=1*math.sin(math.radians(angle-90))

    return x,y

def gameloop():
    running = True
    angle = 0
    angle_change = 0
    changeX = 0
    changeY=0
    x=0
    y=0
    change_x=0
    change_y=0
    speed = 1
    speed_change = 1
    rect = role_model.get_rect()
    while running == True:


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False


            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:


                    angle_change = 5
                    #changeX=0
                    #changeY=0
                if event.key == pygame.K_RIGHT:


                    angle_change = -5
                    #changeX=0
                    #changeY=0
                if event.key == pygame.K_UP:

                    #angle_change =0
                    changeX=-x
                    changeY=y
                    speed_change = speed_change**2 +1

                if event.key == pygame.K_DOWN:

                    #angle_change =0
                    changeX=x
                    changeY=-y
                    speed_change = -(speed_change**2 + 1)

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    angle_change = 0

                if event.key == pygame.K_RIGHT:
                    angle_change = 0

                if event.key == pygame.K_UP:

                    changeX=0
                    changeY=0
                    speed = 1
                    speed_change=1
                if event.key == pygame.K_DOWN:

                    changeX=0
                    changeY=0
                    speed = 1
                    speed_change=1

        if angle == -360 or angle == 360:
            angle = 0


        angle+=angle_change
        change_x+=changeX
        change_y+=changeY
        speed+=speed_change
        if speed > 20:
            speed = 20
        screen.fill(white)
        x,y=carRotationPos(angle)

        x=round(x,5)*speed
        y=round(y,5)*speed

        rot_image,rot_rect=rotate(car_image,rect,angle)
        rot_rect=list(rot_rect)

        rot_rect1=rot_rect[0]+display_width/2-car_width/2
        rot_rect2=rot_rect[1]+display_height/2-car_height/2
        rot_rect1+=change_x
        rot_rect2+=change_y
        del rot_rect[0]
        del rot_rect[1]
        rot_rect.insert(0,rot_rect1)
        rot_rect.insert(1,rot_rect2)

        screen.blit(rot_image,rot_rect)

        pygame.display.update()
        clock.tick(FPS)

gameloop()
pygame.quit()

1 个答案:

答案 0 :(得分:2)

当您按UP/DOWN时,您设置changeX = xchangeY = y并使用changeXchangeY移动汽车。

当您按LEFT/DOWN时,您会更改angle并计算新的xy,但这不会更改changeXchangeY所以汽车仍然朝同一方向移动(使用相同的changeXchangeY)。

编辑现在,当你向前移动时它会正确转动,但仍然存在向后加速的问题。我正在努力。

我在汽车行驶时使用moving_up / moving_down更新changeXchangeY - 因此它使用当前角度和x,{{1}改变方向。

编辑:加速问题解决:当你上下行时你必须使用y。因为speed_change = speed_change**2 + 1x_change = x将改变方向,所以当你走向DOWN时不需要负值。

新代码:

顺便说一句:我添加ESC退出程序,将BACKSPACE添加到屏幕中心(重置位置)

y_change = -y