Error initializing PYGAME

Asked

Viewed 363 times

0

I’m creating a simple replica of the game Space Invaders in Python3.7, at first I was creating the screen all with Turtle, but the game entered in a kind of infinite loop and did not execute the commands. After a friend suggested to use Bib pygame I developed the code below.

Code:

from turtle import Turtle, Screen
import pygame

pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Space Invaders')
clock = pygame.time.Clock()

#Cria a borda
border_pen = Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-270, -260)
border_pen.pendown()
border_pen.pensize(3)

for side in range(4):
    border_pen.fd(530)
    border_pen.lt(90)
border_pen.hideturtle()

#Cria a nave do usuario
player = Turtle()
player.color("blue")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0, -200)
player.setheading(90)

playerspeed = 10

#Mover a nave
def move_left():
    x = player.xcor()
    x -= playerspeed
    player.setx(x)

def move_right():
    x = player.xcor()
    x += playerspeed
    player.setx(x)

#Cria movimento de tecla
screen = Screen()
screen.listen()
screen.onkey(move_left, "Left")
screen.onkey(move_right, "Right")

crashed = False

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

I’m having the following mistake:

2019-04-12 15:44:18.808 Python[9273:62443] -[PYGSDLApplication 
_setup:]: unrecognized selector sent to instance 0x7fe3adf83cf0
2019-04-12 15:44:18.815 Python[9273:62443] *** Terminating app due to 
uncaught exception 'NSInvalidArgumentException', reason: '- 
[PYGSDLApplication _setup:]: unrecognized selector sent to instance 
0x7fe3adf83cf0'
*** First throw call stack: { ... }

libc++abi.dylib: terminating with uncaught exception of type 
NSException
  • how did you install this pygame? What operating system is using?

1 answer

1

Pygame is not meant to "chat" with Tkinter, where Turtle runs - its program is Frankish in its current state, creating the two interfaces - in theory, there should be no conflicts - Each library would have its own window and even be possible with some adjustments to use both simultaneously (the way it is not, because the calls you put to "Turtle" block the program). But it can also be the cause of your problem - either that, or pygame is broken in your system.

Since you didn’t say how you installed the pygame or what operating system you’re using, that’s all you can say -

Try to create a minimum program, only from pygame, no matter what Turtle (just open the window, wait a while and close) - then you create the code as it works.

And also check how you installed pygame - the correct is to install from the command line with pip install pygame - if you downloaded an installer from some site, it may be the cause of your error.

In the meantime, you can try to take the code using only Turtle, at the point you were before, and create a new question about it - I believe you can create a space Invaders using Turtle, and it can be an interesting exercise.

I must have some space-Invaders using pygame on github as well, if you want to get an idea of structure - I usually use examples when I give programming workshops - (looking there) - no, I have one developed to run in the browser, with Brython - but not with Pygame. This is complicated to install because it depends on an old version of brython - but still I think it’s clean and cool code for a Space Invaders: https://github.com/jsbueno/browser_invaders

Browser other questions tagged

You are not signed in. Login or sign up in order to post.