mercredi 2 août 2017

random contiguous drawing in python

using python 3.4 I'm into my second program and have hit a wall.

I'm intending to draw random blocks on the screen while keeping the originals as to flood the screen . i have code that will draw random blocks but its missing the code needed to keep them connected. as in the new block needs to be next to any existing block. can anyone help. Also bonus points if you can help me detect the edge of the screen and to keep it drawing from a different spot that is still on screen. although i feel that is asking a lot i just have a feeling its beyond my skills right now.

code as follows:

import random, pygame, sys
from random import randint
from pygame.locals import *
pygame.init()

START_POSX1 = randint(1,400)
START_POSY1 = randint(1,300)
START_POSX2 = randint(1,400)
START_POSY2 = randint(1,300)

a=[]

tl=(-5,-5) # defines the positions of new rects to be drawn based off 
tm=(0,-5) # the START_POSX and START_POSY variables.
tr=(+5,-5) # tl = top left tm = top mid tr = top right etc
ml=(-5,0)
mr=(+5,0)
bl=(-5,+5)
bm=(0,+5)
br=(+5,+5)
d=[tl,tm,tr,ml,mr,bl,bm,br]


RANDOM_COLOR1 = (randint(1,255), randint(1,255), randint(1,255))
RANDOM_COLOR2 = (randint(1,255), randint(1,255), randint(1,255))
RANDOM_COLOR3 = (randint(1,255), randint(1,255), randint(1,255))
COLOR1 = RANDOM_COLOR1
COLOR2 = RANDOM_COLOR2
COLOR3 = RANDOM_COLOR3
BLACK = (0,0,0)

SCREEN = pygame.display.set_mode((400,300))
def PIXEL(x,y): # a different rect object not used currently

    pygame.draw.rect(SCREEN,COLOR1,(x,y,5,5))
    return x,y

PIXEL1 = pygame.draw.rect(SCREEN,COLOR2,(START_POSX2,START_POSY2,5,5))
# above is for format reference and a place holder to test different
# outcomes
# also it drastically changes the output of the program if you put this 
# inside the move() function and remove the "global" statement
# if you put it in the function it only draws the 8 surronding points of
# the original PIXEL1 object

#PIXEL(START_POSX1,START_POSY1) tests the PIXEL function

def move(): # defines what and how to re-draw new rects
    global PIXEL1
    #PIXEL1=pygame.draw.rect(SCREEN,COLOR2(START_POSX2,START_POSY2,5,5))
    r=d[randint(0,7)] # defines r to the random output of list "d"

    PIXEL1.move_ip(r)
    PIXEL1 = pygame.draw.rect(SCREEN,COLOR2,(PIXEL1)) 
    return PIXEL1 # returns the rect properties, to store in a list

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    move() # cals the move function
    a.append(move()) # adds the rect coordinates to a list named "a"
    if PIXEL1 in a:
        PIXEL1= pygame.draw.rect(SCREEN,COLOR2,(a[-1]))
        # supposed to redefine PIXEL1 to the coordinates last entered to
        # the list so it can reevaluate the next move, it may work but
        # not doing as i intended, probably unnecessary. runs without it


    pygame.time.wait(100)
    pygame.display.update()

pygame.draw.rect(SCREEN, COLOR1, (5,5,5,5)) a rect object




Aucun commentaire:

Enregistrer un commentaire