0

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()
2
  • did you run it in console/terminal/cmd.exe to see error message? Code works correctly on LInux so problem can be your system. On some system pygame can close if you don't get events. Commented Jul 7, 2020 at 4:15
  • I just ran it in my terminal in my MacOs Commented Jul 7, 2020 at 15:03

1 Answer 1

3
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.

Sign up to request clarification or add additional context in comments.

7 Comments

Hey @RocknRollDelta! Thanks for the effort in answering my question! I tried doing what you told me, but the pygame window still doesn't open...
hmmm i think i know the problem you forgot to add pygame.init() after pygame.
Hey! But where should I put this?
after import pygame
I did this. It told me for the first time to for me to allow terminal to open applications, some preference. So I thought it finally worked, but it still just keeps loading forever.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.