Pygame:使精灵以恒定速度沿预定路径移动

时间:2013-11-16 23:45:00

标签: python pygame

我正在Pygame进行塔防游戏,敌人沿着我为他们设定的路径向基地前进;如果你玩过VR Defender Y3K和Canyon Defense这样的Flash游戏我的想法是完全相同的概念。但是,我无法让精灵沿着涉及方向变化的路径移动,例如路径左转或右转。

所以我的问题是,一个简单的方法是让精灵沿着预定的路径移动,并以恒定的速度改变方向?

编辑:这是我到目前为止的代码。

#Project 2 v1.00
import pygame, sys, time
from math import *
from pygame.locals import *

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 255, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

surface = pygame.display.set_mode((500,500),0, 30)

class PlaceHolder:
    def __init__(self, win, left, top, width, height, color):
        self.win = win
        self.rect = pygame.Rect(left, top, width, height)
        self.color = color

    def move(self,x,y,duration):
        #self.rect = self.rect.move(self.velocity[0],self.velocity[1])
        xpointlist = [60, 60]
        ypointlist = [236, 136]
        self.rect.left += x
        self.rect.top += y
        self.duration = duration
        counter = 0
        rate = 10
        for counter in range(len(xpointlist)-1):
            dx = x - xpointlist[counter]
            dy = y - ypointlist[counter]
            distance = sqrt(dx*dx + dy*dy)
            return distance
        duration = distance/rate

    def draw(self):
        pygame.draw.ellipse(surface, self.color, self.rect, 0)

class projectWin:
    def __init__(self, width, height, caption):
        self.caption = caption
        pygame.display.set_caption(self.caption)
        self.width = width
        self.height = height
        self.placeHolders = []
        self.placeHolders.append(PlaceHolder(surface, 1, 236, 10, 10, GREEN))
        #placeHolder1 = PlaceHolder(surface, 10, 236, 10, 10, GREEN)

    def grid(self):
        for i in range(24, 499, 25): #draw the vertical lines
            pygame.draw.line(surface, WHITE, (i, 0), (i, 499), 1)
        for j in range(24, 499, 25): #draw the horizontal lines
        pygame.draw.line(surface, WHITE, (0, j), (499, j), 1)

    def path(self):
        #the two lines are separated for ease of change
        #line 1
        pygame.draw.line(surface, GREEN, (0, 224), (49, 224), 2)
        pygame.draw.line(surface, GREEN, (49, 224), (49, 149), 2)
        pygame.draw.line(surface, GREEN, (49, 149), (349, 149), 2)
        pygame.draw.line(surface, GREEN, (349, 149), (349, 274), 2)
        pygame.draw.line(surface, GREEN, (349, 274), (99, 274), 2)
        pygame.draw.line(surface, GREEN, (99, 274), (99, 349), 2)
        pygame.draw.line(surface, GREEN, (99, 349), (374, 349), 2)
        pygame.draw.line(surface, GREEN, (374, 349), (374, 224), 2)
        pygame.draw.line(surface, GREEN, (374, 224), (499, 224), 2)

        #line 2
        pygame.draw.line(surface, GREEN, (0, 249), (74, 249), 2)
        pygame.draw.line(surface, GREEN, (74, 249), (74, 174), 2)
        pygame.draw.line(surface, GREEN, (74, 174), (324, 174), 2)
        pygame.draw.line(surface, GREEN, (324, 174), (324, 249), 2)
        pygame.draw.line(surface, GREEN, (324, 249), (75, 249), 2)
        pygame.draw.line(surface, GREEN, (74, 249), (74, 374), 2)
        pygame.draw.line(surface, GREEN, (74, 374), (399, 374), 2)
        pygame.draw.line(surface, GREEN, (399, 374), (399, 249), 2)
        pygame.draw.line(surface, GREEN, (399, 249), (499, 249), 2)

    def base(self): #what the player has to protect
        base = pygame.Rect(474, 187, 99, 99)
        pygame.draw.ellipse(surface, BLUE, base, 0)

pygame.init()

win = projectWin(500, 500, 'Project 2 v1.00')
Quit = False
while not Quit:
    surface.fill(BLACK)
    #draw the grid, path, and base on the screen
    win.grid()
    win.path()
    win.base()
    for i in win.placeHolders: #draw and move the placeholder enemy assets
        i.move(59, 0, duration)
        i.move(0, -100, duration)
        i.draw()
    pygame.display.update()
    time.sleep(0.02)
    for event in pygame.event.get():
        if event.type == QUIT:
            Quit = True

pygame.quit() #always exit cleanly
sys.exit()

1 个答案:

答案 0 :(得分:0)

如果您提供一些代码,我可以更好地回答我的问题,但我可以给您一些见解:

在您尝试制作的游戏中,您可以进行以下操作:

xdirection = 1
ydirection = 1

你可以将方向乘以控制精灵的方向。您希望它们越快,方向数应该越大。您可以使用正面和负面来控制它们将采用的方式。

同样,这种反应完全是推测性的,因为你没有显示任何代码。如果这没有用,请告诉我,我会尝试相应地更改我的回复。