laserz_und_stuff/Lasers_und_stuff/main.py

250 lines
7.8 KiB
Python
Raw Normal View History

2012-11-10 16:19:47 +00:00
'''
Created on Nov 10, 2012
@author: attero
'''
import random, pygame, sys
#import time
from pygame.locals import *
from math import fabs
from math import ceil
#import and init pygame
pygame.init()
def main():
#create the screen
window = pygame.display.set_mode((640, 480))
#draw a line
pygame.draw.line(window, (255, 255, 255), (0,0), (300, 50))
#draw it to the screen
GRAY = ( 182, 182, 182)
VIOLET = (150, 100, 190)
RED = (150, 0, 0)
GREEN = (0, 150, 0)
BLUE = (0, 0, 150)
VERYLIGHT = (210, 210, 210)
BACKGROUND_COLOR = GREEN
colors = [GRAY, VIOLET, RED, GREEN, BLUE, VERYLIGHT]
window.fill(BACKGROUND_COLOR)
mouseClicked = False
pygame.display.flip()
cannon = CannonController()
screen = Screen(window, colors)
screen.draw_surface(cannon)
gunpoint = Gunpoint((5, 4), (200, 4), (5, 210))
coords = gunpoint.aim((0,0))
#input handling ( code):
while True:
screen.draw_surface(cannon)
mouseClicked = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == KEYUP and event.key == K_SPACE:
if cannon.fired:
cannon.fired = False
else:
cannon.fired = True
elif event.type == KEYUP and event.key == K_RIGHT:
cannon.move_right(2)
elif event.type == KEYUP and event.key == K_LEFT:
cannon.move_left(2)
elif event.type == KEYUP and event.key == K_UP:
cannon.move_up(2)
elif event.type == KEYUP and event.key == K_DOWN:
cannon.move_down(2)
elif event.type == KEYUP and event.key == K_l:
if cannon.laser:
cannon.disable_laser()
if "Laser enabled" in screen.communicates :
screen.communicates.remove("Laser enabled")
screen.communicates.append("Laser disabled")
else:
cannon.enable_laser()
if "Laser disabled" in screen.communicates :
screen.communicates.remove("Laser disabled")
screen.communicates.append("Laser enabled")
2012-11-10 17:41:41 +00:00
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
2012-11-10 16:19:47 +00:00
elif event.type == MOUSEBUTTONUP:
print "mouse button up"
mouseClicked = True
screen.change_color()
if mouseClicked :
print("Mouse was clicked!")
coords = [gunpoint.aim((x/10.0,x/10.0)) for x in range(0,11)]
samples = [(x/10.0,x/10.0) for x in range(0, 11)]
print samples
print coords
2012-11-10 17:41:41 +00:00
2012-11-10 16:19:47 +00:00
2012-11-10 17:41:41 +00:00
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()
2012-11-10 16:19:47 +00:00
2012-11-10 17:41:41 +00:00
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)
2012-11-10 16:19:47 +00:00
class Screen:
def __init__(self, window, colors):
self.communicates = []
self.communicates.append("Press space to fire!")
self.communicates.append("Use arrows to adjust position")
2012-11-10 17:41:41 +00:00
self.communicates.append("Press C for calibration")
2012-11-10 16:19:47 +00:00
self.communicates.append("Press L to enable laser")
self.window = window
self.colors = colors
self.color = random.choice(self.colors)
def change_color(self):
self.color = random.choice(self.colors)
def draw_surface(self, cannon):
self.window.fill(self.color)
for i, text in enumerate(self.communicates):
self.print_text(text, 20, 20 + i*20, (0, 0, 0), 20, self.window)
if cannon.fired:
2012-11-10 17:41:41 +00:00
self.print_text("Fired!", 20, 150, (150, 20, 40), 40, self.window)
2012-11-10 16:19:47 +00:00
self.print_text(cannon.get_data_to_send().__str__(), 20, 300, (0, 0, 0), 30, self.window)
pygame.display.flip()
def print_text(self, text,xx,yy,color,text_size, screen):
font = pygame.font.SysFont("Courier New",text_size)
ren = font.render(text,1,color)
screen.blit(ren, (xx,yy))
class CannonController :
def __init__(self):
print("cannon controller started")
self.position = [0, 0]
self.fired = False
self.laser = False
def fire(self):
self.fired = True
def enable_laser(self):
self.laser = True
def disable_laser(self):
self.laser = False
def move_left(self, change):
self.position[0] -= change
def move_right(self, change):
self.position[0] += change
def move_up(self, change):
self.position[1] += change
def move_down(self, change):
self.position[1] -= change
def check_validity_of_data(self):
if(fabs(self.position[0]) > 255):
return False
if (fabs(self.position[1]) > 255):
return False
return True
def get_data_to_send(self):
# ('a' - os X, 'b' - os Y, 'c' - Strzal, 'd' - laser)
if self.check_validity_of_data():
data = []
data.extend(self.position)
if(self.fired):
data.append(255)
else:
data.append(0)
if(self.laser):
data.append(255)
else:
data.append(0)
return data
else:
return [0, 0, 0, 0]
class Gunpoint :
def __init__(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]
2012-11-10 17:41:41 +00:00
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]
2012-11-10 16:19:47 +00:00
def aim(self, point):
horizontal = ceil(self.beginnig_horizontal + self.horizontal_angle*point[0])
vertical = ceil(self.beginnig_vertical + self.vertical_angle*point[1])
return [horizontal, vertical]
if __name__ == '__main__':
main()