def scanner(): global monster, player if player.hp < 1: print("You Died!") t.sleep(1) ter=input(c.yellow+"Do you want to keep playing, or quit? (1), (2)"+c.reset+" >>>"+c.violet).strip() if ter == '1': tower.tower() elif ter == '2': exit() else: tower.tower() elif monster.hp < 1: print(c.yellow+"You won!") cl.Player.gold+=100000 save.save_game() t.sleep(1) print("Blinding light begins too engulf you.") t.sleep(1.25) print(c.clear) print(c.reset+".") t.sleep(1) print(c.clear) print("..") t.sleep(1) print(c.clear) print("...") t.sleep(1) print(c.clear) print("You awake.") t.sleep(1.5) print(c.clear) credits.roll() exit()
def hub(): save.save_game() print(c.clear) print(c.yellow+"Welcome to the Pines!") hubquestion=input("Would you like to go to the field, shop, inn, or the old tower? (1), (2), (3), (4)"+c.reset+" >>>"+c.violet) if hubquestion=="1": print(c.yellow+"You decide to go to the field.") t.sleep(1.3) f.field() elif hubquestion=="2": print(c.yellow+"You decide to go to the shop.") t.sleep(1.3) shop() elif hubquestion=="3": print(c.yellow+"You decide to go to the inn.") t.sleep(1.3) inn() elif hubquestion=="4": print(c.yellow+"You take a lantern and travel to the old tower.") t.sleep(1.25) tower.tower() else: print("I don't understand...") t.sleep(1) hub()
def hub(): save.save_game() print(c.clear) print(c.yellow + "Welcome to the Pines!") hubquestion = input( "Would you like to go to the field, shop, inn, or the old tower? (1), (2), (3), (4)" + c.reset + " >>>" + c.violet) if hubquestion == "1": print(c.yellow + "You decide to go to the field.") t.sleep(1.3) f.field() elif hubquestion == "2": print(c.yellow + "You decide to go to the shop.") t.sleep(1.3) shop() elif hubquestion == "3": print(c.yellow + "You decide to go to the inn.") t.sleep(1.3) inn() elif hubquestion == "4": print(c.yellow + "You take a lantern and travel to the old tower.") t.sleep(1.25) tower.tower() else: print("I don't understand...") t.sleep(1) hub()
def placeTowerAt(self, x, y, towerType): x = int(x/51) y = int(y/51) if(self.checkIfCanPlace(x, y) == True): t = tower(towerType) #todo change t.pos(x, y) towers.append(t) self.tiles[x][y] = maptile.Tile("T", x,y) self.update(self.mySurface) return towerType
def tower_loss(): batch_x, batch_y = mnist.train.next_batch(batch_size=BATCH_SIZE) batch_x_shaped = tf.reshape(batch_x, [-1, 28, 28, 1]) ##None -> batch size can be any size, 784 -> flattened mnist image #x = tf.placeholder(np.float32, [10, 28, 28, 1]) ##10 output classes #y = tf.placeholder(np.float32, shape=[10, 10], name='y_logits') logits = tower.tower(batch_x_shaped) # Reuse variables for the next tower. losses = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=batch_y)) return losses
def run_cnn(): mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # Python optimisation variables learning_rate = 0.1 epochs = 10 batch_size = 50 global_step = tf.train.get_or_create_global_step() x = tf.placeholder(tf.float32, [None, 28, 28, 1], name='x') y = tf.placeholder(tf.float32, [None, 10], name='y') logits = tower.tower(x) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y)) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # add an optimiser train_op = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(loss) # setup the initialisation operator init_op = tf.global_variables_initializer() # setup recording variables # add a summary to store the accuracy tf.summary.scalar('accuracy', accuracy) merged = tf.summary.merge_all() writer = tf.summary.FileWriter('C:\\Users\\Andy\\PycharmProjects') config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1) with tf.Session(config=config) as sess: sess.run(init_op) for count in range(1000): batch_x, batch_y = mnist.train.next_batch(batch_size=batch_size) batch_n = np.reshape(batch_x, [-1, 28, 28, 1]) l, _ = sess.run([loss, train_op], feed_dict={ x: batch_n, y: batch_y }) if not count % 10: test_x, test_y = mnist.test.next_batch(batch_size=1000) test_n = np.reshape(test_x, [-1, 28, 28, 1]) lo, accu = sess.run([loss, accuracy], feed_dict={ x: test_n, y: test_y }) print('train loss is', l) print("loss is: ", lo) print("accuracy is: ", accu) print()
def scanner(): global monster, player if player.hp < 1: print("You Died!") t.sleep(1) ter = input(c.yellow + "Do you want to keep playing, or quit? (1), (2)" + c.reset + " >>>" + c.violet).strip() if ter == '1': tower.tower() elif ter == '2': exit() else: tower.tower() elif monster.hp < 1: print(c.yellow + "You won!") cl.Player.gold += 100000 save.save_game() t.sleep(1) print("Blinding light begins too engulf you.") t.sleep(1.25) print(c.clear) print(c.reset + ".") t.sleep(1) print(c.clear) print("..") t.sleep(1) print(c.clear) print("...") t.sleep(1) print(c.clear) print("You awake.") t.sleep(1.5) print(c.clear) credits.roll() exit()
def main(): port = 24654 log_dir = './sync' log_dir_test = './cen_test_logdir_%s_%s' % (FLAGS.job_name, FLAGS.task_index) config_ps = tf.ConfigProto(intra_op_parallelism_threads=3, inter_op_parallelism_threads=3) cluster = tf.train.ClusterSpec({ 'ps': ['localhost:%d' % port], 'worker': [ 'localhost:%d' % (port + 1), 'localhost:%d' % (port + 2), 'localhost:%d' % (port + 3), 'localhost:%d' % (port + 4) ] }) if FLAGS.job_name == 'ps': with tf.device('/job:ps/task:0/cpu:0'): server = tf.train.Server(cluster, job_name='ps', task_index=FLAGS.task_index, config=config_ps) server.join() else: is_chief = (FLAGS.task_index == 0) config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1) server = tf.train.Server(cluster, job_name='worker', task_index=FLAGS.task_index, config=config) worker_device = '/job:worker/task:%d/cpu:0' % FLAGS.task_index with tf.device( tf.train.replica_device_setter( worker_device=worker_device, ps_device='/job:ps/task:0/cpu:0/', ps_tasks=1)): global_step = tf.Variable(0) xx = tf.placeholder(tf.float32, [None, 28, 28, 1], name='xxx') yyy = tf.placeholder(tf.float32, [None, 10], name='yy') _x, _y = mnist.train.next_batch(batch_size=BATCH_SIZE) batch_n = np.reshape(_x, [-1, 28, 28, 1]) logits = tower.tower(batch_n) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=_y)) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(_y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar('loss', loss) tf.summary.scalar('accuracy', accuracy) opt = tf.train.SyncReplicasOptimizer( tf.train.GradientDescentOptimizer(0.1), replicas_to_aggregate=4, total_num_replicas=4) train_op = opt.minimize(loss, global_step=global_step) init_op = tf.global_variables_initializer() if is_chief: chief_queue_runner = opt.get_chief_queue_runner() init_tokens_op = opt.get_init_tokens_op(0) saver = tf.train.Saver() merged = tf.summary.merge_all() stop_hook = tf.train.StopAtStepHook(last_step=200) summary_hook = tf.train.SummarySaverHook(save_steps=10, output_dir=log_dir, summary_op=merged) sv = tf.train.Supervisor(is_chief=is_chief, init_op=init_op, summary_op=merged, logdir=log_dir, saver=saver, global_step=global_step, save_model_secs=100, save_summaries_secs=10) sess = sv.prepare_or_wait_for_session(server.target, config=config) if is_chief: sv.start_queue_runners(sess, [chief_queue_runner]) sess.run(init_tokens_op) f = open( './cen_logdir_%s_%s.txt' % (FLAGS.job_name, FLAGS.task_index), 'w') start_time = time.time() end_time = False for i in range(10000): print(FLAGS.task_index, 'i is', i) if sv.should_stop(): end_time = time.time() print('worker', FLAGS.task_index, 'end all process') print('time is', end_time - start_time) break else: batch_x, batch_y = mnist.train.next_batch( batch_size=BATCH_SIZE) batch_n = np.reshape(batch_x, [-1, 28, 28, 1]) accu_value, loss_value, _ = sess.run( [accuracy, loss, train_op], feed_dict={ xx: batch_n, yyy: batch_y }) #print('tower_%d, loss is: %.4f, accuracy is %.3f' %(FLAGS.task_index, loss_value, accu_value)) # print() # print() # print('i is', i) # if i ==300: end_time = time.time() # if (not (i+1) % 5): #and (FLAGS.task_index==2): # print("\n\nloss and accuracy") # test_x, test_y = mnist.test.next_batch(batch_size=5000) # test_n = np.reshape(test_x, [-1, 28, 28, 1]) # lo, accu, summ = sess.run([loss, accuracy, merged], feed_dict={x:test_n, y:test_y}) # print("\n\nloss of test dataset is: ",lo) # print("accuracy of test dataset is: %.3f\n\n"%accu) # if FLAGS.task_index==2: # f.write(str(lo)+' '+ str(accu)+ '\n') # if end_time: # f.write(str(end_time - start_time)) # break sv.stop()
def main(): port = 14976 log_dir = './flocking_noncenter_%.3f_%.3f_%s_%s_r0.1' % ( ATTR, REPU, FLAGS.job_name, FLAGS.task_index) config_ps = tf.ConfigProto(intra_op_parallelism_threads=3, inter_op_parallelism_threads=3) cluster = tf.train.ClusterSpec({ 'ps': ['localhost:%d' % port], 'worker': [ 'localhost:%d' % (port + 1), 'localhost:%d' % (port + 2), 'localhost:%d' % (port + 3), 'localhost:%d' % (port + 4) ] }) if FLAGS.job_name == 'ps': with tf.device('/job:ps/task:0/cpu:0'): server = tf.train.Server(cluster, job_name='ps', task_index=FLAGS.task_index) server.join() else: is_chief = (FLAGS.task_index == 0) config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1) server = tf.train.Server(cluster, job_name='worker', task_index=FLAGS.task_index, config=config) with tf.device('/job:ps/task:0/cpu:0'): global_step = tf.Variable( 0, name='global_step', trainable=False, ) worker_device = '/job:worker/task:%d/cpu:0' % FLAGS.task_index f_getter = FlockingCustomGetter(num_towers=NUM_TOWERS, tower_index=FLAGS.task_index) x = tf.placeholder(tf.float32, [None, 28, 28, 1], name='x') y = tf.placeholder(tf.float32, [None, 10], name='y') logits = tower.tower(x, f_getter) loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=y)) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar('loss', loss) tf.summary.scalar('accuracy', accuracy) sgd_opt = tf.train.GradientDescentOptimizer(0.01) opt = FlockingOptimizer(opt=sgd_opt, num_towers=NUM_TOWERS, tower_index=FLAGS.task_index, attraction=ATTR, repulsion=REPU) init = tf.global_variables_initializer() init_local = tf.variables_initializer(tf.local_variables()) scaff = tf.train.Scaffold(init_op=init, local_init_op=[init_local]) merged = tf.summary.merge_all() grads_and_vars = opt.compute_gradients(loss) train_op = opt.apply_gradients_and_flocking(grads_and_vars, global_step) stop_hook = tf.train.StopAtStepHook(last_step=10) summary_hook = tf.train.SummarySaverHook(save_steps=20, output_dir=log_dir, summary_op=merged) with tf.train.MonitoredTrainingSession(master=server.target, scaffold=scaff, hooks=[stop_hook, summary_hook], config=config) as sess: for i in range(10000): if sess.should_stop(): break else: batch_x, batch_y = mnist.train.next_batch( batch_size=BATCH_SIZE) batch_n = np.reshape(batch_x, [-1, 28, 28, 1]) loss_value, accu_value, _ = sess.run( [loss, accuracy, train_op], feed_dict={ x: batch_n, y: batch_y }) print('tower_%d, loss is: %.4f, accuracy is %.3f' % (FLAGS.task_index, loss_value, accu_value))
def __init__(self): #init self.menu_buttons = [] self.resolution = resolution_dict.get(starting_resolution) pygame.init() pygame.display.set_caption("Video Game") self.window = pygame.display.set_mode(self.resolution) self.graphics = self.default_graphics() self.state = "main" running = True clock = pygame.time.Clock() #draw board self.maps = map_selection() self.board = board(self.maps.map_list[3]) #virtual canvas buffer buffer = pygame.Surface(self.resolution, pygame.SRCALPHA) #test blob class blob: def __init__(self, parent): self.distance = 0.5 self.parent = parent self.speed = 0.1 self.color = (125, 125, 125) self.size = 10 self.center = (-1, -1) self.radius = 4 def update(self): self.distance += self.speed if self.distance > self.parent.board.path.length: #self.distance = 0.5 pass self.center = self.parent.board.path.get_position( self.distance) self.center = self.center[0] * self.parent.board.scale + self.parent.board.scale / 2 - self.radius,\ self.center[1] * self.parent.board.scale + self.parent.board.scale / 2 - self.radius def draw(self): graphics = pygame.Surface(self.parent.resolution, pygame.SRCALPHA) rectangle = pygame.Rect(self.center, (self.radius * 2, self.radius * 2)) pygame.draw.rect(graphics, self.color, rect=rectangle) return graphics enemies = [] start = 0.5 for each in range(0, 16): enemy = blob(self) enemy.distance += start enemies.append(enemy) start -= 1.5 towers = [] tower_locations = [(8, 9), (12, 9)] for point in tower_locations: additional = tower(self.board.scale) additional.location = point towers.append(additional) """ waiting = True while waiting: for event in pygame.event.get(): if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: waiting = False """ while running: buffer.fill((0, 0, 0)) time_delta = clock.tick(60) / 1000.0 #time_delta = clock.tick(30) / 1000.0 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: running = False if event.type == pygame.USEREVENT: if self.state == "main": pass buffer.blit(self.graphics, (0, 0)) buffer.blit(self.board.draw(), (0, 0)) for enemy in enemies: enemy.update() buffer.blit(enemy.draw(), (0, 0)) range_color = (0, 0, 0, 50) circle = pygame.Surface(self.resolution, pygame.SRCALPHA) for tower_member in towers: pygame.draw.circle(circle, range_color, tower_member.get_center(), tower_member.range * self.board.scale) buffer.blit(circle, (0, 0)) for tower_member in towers: buffer.blit(tower_member.draw(), tower_member.top_left()) self.board.draw_ends(buffer) self.window.blit(buffer, (0, 0)) pygame.display.update() pygame.quit()