Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyGame] How to create a sprite in a different module
#1
I am trying to make a Space-Invaders-type game with PyGame and I want to separate some of the classes into different modules and then call those classes from my main module.
The problem I have is that when I create a sprite in a module other than my main, I get an error.

İmage


This is because I am trying to add the sprite to the sprite group that belongs to my main module. I know that many problems can arise when importing two modules from each other but I'm not sure how else to solve the problem. I am still new to coding games and would really appreciate any help thrown my way!

İmage


self.all_sprites is the group that I want to add my bullet sprites to. The bullet sprites are initiated in a different module shown below.

İmage


Would it work if I create a sprites group in the module that it is initiated in and then call that group from the main module? I haven't tried this yet but are there any other good solutions to this?
metulburr wrote Aug-15-2019, 10:12 AM:
Please do not post images of text/code/errors.

Copy and paste the code and/or errors using the forums BBCode.
Quote
#2
(Aug-15-2019, 03:04 AM)I_like_pepperjack Wrote: I know that many problems can arise when importing two modules from each other but I'm not sure how else to solve the problem.
This is called circular imports and it will never work. It means you are structuring your code incorrectly.

I still do not follow what you are asking? All i see are errors which does not give a lot of information. What is your directory structure of the game and al its modules? To be honest it would be better to upload your game to github to be able to run the code and see the directory structure.

Please do not post images of text. It makes it harder going to and from another site to view the error. Most of the time I ignore users that post images of text instead of using BBCode. If you following the rules of the forum you would be more apt to get responses. IT also illustrates to everyone that you have not read our rules.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Need help making a sprite GalaxyCoyote 4 298 Aug-11-2019, 09:12 PM
Last Post: metulburr
  creating sprite mask pfaber11 5 495 Jun-12-2019, 09:39 PM
Last Post: pfaber11
  [PyGame] assigning rect to sprite pfaber11 1 257 May-18-2019, 05:39 PM
Last Post: metulburr
  moving a sprite pfaber11 3 478 May-15-2019, 12:52 PM
Last Post: pfaber11
  [PyGame] Need Help With Sprite ghost0fkarma 2 883 Jan-09-2018, 02:14 PM
Last Post: ghost0fkarma

Forum Jump:


Users browsing this thread: 1 Guest(s)