程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Pygame 旋转 rect 问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Pygame 旋转 rect 问题?

开发过程中遇到Pygame 旋转 rect 问题的问题如何解决?下面主要结合日常开发的经验,给出你关于Pygame 旋转 rect 问题的解决方法建议,希望对你解决Pygame 旋转 rect 问题有所启发或帮助;

我有关于旋转矩形碰撞的问题。

这是我的汽车课:

class Car:
def __init__(self,x,y):
    self.x = x
    self.y = y
    self.default_image = pygame.image.load("car.png")
    self.image = self.default_image
    self.w = self.image.get_size()[0]
    self.h = self.image.get_size()[1]
    self.right_image = pygame.transform.rotate(self.default_image,-10)
    self.left_image = pygame.transform.rotate(self.default_image,10)
    self.rot = 0
def render(self):
    screen.blit(self.image,self.getRect())
def getRect(self):
    return self.default_image.get_rect(center=(self.x,self.y))
def getMask(self):
    return polygon(calculate(self.getRect(),self.rot))
def right(self):
    self.rot = -10
    self.image = self.right_image
def left(self):
    self.rot = 10
    self.image = self.left_image
def default(self):
    self.rot = 0
    self.image = self.default_image
def drawHitBox(self):
    pygame.draw.lines(screen,(242,236,12),True,calculate(self.getRect(),self.rot),1)

这是我的“计算”功能:

def calculate(irect,anglE):
    pivot = math.Vector2(irect[0],irect[1])
    p0 = (math.Vector2(irect.topleft) - pivot).rotate(-anglE) + pivot 
    p1= (math.Vector2(irect.topright) - pivot).rotate(-anglE) + pivot 
    p2 = (math.Vector2(irect.bottomright) - pivot).rotate(-anglE) + pivot 
    p3 = (math.Vector2(irect.bottomleft) - pivot).rotate(-anglE) + pivot
    return [p0,p1,p2,p3]

这是我检查碰撞的代码:

for i in vehicles:
        i.render()
        if i.getMask().intersects(car.getMask()):
            death = True

做了一个函数来绘制汽车的hitBox。但是当car的角度不为0时,car和hitBox是不同的。

Pygame 旋转 rect 问题

Pygame 旋转 rect 问题

Pygame 旋转 rect 问题

我正在使用匀称的多边形来制作旋转的矩形。

我的英语不是很好,我尽量告诉你。

解决方法

图像围绕其中心旋转。您还需要围绕其中心旋转矩形因此,枢轴点需要是矩形的中心,而不是其左上角:

import random cards = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"] suits = ["Diamonds","Hearts","Spades","Clubs"] print(random.choice(cards) + " of ") print(random.choice(suits))

pivot = math.Vector2(irect[0],irect[1])

pivot = math.Vector2(irect.center) 函数:

calculate

大佬总结

以上是大佬教程为你收集整理的Pygame 旋转 rect 问题全部内容,希望文章能够帮你解决Pygame 旋转 rect 问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。