tanques/clase_bala.py
2023-06-01 00:39:26 +02:00

47 lines
1.8 KiB
Python

import pygame
from pygame.sprite import Sprite
#from clase_tanque import Tanque
class Bala(Sprite):
''' Clase para gestionar las balas disparadas '''
def __init__(self, juego, direccion):
''' Crear objeto desde posicion del tanque '''
super().__init__()
self.screen = juego.screen
self.settings = juego.settings
self.color = self.settings.bala_color
# Crear rectangulo para la bala en posicion (0,0) y luego la colocamos
self.rect = pygame.Rect(0,0, self.settings.bala_ancho, self.settings.bala_largo)
self.rect.midtop = juego.tanque.rect.midtop
self.direccion_bala = direccion
# defecto
self.x = float(self.rect.x)
self.y = float(self.rect.y)
if self.direccion_bala == 'Arr':
self.x = float(self.rect.x)
self.y = float(self.rect.y)
if self.direccion_bala == 'Aba':
self.x = float(self.rect.x)
self.y = float(self.rect.y) + 64
if self.direccion_bala == 'Dcha':
self.x = float(self.rect.x) + 31
self.y = float(self.rect.y) + 30
if self.direccion_bala == 'Izda':
self.x = float(self.rect.x) - 31
self.y = float(self.rect.y) + 30
def update(self):
if self.direccion_bala == 'Arr':
self.y -= self.settings.bala_velocidad
if self.direccion_bala == 'Aba':
self.y += self.settings.bala_velocidad
if self.direccion_bala == 'Dcha':
self.x += self.settings.bala_velocidad
if self.direccion_bala == 'Izda':
self.x -= self.settings.bala_velocidad
self.rect.x = self.x
self.rect.y = self.y
def pintar_bala(self):
pygame.draw.rect(self.screen, self.color, self.rect)