Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing module error
#5
I imported gaming into char.py to access the variables of gaming.py.

char.py
import random,pygame,Game

class Char():
    
    def __init__(self):
        pass
    
    def get_position(self,mapheight,mapwidth,floormap):
        while True:
            column = random.randint(2, mapheight - 3)
            row = random.randint(2, mapwidth - 3)
            if floormap[column][row] == "dirt":
                return [row, column]
    
    def bloock(self,ydir, xdir, pos, direct, nocrashdir,getblock,playerpos,floormapid,floormap):

        if getblock == Game.coal or getblock == Game.coal1 or getblock == Game.coal2:

            pygame.mixer.music.load("coal.wav")
            pygame.mixer.music.play()

            if getblock == Game.coal or Game.coal1:
                Game.click = floormapid[playerpos[1]+ydir][playerpos[0]+xdir]
                Game.click += 1
                floormapid[playerpos[1]+ydir][playerpos[0]+xdir] = Game.click
            if getblock == Game.coal and floormapid[playerpos[1]+ydir][playerpos[0]+xdir] == 4:
                floormap[playerpos[1]+ydir][playerpos[0]+xdir] = Game.coal1
            if getblock == Game.coal1 and floormapid[playerpos[1]+ydir][playerpos[0]+xdir] == Game.coaldur:
                floormap[playerpos[1]+ydir][playerpos[0]+xdir] = Game.coal2
            if getblock == Game.coal2:
                Game.click = 0
                floormap[playerpos[1]+ydir][playerpos[0]+xdir] = Game.dirt
When i run char.py, it is working perfectly, but char.py is not my main working module.It is gaming.py. However, when i run gaming.py it still says " module "char" has no attribute "Char" ".So, i am suppose to initialize gaming.py to play the game, not char.py. Isn't is true?

These are some important points of gaming.py. Maybe i am making a mistake at these points;

gaming.py
import pygame,sys,random,time
import char

pygame.init()
pygame.mixer.init()


tilesize = 50
...
charclass=char.Char()

playerpos = charclass.get_position(mapheight,mapwidth,floormap)
monsterpos = charclass.get_position(mapheight,mapwidth,floormap)
monster1pos = charclass.get_position(mapheight,mapwidth,floormap)

while True:

   for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and playerpos[0] <= mapwidth - 1:
            if playerpos[0]==mapwidth-1:
                playerdir = playerright
            else:
                playerdir = playerright
                getblock = floormap[playerpos[1]][playerpos[0]+1]
                ydir = 0
                xdir = 1
                pos = 0
                direct = playerright
                nocrashdir = 1
                charclass.bloock(ydir, xdir, pos, direct, nocrashdir,getblock,playerpos,floormapid,floormap)
Reply


Messages In This Thread
Importing module error - by walernor - Feb-01-2019, 04:16 PM
RE: Importing module error - by ichabod801 - Feb-01-2019, 04:21 PM
RE: Importing module error - by walernor - Feb-01-2019, 04:56 PM
RE: Importing module error - by ichabod801 - Feb-01-2019, 06:18 PM
RE: Importing module error - by walernor - Feb-02-2019, 08:50 AM
RE: Importing module error - by ichabod801 - Feb-02-2019, 04:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 307 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 984 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  pyarrow error when importing pandas sravva 1 973 Jun-06-2023, 05:09 PM
Last Post: snippsat
  My code displays too much output when importing class from a module lil_e 4 1,203 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Importing module in jupyter Noteboook ajitnayak1987 0 1,772 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  ERROR: importing desired module mbgamer28 0 1,701 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
Bug Error while importing numpy Erfan 3 3,318 Nov-28-2020, 07:49 AM
Last Post: bowlofred
  importing module - not working jdhamblett 3 3,065 Jun-22-2020, 07:33 PM
Last Post: jdhamblett
  importing same python library in multiple custom module escape_freedom13 6 3,884 May-10-2020, 07:01 PM
Last Post: escape_freedom13
  vlc module error pythonprogrammer 1 2,905 Apr-23-2020, 04:16 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020