在pygame三角中工作

2024-05-19 09:49:00 发布

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

我在做一个游戏游戏,子弹可以朝鼠标的方向射击。我在用 一个类来定义列表中的项目符号:

class Bullet:
    def __init__(self,pos,speed,size):
        self.pos = pos
        self.speed = speed
        self.size = size
    def move(self):
        self.pos[0] = int(self.pos[0] + self.speed[0])
        self.pos[1] = int(self.pos[1] + self.speed[1]) 

我用这个三角函数,得到子弹射击角度的向量。在

^{pr2}$

我没有使用角度,因为我必须解决一个pygame舍入错误。 这些是我插入函数的变量。在

mousex, mousey = pygame.mouse.get_pos()
    startx = 50
    starty = 400
aim = getUnitVector(startx, starty, mousex, mouse

这就是我如何瞄准,让子弹从x,y开始射出

if pygame.mouse.get_pressed()[0]:
        if reload>10:
            bx = BULLETSPEED * aim[0]
            by = BULLETSPEED * aim[1]           
            bullet = Bullet([startx,starty], [bx,by],10)
            bullets.append(bullet)
            reload=0
        reload = reload + 1

我只想让你知道。我正在做一个学校作业,下一单元我会更深入地学习向量和trig,所以我现在不想花太多时间学习这些东西:L。另外,如果你知道任何活跃的python论坛,可能对回答这个问题更有帮助,请评论。我找不到。 谢谢你抽出时间。在

我可能只是建立了一个解决办法,只允许它拍摄,如果鼠标在20像素以内,这样错误就最小化了。在


Tags: posself游戏sizedef鼠标pygamereload
1条回答
网友
1楼 · 发布于 2024-05-19 09:49:00

bullet类应该有一个rectangle属性,以便子弹的移动方向穿过子弹的中心。在

这样做首先需要阅读Pygame文档:Pygame.Rect Docs

将弹头矩形置于移动方向的中心可通过如下移动方法实现:

    def move(self):
        self.dir = self.get_direction(self.target) # get direction
        if self.dir: # if there is a direction to move

            self.trueX += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
            self.trueY += (self.dir[1] * self.speed)
            self.rect.center = (round(self.trueX),round(self.trueY)) # apply values to bullet.rect.center

更多信息可以在这个有用的精灵移动示例中找到:Pygame Sprite Movement

相关问题 更多 >

    热门问题