简体   繁体   中英

pygame_sdl2 - app is crashing on mobile during pygame.mouse.get_pressed()

I am developing a game for android with pygame and pygame_sdl2 , and rapt for deploy.

Currently, the game is at a very first stage of development.

I have a joypad draw in the bottom left of my screen, and, when the mouse is pressed in the coordinates of the joypad's buttons, a character is moved according to the button durection. The mouse events are used as touch events as in this (working) example .

The problem is: the game is working on my laptop (I am using Ubuntu), but not on my mobile device. When I start the game, everything shows up as expected, but as soon as I touh the screen (no matter if it's on the buttons or somewhere else), the app goes in background.

My game is in a public github repo if you want to see the whole code (everything relevant is in the main.py file).

I think the error is triggered by the call to pygame.mouse.get_pressed() .

Here is some relevant code which hopefully would help in understanding the problem.

Joypad class (btn_pressed method)

def btn_pressed(self, mouse_event):
    # check if left mouse is being pressed
    if pygame.mouse.get_pressed()[0]:
        x, y  = mouse_event.pos
        if self.btn_up.rect.collidepoint(x, y):
            return 'UP'
        elif self.btn_down.rect.collidepoint(x, y):
            return 'DOWN'
        elif self.btn_left.rect.collidepoint(x, y):
            return 'LEFT'
        elif self.btn_right.rect.collidepoint(x, y):
            return 'RIGHT'

Character class (move method)

def move(self, joypad_direction):
    """
    move the character
    to be used along with Joypad.btn_pressed returns
    ('UP', 'DOWN' 'LEFT', 'RIGHT')
    """

    self.dx = 0
    self.dy = 0

    # check for horizontal move
    if joypad_direction == 'LEFT':
        self.dx = -self.speed
        self.rect.move_ip(-self.speed, 0)
    if joypad_direction == 'RIGHT':
        self.dx = +self.speed
        self.rect.move_ip(self.speed, 0)

    self.dx = 0

    # check for vertical move
    if joypad_direction == 'UP':
        self.dy = -self.speed
        self.rect.move_ip(0, -self.speed)
    if joypad_direction == 'DOWN':
        self.dy = +self.speed
        self.rect.move_ip(0, self.speed)
        self.dy = 0

main loop

while True:

    ev = pygame.event.wait()

    # If not sleeping, draw the screen.
    if not sleeping:
        hero.move(joypad.btn_pressed(ev))
        screen.fill((0, 0, 0, 255))

        joypad.buttons.draw(screen)

        if x is not None:
            screen.blit(hero.image, (x, y))

        pygame.display.flip()

As explained in the comments, there was an issue in my main loop.

I simply had to change my code from:

if not sleeping:
    if not sleeping:
        hero.move(joypad.btn_pressed(ev))

to:

if not sleeping:
    if ev.type == pygame.MOUSEMOTION or ev.type == pygame.MOUSEBUTTONUP:
        hero.move(joypad.btn_pressed(ev))

so that "Joypad class (btn_pressed method)" does not get an error when calling x, y = mouse_event.pos when the event was (eg) QUIT, thus not returning any coordinates (see also the image belowe taken from the pygame's event documentation page ).

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM