1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| import pygame, random
from pygame.locals import *
flags = DOUBLEBUF
width, height = 910, 540
RED = (255,0,0)
BLUE = (0,0,255)
GREEN = (0,255,0)
pygame.init()
SCREEN = pygame.display.set_mode((width, height), flags, 16)
SCREEN.fill((255,255,255))
pygame.display.update()
def draw(ending=False):
global COORDS
nb = len(COORDS)
if not ending:
for i, coords in enumerate(COORDS):
x0, y0 = coords
next_index = (i+1)-nb
if next_index:
x1, y1 = COORDS[next_index]
pygame.draw.line(SCREEN, RED, (x0, y0), (x1, y1))
else:
pygame.draw.polygon(SCREEN, RED, COORDS)
COORDS = []
pygame.display.update()
def delete():
global COORDS
SCREEN.fill((255,255,255))
COORDS = COORDS[:-1]
draw()
def cross(X, Y):
SCREEN.set_at((X, Y), BLUE)
pygame.draw.line(SCREEN, GREEN, (X-1, Y), (X-31, Y))
pygame.draw.line(SCREEN, GREEN, (X+1, Y), (X+31, Y))
pygame.draw.line(SCREEN, GREEN, (X, Y-1), (X, Y-31))
pygame.draw.line(SCREEN, GREEN, (X, Y+1), (X, Y+31))
pygame.display.update()
COORDS = []
coord = (0,0)
space_pressed = False
STARTED = False
while 1:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
if event.button == BUTTON_LEFT:
x, y = pygame.mouse.get_pos()
if (x, y) != coord:
COORDS.append((x,y))
draw()
coord = (x, y)
if event.button == BUTTON_RIGHT:
draw(True)
if event.type == KEYDOWN :
if event.key == K_ESCAPE:
pygame.quit()
break
if event.key == K_BACKSPACE:
delete()
STARTED = False
if event.key == K_SPACE:
if not STARTED:
COPY_SCREEN = pygame.Surface.copy(SCREEN)
STARTED = True
space_pressed = True
if event.type == KEYUP :
if event.key == K_SPACE:
space_pressed = False
if space_pressed:
array = pygame.surfarray.pixels_green(COPY_SCREEN)
X = random.randint(0, width-1)
while not any(array[X]):
X = random.randint(0, width-1)
Y_coords = []
for y, pixel in enumerate(array[X]):
if pixel:
Y_coords.append(y)
Y = random.choice(Y_coords)
cross(X,Y)
print(X, Y) |
Partager