0

I am trying to make a simple snake game off a tutorial and the game is incomplete but I am supposed to be able to get the pygame gameboard or surface to open and show the basic grid structure but it shows the pygame window as not responding. I've already installed pygame on the anaconda prompt with pip install pygame. I am using Spyder 5, and python 3.8.10 and pygame 2.0.1.

import math 
import random
import pygame 
import tkinter as tk
from tkinter import messagebox

class cube(object):
    rows = 0
    w = 0
    def _init_(self, start,dirnx=1, dirny=0, color=(255, 0, 0)):
    pass

    def move(self, dirnx, dirny):
        pass

    def draw(self, surface, eyes=False):
        pass

class snake(object):
    def _init_(self, color, pos):
        pass

    def move(self):
        pass

    def reset(self, pos):
        pass 

    def addCube(self):
        pass

    def draw(self, surface):
        pass


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 drawWindow (surface):
    global rows, width 
    surface.fill((0, 0, 0))
    drawGrid(width, rows, surface)
    pygame.display.update()

def randomSnack(rows,  items):
    pass

def message_box(subject, content):
    pass

def main() :
    global width, rows
    width = 500
    height = 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: 
       pygame.time.delay(50)
       clock.tick(10)
       redrawWindow(win)
   
   
pass


main()

2 Answers2

0

I don't know if it is a typo, but I think the

redrawWindow(win)

in the while loop should actually be

drawWindow(win)

as you named the function.

And def __init__ should consist of two underscores before and after.

0

You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

def main() :
    global width, rows
    width = 500
    height = 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: 
       
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               flag = False

       clock.tick(10)
       redrawWindow(win)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174