Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3356522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:31:23+00:00 2026-05-18T02:31:23+00:00

I can’t figure out what’s happening here. I’ve got a few lists set up,

  • 0

I can’t figure out what’s happening here. I’ve got a few lists set up, one for each individual color with corresponding RGB values as its member, and then the list, colors[], that contains each individual color list. Then I’ve got a nested for loop: the outer loop creates columns of color-filled rectangles and the inner loop advances the row. Not complicated, as I see it.

I’m trying to use numdown to traverse the colors[] list so that every two rows the color changes to the member corresponding in colors[].

The problem is that when I use the inner list’s numover, it works fine, except obviously I get the wrong color pattern (colors advance across rather than down). If I use numdown to traverse the list, only the member white seems to be accessed, even though if in the inner for-loop I ‘print(numdown)’ or even ‘print(colors[numdown])’ the correct value is printed.

Why is this the case? Why if I use the inner-for’s numover are the list member’s accessed correctly, but if I use the outer-for’s numdown it breaks?

It occurs to me that this might have something to do with pygame, though I wouldn’t have any idea what.

(Additionally, as really I am just starting with Python, if you see anything else worth jumping on, method or style-wise, please feel free to point it out.)

import pygame, sys
from pygame.locals import *

#initialize pygame
pygame.init()

#assign display window dimensions
winwidth = 400
winheight = 700

#number of rows, number of colums
numrows = range(1,11)
numcols = range(1,11)

#Keeping brick size proportionate to the window size
brickwidth = winwidth / (len(numrows))  
brickheight = winheight / 4 

#Pixel space above the breakout area
bricktopspace = winheight / 7

#Set display window width, height 
windowSurface = pygame.display.set_mode((winwidth, winheight), 0, 0)

brickxcoord = 0
    blue = [0, 0, 255]  
    green = [0, 255, 0]  
    yellow = [255, 255, 0]  
    red = [255, 0, 0]  
    white = [255, 255, 255]  

    colors = range(0,11)    
    colors[1] = white
    colors[2] = white
    colors[3] = red
    colors[4] = red
    colors[5] = green
    colors[6] = green
    colors[7] = yellow
    colors[8] = yellow
    colors[9] = blue
    colors[10] = blue

    class Setup():

        for numdown in numcols:

            for numover in numrows:

                print(numdown)
                pygame.draw.rect(windowSurface, colors[numdown], (brickxcoord, 
                bricktopspace, brickwidth, brickheight))
                brickxcoord = brickxcoord + brickwidth

            bricktopspace = bricktopspace + brickheight

class Main():

    Setup()
    pygame.display.update()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:31:23+00:00Added an answer on May 18, 2026 at 2:31 am

    uhh… a bit late but if you’re still around. Something like this?

    I’ve modified it a bit but trying to keep as close to your structure as possible:

    import pygame, sys
    from pygame.locals import *
    
    #assign display window dimensions
    winwidth = 400
    winheight = 700
    
    #number of rows, number of colums
    numrows = 10
    numcols = 10
    
    #Keeping brick size proportionate to the window size
    brickwidth = winwidth / numcols 
    brickheight = winheight / numcols 
    
    #initialize pygame
    pygame.init()
    #Set display window width, height
    windowSurface = pygame.display.set_mode((winwidth, winheight), 0, 0)
    
    #Colours
    blue = [0, 0, 255]  
    green = [0, 255, 0]  
    yellow = [255, 255, 0]
    red = [255, 0, 0]  
    white = [255, 255, 255]
    
    colours = [white, white, red, red, green, green, yellow, yellow, blue, blue]
    
    class Setup():
        def __init__(self):
            #Setup nest for loop to generate 2d array of blocks.
            for y in range(0, numrows):
                for x in range(0, numcols):
                    #Using modulo to get the different colours for rows, we use y as the changing key
                    col_index = y % len(colours)
                    pygame.draw.rect(windowSurface, colours[col_index], (x*brickwidth, y*brickheight, brickwidth, brickheight))
    
    class Main():
        Setup()
        pygame.display.update()
    

    Because you wanted a fix number or rows and columns, you can have two variables stating how many rows and columns you need. Then you use those to determine the sizes of the blocks as you have done.

    I’ve changed the colours to the array as suggested, personally that’s how I’d do it too (for one it’s shorter and you can read it as a sequence). Also, if you wanted to change the sequence, you just have to move the items around.

    Lastly, I used two for loops that use the numrows and numcols as the range limits. If you think of your times tables, including the 0, it creates a perfect grid. Just think of the first loop as the rows and the nested loop as the column.

    Well, good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you suggest some good MVC framework for perl -- one I am aware
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I use the following across all browsers? <a href=# onclick=doSomething()>Click here.</a> Is this
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Can anyone tell me how I can display a status message like 12 seconds
Can you tell me what is the difference between abstraction and information hiding in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.