class SBullet(Bullet): images = {} delta = [Vector2(0, 0), Vector2(-0.5, -1), Vector2(-1, -0.5), Vector2(-0.5, 0), Vector2(0, -0.5)] def __init__(self, tank, location, direction): Bullet.__init__(self, tank) self.set_image(direction) self.rect = self.image.get_rect() self.set_location(location) #print self.rect self.speed = 200 self.mcontroller = MotionController(self, Vector2(StepX,StepY)) self.state = 0 self.atk = 40 def set_image(self, direction): self.direction = direction self.image = SBullet.images[direction] def set_location(self, location): x, y, w, h = self.rect self.location = self.rect.topleft = location + SBullet.delta[self.direction] * Vector2(w, h) def get_rect(self): return self.rect def get_direction(self): return self.direction def shoot(self): self.state = 1 def update(self, timepased): if self.state == 1: self.location, self.direction = self.mcontroller.update_state(timepased, self.direction) self.image = SBullet.images[self.direction] self.rect.topleft = self.location if self.collision_update(): self.kill()
class Tank(pygame.sprite.Sprite): images = {} delta = [Vector2(0, 0), Vector2(0.5, 0), Vector2(0, 0.375), Vector2(0.5, 1), Vector2(1, 0.375)] def __init__(self, location, direction): pygame.sprite.Sprite.__init__(self) self.direction = direction self.set_image(self.direction) self.rect = self.image.get_rect() self.set_location(location) self.speed = 80 self.mcontroller = MotionController(self, Vector2(StepX, StepY)) self.att = Attribute(self) self.name = "TANK" def shoot(self, shoot): if len(self.bullets.sprites()) > 0: return if shoot == 1: x, y, w, h = self.rect # print x,y,w,h pos = self.location + Tank.delta[self.direction] * Vector2(w, h) bullet = SBullet(self, pos, self.direction) bullet.add(self.bullets_group) bullet.add(self.bullets) bullet.shoot() SoundPlayer.play_shoot_sound() def set_image(self, direction): self.image = Tank.images[direction] def set_direction(self, direction): self.direction = direction self.set_image(self.direction) def set_location(self, location): self.rect.topleft = self.location = location def set_bullets(self, bullets): self.bullets_group = bullets self.bullets = pygame.sprite.RenderUpdates() def step_back(self): self.set_location(self.mcontroller.step_back()) def moved(self): dx, dy = self.location - self.mcontroller.lastlocation dx = abs(dx) dy = abs(dy) if (dx + dy) <= 0.00001: return False else: return True def on_attacked(self, atk): self.att.on_attacked(atk) def on_collision_with(self, mover): if not self.moved(): # print "not moved, return" return False # print "direction", self.direction if abs(self.direction - mover.direction) == 2: # self.step_back() # print "facing each other, step back" return True rect = self.rect.copy() # print "self.rect before", self.rect rect.topleft = self.mcontroller.step_back() # print "self.rect", self.rect # print "rect", rect if not rect.colliderect(mover.rect): # self.step_back() # print "initiative" return True # else: # print "passive" return False def blocked(self, rect): self.set_location(self.mcontroller.backwards(rect)) def collision_update(self): # collide_arr = sprite.spritecollide(self, enemy_tanks, 0) spritecollide = self.rect.colliderect for group in [enemy_tanks.sprites(), players.sprites()]: for s in group: if spritecollide(s.rect): if self.on_collision_with(s): self.step_back() return for group in [stones.sprites(), rivers.sprites(), bricks.sprites()]: for s in group: if spritecollide(s.rect): self.blocked(s.rect) return def update_control(self, timepased, direction, shoot): location, direction = self.mcontroller.update_state(timepased, direction) self.set_direction(direction) self.set_location(location) self.shoot(shoot) self.collision_update() self.att.update_att()