简体   繁体   English

在Python上同时运行两个函数

[英]Running two functions simultaneously on Python

I've searched far and wide on the internet and can't find a suitable situation for running my two functions at once. 我在互联网上进行了广泛搜索,找不到适合同时运行两个功能的情况。 Basically, one function contains the code for using arrow keys to move a smiley face around, and the other one makes a red circle fall from the top of the screen. 基本上,一个函数包含用于使用箭头键在周围移动笑脸的代码,另一个函数使红色圆圈从屏幕顶部掉落。 Here is my code in main.py : 这是我在main.py代码:

    import game, prepare
    from multiprocessing import Process
    prepare.setuppygame()
    prepare.setupSM()
    prepare.setupRC()

    #display score for the first time
    prepare.disscore()

    #set starting positions for first time
    game.setStartingPositions()

    if __name__ == '__main__':
      p1 = Process(target=game.go)
      p1.start()
      p2 = Process(target=game.RCfall)
      p2.start()
      p1.join()
      p2.join()

I'm not getting one screen with a falling circle and the ability to move around a smiley; 我的屏幕没有一个掉落的圆圈,也没有一个在笑脸周围移动的能力。 I get one screen with a smiley that you can move around and another pygame screen that has a falling circle, but not together. 我得到一个带有笑脸的屏幕,您可以在其中移动,而另一个pygame屏幕却有一个下降的圆圈,但不能一起移动。 Why isn't multiprocessing working? 为什么多处理不起作用? I'm also open to using threads, if they would work. 我也愿意使用线程,如果它们可以工作的话。

Infrastructure: Geany, Python2.7, Windows 7 but programming for Raspberry Pi 基础架构:Geany,Python2.7,Windows 7但为Raspberry Pi编程

I believe going through this tutorial on the pygame website 我相信在pygame网站上阅读本教程

http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html

will give you an idea on how to simply accomplish what you want. 将为您提供有关如何简单地完成所需内容的想法。 It shows how to make an image move by itself on the screen while you move a control image with the cursor. 它显示了如何使用光标移动控制图像时如何使图像在屏幕上自行移动。 Not precisely what you want but pretty damn close. 并非完全是您想要的,而是非常接近。

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

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