在 Pygame 中正确旋转 Cannon 的问题仍然存在 [英] Still Having Problems With Rotating Cannon's Properly Towards The Player Pygame

查看:58
本文介绍了在 Pygame 中正确旋转 Cannon 的问题仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前很少有人帮助我解决这个问题,但旋转仍然很混乱我的大炮不会向玩家旋转,很好,它们都不合适我真的需要帮助遇到这个问题,这是我第一次尝试让某些东西朝向玩家旋转我试图让我的大炮口向玩家旋转,但

图像的中心是佳能的旋转点.您可以将图像缩放到您想要的任何大小.例如 100x100:

self.shootsright = pygame.image.load("canss.png")self.shootsright = pygame.transform.smoothscale(self.shootsright, (100, 100))

另请参阅

few people have helped me with this problem before but the rotation is still messed up my Cannons wont rotate towards the player good they are all out of place I really need help with this problem its my first time trying to make something rotate towards a player I am trying to make my cannon mouth rotate towards player, but the rotation is messed up and up I don't know how to fix it.

    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

My full enemyshoot class

    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 = 10
            self.hitbox = (self.x + -20, self.y + 30, 31, 57)
           #-------------------------------------------------------
            # Make a Reference Copy of the bitmap for later rotation
            self.shootsright = pygame.image.load("canss.png")
            self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()-150,self.shootsright.get_height()-150))            
            self.image    = self.shootsright
            self.rect     = self.image.get_rect()
            self.position = pygame.math.Vector2( (x, y) )
            self.isLookingAtPlayer = False
            self.look_at_pos = (x,y)
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            window.blit(self.image, self.rect)
            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(-dy, dx)
            self.image = pygame.transform.rotate(self.shootsright, angle)
            self.rect  = self.image.get_rect(center = self.rect.center)

            # ------------
            self.hits = (self.x + 20, self.y, 28,60)
            pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 100, 10)) # NEW
            pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 100 - (5 * (10 - self.health)), 10))
            self.hitbox = (self.x + 200, self.y + 200, 51, 65)
        def lookAt( self, coordinate ):
            self.look_at_pos = coordinate


            
    black = (0,0,0)
    enemyshooting = []
    platformGroup = pygame.sprite.Group
    platformList = []
    level = ["                                                                                                                     p               p           p                         p                        p        ",
             "                                       ",
             "                             ",
             "                                      ",
             "                                  ",
             "                           ",
             "                                      ",
             "                                      ",
             "                                    ",
             "                                   ",
             "                    ",]
    for iy, row in enumerate(level):
        for ix, col in enumerate(row):
            if col == "p":
                new_platforms = enemyshoot(ix*10, iy*50, 10,10,(255,255,255))
                enemyshooting.append(new_platforms)

This is where the cannons rotate towards the player, wherever they are

            for enemyshoot in enemyshooting:
                if not enemyshoot.isLookingAtPlayer:
                    enemyshoot.lookAt((playerman.x, playerman.y)) 

解决方案

First of all the cannon image is far too tall. Clip the border and use a smaller image. For instance:

The center of the image is the rotation point of the canon. You can scale the image to any size you want. For instance 100x100:

self.shootsright = pygame.image.load("canss.png")
self.shootsright = pygame.transform.smoothscale(self.shootsright, (100, 100))       

See also How to rotate an image(player) to the mouse direction?.

Anyway you have to blit the cannon after rotating the image and computing the rectangle.

class enemyshoot:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y

        # [...]
        
        self.shootsright = pygame.image.load("canss.png")
        self.shootsright = pygame.transform.smoothscale(self.shootsright, (100, 100))            

        self.image = self.shootsright
        self.rect  = self.image.get_rect(center = (self.x, self.y))
        self.look_at_pos = (self.x, self.y)

        self.hitbox = (*self.rect.topleft, *self.rect.size)

    def draw(self):
        
        dx = self.look_at_pos[0] - self.x
        dy = self.look_at_pos[1] - self.y
        angle = (180/math.pi) * math.atan2(dx, dy)
        self.image = pygame.transform.rotate(self.shootsright, angle)
        self.rect  = self.image.get_rect(center = (self.x, self.y))

        window.blit(self.image, self.rect)
        self.hitbox = (*self.rect.topleft, *self.rect.size)
 
    def lookAt( self, coordinate ):
        self.look_at_pos = coordinate

Minimal example:

这篇关于在 Pygame 中正确旋转 Cannon 的问题仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆