-1

When I compile my code there are no errors but the pygame window doesn't pop up even though the code compiles. I just keep getting the "Hello from the pygame community" message. I tried running other programs and my version of pygame still works.

import pygame
import os

class Application:
     def __init__(self):
         self.isRunning = True
         self.displaySurface = None 
         self.fpsClock = None 
         self.attractors = []
         self.size = self.width, self.height = 1920, 1080 
         
         pygame.quit()

1 Answers1

0

There are couple of changes to be made:

  1. self.displaySurface should be equal to pygame.display.set_mode(self.size)
self.displaySurface = pygame.display.set_mode(self.size) #Making the screen

(Note that you need to move the line self.size = (self.width, self.height) = 1920, 1080 above self.displaySurface = pygame.display.set_mode(self.size), so that you can use self.size in self.displaySurface = pygame.display.set_mode(self.size))

  1. You need to use a while loop to keep check of all the events taking place on the game screen and to update the screen at every single frame
while self.isRunning:
     for event in pygame.event.get():#Get all the events
         if event.type == pygame.QUIT:#If the users closes the program by clicking the 'X' button
             pygame.quit()#De-initialise pygame
             sys.exit()#Quit the app
     pygame.display.update() #Update the screen
  1. To execute the __init__ method of the class you need to create an object
app = Application()#Creating an object

So the final code should look somewhat like:

import pygame
import os
import sys

class Application:

     def __init__(self):
         pygame.init()
         self.isRunning = True
         self.size = (self.width, self.height) = 1920, 1080 #A tuple
         self.displaySurface = pygame.display.set_mode(self.size) #Making the screen
         self.fpsClock = None 
         self.attractors = []
          
         while True:
             for event in pygame.event.get():#Get all the events
                 if event.type == pygame.QUIT:#If the user closes the program by clicking the 'X' button
                     pygame.quit()#De-initialise pygame
                     sys.exit()#Quit the app
             pygame.display.update() #Update the screen

app = Application()#Creating an object

CopyrightC
  • 857
  • 2
  • 7
  • 15