有没有办法使用class lbuild(pygame.sprite.Sprite):
#This class represents alevel builder. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height,x,y):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
# Draw the car (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
self.rect.x=x
self.rect.y=y
all_sprites_list = pygame.sprite.Group()
movblock=pygame.sprite.Group()#sprite group
def level1():
global all_sprites_list
global movblock
xpos=0
for x in range(50):
all_sprites_list.add(lbuild(GREY,20,20,xpos,680))
xpos =xpos+20
ypos=660
xpos2 =40
for x in range(2):
all_sprites_list.add(lbuild(black,60,20,xpos2,ypos))
ypos=ypos-20
mblk=lbuild(RED,100,20,120,600)#draws the block
movblock.add(mblk)#adds it to the sprite group
clock=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
#Game Logic
all_sprites_list.update()
#Drawing on Screen
screen.fill(WHITE)
#Draw The Road
spd=5
if mblk.rect.x>200:#supposed to cheak if the block x postion a has reached 200 and the reverse its direction but instead it looks like it is vibrating
spd= -spd
if mblk.rect.x<100:
spd= -spd
mblk.rect.x+=spd
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
all_sprites_list.draw(screen)
movblock.draw(screen)
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(60)
获取标签中的所有chrome.tabs.executeScript
?
我尝试frameId
然后让代码向背景发送消息,然后将消息发送回executeScript()
的内容,但它是异步的。并且,frameId
需要同步返回。
答案 0 :(得分:2)
是的,您可以使用chrome.tabs.executeScript()
(MDN)获取标签的frameId列表。听起来您已经确定了,这样做有点令人费解。您可以使用带有chrome.tabs.executeScript()
选项的allFrames:true
将内容脚本注入选项卡中的所有框架。然后,您的内容脚本需要使用chrome.runtime.sendMessage()
(MDN)将消息发送回后台上下文。然后,frameId
将作为对象的属性作为第二个参数(sender
(MDN)传递给您的chrome.runtime.onMessage
({{3}听众。您需要自己累积列表。
chrome.webNavigation.getAllFrames()
)正如评论中提到的wOxxOm,从MDN(chrome.webNavigation.getAllFrames()
)获取它会容易得多。
无法使用同步接口获取选项卡的frameId列表。正如wOxxOm所提到的,唯一的方法是在需要之前获取信息。如果您需要拨打chrome.tabs.executeScript()
,则可以通过chrome.tabs.executeScript()
的回调拨打chrome.webNavigation.getAllFrames()
。