簡體   English   中英

Pygame:讓精靈以恆定的速度沿着預定路徑移動

[英]Pygame: Making a sprite move along a predetermined path at a constant speed

我正在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()

如果您提供一些代碼,我會更好地回答,但我可以為您提供一些見解:

在您嘗試制作的游戲中,您可以進行以下操作:

xdirection = 1
ydirection = 1

您可以將方向乘以方向,以控制精靈。 您希望他們走得越快,方向編號應該越大。 您可以使用正負來控制它們的前進方向。

同樣,此響應完全是推測性的,因為您未顯示任何代碼。 如果這沒有幫助,請告訴我,我將嘗試並相應地更改我的回復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM