Jump to content
  • Advertisement
Sign in to follow this  
colts18

Collision Detection issues

This topic is 2757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,


I am creating a game and I am getting an error I have never gotten before. I see that it says self is not defined but I don't see how it is not defined. I cannot figure out how to fix it can some one help?

Here is the error:
Traceback (most recent call last):
File "/home/bolt/Python/game.py", line 95, in <module>
tankcollide = pygame.sprite.spritecollide(self.tank, self.enemy_tank)
NameError: name 'self' is not defined

Here is my code:
import pygame

class Tank(pygame.sprite.Sprite):

def __init__(self):
self.Tank = pygame.image.load("tank.png")
self.x = -200
self.y = 20
self.firecountdown=0
self.rect = self.Tank.get_rect()

def draw(self, surface):
surface.blit(self.Tank, (self.x, self.y))
def update(self, dt):
if(self.firecountdown > 0):
self.firecountdown-= dt
def fire(self, bullet):
if(self.firecountdown <=0) :
bullet.x = self.x
bullet.y = self.y
bullet.fired = True
bullet.speed = 10
self.firecountdown = 1000


class Bullet(pygame.sprite.Sprite):
def __init__(self):
self.bullet = pygame.image.load("tank_bullet.png")
self.y = 0
self.x = 0
self.speed = 0
self.forwardx = 1
self.forwardy = 0
self.fired = False
def draw(self, surface):
surface.blit(self.bullet, (self.x, self.y))
def update(self, dt):
self.x += self.forwardx * self.speed
self.y += self.forwardy * self.speed
if self.x > 640:
self.fired = False

class Lvl_1:
def __init__(self):
self.floor = pygame.image.load("floor.png")
def draw(self, surface):
surface.blit(self.floor, (0, 0))

class Enemy_tank(pygame.sprite.Sprite):
def __init__(self):
self.enemy_tank = pygame.image.load("enemy_tank.png")
self.rect = self.enemy_tank.get_rect()
def draw(self, surface):
surface.blit(self.enemy_tank, (200, -40))
def update(self, dt):
return

class Enemy_bullet(pygame.sprite.Sprite):

def __init__(self):
self.enemy_bullet = pygame.image.load("enemy_bullet.png")
self.y = 0
self.x = 0
self.speed = 0
self.forwardx = 1
self.forwardy = 0
self.fired = False
def draw(self, surface):
surface.blit(self.enemy_bullet, (self.x, self.y))
def update(self, dt):
self.x += self.forwardx * self.speed
self.y += self.forwardy * self.speed
if self.x > 640:
self.fired = False



pygame.init()
background_color = (0,0,0)
(width, height) = (640, 480)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Game Window')
clock = pygame.time.Clock()
dt = clock.tick(30)
lvl_1 = Lvl_1()
bullet = Bullet()
enemy_tank = Enemy_tank()
Tank = Tank()
enemy_bullet = Enemy_bullet()
pygame.key.set_repeat(1,50)
running = True

while running:

tankcollide = pygame.sprite.spritecollide(self.tank, self.enemy_tank)
for t in tankcollide:
if self.tank.colliderect(enemy_tank.rect):
self.tank.centerx += 1

Tank.update(dt)
bullet.update(dt)

screen.fill (background_color)
Tank.draw(screen)
lvl_1.draw(screen)
if bullet.fired == True:
bullet.draw(screen)
enemy_tank.draw(screen)

pygame.display.flip()
clock.tick(30)

for event in pygame.event.get():


if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
Tank.x += 5
if event.key == pygame.K_LEFT:
Tank.x -= 5
if event.key == pygame.K_SPACE:
Tank.fire(bullet)

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!