1

I have an issue. Here is my code :

import pygame
import tkinter as tk
class cube():
    rows = 20
    w = 500
    def __init__(self, start, dirnx=1, dirny=0, color=(255, 0, 0)):
        pass
class snake(object):
    def __init__(self, color, pos):
        pass
def drawGrid(w, rows, surface):
    b = 255
    sizeBtwn = w // rows
    x = 0
    y = 0
    for l in range(rows):
        x = x + sizeBtwn
        y = y + sizeBtwn
        pygame.draw.line(surface, (b, b, b), (x,0), (x,w))
        pygame.draw.line(surface, (b, b, b), (0,y), (w,y))
def redrawWindow(surface):
    global rows, width
    surface.fill((0,0,0))
    drawGrid(width, rows, surface)
    pygame.display.update()
def main():
    global width, rows
    width = 1280
    height = 720
    rows = 40
    win = pygame.display.set_mode((width, height))
    s = snake((255, 0, 0), (10, 10))
    run = True
    clock = pygame.time.Clock()
    while run:
        clock.tick(30)
        redrawWindow(win)
if __name__ == "__main__":
    main()

When I launch I have nothing. I have a grey window. But I should have a black window with grids... Why does nothing appear ?

Thank you to help me !

1 Answers1

0

You have to handle the events, by either pygame.event.pump() or pygame.event.get().
This functions do not handle the internal events, too. This is necessary to keep the system responding and for updating the display.

def main():
    # [...]

    clock = pygame.time.Clock()
    while run:

        # handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        clock.tick(30)
        redrawWindow(win)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • pygame doesn't quit instantly. It's just my window is grey and only grey. –  Feb 08 '20 at 17:06
  • It's weird...not for me. –  Feb 08 '20 at 20:58
  • Thank you ! I added : `for event in pygame.event.get(): if event.type == pygame.QUIT: run = False` And now it's working...but why ? –  Feb 08 '20 at 21:00
  • @MrRex What was the issue? – Rabbid76 Feb 08 '20 at 21:01
  • I don't understand why it's working after writing this : `for event in pygame.event.get(): if event.type == pygame.QUIT: run = False ` –  Feb 08 '20 at 21:03
  • These lines are juste to close pygame window, no ? –  Feb 08 '20 at 21:03
  • @MrRex That is exactly what I've written and explained in my answer. It is because now the (internal) events are handled by `pygame.event.get()`. – Rabbid76 Feb 08 '20 at 21:12