• Advertisement
Sign in to follow this  

Callable Object in Python

This topic is 2458 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 a little confused, I am getting an error that says Tank is not callable. I am not sure what to do; it was working fine until I added the collision detection. Does anyone know how to fix this?


import pygame
import math

class Tank(pygame.sprite.Sprite):

def __init__(self, init_pos, is_npc=True):

pygame.sprite.Sprite.__init__(self)
self.Tank = pygame.image.load("tank.png")
self.x = -200
self.y = 20
self.firecountdown=0

self.center = init_pos
self.rect = self.Tank.get_rect()

self.velocity = [0, 0]

self.is_npc = bool(is_npc)

def _can_move_to(self, new_rect):
for sprite in self.groups()[0]:
if sprite is not self:
return not sprite.rect.colliderect(new_rect)

def draw(self, surface):
surface.blit(Tank, (self.x, self.y))

def update(self, dt,):
keys = pygame.key.get_pressed()

if not self.is_npc:
if keys[pygame.K_RIGHT]:
self.velocity[0] = 250
elif keys[pygame.K_LEFT]:
self.velocity[0] = -250
else:
self.velocity[0] = 0

velocity = [v * t_passed for v in self.velocity]

if self._can_move_to(self.rect.move(*velocity)):
self.rect.move_ip(*velocity)

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):
pygame.sprite.Sprite.__init__(self)
self.x = 200
self.y = -40
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, (self.x, self.y))
def update(self, dt):
pass

class Enemy_bullet(pygame.sprite.Sprite):
pass

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()
t_passed = clock.tick(30)
Tank = Tank(pygame.sprite.Sprite)
lvl_1 = Lvl_1()
bullet = Bullet()
enemy_tank = Enemy_tank()
enemy_bullet = Enemy_bullet()
pygame.key.set_repeat(1,50)
running = True
speed = 100

t_accumalator = 0.0
t_step = 0.01

tanks = pygame.sprite.Group()
tanks.add (Tank((100, 150), False))
tanks.add (Tank((300, 150)))


while running:

t_accumalator += clock.tick() / 1000.0

for t in tanks:
t.update(t_passed)

bullet.update(t_passed)

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)

pygame.event.pump()

for event in pygame.event.get():

if event.type == pygame.QUIT:
running = False
exit()
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)

while t_accumalator >= t_step:
for tank in tanks:
tank.update(t_step, keys)

t_accumalator -= t_step
pygame.display.flip()

Share this post


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

  • Advertisement