本文整理汇总了Python中grid.Grid.update方法的典型用法代码示例。如果您正苦于以下问题:Python Grid.update方法的具体用法?Python Grid.update怎么用?Python Grid.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grid.Grid
的用法示例。
在下文中一共展示了Grid.update方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import update [as 别名]
def main(stdscr):
cursor = Cursor(stdscr)
url = utils.ip + "token/get"
token = utils.request(url)['token']
grid = Grid(stdscr, x_limit, y_limit, cursor)
sel_bool = False
sel_soldiers = []
while True:
key = stdscr.getch()
# quit game
if key == ord('q'):
exit(stdscr)
# create soldier
elif key == ord('c'):
create_soldier(token)
# move cursor
elif key in directions:
cursor.move_cursor(key)
# select tiles
elif key == ord('s') and not sel_bool:
cursor.select()
key = 0
sel_bool = True
sel_soldiers = []
# finish selecting
elif key is ord('s') and sel_bool:
cursor.deselect()
x_r = sorted((cursor.select_coords[0], cursor.x))
y_r = sorted((cursor.select_coords[1], cursor.y))
debug_file.write(str(x_r) + "\n" + str(y_r) + "\n")
for x in range(x_r[0], x_r[1]+1):
for y in range(y_r[0], y_r[1]+1):
if (x,y) in grid.grid:
debug_file.write("inserting")
sel_soldiers.append(grid.grid[(x,y)])
sel_bool = False
if key is ord('q'):
exit(stdscr)
# move soldiers (soldiers must be selected first)
elif key == ord('m'):
key = 69
debug_file.write(str(sel_soldiers))
if sel_soldiers:
debug_file.write("moving soldiers\n")
move_soldiers(cursor.position(), sel_soldiers)
sel_bool = False
elif key == ord('d'):
raise Exception(grid.request())
grid.debug(str(key))
grid.update(key, sel_bool)
grid.display()
示例2: __init__
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import update [as 别名]
class GameOfLife:
def __init__(self, size):
self._grid = Grid(size)
def get_board(self):
self._grid.update()
return self._grid.get_data()
示例3: Gameplay
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import update [as 别名]
class Gameplay(GameState):
def __init__(self):
super(Gameplay, self).__init__()
self.grid = Grid()
self.sim_speed = 45
self.timer = 0
def startup(self, persistent):
self.persist = persistent
def get_event(self,event):
if event.type == pg.QUIT:
self.quit = True
elif event.type == pg.KEYUP:
if event.key == pg.K_ESCAPE:
self.quit = True
def update(self, dt):
self.timer += dt
while self.timer > self.sim_speed:
self.grid.update(self.sim_speed)
self.timer -= self.sim_speed
def draw(self, surface):
self.grid.draw(surface)
示例4: Agent
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import update [as 别名]
class Agent(object):
"""Class handles all command and control logic for a teams tanks."""
def __init__(self, bzrc):
self.bzrc = bzrc
self.constants = self.bzrc.get_constants()
self.commands = []
mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
self.numoftanks = len(mytanks)
self.timeuntilshot = [0]*self.numoftanks
self.ismoving = [True]*self.numoftanks
# Create Grid of points
truepositive = float(self.constants['truepositive'])
truenegative = float(self.constants['truenegative'])
self.grid = Grid(800, 800, truepositive, truenegative)
self.grid.init_window(800, 800)
# For PD Control
self.old_angle = [0]*self.numoftanks
self.old_speed = [0]*self.numoftanks
# For graph
self.final_goals = [0]*self.numoftanks
self.graph = OccupancyGraph(self.grid)
self.current_goals = [None]*self.numoftanks
self.goal_paths = []
for i in xrange(self.numoftanks):
self.goal_paths.append(Path())
def tick(self, time_diff):
"""Some time has passed; decide what to do next."""
mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
self.mytanks = mytanks
self.commands = []
for tank in mytanks:
occGrid = self.bzrc.get_occgrid(tank.index)
# update the grid
self.grid.update(occGrid)
# update the graph
self.graph.updateGraph(occGrid[0][0], occGrid[0][1], len(occGrid[1]), len(occGrid[1][0]))
# move
self.create_command(tank, time_diff)
self.grid.draw_grid()
results = self.bzrc.do_commands(self.commands)
def attack_enemies(self, tank):
"""Find the closest enemy and chase it, shooting as you go."""
best_enemy = None
best_dist = 2 * float(self.constants['worldsize'])
for enemy in self.enemies:
if enemy.status != 'alive':
continue
dist = math.sqrt((enemy.x - tank.x)**2 + (enemy.y - tank.y)**2)
if dist < best_dist:
best_dist = dist
best_enemy = enemy
if best_enemy is None:
command = Command(tank.index, 0, 0, False)
self.commands.append(command)
else:
self.move_to_position(tank, best_enemy.x, best_enemy.y)
def move_to_position(self, tank, target_x, target_y):
"""Set command to move to given coordinates."""
target_angle = math.atan2(target_y - tank.y,
target_x - tank.x)
relative_angle = self.normalize_angle(target_angle - tank.angle)
command = Command(tank.index, 1, 2 * relative_angle, True)
self.commands.append(command)
def create_command(self, tank, time_diff):
"""Set command to move to given coordinates."""
speed = 1
angvel = 0
shoot = True
kp = 1
kd = -.07
goal_threshold = 15.0
goal_timer_threshold = 15
if time_diff <= 0:
return
# see if we need a new visibility graph and path
goal = self.goal_paths[tank.index].get_next(tank.x, tank.y, goal_threshold)
if (self.current_goals[tank.index] == None):
self.current_goals[tank.index] = (goal, 0.0)
current_goal = self.current_goals[tank.index][0]
#.........这里部分代码省略.........
示例5: Grid
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import update [as 别名]
pygame.init()
screen = pygame.display.set_mode((640, 640))
pygame.display.set_caption("Game of Life")
fps_clock = pygame.time.Clock()
grid = Grid()
running = False
start_t = time.time()
while True:
# handle events
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
grid.handle_click(pygame.mouse.get_pos())
elif event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if running:
grid.update()
elif time.time() - start_t > 10:
running = True
screen.fill((255, 255, 255))
grid.draw(screen)
pygame.display.update()
fps_clock.tick(50)
示例6: Game
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import update [as 别名]
class Game():
def __init__(self):
#window setup
pygame.display.set_caption('A* Visual')
# initiate the clock and screen
self.clock = pygame.time.Clock()
self.last_tick = pygame.time.get_ticks()
self.screen_res = [750, 495]
self.font = pygame.font.SysFont("Calibri", 16)
self.screen = pygame.display.set_mode(self.screen_res, pygame.HWSURFACE, 32)
self.pathing = False
self.pathing_type = '*'
self.show_checked = False
self.grid = Grid(self)
while 1:
self.Loop()
def Run(self):
self.pathing = True
self.grid.clearPath()
node_array = self.Convert()
path, check = astar(node_array, (0,0), (29, 49), self.pathing_type)
for pos in check:
self.grid.nodes[pos[0]][pos[1]].checked = True
if path != False:
for pos in path:
self.grid.nodes[pos[0]][pos[1]].in_path = True
else:
pass
print len(path)
self.pathing = False
def Clear(self):
self.grid = Grid(self)
def Convert(self):
array = [[self.grid.nodes[col][row].solid for row in xrange(self.grid.length)] for col in xrange(self.grid.width)]
nodes = numpy.array(array)
return nodes
def blitInfo(self):
text = self.font.render("Press Enter to find path, press Space to clear board", 1, (255,255,255))
text2 = self.font.render("Press c to toggle checked nodes, and 1 and 2 to switch pathing types", 1, (255,255,255))
check = self.font.render("Checked nodes: " + str(self.show_checked), 1, (255,255,255))
ptype = self.font.render("Pathing type: " + self.pathing_type, 1, (255,255,255))
self.screen.blit(text, (5, 5))
self.screen.blit(text2, (5, 25))
self.screen.blit(check, (500, 5))
self.screen.blit(ptype, (500, 25))
def Loop(self):
# main game loop
self.eventLoop()
self.Tick()
self.Draw()
pygame.display.update()
def eventLoop(self):
# the main event loop, detects keypresses
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RETURN:
self.Run()
if event.key == K_SPACE:
self.Clear()
if event.key == K_1:
self.pathing_type = '+'
if event.key == K_2:
self.pathing_type = '*'
if event.key == K_c:
print self.show_checked
if self.show_checked:
self.show_checked = False
else:
self.show_checked = True
def Tick(self):
# updates to player location and animation frame
self.ttime = self.clock.tick()
self.mpos = pygame.mouse.get_pos()
def Draw(self):
#.........这里部分代码省略.........
示例7: Agent
# 需要导入模块: from grid import Grid [as 别名]
# 或者: from grid.Grid import update [as 别名]
class Agent(object):
"""Class handles all command and control logic for a teams tanks."""
def __init__(self, bzrc):
self.bzrc = bzrc
self.constants = self.bzrc.get_constants()
self.commands = []
mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
self.numoftanks = len(mytanks)
self.timeuntilshot = [0]*self.numoftanks
self.ismoving = [True]*self.numoftanks
# Create Grid of points
truepositive = float(self.constants['truepositive'])
truenegative = float(self.constants['truenegative'])
self.grid = Grid(800, 800, truepositive, truenegative)
self.grid.init_window(800, 800)
# For PD Control
self.old_angle = [0]*self.numoftanks
self.old_speed = [0]*self.numoftanks
self.sample_radius = 100
self.k = 0.1
self.field = PotentialFieldsCalculator(self.grid, self.sample_radius, 80.0, 0.0, 0.3, 0.45)
def tick(self, time_diff):
"""Some time has passed; decide what to do next."""
mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
self.mytanks = mytanks
self.commands = []
for tank in mytanks:
# update the grid
self.grid.update(self.bzrc.get_occgrid(tank.index))
# move
self.create_command(tank, time_diff)
self.grid.draw_grid()
results = self.bzrc.do_commands(self.commands)
def create_command(self, tank, time_diff):
"""Set command to move to given coordinates."""
#pprint (vars(tank))
speed = 1
angvel = 0
shoot = True
kp = 1
kd = -.07
if time_diff <= 0:
return
# get potential field at this location and add to enemy coords
x, y = self.field.calculate_potential(tank.x, tank.y)
# PD Controller - angle
target_angle = math.atan2(y, x)
angle_remaining = self.angle_remaining(tank.angle, target_angle)
angvel = self.pd_angvel(tank, target_angle, time_diff)
if angvel > 1:
angvel = 1
elif angvel < -1:
angvel = -1
if abs(angle_remaining) <= math.pi / 6:
speed = self.pd_speed(tank, x, y, time_diff)
else:
speed = 1 - abs(angle_remaining / math.pi)
if speed > 1:
speed = 1.0
elif speed < -1:
speed = -1.0
#print "speed", speed
#self.printSurrounding(tank.x, tank.y, 3)
command = Command(tank.index, speed, angvel, shoot)
self.commands.append(command)
self.old_angle[tank.index] = tank.angle
def printSurrounding(self, x, y, radius):
print ':::New Grid:::', x, y
surroundingGrid = self.grid.getSurroundings(x, y, radius)
for x in range(len(surroundingGrid)):
print surroundingGrid[x]
def pd_angvel(self, tank, target_angle, time_diff):
"""PD Controller for the angular velocity of the tank."""
kp = 1.0
#.........这里部分代码省略.........