到目前为止,我已经创建了一个矩形,它跟随用户的鼠标移动,并通过按下 a
键逆时针旋转,按下 d
键顺时针旋转。
目前该矩形跟随鼠标移动,但一旦用户开始旋转矩形,矩形就会变得非常卡顿和畸形。我需要帮助保持矩形不变和恒定的FPS。谢谢你的帮助!
以下是代码:
import pygame as py
# define constants
WIDTH = 500
HEIGHT = 500
FPS = 200
# define colors
BLACK = (0 , 0 , 0)
GREEN = (0 , 255 , 0)
# initialize pygame and create screen
py.init()
screen = py.display.set_mode((WIDTH , HEIGHT))
# for setting FPS
clock = py.time.Clock()
rot = 0
rot_speed = 2
# define a surface (RECTANGLE)
image_orig = py.Surface((1 , 100))
# for making transparent background while rotating an image
image_orig.set_colorkey(BLACK)
# fill the rectangle / surface with green color
image_orig.fill(GREEN)
# creating a copy of orignal image for smooth rotation
image = image_orig.copy()
image.set_colorkey(BLACK)
# define rect for placing the rectangle at the desired position
rect = image.get_rect()
x, y = py.mouse.get_pos()
rect.center = (x, y)
# keep rotating the rectangle until running is set to False
running = True
while running:
x, y = py.mouse.get_pos()
# set FPS
clock.tick(FPS)
# clear the screen every time before drawing new objects
screen.fill(BLACK)
# check for the exit
for event in py.event.get():
if event.type == py.QUIT:
running = False
# making a copy of the old center of the rectangle
old_center =(x, y)
# defining angle of the rotation
rot = (rot + rot_speed) % 360
# rotating the orignal image
keys = py.key.get_pressed()
rot_speed = .2
image_orig = py.transform.rotate(image_orig , 0)
rect = image_orig.get_rect()
# set the rotated rectangle to the old center
rect.center = (x, y)
# drawing the rotated rectangle to the screen
screen.blit(image_orig , rect)
# flipping the display after drawing everything
py.display.flip()
if(keys[py.K_a]):
rot_speed = .2
image_orig = py.transform.rotate(image_orig , rot)
rect = image_orig.get_rect()
# set the rotated rectangle to the old center
rect.center = (x, y)
# drawing the rotated rectangle to the screen
screen.blit(image_orig , rect)
# flipping the display after drawing everything
py.display.flip()
if(keys[py.K_d]):
rot_speed = -.2
image_orig = py.transform.rotate(image_orig , rot)
rect = image_orig.get_rect()
# set the rotated rectangle to the old center
rect.center = (x, y)
# drawing the rotated rectangle to the screen
screen.blit(image_orig , rect)
# flipping the display after drawing everything
py.display.flip()
rect.center = (x, y)
py.quit()