精灵在ci上的旋转

2024-09-30 00:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个精灵在以屏幕中心为中心的圆上移动,半径为30像素。移动或多或少是正常的,但精灵根本不旋转。我试过一些教程(例如How to animate scale and rotate in pygame using rotozoom),当精灵沿着一条线移动或随机改变它的轨迹时,没有问题。所以当它沿着圆圈移动的时候有什么问题?代码如下:

class EnemiesStrong(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)     
        self.image = pygame.image.load("enemy3.png").convert_alpha()
        self.image2 = self.image        
        self.rect = self.image2.get_rect()  
        self.angle = 0

    def initLoc(self, pos, x_pos, y_pos):

        self.rect.x = pos[0]/2 + x_pos
        self.rect.y = y_pos


    def update(self, pos, x_pos, y_pos, quadrant, newcoord):
        #print(quadrant)

        self.rect.y = y_pos


        if (quadrant==1 and newcoord==0):
            self.rect.x-=1            
        elif (quadrant==1 and newcoord==1):
            self.rect.x=x_pos

        elif (quadrant==2):           

            oldcenter = self.rect.center
            **self.image2 = pygame.transform.rotate(self.image, self.angle)**
            self.rect = self.image2.get_rect()
            self.rect.center = oldcenter  
            self.rect.x+=1


        elif (quadrant==3 and newcoord==0):
            self.rect.x+=1   

        elif (quadrant==4 and newcoord==0):
            self.rect.x-=1
        elif (quadrant==4 and newcoord==1):
            self.rect.x=x_pos                   

    def turnLeft(self, pos):

        self.angle = (self.angle + 10) % 360
        print(self.angle)

    def whereEnemy(self):
        return (self.rect.x, self.rect.y)    

def OrbitalCoordinates(loc, xpos, radius, quadrant):

    #do lots of stuff to determine the location of the ship in the screen
    #and return its Y-coordinate
    disc = np.power(radius,2) - np.power(xpos - loc[0]/2,2)
    ypos = loc[1]/2 - np.sqrt(disc)                
    return ypos

enemy_s = EnemiesStrong()
ypos = OrbitalCoordinates(screenpar, xpos + screenpar[0]/2, radius,2)
enemy_s.initLoc(screenpar, xpos, ypos)
enemiesupdate.add(enemy_s)
where_strong_enemy = enemy_s.whereEnemy()
bg = pygame.image.load("outer_space.jpg")
screenpar = [bg.get_width(), bg.get_height()]

def main():
   #various useless crap
   for enemy_s in enemiesupdate:
         #do lots of stuff to determine the x- position and direction of movement of the ship 
         #and pass these values to OrbitalCoordinates  
         enemy_s.turnLeft(screenpar) 
         ypos = OrbitalCoordinates(screenpar, where_strong_enemy[0], radius, quadrant)
         enemy_s.update(screenpar, where_strong_enemy[0], ypos, quadrant, 0)
         where_strong_enemy = list(enemy_s.whereEnemy())
         #then check if the y-coordinates are close to the middle of the screen to tweak them a                          
         #little bit
screen.blit(bg, [0, 0])   
enemiesupdate.draw(screen)
pygame.display.flip() 

我省略了我认为不必要的代码块。我肯定我犯了个愚蠢的错误。你知道吗


Tags: andofthetoposrectimageself

热门问题