Created
January 10, 2016 19:08
-
-
Save last-ent/f26d706e0de5e907a80c to your computer and use it in GitHub Desktop.
This file contains 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
import sys | |
import pygame | |
import pprint | |
# Create Main Window | |
window = pygame.display.set_mode((800, 600)) | |
pygame.display.set_caption("Simple Line") | |
# Start PyGame Instance | |
pygame.init() | |
# pygame.display.init() | |
# Set Initial Logic State | |
white = (255,255,255) | |
mouse_clicked = False | |
myfont = pygame.font.Font(None, 15) | |
class Pos(object): | |
def __init__(self, val, nm): | |
self.val = val | |
self.nm = nm | |
def update(self, nval): | |
print("Updating {} w/ pos: {}".format(self.nm, nval)) | |
self.val = nval | |
start_pos = Pos((), 'start_pos') | |
end_pos = Pos((), 'end_pos') | |
clock = pygame.time.Clock() | |
# Logic | |
def draw(canvas, spos, end_pos): | |
if not end_pos: | |
end_pos = spos | |
print("drawing line between {} & {}".format(spos, end_pos)) | |
pygame.draw.line(canvas, white, spos, end_pos) | |
# return end_pos | |
def show_pos(pos, screen=window): | |
tet = myfont.render("{}".format(pos), 1, (255,255,0)) | |
x,y = tet.get_size() | |
screen.fill(pygame.Color("black"), (10,10, x+5, y+5)) | |
window.blit(tet, (10,10)) | |
# Start Display Loop | |
while True: | |
clock.tick(60) | |
# Handle Events | |
events = pygame.event.get() | |
# if events: | |
# pprint.pprint(events[0].type) | |
for event in events: | |
if event.type == pygame.QUIT: | |
sys.exit() | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
mouse_clicked = True | |
start_pos.update(pygame.mouse.get_pos()) | |
end_pos.update(pygame.mouse.get_pos()) | |
show_pos(start_pos.val) | |
print('d') | |
if event.type == pygame.MOUSEBUTTONUP: | |
mouse_clicked = False | |
start_pos.update(()) | |
end_pos.update(()) | |
print('u') | |
if event.type == pygame.MOUSEMOTION: | |
end_pos.update(pygame.mouse.get_pos()) | |
if mouse_clicked: | |
draw(window, start_pos.val, end_pos.val) | |
start_pos.update(end_pos.val) | |
# Update Window after logic | |
pygame.display.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment