###### tags: `Python` # gold miner 一個早上的結果 ``` import sys,time import random import pygame from pygame. locals import QUIT WINDOWS_WIDTH = 799 WINDOWS_HIGHT = 584 Gamer_coordinate = [385,80] def main(): pygame.init() screen = pygame.display.set_mode((WINDOWS_WIDTH,WINDOWS_HIGHT)) pygame.display.set_caption("Gold miner") background = pygame.image.load("image/back.png").convert() player_gamer = pygame.image.load("image/clip.png").convert() screen.blit(background,[0, 0]) angle = 0 while True: pygame.display.flip() if angle < 90: angle += 1 screen.blit(pygame.transform.rotate(player_gamer, angle), Gamer_coordinate) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() if __name__ =='__main__': main() ``` ![](https://i.imgur.com/KrTwVni.png) # 設定鉤子旋轉遇到問題 ![](https://i.imgur.com/yUCpgN7.png) 在這個網頁找到了方法[點我](https://answerlib.com/question/47843/how-do-i-rotate-an-image-around-its-center-using-pygame.html) ![](https://answerlib.com/upload/2021/11/03/1400155597.gif) 用這個固定選轉點只讓它旋轉正負45度 ## 固定旋轉軸在中心 > 其實是假固定中心點 > 每當圖片要旋轉時 > 會從新計算圖片左上角偏移位置 > 重新設置圖片的位置 > 讓他看起來像是圖片中心點被固定了一樣 ``` def blitRotate(surf, image, pos, originPos, angle): # offset from pivot to center image_rect = image.get_rect(topleft=(pos[0] - originPos[0], pos[1] - originPos[1])) offset_center_to_pivot = pygame.math.Vector2(pos) - image_rect.center # roatated offset from pivot to center rotated_offset = offset_center_to_pivot.rotate(-angle) # roatetd image center rotated_image_center = (pos[0] - rotated_offset.x, pos[1] - rotated_offset.y) # get a rotated image rotated_image = pygame.transform.rotate(image, angle) rotated_image_rect = rotated_image.get_rect(center=rotated_image_center) # rotate and blit the image surf.blit(rotated_image, rotated_image_rect) ```