Python Android 上的 Pygame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23934370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:42:49  来源:igfitidea点击:

Pygame on Android

androidpythonpygame

提问by CuteProgrammerGirlxoxo

I was wondering if someone could give me a detailed explanation on how to run a game/app developed using Pygame on an Android phone. I recently finished programming PacMan and it works perfectly on my computer, but I think it would be awesome if I could get it running on my phone. I tried following the instructions at http://pygame.renpy.org/android-packaging.html, but every time i run "import android" on the IDLE I get an error saying it did not find the module. Could someone clearly explain how to set up the android module?

我想知道是否有人可以详细解释如何在 Android 手机上运行使用 Pygame 开发的游戏/应用程序。我最近完成了 PacMan 的编程,它在我的电脑上运行良好,但我认为如果我能在我的手机上运行它会很棒。我尝试按照http://pygame.renpy.org/android-packaging.html 上的说明进行操作,但是每次我在 IDLE 上运行“import android”时,我都会收到一条错误消息,说它没有找到该模块。有人可以清楚地解释如何设置android模块吗?

Also, in my program I used code such as if (event.key == K_UP or event.key == K_w): direction = UP. However there are no arrow keys on a phone. What code would I need to use to see if the user swiped the screen with their fingers from up -> down or left -> right, etc.

此外,在我的程序中,我使用了诸如 if (event.key == K_UP or event.key == K_w): direction = UP 之类的代码。但是手机上没有方向键。我需要使用什么代码来查看用户是否用手指从上 -> 下或向左 -> 右等滑动屏幕。

Any help would be great. Thanks <3

任何帮助都会很棒。谢谢 <3

回答by Fredwordsplat

There is a pyGame subset for android. However this requires special reworking and changing the program. Hopefully it will not be to hard.

android 有一个 pyGame 子集。然而,这需要特殊的返工和更改程序。希望不会太难。

http://pygame.renpy.org/writing.html

http://pygame.renpy.org/writing.html

http://pygame.renpy.org/index.html

http://pygame.renpy.org/index.html

However about your second question i am unable to awnser because I am Not yet experienced enough.

但是,关于您的第二个问题,我无法回答,因为我还没有足够的经验。

回答by Anihs Emma

i think the pygame subset for android would be good but i dont trust its functionality, i use kivy as its cross platform and if you ever decide to use the pygame subset for android your touch of flips on screen of an android device would be your mouse movement on the desktop so i ma saying treat the touch as the mouse good luck

我认为适用于 android 的 pygame 子集会很好,但我不相信它的功能,我使用 kivy 作为它的跨平台,如果您决定使用适用于 android 的 pygame 子集,您在 android 设备屏幕上的翻转触摸将是您的鼠标在桌面上移动所以我想说把触摸当作鼠标祝你好运

回答by KitsuneKarishnacov

There are some pretty good answers for your first part already so I won't answer that. (I came here looking into what to use for it too!)

你的第一部分已经有一些很好的答案,所以我不会回答。(我来这里也是为了研究它的用途!)

However the second part of your question should be a lot easier.

但是,您问题的第二部分应该容易得多。

Have a mouse object that on a mouse down event will save the coordinates of the touch to an MX and MY variable Then when the mouse up event is triggered takes the new coordinates and calculates a vector using the MX and MY and this new point ie. The distance and angle of the swipe. Use trigonometry or the math module for the angle (research arctan2).

有一个鼠标对象,在鼠标按下事件上将触摸坐标保存到 MX 和 MY 变量然后当鼠标向上事件被触发时获取新坐标并使用 MX 和 MY 以及这个新点计算向量,即。滑动的距离和角度。对角度使用三角函数或数学模块(研究 arctan2)。

You can then use this in an if, elif, else to determine what quadrant the angle was and the distance to determine whether the swipe was valid if it's greater than a certain value.

然后,您可以在 if、elif、else 中使用它来确定角度是哪个象限,以及确定滑动是否有效(如果它大于某个值)的距离。

I'm on mobile so unfortunately I can't give an example, however I'm certain you're apt to work out the solution with this guidance.

我在移动设备上,所以很遗憾我不能举一个例子,但是我确信你很容易根据这个指导找出解决方案。

回答by flt

For your second question, there is an answer in another website.

对于你的第二个问题,在另一个网站上有答案。

https://amp.reddit.com/r/Python/comments/2ak14j/made_my_first_android_app_in_under_5_hours_using/

https://amp.reddit.com/r/Python/comments/2ak14j/made_my_first_android_app_in_under_5_hours_using/

it says You can try code like this

它说你可以尝试这样的代码

if android:
    android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)

I hope it will help you.

我希望它会帮助你。