3

When i try to run this piece of code the pygame window created in the game_start function does not launch. When i remove the game_main_loop function it does. I cannot figure out what is wrong with the function, anybody have any ideas?

#Modules
import libtcodpy as libtcod
import pygame

#Game Files
import constants

pygame.init()

def game_start():
    '''this function initialises the main window and the pygame library'''
    #initialise the game
    MAIN_SURFACE = pygame.display.set_mode((constants.WIDTH,constants.HEIGHT))


def game_main_loop():
    '''in this function the game is looped'''
    game_quit = False

    while not game_quit:
        #get player input
        event_list = pygame.event.get()

        #process player input
        for event in event_list:
            if event.type == pygame.QUIT:
                game_quit = True

        #draw the game
        #quit the game
    pygame.quit()
    exit()
  • Your first debugging step should be to comment out all the code from `game_main_loop` and see if the behavior remains the same. – Joey Harwood Dec 13 '17 at 21:44
  • where is rest of code - you defined functions but you don't execute them, so they can't create window. – furas Dec 13 '17 at 21:55

1 Answers1

1

First, maybe unify your comments, either choose # or '''.

Second, possibly the screen is never initalized because it is not encompassing the entire file. Maybe remove game_start and use set_mode after your import statements.

Example:

#Modules
import libtcodpy as libtcod
import pygame

#Game Files
import constants

pygame.init()

MAIN_SURFACE = pygame.display.set_mode((constants.WIDTH,constants.HEIGHT))


def game_main_loop():
    '''in this function the game is looped'''
    game_quit = False

    while not game_quit:
        #get player input
        event_list = pygame.event.get()

        #process player input
        for event in event_list:
            if event.type == pygame.QUIT:
                game_quit = True

        #draw the game
        #quit the game
    pygame.quit()
    exit()
    # After, just call the game function

Maybe take a look at PEP-8 while you are at it, It may be helpful later.

Mercury Platinum
  • 1,549
  • 1
  • 15
  • 28
  • Use only `#` for comments, triple quotes `"""Bla bla"""` are for docstrings. Docstrings should explain the purpose of a function and how it should be used, comments should explain *why* something that isn't obvious is there. – skrx Dec 14 '17 at 04:48