Why Is Nothing Drawn in Pygame At All

Why is nothing drawn in PyGame at all?

You need to update the display.
You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().

See pygame.display.flip():

This will update the contents of the entire display.

While pygame.display.flip() will update the contents of the entire display, pygame.display.update() allows updating only a portion of the screen to updated, instead of the entire area. pygame.display.update() is an optimized version of pygame.display.flip() for software displays, but doesn't work for hardware accelerated displays.

The typical PyGame application loop has to:

  • handle the events by calling either pygame.event.pump() or pygame.event.get().
  • update the game states and positions of objects dependent on the input events and time (respectively frames)
  • clear the entire display or draw the background
  • draw the entire scene (draw all the objects)
  • update the display by calling either pygame.display.update() or pygame.display.flip()
  • limit frames per second to limit CPU usage with pygame.time.Clock.tick
import pygame
from pygame.locals import *
pygame.init()

DISPLAY = pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
clock = pygame.time.Clock()

run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == QUIT:
run = False

# clear display
DISPLAY.fill(0)

# draw scene
pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))

# update display
pygame.display.flip()

# limit frames per second
clock.tick(60)

pygame.quit()
exit()

Sample Image repl.it/@Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop

Nothing is drawn after pygame instantiation

pygame.quit() is a function and uninitialize all PyGame modules. When you do

if event.type == pygame.quit():

the function is called and all PyGame modules are uninitialized.

The type attribute of pygame.event.Event() object indicates the type of event. You need to compare the event type to the enumeration constant that identifies the event. The quit event is identified by pygame.QUIT (see pygame.event module):

Hence, you have to compete with pygame.QUIT instead of pygame.quit():

for event in pygame.event:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

Additionally there is an Indentation issue. You have to draw the scene and update the display in the application loop rather than then event loop:

from ship import Ship
from settings import Settings
import sys
import pygame


class Alien_invasion:
"""class to manage the game"""

def __init__(self):
"""initialise the game and create game ressources"""
pygame.init()
#print("ouzou")
self.settings=Settings()
self.screen = pygame.display.set_mode(self.settings.screen_height,self.settings.screen_width)

pygame.display.set_caption("Alien invasion")
self.ship = Ship(self)

def run_game(self):
"""start the game by calling a main loop"""
while True:
print("izann")
# wait keyboard or mouse event
for event in pygame.event:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# INDENTATION

#<--|

# draw the screen after all the changes occured
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
print("izan")
pygame.display.flip()
print("izan ssin")

if __name__ == "__main__":
partie = Alien_invasion()
partie.run_game()

Pygame: Can't draw anythin on screen after pygame.Surface.fill()

It's because s_draw_loop is a class, not a function.

The drawing code inside s_draw_loop is only executed once, before entering the game loop, when the python runtime reads the code of the class.

Calling s_draw_loop() inside your game loop does actually nothing (except creating an useless instace of that class that does nothing).


Simply change

class s_draw_loop():
...

to

def s_draw_loop():
...

so the code inside s_draw_loop gets executed every frame.

Pygame runs without any issues, but nothing is drawn on the screen

According to this issue at GitHub this is a bug of Python installed with Hombrew.

UPDATE FIX: if you download the official macOS x64 installer package of Python 3.7.2 from the official python page and then pip3 install pygame it works.

Pygame drawing not showing in Pygame window

You do not want to fill the screen with green every 60 ticks

To fix this, simply put screen.fill(GREEN) outside of the Main loop.

The only time you want screen.fill inside your while loop, is when your adding movement into your program.

I strongly suggest you make a function called draw and draw things outside of your while loop.

Pygame doesn't draw

First of a quick suggestion:

People are much more likely to help you if they don't have to download a zip file, next time just post the code parts you suspect not to work.

Anyways, problem seems to be in your main loop:

#Keyboard events
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = 0

#Mouse events
#todo

#Grid update <------- here you update the grid and the cells are being drawn
cb.draw()

#Graphical output <------------ here you're filling the WHOLE screen with white
screen.fill(THECOLORS["white"])

pygame.display.flip()

You need to move your screen.fill call above cb.draw so you don't paint over the cells.

Also in cell.py your drawing code is A) Broken and B) bad.

Instead of setting every pixel on its own, which is slow and in it's current state doesn't draw the cells correctly, you can just as well draw rectangle:

pygame.draw.rect(self.surface, (100, 10, 10), (self.pos[0], self.pos[1], self.size, self.size))

Pygame drawing doesn't work correctly

def enemySquares():
enemySurf = pygame.Surface(screen.get_size())
red = pygame.Color(255,0,0)
enemy = pygame.Rect(200,50,20,20)
pygame.draw.rect(enemySurf, red, enemy, 0)

The function above does draw a red rectangle - on a new surace it creates, not on
the "screen" surface.

Just drop your enemySurf = ... line there and change pygame.draw.rect(enemySurf, red, enemy, 0)
to
pygame.draw.rect(screen, red, enemy, 0) to have the rectangle apear on the next call to pygame.display.flip()



Related Topics



Leave a reply



Submit