2013-10-23 4 views
0

파이 게임에서 회전하는 이미지/rect의 방향을 어떻게 바꾸시겠습니까? 양수 및 음수 값 적용은 작동하지만 내 창 전체에서 한 방향 만 회전 할 수있는 것처럼 보입니다. 회전 방향을 바꾸는 방법이 있습니까?회전 방향 변경 파이 게임


아마도 방사 이미지 5 초마다, 또는 X 또는 Y 축선을 타격 할 때 스핀의 방향을 바꿀 수있는 경우의 회전을 변경. 아래에 몇 가지 코드를 추가했습니다.

속도를 지정하고 위치 절이있는 한 이동 방향 전환이 쉬운 것 같습니다 (rect.move_ip). 불행히도 회전은 그렇게하지 않습니다. 여기서 제가 회전 할 각도를 추가 할 것입니다,하지만 제가 시도한 것에 상관없이 나는 회전을 무효화 할 수 없습니다.


내가 정말 이미지의 회전을 반전하는 방법을 알고 싶습니다
def rotate_image(self): #rotate image 
    orig_rect = self.image.get_rect() 
    rot_image = pygame.transform.rotate(self.image, self.angle) 
    rot_rect = orig_rect.copy() 
    rot_rect.center = rot_image.get_rect().center 
    rot_image = rot_image.subsurface(rot_rect).copy() 
    return rot_image 

def render(self): 
    self.screen.fill(self.bg_color) 
    self.rect.move_ip(0,5)     #Y axis movement at 5 px per frame 
    self.angle += 5    #add 5 anglewhen the rect has not hit one of the window 
    self.angle %= 360 

    if self.rect.left < 0 or self.rect.right > self.width:   
     self.speed[0] = -self.speed[0] 
     self.angle = -self.angle   #tried to invert the angle 
     self.angle -= 5     #trying to negate the angle rotation 
     self.angle %= 360 

    self.screen.blit(self.rotate_image(),self.rect) 
    pygame.display.flip() 

. 당신은 자신의 예를 제시 할 수 있습니다.

지금까지 내가 아는 한, 당신은 공식 문서 여기

http://www.pygame.org/docs/ref/transform.html 을 확인할 수 있습니다

pygame.transform.rotate(surface, angle) 

를 사용하여 이미지를 회전하는 단 하나의 방법은 예제 코드가된다

+1

'pygame.transform.rotate()'에 대한 [documentation] (http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotate) "Unfiltered counterclockwise rotation. 각도 인수는도를 나타내며 부동 소수점 값이 될 수 있습니다. 음수 값은 시계 방향으로 회전합니다."... 그래서 당신의 말은 사실이 아닙니다. – martineau

+0

시계 방향과 반 시계 방향 회전을 다르게 처리 할 필요가 없습니다. 'self.delta_angle' 이미지를 각 반복마다 회전시키고 싶다고합시다. 'self.angle = (self.angle + self.delta_angle) % 360'을 사용하고,'rot_image = pygame.transform.rotate (self.image, self.angle)'가 뒤 따른다. 'self.delta_angle> 0'의 경우 회전은 시계 반대 방향이고'self.delta_angle <0' 인 경우 회전은 시계 방향입니다. 회전 방향을 역전 시키려면'self.delta_angle = -self.delta_angle'을 만드십시오. – martineau

답변

1

: -

screen = pygame.display.set_mode((640,480)) 
    surf = pygame.image.load("/home/image.jpeg").convert() 
    while True: 
     newsurf = pygame.transform.rotate(surf, -90) 
     screen.blit(newsurf, (100,100)) 
     pygame.display.flip() 
     time.sleep(2) 

이 코드는 2 초마다 이미지를 시계 방향으로 90도 회전 한 상태로 유지합니다. 호프가 도움이 되었기를 바랍니다.