def update(self, elapsedTimeSec): Actor.update(self, elapsedTimeSec) # Handle mouse rotation mousePos = pygame.mouse.get_pos() angleToMouse = (Vec2d(mousePos) - self.getPosition()).get_angle() - 90 self._rotate(angleToMouse) # Handle keyboard movement pressedKeys = pygame.key.get_pressed() self._velocity = Vec2d(0, 0) if pressedKeys[K_w]: self._velocity.y -= 1.0 if pressedKeys[K_s]: self._velocity.y += 1.0 if pressedKeys[K_a]: self._velocity.x -= 1.0 if pressedKeys[K_d]: self._velocity.x += 1.0 self._velocity = self._truncate(self._velocity * self._maxVelocity, self._maxVelocity) self._updatePosition() # Handle firing laser if self._fireDurationSec > 0: self._laser.fire(self.rect.center, self._rotation) self._fireDurationSec -= elapsedTimeSec else: self._laser.stop()
def simulate(num_epochs:int): ''' runs our simulation with 2 actors :param num_epochs: the number of iterations we want to run :type num_epochs: int :return: None ''' data = {"actor1_money":[], "actor2_money":[], "actor1_choice":[], "actor2_choice":[]} player1 = Actor(avaliable_funds = 10, lower_threshhold=5, betrayal_rate=.03) player2 = Actor(avaliable_funds = 10, lower_threshhold=5, betrayal_rate=.90) for i in range(num_epochs): choice1 = player1.make_choice() choice2 = player2.make_choice() processChoice(choice1, choice2, player1, player2) data["actor1_money"].append(player1.avaliable_funds) data["actor2_money"].append(player2.avaliable_funds) data["actor1_choice"].append(choice1) data["actor2_choice"].append(choice2) player1.update() player2.update() df = pd.DataFrame(data) print(df) plt.plot(range(0,num_epochs), df['actor1_money']) plt.plot(range(0,num_epochs), df['actor2_money']) plt.show() plt.clf() counts1 = df.groupby(['actor1_choice'])['actor1_choice'].count() counts2 = df.groupby(['actor2_choice'])['actor2_choice'].count() print(counts2)
def update(self, elapsedTimeSec): Actor.update(self, elapsedTimeSec) # Handle mouse rotation mousePos = pygame.mouse.get_pos() angleToMouse = (Vec2d(mousePos) - self.getPosition()).get_angle() - 90 self._rotate(angleToMouse) # Handle keyboard movement pressedKeys = pygame.key.get_pressed() self._velocity = Vec2d(0, 0) if pressedKeys[K_w]: self._velocity.y -= 1.0 if pressedKeys[K_s]: self._velocity.y += 1.0 if pressedKeys[K_a]: self._velocity.x -= 1.0 if pressedKeys[K_d]: self._velocity.x += 1.0 self._velocity = self._truncate(self._velocity * self._maxVelocity, self._maxVelocity) self._updatePosition() # Handle firing laser if self._fireDurationSec > 0: self._laser.fire(self.rect.center, self._rotation) self._fireDurationSec -= elapsedTimeSec else: self._laser.stop()
def update(self, elapsedTimeSec): Actor.update(self, elapsedTimeSec) desiredVelocity = (self._target.getPosition() - self.getPosition()).normalized() desiredVelocity *= self._maxVelocity steering = desiredVelocity - self._velocity steering = self._truncate(steering, 8) steering = steering / 10 self._velocity = self._truncate(self._velocity + steering, self._maxVelocity) rotation = self._velocity.get_angle() - 90 self._rotate(rotation) self._updatePosition()
class Game: def __init__(self): self.renderer = None self.clock = None def setup(self): pygame.init() pygame.display.set_caption("Map Search Test - David Tyler (2013)") pygame.event.set_allowed([QUIT, KEYDOWN]) screen = pygame.display.set_mode((1280, 960)) # TODO: Config options self.clock = pygame.time.Clock() # self.tile_map = MapLoader().loadMap("../res/maps/map.txt") self.tile_map = MazeMapCreator.create_maze(48, 48) # self.tile_map = CityMapCreator.create_city(48, 48) self.actor = Actor(self.tile_map, pygame.time.get_ticks()) self.tile_screen_converter = TileScreenConverter(20, 20) self.renderer = Renderer(screen, self.tile_screen_converter) def run(self): if self.renderer != None and self.clock != None: while True: self.clock.tick(30) # TODO: Move event handling for event in pygame.event.get(): if event.type == QUIT: self.exit_game() elif event.type == MOUSEBUTTONDOWN and event.button == 1: x, y = self.tile_screen_converter.screen_to_tile(event.pos[0], event.pos[1]) self.actor.set_goal(x, y) self.actor.update(pygame.time.get_ticks()) self.renderer.draw_game(self.tile_map, self.actor) def exit_game(self, rc=0): pygame.quit() sys.exit(rc)