[英]Pygame: Making a sprite move along a predetermined path at a constant speed
[英]Why does my sprite move at a constant speed
我正在編寫一個涉及與蛇類似的機制的游戲,但不同之處在於玩家應該在移動時成長,而不是在他們到達蘋果時。 我的問題是,當游戲開始時,由於我使用蛇代碼作為參考,當我希望它只從起點開始增長時,玩家會從屏幕的后面增長到屏幕的邊緣。 我的想法是它應該檢查起始空間中是否有一個塊,如果有它應該通過,如果沒有則添加一個立方體。
這是添加多維數據集的代碼:
def addCube(self):
tail = self.body[-1]
dx, dy = tail.dirnx, tail.dirny
# check what direction we are currently moving in to determine if we need to add the cube to the left, right, above or below.
if dx == 1 and dy == 0:
self.body.append(cube((tail.pos[0] - 1, tail.pos[1])))
elif dx == -1 and dy == 0:
self.body.append(cube((tail.pos[0] + 1, tail.pos[1])))
elif dx == 0 and dy == 1:
self.body.append(cube((tail.pos[0], tail.pos[1] - 1)))
elif dx == 0 and dy == -1:
self.body.append(cube((tail.pos[0], tail.pos[1] + 1)))
self.body[-1].dirnx = dx
self.body[-1].dirny = dy
這是主 function 內的一個循環,它調用添加立方體 function
flag = True
# STARTING MAIN LOOP
loop = 0
while flag:
#pygame.time.delay(10) # This will delay the game so it doesn't run too quickly
clock.tick(60) # Will ensure our game runs at 10 FPS
s.move()
if loop >= 1:
s.addCube()
loop += 1
for x in range(len(s.body)):
if s.body[x].pos in list(map(lambda z: z.pos, s.body[
x + 1:])): # This will check if any of the positions in our body list overlap
print('Score: ', len(s.body))
s.reset((10, 10))
break
如果你想讓玩家從起點開始成長,改變
if dx == 1 and dy == 0:
self.body.append(cube((tail.pos[0] - 1, tail.pos[1])))
elif dx == -1 and dy == 0:
self.body.append(cube((tail.pos[0] + 1, tail.pos[1])))
elif dx == 0 and dy == 1:
self.body.append(cube((tail.pos[0], tail.pos[1] - 1)))
elif dx == 0 and dy == -1:
self.body.append(cube((tail.pos[0], tail.pos[1] + 1)))
只是
self.body.append(cube((tail.pos[0], tail.pos[1])))
所以它長在尾巴的頂部所以當尾巴的rest移動時,它會停留在那里然后移動,就像在蛇中一樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.