Nicolas F
Nicolas F

Reputation: 515

Python application doesn't open with Pygame (python3)

I'm trying to build a snake game in my Mac using Python3 and Pygame, but when I run the game with python3 snakegame.py, the app called Python just keeps jumping up and down on the desktop (I assume loading to open), but it never does open. I'm not sure if I have to open it with Python Launcher or something else. Thank you! Here is my code if you need it:

import pygame

def drawGrid(w, rows, surface):
    sizeBtwn = w // rows
    x = 0
    y = 0
    for l in range(rows):
        x = x + sizeBtwn
        y = y + sizeBtwn

        pygame.draw.line(surface, (255,255,255), (x,0),(x,w))
        pygame.draw.line(surface, (255,255,255), (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 = 500
    rows = 20
    win = pygame.display.set_mode((width, width))
    #s = snake((255, 0, 0), (10, 10))
    flag = True;

    clock = pygame.time.Clock()

    while flag == True:
        pygame.time.delay(50)
        clock.tick(10)
        redrawWindow(win)
    pass

main()

Upvotes: 0

Views: 85

Answers (1)

user13392187
user13392187

Reputation:

import pygame
pygame.init()

def drawGrid(w, rows, surface):
    sizeBtwn = w // rows
    x = 0
    y = 0
    for l in range(rows):
        x = x + sizeBtwn
        y = y + sizeBtwn

        pygame.draw.line(surface, (255,255,255), (x,0),(x,w))
        pygame.draw.line(surface, (255,255,255), (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 = 500
    rows = 20
    win = pygame.display.set_mode((width, width))
    #s = snake((255, 0, 0), (10, 10))
    flag = True;

    clock = pygame.time.Clock()

    while flag == True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                flag = False
                
        pygame.time.delay(50)
        clock.tick(10)
        redrawWindow(win)
    pass

main()

I have updated your program with the ability to quit, and when i tested it, the window went non responding, that is because you didn't check the events that are currently occuring. So i added a loop to check the events, and now your program stays responding.

Upvotes: 3

Related Questions