Pygame, self is not defined [closed]

2024/11/10 12:13:38

So I'm getting this error when I compile, I don't understand though because I've got 2 identical classes, one works fine the other throws out this error.

if self.currentState2==1: NameError: name 'self' is not defined


class EnemyShip(pygame.sprite.Sprite):def __init__(self, (x, y), playerShip):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("enemy.png")self.image = self.image.convert()tranColor = self.image.get_at((1, 1))self.image.set_colorkey(tranColor)self.rect = self.image.get_rect()self.enemyX = float(x)self.enemyY = float(y)self.ship = playerShipself.count = 0self.currentState2 = 0def update(self):self.count += 1x_component = self.ship.rect.centerx-self.enemyXy_component = self.ship.rect.centery-self.enemyYdistance = math.hypot(x_component, y_component)if distance < 100:self.currentState2 = 1print distanceelif distance > 100:self.currentState2 = 0print "test2"if self.count < 600:self.caculateNextPosition()self.rect.center = (self.enemyX, self.enemyY)elif self.count < 600:self.caculateNextPositionEvade()self.rect.center = (self.enemyX, self.enemyY)else:self.count = 0    def caculateNextPosition(self):shipX = self.ship.rect.centerxshipY = self.ship.rect.centeryif self.currentState2==1:if self.enemyY < shipY:self.enemyY += 4elif self.enemyY > shipY:self.enemyY -= 4if self.enemyX < shipX:self.enemyX += 4elif self.enemyX > shipX:self.enemyX -= 4elif self.currentState2==0: if self.enemyY < shipY:self.enemyY += 0elif self.enemyY > shipY:self.enemyY -= 0if self.enemyX < shipX:self.enemyX += 0elif self.enemyX > shipX:self.enemyX -= 0
Answer

Your indentation is incorrect in the method calculateNextPosition. As a result, the series of if statements are executed in the body of the class (not the method), where self isn't defined.

https://en.xdnf.cn/q/119774.html

Related Q&A

Python 3- assigns grades [duplicate]

This question already has answers here:Python 3- Assign grade(2 answers)Closed 8 years ago.• Define a function to prompt the user to enter valid scores until they enter a sentinel value -999. Have …

how to read video data from memory use pyqt5

i have an encrypted video file, i want to decrypt this file into memory and then use this data play video. but qt mediaplayer class is to pass a file name in, i need to have any good way?this is my co…

Pandas apply custom function to DF

I would like to create a brand new data frame by replacing values of a DF using a custom function. I keep getting the following error "ValueError: The truth value of a Series is ambiguous. Use a.e…

Economy Bot Daily Streak

I have a Discord.py economy bot that includes a daily command It gives everyone each day $50, but there is also a streak system. The first time they claim their daily, the bot gives them $50, day 2 is …

Normalise JSON with Python

Prompt me, please, how to normalize this JSON file using Python? This question is related to the previous The current JSON contains: {"total_stats": [{"domain": "domain.com&qu…

How to change decimal separator?

I have an Excel spreadsheet (an extract from SAP). I turn this into a DataFrame, do calculations and save it to an SQLite database. The Excel spreadsheet has comma as decimal separator. The SQLite data…

Unable to scrape the name from the inner page of each result using requests

Ive created a script in python making use of post http requests to get the search results from a webpage. To populate the results, it is necessary to click on the fields sequentially shown here. Now a …

Python Integer and String Using [duplicate]

This question already has an answer here:How can I concatenate str and int objects?(1 answer)Closed 7 years ago.for size in [1, 2, 3, 4]:result = 0print("size=" + str(size))for element in ra…

Beginner to python: Lists, Tuples, Dictionaries, Sets [duplicate]

This question already has an answer here:What is the difference between lists,tuples,sets and dictionaries? [closed](1 answer)Closed 3 years ago.I have been trying to understand what the difference is…

TypeError: NoneType object is not iterable in Python in csv

I am new to python, and trying to create a program which opens a csv file. The user is supposed to enter a barcode , then the program finds that product and the cost of the product. However I got an er…