阿卡演员模式

时间:2015-06-05 19:21:24

标签: design-patterns akka actor

我最近问了一个关于Akka PoisonPill的问题,我收到的excellent answer提到了Akka“ actor pattern ”,称为“一次性演员”。

基本上,根据该用户的说法,一次性演员是一旦不再需要就被杀死(通过import pygame as pg import numpy as np from pygame.locals import * from random import * #Make a first "game" which is actually the menu res = (1000,800) screen = pg.display.set_mode(res) pg.display.set_caption('Rocket game') black = (0,0,0) white = (255,255,255) backgroundmenu = pg.image.load("menuscreen.jpg") pg.init() running1 = True while running1: pg.event.pump() keys = pg.key.get_pressed() if keys[pg.K_1]: running1 = False running2 = True elif keys[pg.K_2]: running1 = False running2 = False for event in pg.event.get(): if event.type == pg.QUIT: running1 = False screen.blit(backgroundmenu,(0,0)) pg.display.flip() pg.quit() pg.init() #Setup screen and define colors res = (1000,800) screen = pg.display.set_mode(res) pg.display.set_caption('Rocket game') #pg.image.load("space.jpg") black = (0,0,0) white = (255,255,255) background1 = pg.image.load("space.jpg").convert() background2 = pg.image.load("space.jpg").convert() background3 = pg.image.load("space.jpg").convert() #screen.blit(background1,(0,0)) x_back = 0 screenwidth = 1000 #Initialize variables and clock vx = 0 vy = 0 x = 200 y = 600 t0 = 0.001*pg.time.get_ticks() maxdt = 0.5 #Load rocket and asteroids rocketgif = pg.image.load("rocket.gif") rocketimg = pg.transform.scale(rocketgif, (100,100)) asteroidgif = pg.image.load("Asteroid.gif") asteroidimg = pg.transform.scale(asteroidgif, (75,75)) #Load music pg.mixer.music.load('gamesound.mp3') pg.mixer.music.set_endevent(pg.constants.USEREVENT) pg.mixer.music.play() #clock = pg.time.Clock() #Generate random asteroids Nasteroid = 5 x_ast = 999 dx = 5 i = 0 asteroidpos_y= [] while i<Nasteroid: y = randint(0,800) asteroidpos_y.append(y) i = i + 1 #Start game loop running2 = True while running2: t = 0.001*pg.time.get_ticks() dt = min(t-t0,maxdt) i = 0 pg.event.pump() while i<Nasteroid: x_ast -= dx y = y screen.blit(asteroidimg,(x_ast,asteroidpos_y[i-1])) i = i + 1 for event in pg.event.get(): if event.type == pg.QUIT: #Quit using red cross button running2 = False elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE: running2 = False #Quit using key combination elif event.type == pg.KEYDOWN: #Make user control rocket movement #and stop if key is released. if event.key == pg.K_LEFT: vx = -8 elif event.key == pg.K_RIGHT: vx = 8 elif event.key == pg.K_UP: vy = -8 elif event.key == pg.K_DOWN: vy = 8 elif event.type == pg.KEYUP: if event.key == pg.K_LEFT: vx = 0 elif event.key == pg.K_RIGHT: vx = 0 elif event.key == pg.K_UP: vy = 0 elif event.key == pg.K_DOWN: vy = 0 elif event.type == pg.constants.USEREVENT: pg.mixer.music.load("gamesound.mp3") pg.mixer.music.play() #Make the screen scroll behind the rocket to make it "move" (3x the same background behind eachother) screen.blit(background1,(x_back,0)) screen.blit(background2,(x_back - screenwidth,0)) screen.blit(background3,(x_back - (2*screenwidth),0)) x_back = x_back - 1 if x_back == screenwidth: x_back = 0 #Update the position of the rocket. If the rocket hits the edges the game stops. x = x + vx y = y + vy """if x <= 0 or x >=100: running2 = False if y <= 0 or y >=800: running2 = False""" pg.event.pump() screen.blit(rocketimg,(x,y)) pg.display.flip() pg.quit() ),因此将资源释放回演员系统。

我用Google搜索了“Akka演员模式”,“Akka一次性演员”等等,并没有发现任何已知演员模式集合的迹象。

所以我问:这个“一次性演员模式”是本土的东西,还是实际上有一套已知的Akka演员模式?如果是后者,他们在哪里记录?

2 个答案:

答案 0 :(得分:1)

有一些很棒的书可以解释一些有用的模式,例如Akka Concurrency或Effective Akka以及Akka in Action(以及其他)。我们目前还在编写反应式设计模式,这也非常适用于演员。

答案 1 :(得分:0)

Akka Concurrency的作者

Derek Wyatt Let it crash 上撰写了一篇关于“Akka 2中的关机模式”的精彩博客文章,他还描述了inside main 0 0 模式 1

1 模式不在{{3>}中设计模式:可重用面向对象软件的元素中的严格定义中。