2

I'm programming a tower defence game in pygame. I want to split my code into 3 different python files and run them as modules.

I want one file to contain the code needed for the menu screen, another for the main game loop and another file to contain the code for generating a map and blitting it onto the screen.

Is it possible to blit from one file to a pygame display created in another python file? If so how?

Also, any other advice on the most appropriate way to modulate my code would be very helpful.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
omnistat
  • 63
  • 5
  • 2
    Have you tried? I mean, the `Surface.blit()` method requires a source surface in the 1st parameter, so, as lons as you pass it in the call, it does not matter in wich module the code resides. You only have to import them correctly. Can you add some piece of code to clarify your question? – Gomes J. A. Jan 23 '18 at 01:51
  • 3
    Modules/files are just a way to organize your code in the same way a helper function helps keep code seperate and organized. There's no technical limitation specific to pygame or blitting. It kind of sounds like you just need to read up basic python code organization. Since this is a really vague broad question, I can't really help much more than this. You may want to look up a basic python multiple-files tutorial. – Blake O'Hare Jan 23 '18 at 01:53
  • 1
    I have [this old project](https://github.com/overdev/SpaceGame/tree/master/spacegame) that, maybe, serve as an example. It has blitting code outside the main loop, among other things. Take a look. – Gomes J. A. Jan 23 '18 at 02:00
  • Alright so i've managed to get round to testing and i think the issue i'm having mainly is importing variables across from the main module to the second one. For example `screen = pg.display.set_mode((game_screen_w + 200, game_screen_h))` is required for me to use `screen.fill(BLUE)`as its the location i need to blit to but i'm not sure how to import that to the second module. `from main import *` doesn't work and listing each variable would be too tedious. – omnistat Jan 25 '18 at 18:13

0 Answers0