Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- from time import sleep
- width = 640
- height = 480
- screen = pygame.display.set_mode((width,height))
- def draw_pixel(x,y,r=255,g=255,b=255):
- screen.set_at((int(x),int(y)), (r,g,b))
- def draw_line(x1,y1,x2,y2):
- if x1 == x2:
- if y2 < y1:
- for y in range(y1,y2-1, -1):
- draw_pixel(x1,y)
- return
- else:
- for y in range(y1,y2+1):
- draw_pixel(x1,y)
- return
- if y1 == y2:
- if x2 < x1:
- for x in range(x1,x2-1,-1):
- draw_pixel(x,y1)
- return
- else:
- for x in range(x1,x2+1):
- draw_pixel(x,y1)
- return
- rise = float(y2 - y1)
- run = float(x2 - x1)
- riser = rise/run
- runr = 1
- slope = riser/runr
- if rise < run:
- if x1 < x2:
- for x in range(x1, x2-1,-1):
- y = round(slope*x+y1)
- draw_pixel(x,y)
- else:
- for x in range(x1, x2+1):
- y = round(slope*x+y1)
- draw_pixel(x,y)
- else:
- if y2 < y1:
- for y in range(y1, y2-1, -1):
- x = round((y-y1)/slope+x1)
- draw_pixel(x,y)
- else:
- for y in range(y1, y2+1):
- x = round((y-y1)/slope+x1)
- draw_pixel(x,y)
- def draw_circle(radius, x=0, y=0):
- assert radius >= 1, 'first argument radius must be 1 or greater'
- points_list = []
- mirrored_list = []
- for xc in range(1,radius+1):
- high = radius
- low = 0
- guess = (high + low) / 2.0
- ctr = 1
- while abs(radius - math.sqrt((xc**2 + guess**2))) > 0.000000001 and ctr <100:
- if (xc**2 + guess**2) < radius**2:
- low = guess
- else:
- high = guess
- ctr +=1
- guess = (high + low) / 2.0
- assert ctr <= 100, 'iteration count exceeded in draw_point'
- yc = round(guess)
- points_list.append((xc,yc))
- points_list.append((yc,xc))
- points_list = list(set(points_list))
- for coord in points_list:
- draw_pixel(coord[0]+x,coord[1]+y)
- mirrored_list.append(((coord[0]*-1),coord[1]))
- mirrored_list.append(((coord[0]*-1),(coord[1])*-1))
- mirrored_list.append((coord[0],(coord[1]*-1)))
- for coord in mirrored_list:
- draw_pixel(coord[0]+x,coord[1]+y)
- draw_circle(100, 200, 200)
- pygame.display.flip()
- sleep(1)
- draw_line(30,30, 40, 250)
- pygame.display.flip()
- sleep(1)
- draw_line(30,30, 30,200)
- pygame.display.flip()
- sleep(1)
- draw_line(30,30, 200,30)
- pygame.display.flip()
- sleep(1)
- draw_line(100, 100, 50, 300)
- pygame.display.flip()
- sleep(1)
- draw_line(100, 100, 100, 1)
- pygame.display.flip()
- sleep(1)
- draw_line(100, 100, 50, 100)
- pygame.display.flip()
- sleep(1)
- #this one below this comment doesn't work
- draw_line(100, 100, 50, 10)
- pygame.display.flip()
- sleep(1)
- draw_line(100, 100, 10, 50)
- pygame.display.flip()
- sleep(1)
- sleep(10)
Add Comment
Please, Sign In to add comment