Pygame doesn't draw

Question:

I need help with a program I’m making. It’s a version of Conway’s Game of Life.
This game is right now made out of 3 files: main.py, cellBoard.py, cell.py

main.py takes care to instance cellboard and make it update its data, give it mouse input, and tell it to draw itself (an instance of the pygame surface is given to it, which handles it to the cells which are the actual ones that draw themselves)

cellboard.py creates a list of cells based off their size and the screen’s size, to fill it properly. It’s a 2D list. When it creates the cells it sets their state (alive currently) and handles them an instance of its instance of the original surface instance.

cell.py contains all the things a cell can do: die, live, be toggled, be drawn.

In fact, when I need to draw the whole board I just call cellBoard’s own draw() and it should take care of calling each cell’s draw. And it does.
The execution gets to the point where the cell should be drawn (checked with prints) and the pixel filling function is executed (using a for loop to cover an area). But nothing is actually drawn to the screen, or at least nothing is visible.

I have no idea what is causing this. I checked the code multiple times, I’ve even rewritten the whole program from scratch to make it more tidy (and I had the same problem as now)

What is causing this? My idea would be that somehow the instance of surface Cell gets is not good anymore to work because something happened to it (it goes through cellboard before getting to each cell, could that be the problem?)

Here’s the source code (all 3 files, they are very short and barebones so they should be easy to read) http://dl.dropbox.com/u/2951174/src.zip

Thanks in advance to anyone who feels like helping. I need to complete this project very fast so your help would be greatly appreciated.

Asked By: Gabriele Cirulli

||

Answers:

First of a quick suggestion:
People are much more likely to help you if they don’t have to download a zip file, next time just post the code parts you suspect not to work.

Anyways, problem seems to be in your main loop:

#Keyboard events
events = pygame.event.get()
for event in events:
    if event.type == pygame.QUIT:
        running = 0

#Mouse events
#todo

#Grid update  <------- here you update the grid and the cells are being drawn
cb.draw()

#Graphical output    <------------ here you're filling the WHOLE screen with white
screen.fill(THECOLORS["white"])    

pygame.display.flip()

You need to move your screen.fill call above cb.draw so you don’t paint over the cells.

Also in cell.py your drawing code is A) Broken and B) bad.

Instead of setting every pixel on its own, which is slow and in it’s current state doesn’t draw the cells correctly, you can just as well draw rectangle:

pygame.draw.rect(self.surface, (100, 10, 10), (self.pos[0], self.pos[1], self.size, self.size))
Answered By: Ivo Wetzel

i have the same problem but it like this:

import pygame
pygame.init()
screen = pygame.display.set_mode((400,400))
black = (0,0,0)
white = (255,255,255)
pygame.display.set_caption('Fyt')
R = True
while R:
    screen.fill(black)
    pygame.draw.rect(screen, white, (25, 150, 75, 25))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            R = False
pygame.quit()

but it doesn’t draw the rect

Answered By: ịgdfu
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.