简体   繁体   English

pygame_sdl2-应用在pygame.mouse.get_pressed()期间在移动设备上崩溃

[英]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. 我开发的Android游戏pygamepygame_sdl2 ,并rapt的部署。

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. 问题是:游戏可以在我的笔记本电脑上运行(我正在使用Ubuntu),但不能在移动设备上运行。 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). 如果您想查看整个代码,则我的游戏在公共github存储库中(所有相关内容都在main.py文件中)。

I think the error is triggered by the call to pygame.mouse.get_pressed() . 我认为错误是由pygame.mouse.get_pressed()的调用触发的。

Here is some relevant code which hopefully would help in understanding the problem. 这是一些相关的代码,希望可以帮助您理解问题。

Joypad class (btn_pressed method) Joypad类(btn_pressed方法)

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 ). 这样,当事件为(例如)QUIT时,调用x, y = mouse_event.pos时,“ Joypad类(btn_pressed方法)”不会出错,因此不返回任何坐标(另请参见以下来自pygame事件文档的图像)页 )。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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