本文整理汇总了Python中simulation.Simulation.getState方法的典型用法代码示例。如果您正苦于以下问题:Python Simulation.getState方法的具体用法?Python Simulation.getState怎么用?Python Simulation.getState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类simulation.Simulation
的用法示例。
在下文中一共展示了Simulation.getState方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
# 需要导入模块: from simulation import Simulation [as 别名]
# 或者: from simulation.Simulation import getState [as 别名]
class Game(object):
"""Represents the game part of the test bed.
This part contains code for displaying the state of the simulation in a way
that makes it appear like a playable game.
"""
# Pixels Per (simulation) Unit
PPU = 30.0
# Game ticks per sim tick
TICKFACTOR = 1.0
CAT_IMG = 'nyan.png'
DOG_IMG = 'dog.png'
GOAL_IMG = 'bow.png'
DEBUG = True
def __init__(self):
"""
"""
self.input = InputState()
self.logger = game.datalogger.GameDataLogger()
self.cat_ais = [ai.random_ai, ai.exit_achiever, ai.potential_field_cat]
self.current_ai = 0
self.simulationInit()
self.screen = pygame.display.set_mode((int(math.ceil(self.field[0]*self.PPU)),
int(math.ceil(self.field[1]*self.PPU))))
pygame.display.set_caption('Dead End')
bg = pygame.Surface(self.screen.get_size())
self.background = bg.convert()
self.background.fill((200,200,200))
self.font = pygame.font.Font(None, 35)
self.subfont = pygame.font.Font(None, 20)
self.clock = pygame.time.Clock()
self.wins = 0
self.losses = 0
self.interest = 0.
self.entityInit()
def simulationInit(self):
cat_ai = self.cat_ais[self.current_ai]
self.current_ai = (self.current_ai + 1) % len(self.cat_ais)
self.simulation = Simulation(cat_ai, ai.f, num_dogs=4)
self.simstate = self.simulation.getState()
self.field = self.simulation.getFieldSize()
self.tickcount = 0
self.run = True
self.gameover = False
self.logger.gameStarted(self.field)
def entityInit(self):
self.cat = GameEntity(self.CAT_IMG,
self._simToGamePosition(self.simstate["cat"]))
self.dogs = []
for dog in self.simstate["dogs"]:
self.dogs.append(GameEntity(self.DOG_IMG, self._simToGamePosition(dog)))
self.goal = GameEntity(self.GOAL_IMG,
self._simToGamePosition(self.simstate["goal"]))
def _simToGamePosition(self, position):
"""Convert a simulation position to a game position
Arguments:
- `position`:
"""
return (position[0]*self.PPU, position[1]*self.PPU)
def mainLoop(self, ):
"""The main loop of the game.
"""
while self.run:
self.handleEvents()
self.updateGameState()
self.clock.tick(60)
self.draw()
def draw_text(self, text, x, y):
text_s = self.subfont.render(text, True, (255,255,255))
text_r = text_s.get_rect()
text_r.x = x
text_r.y = y
self.screen.blit(text_s, text_r)
return text_r.bottom
def draw(self):
"""
"""
self.screen.blit(self.background, (0,0))
self.screen.blit(self.goal.image, self.goal.getRect())
#.........这里部分代码省略.........
示例2: TestSimulation
# 需要导入模块: from simulation import Simulation [as 别名]
# 或者: from simulation.Simulation import getState [as 别名]
class TestSimulation(unittest.TestCase):
"""Tests for the Simulation class
"""
class mock_ai(object):
"""A mock AI callable for testing
"""
def __init__(self, ):
self.calls = 0
self.cat = []
self.goal = []
self.dogs = []
def __call__(self, current, cat, dogs, goal):
"""AI call. Stores data about the call and returns a default direction.
Arguments:
- `cat`: The cat position
- `dogs`: The dog positions
- `goal`: The goal position
"""
self.dogs.append(dogs)
self.cat.append(cat)
self.goal.append(goal)
self.calls += 1
return "left"
def setUp(self, ):
"""Set up a default starting state
"""
self.cat_ai = self.mock_ai()
self.dog_ai = self.mock_ai()
self.DOGS = 5
self.FIELDSIZE = (16,16)
self.sim = Simulation(self.cat_ai, self.dog_ai,
field_size=self.FIELDSIZE, num_dogs=self.DOGS)
def _all_in_stage(self, state):
self.assertTrue(self._in_stage(state["cat"], self.sim.CAT_SIZE))
for dog in state["dogs"]:
self.assertTrue(self._in_stage(dog, (self.sim.DOG_RADIUS*2,
self.sim.DOG_RADIUS*2)))
self.assertTrue(self._in_stage(state["goal"], self.sim.GOAL_SIZE))
def _in_stage(self, x, size):
return (x[0]-(size[0]*0.5) >= 0 and
x[0]+(size[0]*0.5) <= self.FIELDSIZE[0] and
x[1]-(size[1]*0.5) >= 0 and
x[1]+(size[1]*0.5) <= self.FIELDSIZE[1])
def testGetState(self, ):
"""Tests the getState function.
Checks that the correct members exist in the state, and that they are of
correct type.
"""
state = self.sim.getState()
self.assertItemsEqual(state.keys(), ["cat", "dogs", "gameover", "win", "updated", "goal"])
self.assertTrue(isinstance(state["cat"], tuple))
self.assertTrue(isinstance(state["dogs"], list))
for dog in state["dogs"]:
self.assertTrue(isinstance(dog, tuple))
self.assertTrue(isinstance(state["goal"], tuple))
self.assertTrue(isinstance(state["gameover"], bool))
self.assertTrue(isinstance(state["win"], bool))
self.assertTrue(isinstance(state["updated"], datetime))
def testInit(self, ):
"""Test that the simulation is initialized correctly
"""
state = self.sim.getState()
self.assertEqual(len(state["dogs"]), self.DOGS)
self.assertEqual(state["gameover"], False)
self.assertEqual(state["win"], False)
self._all_in_stage(state)
def testSimtickWin(self, ):
"""Test the simtick method for a winning move
"""
# Move cat to the right of the goal (so the ai moving left will win)
goalright = self.sim._goal.getPosition()[0]+self.sim.GOAL_SIZE[0]*0.5
self.sim._cat.setPosition([goalright+self.sim.CAT_SIZE[0]*0.5,0])
old_state = self.sim.getState()
new_state = self.sim.simtick()
self.assertGreater(new_state["updated"], old_state["updated"])
self.assertTrue(new_state["gameover"])
self.assertTrue(new_state["win"])
# Make sure it stops ticking after game is over
done_state = self.sim.simtick()
self.assertEqual(new_state, done_state)
def testSimtickLose(self, ):
#.........这里部分代码省略.........