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()
瑞安·萨克斯(Ryan Saxe)

如果您提供一些代码,我会更好地回答,但我可以为您提供一些见解:

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

xdirection = 1
ydirection = 1

您可以将方向乘以方向,以控制精灵。您希望他们走得越快,方向编号应该越大。您可以使用正负来控制它们的前进方向。

同样,此响应完全是推测性的,因为您未显示任何代码。如果这没有帮助,请告诉我,我将尝试并相应地更改我的回复。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何保持精灵速度恒定?

来自分类Dev

精灵动画的移动速度错误

来自分类Dev

定义iTween路径时的恒定速度问题

来自分类Dev

如何以恒定的速度移动具有动态物理物体的精灵

来自分类Dev

沿着Phaser.IO中的预定义路径移动精灵

来自分类Dev

Qt沿着圆形路径移动文本

来自分类Dev

设置角色控制器以恒定速度移动

来自分类Dev

无法让精灵在圆形路径中移动

来自分类Dev

在颤动中沿着弯曲的路径移动

来自分类Dev

在Python中使用Pygame移动精灵

来自分类Dev

在Pygame中难以让精灵移动

来自分类Dev

pygame中移动的精灵之间的碰撞

来自分类Dev

pygame改变blit的移动速度

来自分类Dev

我无法在pygame中移动精灵

来自分类Dev

按下键时Pygame精灵不移动

来自分类Dev

在pygame中移动精灵时不会闪烁

来自分类Dev

cd沿着标准路径

来自分类Dev

在pygame中前后移动精灵

来自分类Dev

如何沿着简单的路径移动相机

来自分类Dev

如何保持精灵速度恒定?

来自分类Dev

Pygame-以不同速度移动精灵

来自分类Dev

画布沿着路径绘制

来自分类Dev

弯曲路径恒定速度和终点

来自分类Dev

使用Java图形沿着弧形路径移动对象

来自分类Dev

在PyGame中移动精灵

来自分类Dev

试图在pygame中移动精灵

来自分类Dev

在pygame中使用功能移动精灵

来自分类Dev

最大移动速度精灵 PIXI.js

来自分类Dev

Pygame 精灵创建重复而不是移动