ternus / pygame-examples

A collection of short-and-sweet pygame games
63 stars 54 forks source link

Python 2. #5

Open taitep opened 1 year ago

taitep commented 1 year ago

Python 2 should not be used. Period. Fix: Convert all code. @ternus

DonMiller9294 commented 1 year ago

Are you asking how to convert your code to python 3?

taitep commented 1 year ago

no, i just do not think i could convert all code of the repo.

rFurgan commented 1 week ago

There isn't much to do in order to make the codes compatible in Python 3.x

In snake.py and snake2.py the only thing that's causing issues is GRID_WIDTH and GRID_HEIGHT being interpreted as float. This causes issues with the randint function call as it expects two integers as input.

To fix this cast the value to int:

GRID_WIDTH = int(SCREEN_WIDTH / GRIDSIZE)
GRID_HEIGHT = int(SCREEN_HEIGHT / GRIDSIZE)

As for lunarlander.py there are some small issues in the load_image function. The first issue being the way exceptions are handled along with the print of that error message.

So you'd have to change

    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit(message)

to

    # Catch the exception and assign it to the "error" variable
    except pygame.error as error:

        # Here you just need the parenthesis
        print('Cannot load image: ', fullname)

        # Throw the exception with the error message
        raise SystemExit(error)

The second one is more of a warning because the colorkey in the load_image function is compared to -1 with is instead of ==. If you're curious on what the difference is, check this out

So you'd have to change

if colorkey is -1:

to

if colorkey == -1: