Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.
Your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.
If you’re using python 3+ this may also occur if you’re using private variables that start with double underscore, e.g., self.__yourvariable. Just something to take note of for some of you who may run into this issue.
hello, i am working on a game using pygame and i am going for an OOP approach, the files are pretty big, i will post only a portion of the code; also i am using pycharm so i know for a fact my identation, typing are alright.. plus i have been coding in python for a while now but i just can't grasp onto this issue :(, so far in the game i am just in the main menu phase and whenever i try to run my code i get an error that my game object (which has a reference to another class including the main menu game loop) has no attribute ''screenWidth'' but it actually does have a variable for my screen.. i just use it in the main menu to set another midWidth variable half of that variable as value
here is the code:
main.py
from game import Game
myGame = Game()
while myGame.running:
myGame.currentMenu.drawMainMenu()
myGame.gameLoop()game.py
import pygame
from menu import MainMenu
class Game:
def __init__(self):
pygame.init()
self.running, self.playing = True, False
self.upKey, self.downKey, self.selectKey, self.backKey = False, False, False, False
self.currentMenu = MainMenu(self)
self.screenWidth = 1280
self.screenHeight = 720
self.gameWindow = pygame.display.set_mode((self.screenWidth, self.screenHeight))
pygame.display.set_caption("Game Prototype")
self.backgroundImage = pygame.image.load('Assets/Images/bg_greek.jpg')
self.backgroundImage = pygame.transform.scale(self.backgroundImage, (1920, 1080))
def gameLoop(self):
while self.playing:
self.checkEvents()
if self.selectKey:
self.playing = False
self.gameWindow.blit(self.backgroundImage, (-600, -300))
pygame.display.flip()
self.resetKeys()
menu.py
import pygame
class Menu:
def __init__(self, game):
self.gameClass = game
self.midWidth = self.gameClass.screenWidth / 2
self.midHeight = self.gameClass.screenHeight / 2
self.showMenu = True
self.selectionX, self.selectionY = 0, 0
def drawSelection(self):
selectSurface = pygame.Surface((260, 100), pygame.SRCALPHA)
selectSurface.fill((50, 50, 50, 175))
selectRect = selectSurface.get_rect()
selectRect.center = (self.selectionX, self.selectionY)
self.gameClass.gameWindow.blit(selectSurface, selectRect)
class MainMenu(Menu):
def __init__(self, game):
Menu.__init__(self, game)
self.hoveredState = "Start"
self.startX, self.startY = self.midWidth, self.midHeight
self.optionsX, self.optionsY = self.midWidth, self.midHeight + 100
self.creditsX, self.creditsY = self.midWidth, self.midHeight + 200
self.quitX, self.quitY = self.midWidth, self.midHeight + 300
self.selectionX, self.selectionY = self.startX, self.startY
def drawMainMenu(self):
self.showMenu = True
while self.showMenu:
self.gameClass.checkEvents()
self.checkInput()
self.gameClass.gameWindow.blit(self.gameClass.backgroundImage, (-600, -300))
self.drawSelection()
self.gameClass.drawText("Play", 72, self.startX, self.startY)
self.gameClass.drawText("Options", 72, self.optionsX, self.optionsY)
self.gameClass.drawText("Credits", 72, self.creditsX, self.creditsY)
self.gameClass.drawText("Quit", 72, self.quitX, self.quitY)
pygame.display.flip()
self.gameClass.resetKeys()
it is not complete though