我正在处理动作,我已经让红色块跟随白色块,当白色块改变方向时,白色块就是玩家,我让红色块也改变方向,有没有办法让它更平滑,而不是传送尾巴朝向玩家的方向? 视频链接
如果玩家向下移动尾巴的 x 和 y 变化,那么红尾巴如何根据白色块移动,向上、向左、向右也是如此,我想要一种更好的方法来做到这一点,而不需要立即传送到那些位置就像顺利移动到那里
if down:
#-- the tail red change directions
tail.y = player1.y - 80
tail.x = player1.x
if up:
#-- the tail red change directions
tail.y = player1.y + 80
tail.x = player1.x
if left:
#-- the tail red change directions
tail.y = player1.y
tail.x = player1.x + 80
if right:
#-- the tail red change directions
tail.y = player1.y
tail.x = player1.x - 80
我的完整代码
import pygame
pygame.init()
# draw the iwndow
width = 500
height = 750
window = pygame.display.set_mode((height,width))
# the player class
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.speed = 5
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# display the player
color = (255,255,255)
player1 = player(180,190,40,40,color)
# bg
bg = pygame.image.load('2.png')
# the colision to determine the direction the player should move
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.speed = .9
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)
# display the player
color = (255,255,255)
tailcolor = (176, 58, 46)
player1 = player(180,170,50,50,color)
tail = player(100,170,50,50,tailcolor)
def draw():
window.fill((0,0,0))
window.blit(bg,(0,0))
player1.draw()
tail.draw()
# make movements True
right = False
left = False
up = False
down = False
only_up = False
only_left = False
# get position
pos = True
pos2 = 0
# the main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# check for key press
keys = pygame.key.get_pressed()
if down:
#-- the tail red change directions
tail.y = player1.y - 80
tail.x = player1.x
if up:
#-- the tail red change directions
tail.y = player1.y + 80
tail.x = player1.x
if left:
#-- the tail red change directions
tail.y = player1.y
tail.x = player1.x + 80
if right:
#-- the tail red change directions
tail.y = player1.y
tail.x = player1.x - 80
#--------------------------------------------------- player movement
if left == False:
if keys[pygame.K_RIGHT]:
if not keys[pygame.K_UP]:
if not keys[pygame.K_DOWN]:
right = True
if right:
left = False
down = False
up = False
player1.x += player1.speed
if right == False:
if keys[pygame.K_LEFT]:
if not keys[pygame.K_UP]:
if not keys[pygame.K_DOWN]:
left = True
if left:
right = False
down = False
up = False
player1.x -= player1.speed
if down == False:
if keys[pygame.K_UP]:
up = True
if up:
down = False
left = False
right = False
player1.y -= player1.speed
if up == False:
if keys[pygame.K_DOWN]:
down = True
if down:
up = False
left = False
right = False
player1.y += player1.speed
#--------------------------------------------------- player movement
# show everything drawn
draw()
pygame.display.update()
pygame.quit()
我认为你可以通过使用某种延迟来实现这一点。您想要根据“玩家的旧位置”获取尾巴的位置,而不是根据玩家当前的位置获取尾巴的位置。
此外,延迟应该是一个变量,您可以稍后随意更改,以防您想调整尾巴跟随玩家的距离。
这是我要尝试的:
tail_delay
的变量,并为其分配特定的时间。player1
的“当前位置”和时间戳(最终它将是位置的集合)。该对象,我们称之为 player1_positions_record
,将在无限循环中不断更新(这可能会浪费内存 - 不要保存所有位置,您只需要几秒钟,并且比您丢弃的时间更旧)。tail_delay
和player1_positions_record
指定尾部的位置。这样您就可以使用过去的位置 player1
。