Calibration

master
Justyna Att Ilczuk 2012-11-10 18:41:41 +01:00
parent 10e996d8b8
commit 01e56514eb
1 changed files with 57 additions and 2 deletions

View File

@ -81,7 +81,14 @@ def main():
if "Laser disabled" in screen.communicates :
screen.communicates.remove("Laser disabled")
screen.communicates.append("Laser enabled")
elif event.type == KEYUP and event.key == K_c:
point1 = [float(x) for x in ask(window, "Point 1 ").split(",")]
point2 = [float(x) for x in ask(window, "Point 2 ").split(",")]
point3 = [float(x) for x in ask(window, "Point 3 ").split(",")]
print point1, point2, point3
gunpoint.calibrate(point1, point2, point3)
print gunpoint.horizontal_angle, gunpoint.vertical_angle
elif event.type == MOUSEBUTTONUP:
print "mouse button up"
mouseClicked = True
@ -96,14 +103,56 @@ def main():
print samples
print coords
def get_key():
while 1:
event = pygame.event.poll()
if event.type == KEYDOWN:
return event.key
else:
pass
def display_box(screen, message, xx, yy):
"Print a message in a box in the middle of the screen"
font_object = pygame.font.Font(None,18)
pygame.draw.rect(screen, (0,0,0),(xx, yy, 200,20), 0)
pygame.draw.rect(screen, (255,255,255),
(xx -2, yy-2, 202,24), 1)
if len(message) != 0:
screen.blit(font_object.render(message, 1, (255,255,255)),
(xx + 6, yy + 6) )
pygame.display.flip()
def ask(screen, question):
"ask(screen, question) -> answer"
pygame.font.init()
current_string = []
display_box(screen, question + ": " + "".join(current_string), 20, 400)
while 1:
inkey = get_key()
if inkey == K_BACKSPACE:
current_string = current_string[0:-1]
elif inkey == K_RETURN:
break
elif inkey == K_MINUS:
current_string.append("_")
elif inkey <= 127:
current_string.append(chr(inkey))
display_box(screen, question + ": " + "".join(current_string), 20, 400)
return "".join(current_string)
class Screen:
def __init__(self, window, colors):
self.communicates = []
self.communicates.append("Press space to fire!")
self.communicates.append("Use arrows to adjust position")
self.communicates.append("Press C for calibration")
self.communicates.append("Press L to enable laser")
self.window = window
self.colors = colors
@ -118,7 +167,7 @@ class Screen:
self.print_text(text, 20, 20 + i*20, (0, 0, 0), 20, self.window)
if cannon.fired:
self.print_text("Fired!", 20, 100, (150, 20, 40), 40, self.window)
self.print_text("Fired!", 20, 150, (150, 20, 40), 40, self.window)
self.print_text(cannon.get_data_to_send().__str__(), 20, 300, (0, 0, 0), 30, self.window)
pygame.display.flip()
@ -186,6 +235,12 @@ class Gunpoint :
self.beginnig_horizontal = point1[0]
self.beginnig_vertical = point1[1]
def calibrate(self, point1, point2, point3):
self.vertical_angle = point3[1] - point1[1]
self.horizontal_angle = point2[0] - point1[0]
self.beginnig_horizontal = point1[0]
self.beginnig_vertical = point1[1]
def aim(self, point):
horizontal = ceil(self.beginnig_horizontal + self.horizontal_angle*point[0])
vertical = ceil(self.beginnig_vertical + self.vertical_angle*point[1])