lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
530 stars 190 forks source link

Crash in convert_alpha() when using IDE mode #264

Open JeremiahCheatham opened 2 years ago

JeremiahCheatham commented 2 years ago

I had to reinstall my system last night so ended up reinstalling pygame zero again. I'm on Arch Linux. With all the incompatibilities with python and pygame and old code i am running the dev branch of pygame zero. with the standard archlinux SDL, pygame, and python packages. pygame zero is the only one i have to manually install. Also i have to disable it's dependencies or it tries to pull uncompatable pygame packages in to my system. This has been a headache since we first started using pygame zero. Not sure if it's a major python problem or who is at fault. However it has been working for me as long as i use all standard packages and pip the dev version of pygame zero without letting it install pygame. But today it's broken after fresh install. I found that setting the ICON variable or creating an Actor causes a crash. However blitting an image like a background in the draw function doesn't.

import pgzrun

WIDTH = 800
HEIGHT = 600
TITLE = "Astroids!"
ICON = "icon"

pgzrun.go()

or loading an Actor.

import pgzrun
from pgzero.builtins import screen, Actor

WIDTH = 800
HEIGHT = 600

ship = Actor("ship")

def draw():
    screen.blit("backdrop", (0, 0))
    ship.draw()

def update():
    pass

pgzrun.go()

The above code will cause the crash.

Traceback (most recent call last):
  File "/home/jeremiah/Desktop/Tutorials/PyGame-Zero/04-Window-Icon.py", line 8, in <module>
    pgzrun.go()
  File "/usr/lib/python3.9/site-packages/pgzrun.py", line 30, in go
    run_mod(mod)
  File "/usr/lib/python3.9/site-packages/pgzero/runner.py", line 181, in run_mod
    PGZeroGame(mod, **kwargs).run()
  File "/usr/lib/python3.9/site-packages/pgzero/game.py", line 236, in run
    self.mainloop()
  File "/usr/lib/python3.9/site-packages/pgzero/game.py", line 294, in mainloop
    self.reinit_screen()
  File "/usr/lib/python3.9/site-packages/pgzero/game.py", line 80, in reinit_screen
    self.show_icon()
  File "/usr/lib/python3.9/site-packages/pgzero/game.py", line 117, in show_icon
    pygame.display.set_icon(pgzero.loaders.images.load(icon))
  File "/usr/lib/python3.9/site-packages/pgzero/loaders.py", line 140, in load
    res = self._cache[key] = self._load(p, *args, **kwargs)
  File "/usr/lib/python3.9/site-packages/pgzero/loaders.py", line 179, in _load
    return pygame.image.load(path).convert_alpha()
pygame.error: No video mode has been set

The equivalent pygame code seems to work just fine.

import pygame

pygame.init()

WIDTH = 800
HEIGHT = 600
TITLE = "Astroids!"
ICON = "images/icon.png"
FPS = 60

running = True

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
pygame.display.set_icon(pygame.image.load(ICON).convert_alpha())

def update_input():
    global running
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

while running:
    update_input()
    pygame.time.Clock().tick(FPS)

pygame.quit()

This issue just started today but i haven't update pygame zero in maybe month or more. The rest of the system is updated daily.

JeremiahCheatham commented 2 years ago

Here is how my pygame zero is installed.

sudo pip install git+https://github.com/lordmauve/pgzero.git --no-deps --upgrade
sudo pip install pyfxr
JeremiahCheatham commented 2 years ago

Moving to pygame zero 1.2.1 did fix the image loading mostly. But it brings other issues. The ICON variable now needs the full file path name with extension. This isn't needed in dev branch.

from pgzero.builtins import screen

Code can't be used. This means both pyflakes and flake8 will complain about error in the code because screen is undefined. I still have to do this with all other builtins. Is there an actual fix for this issue?

And as has been since i first started using pygame zero with my son. Without adding

def update():
    pass

into the code, all early project for pygame zero in all out books will fail since the actual screen is created after the image is drawn. Without the refresh of the update function you never see it. This is a major issue that almost caused us to give up entirely. But it means that all those books either have typos or there is a bug in the program.

lordmauve commented 2 years ago

Without adding

def update():
     pass

into the code

Please can you open a separate issue for this containing a full example program; I don't understand what you are trying to do and what you expect to happen.

JeremiahCheatham commented 2 years ago

Again someone has changed my title to remove needed information. The new title does not fit the bug i have listed above.

IDE Mode as listed here https://pygame-zero.readthedocs.io/en/stable/ide-mode.html

import pgzrun

pgzrun.go()

The above bug is present whether import pygame is present or not. It has nothing to do with this. It has to do with changes made else were. I had looked it up in your changes at one point but it was over my head so i just listed it here. But editing peoples bug reports is well hmmm......

yansnow78 commented 2 years ago

to reproduce the crash , just run the following demo.py from an IDE you will then get the error : pygame.error: No video mode has been set

import pgzrun

WIDTH = 800
HEIGHT = 600

def draw():
    screen.clear()
    screen.draw.circle((400, 300), 30, 'white')
pgzrun.go()

the problem come from the removing of the following lines in runner.py in function prepare_mod
see https://github.com/lordmauve/pgzero/commit/bf48fc964b5168d8e1e101a32adc2ac6cb685c69

PGZeroGame.show_default_icon()
pygame.display.set_mode((100, 100), DISPLAY_FLAGS)

if you add them back it doens't crash anymore

to temporary fix it, just add in your project pygame.display.set_mode((100, 100), DISPLAY_FLAGS) after all imports just before any object creation

roboticsware commented 8 months ago

same here