2016-10-28 52 views
-3

圖像= https://www.dropbox.com/s/nx6yzx8ddhu36m7/car.png?dl=0,當我轉動(按左右鍵),而我加速(按了鍵)我在陌生的路上車移動汽車遊戲不pygame的正常工作

也有一些錯誤的加速

速度不會增加我希望它是的方式。我認爲速度應該隨着時間的推移而增加,但它不會...

任何人都可以通過嘗試代碼來幫助我嗎?

謝謝

這裏是我的代碼:

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() 
+5

「誰能請修正錯誤,」我們將不會解決它,只是幫助它 – Raskayu

+2

*「還有加速的問題」* - 意思是什麼? – UnholySheep

+3

歡迎來到SO!你可以閱讀指南來問問題[這裏](http://stackoverflow.com/help/how-to-ask)。請提供關於您的程序如何發生故障的更多具體細節。 – Jerrybibo

回答

2

當您按下UP/DOWN然後設置changeX = xchangeY = y和汽車使用changeXchangeY移動。

當您按下LEFT/DOWN那就改angle,並計算新xy但是這並沒有改變changeXchangeY因此汽車依然繼續向同一方向(使用相同的changeXchangeY)。


編輯:現在事實正確,當你前進,但仍然是落後的加速問題。我在做這個工作。

我使用moving_up/moving_down更新changeXchangeY當汽車移動 - 因此它使用當前角度和xy改變方向。


編輯:加速解決問題:你有當你去UP和DOWN使用speed_change = speed_change**2 + 1。因爲x_change = xy_change = -y將改變方向,因此當您關閉時不需要負值。

新代碼:

BTW:我補充ESC鍵退出程序和Backspace居中汽車在屏幕上(復位位置)

import pygame 
import math 

# --- constants --- (UPPER_CASE) 

WHITE = (255, 255, 255) 
BLACK = ( 0, 0, 0) 

DISPLAY_WIDTH = 1200 
DISPLAY_HEIGHT = 800 

FPS = 30 

CAR_WIDTH = 76 
CAR_HEIGHT = 154 

# --- functions --- (lower_case) 

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 rotate_car_pos(angle): 
    x = math.cos(math.radians(angle-90)) 
    y = math.sin(math.radians(angle-90)) 
    return x, y 

def gameloop(): 

    # start position - screen center - so I don't have to add center later 
    car_rect = role_model.get_rect(center=screen_rect.center) 

    # --- 

    angle = 0 
    angle_change = 0 

    x = 0 
    y = 0 
    x_change = 0 
    y_change = 0 

    speed = 0 
    speed_change = 0 

    # --- 

    pos_x = 0 
    pos_y = 0 

    #--- 

    moving_up = False 
    moving_down = False 

    #recalculate = True 

    running = True 

    while running: 

     # --- events --- 

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

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_ESCAPE: 
        running = False 

       elif event.key == pygame.K_BACKSPACE: 
        # reset position [for test only] 
        pos_x = 0 
        pos_y = 0 
        angle = 0 

       elif event.key == pygame.K_LEFT: 
        angle_change = 5 

       elif event.key == pygame.K_RIGHT: 
        angle_change = -5 

       elif event.key == pygame.K_UP: 
        moving_up = True 
        x_change = -x 
        y_change = y 
        speed_change = speed_change**2 + 1 

       elif event.key == pygame.K_DOWN: 
        moving_down = True 
        x_change = x 
        y_change = -y 
        speed_change = speed_change**2 + 1 

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

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

       elif event.key == pygame.K_UP: 
        moving_up = False 
        x_change = 0 
        y_change = 0 
        speed = 0 
        speed_change = 0 

       elif event.key == pygame.K_DOWN: 
        moving_down = False 
        x_change = 0 
        y_change = 0 
        speed = 0 
        speed_change = 0 

     # --- updates --- 

     # - pos - 

     if x_change or y_change: 
      pos_x += x_change 
      pos_y += y_change 

      print('[DEBUG]: pos_x, pos_y: ', pos_x, pos_y) 

     # - angle - 

     # rotate olny when moving 
     #if moving_up or moving_down: 
     if angle_change: 
      angle += angle_change 

      while angle > 360: 
       angle -= 360 

      while angle < -360: 
       angle += 360 

      print('[DEBUG]: angle: ', angle) 

     # - speed - 

     if speed_change: 
      speed += speed_change 

      if speed > 20: 
       speed = 20 

      print('[DEBUG]: speed: ', speed) 

     # - others - 

     x, y = rotate_car_pos(angle) 

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

     print('[DEBUG]: x, y: ', x, y) 

     if moving_up: 
      x_change = -x 
      y_change = y 
     elif moving_down: 
      x_change = x 
      y_change = -y 

     #if recalculate: 
     rot_image, rot_rect = rotate(car_image, car_rect, angle) 

     rot_rect.centerx += pos_x 
     rot_rect.centery += pos_y 

     # --- draws --- 

     screen.fill(WHITE) 
     screen.blit(rot_image, rot_rect) 
     pygame.display.update() 

     # --- clock --- 

     clock.tick(FPS) 

# --- main --- 

pygame.init() 

car_image = pygame.image.load('car.png') 
role_model = pygame.image.load('car.png') 

screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT)) 
screen_rect = screen.get_rect() 

clock = pygame.time.Clock() 

gameloop() 

pygame.quit() 
+0

感謝您的大解釋! –