Найти - Пользователи
Полная версия: PyGame
Начало » Python для новичков » PyGame
1
rocket
Всем привет . Изучаю книгу “beginning-game-development-with-python-and-pygame-from-novice-to-professional” . Проблема
возникла в этой прогамме
pygame.init()
background = pygame.image.load(background_image_filename)
sprite = pygame.image.load(sprite_image_filename)
screen = pygame.display.set_mode((640, 480), 0 , 32)
clock = pygame.time.Clock()
sprite_pos = Vector2(200, 150)
sprite_speed = 300.
sprite_rotation = 0.
sprite_rotation_speed = 360.
while True:
     sleep(0.1)
     for event in pygame.event.get():
          if event.type == QUIT:
                exit()
     pressed_keys = pygame.key.get_pressed()
     key_direction = Vector2(0, 0)
     ratation_direction = 0.
     movement_direction = 0.
     if pressed_keys[K_LEFT]:
                 rotation_direction = +1.0
                 print "ratation_direction 1" ,rotation_direction
     if pressed_keys[K_RIGHT]:
                 rotation_direction = -1.0
                 print "ratation_direction 1" ,rotation_direction
     if pressed_keys[K_UP]:
                  movement_direction = +1.0
                  print "movement_direction 1  " ,movement_direction
     if pressed_keys[K_DOWN]:
                  movement_direction = -1.0
                  print "movement_direction 1", movement_direction
     screen.blit(background, (0, 0))
     print "ratation_direction 2" , ratation_direction
     print "movement_direction 2" , movement_direction
     rotated_sprite = pygame.transform.rotate(sprite, sprite_rotation)
     w, h = rotated_sprite.get_size()
     sprite_draw_pos = Vector2(sprite_pos.x - w/2, sprite_pos.y - h/2)
     screen.blit(rotated_sprite, sprite_draw_pos)
     time_passed = clock.tick()
     time_passed_seconds = time_passed / 1000.0
     sprite_rotation += ratation_direction * sprite_rotation_speed* time_passed_seconds
     print "ratation_direction   3", ratation_direction
     #print "sprite_rotation_speed   ", sprite_rotation_speed
     #print "time_passed_secondsi   ", time_passed_seconds 
     #print "sprite rotation     " , sprite_rotation
     heading_x = sin(sprite_rotation * pi/ 180.0)
     heading_y = cos(sprite_rotation * pi /180.0)
     heading = Vector2(heading_x, heading_y)
     heading *= movement_direction
     sprite_pos += heading * sprite_speed * time_passed_seconds
     pygame.display.update()
Программка выводит изображение ,при нажатии горизантальных стрелочек вращает это изображение , вертикальных -
перемещает .
Проблема в том переменая rotation_direction не сохраняет своего значения при выходе из условия if , при этом movement_direction прекрасно работает . Чувствую ошибка очевидная , но не вижу в упор. Заранее спасибо за ответы
py.user.next
пиши везде rotation
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB