Advertisement
GameNationRDF

Python Line Drawer - PyGame

Mar 1st, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. # Python Line drawer using PyGame Library.
  2. # Code by Umut Bilgic.
  3.  
  4. import pygame
  5. from pygame.locals import *
  6. import sys
  7.  
  8. def app_quit():
  9.     pygame.quit()
  10.     sys.exit("app_quit()")
  11.  
  12. def get_line(x1, y1, x2, y2):
  13.     points = []
  14.     issteep = abs(y2-y1) > abs(x2-x1)
  15.     if issteep:
  16.         x1, y1 = y1, x1
  17.         x2, y2 = y2, x2
  18.     rev = False
  19.     if x1 > x2:
  20.         x1, x2 = x2, x1
  21.         y1, y2 = y2, y1
  22.         rev = True
  23.     deltax = x2 - x1
  24.     deltay = abs(y2-y1)
  25.     error = int(deltax / 2)
  26.     y = y1
  27.     ystep = None
  28.     if y1 < y2:
  29.         ystep = 1
  30.     else:
  31.         ystep = -1
  32.     for x in range(x1, x2 + 1):
  33.         if issteep:
  34.             points.append((y, x))
  35.         else:
  36.             points.append((x, y))
  37.         error -= deltay
  38.         if error < 0:
  39.             y += ystep
  40.             error += deltax
  41.     if rev:
  42.         points.reverse()
  43.     return points
  44.    
  45. # Colors #
  46. white = (255,255,255)
  47. black = (0,0,0)
  48. red = (255,0,0)
  49. green = (0,255,0)
  50. blue = (0,0,255)
  51.  
  52. # Screen Size #
  53. width,height = 400,400
  54. screen_size = (width,height)
  55.  
  56. # Inıtialization #
  57. pygame.init()
  58.  
  59. screen = pygame.display.set_mode(screen_size)
  60. pygame.display.set_caption("Pygame")
  61.  
  62. # Main loop #
  63. while True:
  64.     for event in pygame.event.get():
  65.         if event.type == QUIT:
  66.             app_quit()
  67.  
  68.     mouse_state = pygame.mouse.get_pressed()
  69.     draw_pos = pygame.mouse.get_pos()
  70.    
  71.     print ("")
  72.  
  73.     user_pos_1 = input("Enter position 1: ")
  74.     user_pos_2 = input("Enter position 2: ")
  75.  
  76.     pos_1_list = user_pos_1.split(",")
  77.     pos_2_list = user_pos_2.split(",")
  78.  
  79.     X1,Y1 = int(pos_1_list[0]),int(pos_1_list[1])
  80.     X2,Y2 = int(pos_2_list[0]),int(pos_2_list[1])
  81.  
  82.     final_list = get_line(X1,Y1,X2,Y2)
  83.  
  84.     for i in range(0,len(final_list)):
  85.         final_pos = final_list[i]
  86.         screen.set_at(final_pos, white)
  87.  
  88.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement