ARTICLE AD BOX
I have been working on a top-down game where, instead of the player character facing the mouse, the map is rotated around the player. The direction of the map is modified by mouse movement, the player faces the same way. Basically, imagine facing straight down in a 3rd person shooter.
I have been having an issue where the direction the player faces effectively dictates the game's framerate, almost halving it at 45 degrees, then returning to normal at 90. I know rotating bitmap images is processor-intensive, but I figured the exact direction wouldn't have that much of a bearing on the performance.
I researched possible fixes to my problem. Pre-rotating a bunch of different angles isn't really a plausible solution because the maps are large enough that displaying the full map on its own causes performance issues. To solve this, I only draw a few tiles at a time, but this means that to pre-rotate the map I would essentially have to cache every possible angle of every possible combination of tiles that could exist at a given moment, which is not something I think most machines would take well.
Is there some kind of glaring optimisation issue with my code? Do I have any other options as far as ways of rotating the image, potentially outside of the pygame library? Or am I condemned to scrap this game idea or build it in another language/engine?
This is the code from the function that does the map scaling and rotation:
import pygame import math as m from settings import * def camerafy(mapsurface, destsurface, zoom, direction): """ Given a surface input, this function resizes and rotates it to reflect the player's position and direction. """ angle = m.radians(-direction)# I hate working with radians. Conversion is important. # zoompos is just the position to display the map at after map resize but before # rotation zoompos = 0.5*RESOLUTIONMULT*zoom*TILESIZE,0.5*RESOLUTIONMULT*zoom*TILESIZE # The 0.5's just center player # all of this is to calculate the position to display the map in after rotation and # zoom. x1 = zoompos[0]*m.cos(angle) - zoompos[1]*m.sin(angle) y1 = zoompos[0]*m.sin(angle) + zoompos[1]*m.cos(angle) calcpos = -x1, -y1 # rotates and scales the map surface newsurface = pygame.transform.scale_by(mapsurface, RESOLUTIONMULT*zoom) newsurface = pygame.transform.rotate(newsurface, direction) # rect is important for it to rotate on center instead of from the top right or # wherever newrect = newsurface.get_rect(center = (calcpos[0] + RESOLUTION[0]/2, calcpos[1] + \ RESOLUTION[1]/8*6)) destsurface.blit(newsurface, newrect) # draw map on screenThe rest of the project can be found here.


