Created
December 22, 2013 02:34
-
-
Save ToferC/8077804 to your computer and use it in GitHub Desktop.
Map Writer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to identify the coordinates for locations on an image map by having the user tap on the appropriate locations and saving the results into a dictionary that can be called by another script. | |
# Requires an initial dictionary with at least the location name as a key and x, y coordinates as list values 0 and 1. | |
import math, random | |
import city_list | |
from scene import * | |
offset = 100 | |
city_dict = city_list.city_dict | |
global city_dict | |
class MyScene (Scene): | |
global city_dict | |
def setup (self): | |
# Set up the root layer, the data layer and our variables. | |
self.root_layer = Layer(self.bounds) | |
center = self.bounds.center() | |
self.layer = Layer(Rect(0, offset)) | |
self.layer.background = Color(1, 0, 0) | |
self.layer.image = '_ACKS_ipad_map' | |
self.data_layer = Layer(Rect(0,0,1024,offset)) | |
self.data_layer.background = Color(0,0,0) | |
self.x = 0 | |
self.y = 0 | |
self.counter = 0 | |
self.new_list = [] | |
for i in city_dict.iterkeys(): | |
self.new_list.append([i,city_dict[i][0],city_dict[i][1]]) | |
def draw (self): | |
# Renders the scene and requests user to select locations on the map. | |
if self.counter == len(self.new_list): | |
text("You are done!",font_name='Helvetica',font_size=70,x=412,y=350,alignment=6) | |
else: | |
self.root_layer.update(self.dt) | |
self.root_layer.draw() | |
self.layer.update(self.dt) | |
self.layer.draw() | |
self.data_layer.update(self.dt) | |
self.data_layer.draw() | |
self.call = text('Please select '+str(self.new_list[self.counter][0])+' on the map',font_name='Helvetica',font_size=15,x=10,y=60,alignment=6) | |
self.result = text(str(self.new_list[self.counter]),font_name='Helvetica',font_size=15,x=10,y=10,alignment=6) | |
def touch_began(self, touch): | |
# If the list hasnt been completed, returns the selected locations and writes the x, y coordinates to new_list. | |
if self.counter < len(self.new_list): | |
self.x, self.y = touch.location.x, touch.location.y | |
self.new_list[self.counter][1] = self.x | |
self.new_list[self.counter][2] = self.y | |
self.counter += 1 | |
else: | |
# If the list has been completed, takes the x, y coords, inputs them back into city_dict and saves the dictionary in a new file. | |
for i in range(len(self.new_list)): | |
city_dict[self.new_list[i][0]][0] = self.new_list[i][1] | |
city_dict[self.new_list[i][0]][1] = self.new_list[i][2] | |
doc = open('city_list3.py','r+') | |
doc.write('city_dict = '+str(city_dict)) | |
doc.close() | |
def touch_moved(self, touch): | |
pass | |
def touch_ended(self, touch): | |
pass | |
run(MyScene()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Writing the file with "r+" instead of "w" did not make much sense to me.
Adding sublayers to the root_layer simplifies the update() and draw() code.
Reused the city_dict for data storage so new_list could be eliminated.
Added reading and writing to a .json file which is a more standard approach than using city_list.py as a data file.
(I could never really figure out how to create the city_list.py file in a way that would work with the main file.)