我一直试图在使用中围绕其中心旋转图像,pygame.transform.rotate()但它不起作用.特别是挂起的部分是rot_image = rot_image.subsurface(rot_rect).copy().我得到了例外:
ValueError: subsurface rectangle outside surface area
以下是用于旋转图像的代码:
def rot_center(image, angle):
"""rotate an image while keeping its center and size"""
orig_rect = image.get_rect()
rot_image = pygame.transform.rotate(image, angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
Run Code Online (Sandbox Code Playgroud) 我是pygame的新手,想要编写一些代码,只需每10秒将图像旋转90度.我的代码看起来像这样:
import pygame
import time
from pygame.locals import *
pygame.init()
display_surf = pygame.display.set_mode((1200, 1200))
image_surf = pygame.image.load("/home/tempuser/Pictures/desktop.png").convert()
imagerect = image_surf.get_rect()
display_surf.blit(image_surf,(640, 480))
pygame.display.flip()
start = time.time()
new = time.time()
while True:
end = time.time()
if end - start > 30:
break
elif end - new > 10:
print "rotating"
new = time.time()
pygame.transform.rotate(image_surf,90)
pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)
此代码不起作用,即图像不旋转,尽管每10秒钟在终端中打印"旋转".有人能告诉我我做错了什么吗?
以前很少有人帮助我解决这个问题,但轮换仍然很混乱我的大炮不会向玩家旋转,很好,它们都不合适我真的需要帮助解决这个问题,这是我第一次尝试让某些东西向玩家旋转我我试图让我的大炮口向玩家旋转,但旋转混乱了我不知道如何修复它。
class enemyshoot:
def __init__(self,x,y,height,width,color):
# [...............]
self.look_at_pos = (x,y)
def draw(self):
# [............]
self.rect = self.shootsright.get_rect(topleft = (self.x, self.y))
dx = self.look_at_pos[0] - self.rect.centerx
dy = self.look_at_pos[1] - self.rect.centery
angle = (180/math.pi) * math.atan2(-dx, dy)
(window.blit(self.image, self.rect))
self.image = pygame.transform.rotate(self.shootsright, angle)
self.rect = self.image.get_rect(center = self.rect.center)
def lookAt( self, coordinate ):
self.look_at_pos = coordinate
Run Code Online (Sandbox Code Playgroud)
我的完整敌人射击课
shotsright = pygame.image.load("canss.png")
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y =y
self.height = height
self.width = width
self.color = color
self.health …Run Code Online (Sandbox Code Playgroud) 我想计算两点之间的角度。例如,假设我正在制作一个游戏,我想要一把枪指向鼠标光标,我得到将枪旋转到该角度所需的角度。