因此,我使用了第一个答案here中稍微修改过的python脚本版本,并在这里添加了一个名为“SpeechBlock”的类:
level = [
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPSPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",]
# build the level
for row in level:
for col in row:
if col == "P":
p = Platform(x, y)
platforms.append(p)
entities.add(p)
if col == "E":
e = ExitBlock(x, y)
platforms.append(e)
entities.add(e)
if col == "S":
s = SpeechBlock(x, y)
platforms.append(s)
entities.add(s
让它成为一门课:
class SpeechBlock(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image.fill(Color("#0033FF"))
self.x=x
self.y=y
def speak(self):
self.events = [
"test",
]
for row in self.events:
image=pygame.image.load(row+".png")
screen.blit(image, (self.x,self.y))
脚本已经有了一个用于冲突的方法,我在这里添加了最后3行:
ef collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, ExitBlock):
pygame.event.post(pygame.event.Event(QUIT))
if xvel > 0:
self.rect.right = p.rect.left
print("collide right")
if xvel < 0:
self.rect.left = p.rect.right
print ("collide left")
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
if isinstance(p, SpeechBlock):
SpeechBlock.speak(self)
pygame.display.update()
我想要并且仍然想要实现的是,当玩家站在演讲块上时,一个演讲泡泡的图像会闪现在屏幕上。取而代之的是,当玩家站在演讲块的边缘时,图像会出现一小会儿,然后消失,然后再出现一小会儿,以此类推。。。 我做错了什么?我是pygame的新手,所以我不太了解blitting和显示更新/翻转的工作原理。任何帮助都将不胜感激。你知道吗
你必须知道你的游戏是循环的。这个循环的每次迭代称为一个帧。在每一帧中,你清理屏幕上的所有内容,处理所有事件,更新游戏世界,再次绘制所有内容。你知道吗
因此,当您调用
SpeechBlock.speak(self)
和pygame.display.update()
时,您将图像(从speak
方法)绘制到屏幕上,但它们将在下一帧中被删除。你知道吗每个帧不应调用
太长了,读不下去了,下面是DR例子:pygame.display.update()
多次。你应该做的是引入一些新的状态来改变你触摸它时SpeechBlock
的行为。你知道吗在这里您可以看到,我们创建了一个新的
Entity
,当播放器进入SpeechBlock
时显示文本。两秒钟后,SpeechBlock
调用kill
将其从游戏中删除。你知道吗我还更新了一点代码,使用delta时间,这样我们就可以很容易地检查是否已经过了2秒。你知道吗
相关问题 更多 >
编程相关推荐