Made some changes to the angle calculations, works better but still not correct. Here is the updated code and a video showing how it currently works. The image still doesn't point 100% in the direction of the mouse and does not rotate quick enough with the mouse motion.
Still would appreciate any help.
Code:
# mouse image file
mif = os.path.join('images', 'space_craft.png')
def game(screen_resolution = (800, 600), fullscreen = False):
# initialize pygame system
pygame.init()
# set to fullscreen or window size
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]))
# set background screen
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))
background = background.convert()
# load mouse image and rotate 90 degrees
mouse_image = pygame.image.load(mif).convert_alpha()
mouse_image = pygame.transform.rotate(mouse_image, 90)
# hide mouse cursor
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
FPS = 30
game_playtime = 0.0
playgame = True
# play game while true
while playgame is True:
milliseconds = clock.tick(FPS)
# game time played in seconds
game_playtime += milliseconds / 1000.0
# get current mouse position
x, y = pygame.mouse.get_pos()
# get center of screen
res_x = screen_resolution[0] / 2
res_y = screen_resolution[1] / 2
# get rotation angle of mouse location relative to center of screen
angle = atan2((res_y - y), (x - res_x)) / pi * 2.0
mouse_cursor = pygame.transform.rotate(mouse_image, degrees(angle))
# set mouse cursor center of mouse image
x -= mouse_cursor.get_width() / 2
y -= mouse_cursor.get_height() / 2
# put background and mouse cursor image to screen
screen.blit(background, (0, 0))
screen.blit(mouse_cursor, (x, y))
# user inputs
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
# update window caption
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))
# update the screen
pygame.display.flip()
# show mouse cursor
pygame.mouse.set_visible(True)
Video showing how it current works:
http://www.youtube.com/watch?v=0KJ7Z1RGo4k
Thanks
--
Chris
Bookmarks