KoolPenguin
August 12th, 2011, 08:52 PM
I'm trying to get my mouse image to rotate and point in the direction the mouse is moving. For example, I have a space craft image that moves with the mouse and would like to rotate it to the correct angle and point in that direction. The code below is what I have and the image does rotate when the mouse moves but not correctly.
def game(screen_resolution = (800, 600), fullscreen = False):
pygame.init()
if fullscreen:
screen = pygame.display.set_mode((screen_resolution[0],screen_resolution[1]), pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((screen_resolution[0],screen_resolution[1]))
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))
background = background.convert()
mouse_image = pygame.image.load(mif).convert_alpha()
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
FPS = 30
game_playtime = 0.0
playgame = True
while playgame is True:
milliseconds = clock.tick(FPS)
game_playtime += milliseconds / 1000.0
x, y = pygame.mouse.get_pos()
angle = atan2((y-300), (x-400))
mouse_cursor = pygame.transform.rotate(mouse_image, degrees(angle))
x -= mouse_cursor.get_width()/2
y -= mouse_cursor.get_height()/2
screen.blit(background, (0, 0))
screen.blit(mouse_cursor, (x, y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
playgame = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
playgame = False
pygame.display.set_caption('Press ESC to quit. FPS: %.2f (%ix%i), time: %.2f seonds' % (clock.get_fps(),
screen_resolution[0],
screen_resolution[1], game_playtime))
pygame.display.flip()
pygame.mouse.set_visible(True)
Thanks for any help that can be provided.
--
Chris
def game(screen_resolution = (800, 600), fullscreen = False):
pygame.init()
if fullscreen:
screen = pygame.display.set_mode((screen_resolution[0],screen_resolution[1]), pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((screen_resolution[0],screen_resolution[1]))
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))
background = background.convert()
mouse_image = pygame.image.load(mif).convert_alpha()
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
FPS = 30
game_playtime = 0.0
playgame = True
while playgame is True:
milliseconds = clock.tick(FPS)
game_playtime += milliseconds / 1000.0
x, y = pygame.mouse.get_pos()
angle = atan2((y-300), (x-400))
mouse_cursor = pygame.transform.rotate(mouse_image, degrees(angle))
x -= mouse_cursor.get_width()/2
y -= mouse_cursor.get_height()/2
screen.blit(background, (0, 0))
screen.blit(mouse_cursor, (x, y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
playgame = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
playgame = False
pygame.display.set_caption('Press ESC to quit. FPS: %.2f (%ix%i), time: %.2f seonds' % (clock.get_fps(),
screen_resolution[0],
screen_resolution[1], game_playtime))
pygame.display.flip()
pygame.mouse.set_visible(True)
Thanks for any help that can be provided.
--
Chris