制作一个'对象'沿着弯曲的路径移动

时间:2018-05-24 12:58:14

标签: python python-3.x pygame

我试图制作一条在曲线上移动的船,但我只是设法让它从一个点移动到另一个点(直线)。我想让它像一条船在一个国家周围移动一样弯曲,因为它不能越过陆地。

class virus(object):
    moveRight = [image.load("res/virusMove/right1.png"),
                 image.load("res/virusMove/right2.png"),
                 image.load("res/virusMove/right3.png"),
                 image.load("res/virusMove/right4.png"),
                 image.load("res/virusMove/right5.png"),
                 image.load("res/virusMove/right6.png"),
                 image.load("res/virusMove/right7.png"),
                 image.load("res/virusMove/right8.png"),
                 image.load("res/virusMove/right5.png"),]
    moveLeft = [image.load("res/virusMove/left1.png"),
                image.load("res/virusMove/left2.png"),
                image.load("res/virusMove/left3.png"),
                image.load("res/virusMove/left4.png"),
                image.load("res/virusMove/left5.png"),
                image.load("res/virusMove/left6.png"),
                image.load("res/virusMove/left7.png"),
                image.load("res/virusMove/left8.png"),
                image.load("res/virusMove/left5.png")]

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.path = [x, end]
        self.moveUnits = 0
        self.vel = 5

    def draw(self, screen):
        self.move()
        if self.moveUnits +1 >= 24:
            self.moveUnits = 0
        if self.vel > 0:
            screen.blit (self.moveRight[self.moveUnits//3], (self.x, self.y))
            self.moveUnits += 1
        else:
            screen.blit (self.moveLeft[self.moveUnits//3], (self.x, self.y))
                self.moveUnits += 1
    def move(self):
        if self.vel > 0:
            if self.x < self.vel + self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.moveUnits = 0
        else:
            if self.x > self.path[0] - self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.moveUnits = 0               

然后我做了类似

的事情
def drawScreen():
    virus.draw(screen)
virus = virus(100, 640, 64, 64, 750) 

然后我把drawScreen放在我的运行循环中

0 个答案:

没有答案