Python Forum
Why do I have an infinite loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do I have an infinite loop?
#11
In version 3, I decided to encourage the two-color blocks into the three-color rows. All I had to do was rework the functions a bit, change the positions of everything, and add in a grey-scale line.

This is the result, note that it takes a few seconds to do all the calculations:

import pygame
pygame.init()

block=50

screenW=30*block
screenH=16*block

x=0
y=0

screen=pygame.display.set_mode((screenW,screenH))

list=[255,204,153,102,51]

red=False
green=False
blue=False
grey=False

redgreen=False
redblue=False
greenblue=False

def drawR():

    r=255
    g=0
    b=0

    global x
    global y
    global red

    x=0
    y=0

    while r>=0 and red==False:
        pygame.draw.rect(screen,(r,g,b),(x,y,block,block))
        print("red",r,g,b)
        r-=51
        x+=block
        if r<=0:
            red=True

def drawG():

    r=0
    g=255
    b=0

    global x
    global y
    global green

    x=block*5
    y=0

    while g>=0 and green==False:
        pygame.draw.rect(screen,(r,g,b),(x,y,block,block))
        print("green",r,g,b)
        g-=51
        x+=block
        if g<=0:
            green=True

def drawB():

    r=0
    g=0
    b=255

    global x
    global y
    global blue

    x=block*10
    y=0

    while b>=0 and blue==False:
        pygame.draw.rect(screen,(r,g,b),(x,y,block,block))
        print("blue",r,g,b)
        b-=51
        x+=block
        if b<=0:
            blue=True
        
def drawGrey():

    r=255
    g=255
    b=255

    global x
    global y
    global grey

    x=block*15
    y=0

    while b>=0 and grey==False:
        pygame.draw.rect(screen,(r,g,b),(x,y,block,block))
        print("grey",r,g,b)
        r-=51
        g-=51
        b-=51
        x+=block
        if b<=0:
            grey=True

def drawRedGreen():

    r=255
    g=255
    b=0

    global x
    global y
    global redgreen

    x=0
    y=block

    while r>=0 and g>=0 and redgreen==False:
        pygame.draw.rect(screen,(r,g,b),(x,y,block,block))
        print("redgreen",r,g,b)
        r-=51
        y+=block
        if y>=block*6:
            y=block
            x+=block
        if r<=0:
            r=255
            g-=51
        if g<=0:
            r=255
            g=255
            b+=51
        if b>255:
            redgreen=True

def drawRedBlue():

    r=255
    g=0
    b=255

    global x
    global y
    global redblue

    x=0
    y=block*6

    while r>=0 and g>=0 and redblue==False:
        pygame.draw.rect(screen,(r,g,b),(x,y,block,block))
        print("redblue",r,g,b)
        r-=51
        y+=block
        if y>=block*11:
            y=block*6
            x+=block
        if r<=0:
            r=255
            b-=51
        if b<=0:
            b=255
            g+=51
        if g>255:
            redblue=True

def drawGreenBlue():

    r=0
    g=255
    b=255

    global x
    global y
    global greenblue

    x=0
    y=block*11

    while g>=0 and b>=0 and greenblue==False:
        pygame.draw.rect(screen,(r,g,b),(x,y,block,block))
        print("greenblue",r,g,b)
        g-=51
        y+=block
        if y>=block*16:
            y=block*11
            x+=block
        if g<=0:
            g=255
            b-=51
        if b<=0:
            b=255
            r+=51
        if r>255:
            greenblue=True

def main():

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

        global rg
        global rb
        global gb


        drawR()
        drawG()
        drawB()
        drawGrey()
        drawRedGreen()
        drawRedBlue()
        drawGreenBlue()

        pygame.display.update()

main()
All that work just so I could see what color palette I would have with these limitations. And the sad part of it all, it wasn't what I was hoping for. I was trying to go for a retro-game look. After seeing the color wheel for asesprite, I thought limiting the values of r, g, and b to 6 bits (values include 0, 51, 102, 153, 204, and 255) would be sufficient to achieve this. However, this creates far more shades than I expected, and the difference between them is far more subtle than I wanted. I was wanting to go for a traditional animation look in terms of my color palette. Oh well, back to the drawing board. Perhaps I should create more of these with different bit values until I find a palette that pleases me. Its too bad 255 only divides into 5 and 51. Oh well.

Either way, this is solved. I made too elaborate of a loop, and from this I learned its NOT a good idea to just fiddle with the parameters haphazardly until it works. I got a better result by just completely re-doing it. I'm setting this to 'solved', seeing as this thread serves no further purpose.
Reply
#12
(Jan-13-2020, 05:06 AM)xBlackHeartx Wrote: I can make no sense of that code. Also, it imports two modules I've never even heard of.
You really need to learn python. Then dive into pygame.

def draw_color(surface, x, y, color, c_range, block):
    # Save y enter location.
    y_top = y

    # Create 3 ranges. 
    red = range(color.r, -1, -c_range)      # range(start, end, step)
    green = range(color.g, -1, -c_range)
    blue = range(color.b, -1, -c_range)

    # Product just let you loop through all 3 ranges in one line.
    for r, g, b in product(red, green, blue):
        pygame.draw.rect(surface, (r, g, b), (x, y, block, block))
        y += block

        # y is greater than block time four plus y start point.
        if y > block * 4 + y_top:
            x += block
            # Reset y back to start location.
            y = y_top
99 percent of computer problems exists between chair and keyboard.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020