Skip to content

Instantly share code, notes, and snippets.

@annibal
Created June 25, 2013 14:43
Show Gist options
  • Save annibal/5859014 to your computer and use it in GitHub Desktop.
Save annibal/5859014 to your computer and use it in GitHub Desktop.
Charger Tanker code, backup from when enemyes spawn in patterns. Orientation sucks in this =D
import pygame, sys, random, time, math;
class button:
def __init__(self, screen, color, size):
self.screen = screen;
self.color = color;
self.size = size;
def draw(self, x,y):
self.x = x;
self.y = y;
pygame.draw.rect(self.screen, self.color, ((x-self.size/2,y-self.size/2),(self.size,self.size)));
def click(self, x, y):
if((x > self.x - self.size/2) & (x < self.x + self.size/2) & (y > self.y - self.size/2) & (y < self.y + self.size/2)):
pygame.draw.rect(self.screen, (255,255,255), ((self.x-self.size/2,self.y-self.size/2),(self.size,self.size)));
return True;
else:
return False;
import math;
from core import Core;
class Collider:
x = 0;
y = 0;
width = 0;
height = 0;
def __init__(self,x,y,w,h,sign = True):
self.w = w;
self.h = h;
self.x = x;
self.y = y;
if(sign):
Core.signCollider(self);
def hitTest(self,collider):
# print("self.x + self.w: " + str(self.x + self.w) + " <= collider.x - collider.w: " + str(collider.x - collider.w) + "\n"
# "self.x - self.w: " + str(self.x - self.w) + " >= collider.x + collider.w: " + str(collider.x + collider.w) + "\n"
# "self.y + self.h: " + str(self.y + self.h) + " <= collider.y - collider.h: " + str(collider.y - collider.h) + "\n"
# "self.y - self.h: " + str(self.y - self.h) + " <= collider.y + collider.h: " + str(collider.y + collider.h) + "\n"
# );
if ((self.x + self.w >= collider.x - collider.w) &
(self.x - self.w <= collider.x + collider.w) &
(self.y + self.h >= collider.y - collider.h) &
(self.y - self.h <= collider.y + collider.h)):
return True;
else :
return False;
def gotoXY(self,x,y):
self.x = x;
self.y = y;
def getRect(self):
return ((self.x,self.y),(self.w,self.h));
def getOrbitX(self,angle,distance):
pos = math.sin(math.radians(angle))*distance;
return pos;
def getOrbitY(self,angle,distance):
pos = math.cos(math.radians(angle))*distance;
return pos;
def move(self,direction,velocity):
self.x += self.getOrbitX(direction, velocity);
self.y += self.getOrbitY(direction, velocity);
def pointAngle(self,point_x,point_y):
ang = math.degrees(math.atan2(self.y - point_y, self.x - point_x))-90;
return ang;
def pointDistance(self,point_x,point_y):
dis = math.sqrt((self.x-point_x)*(self.x-point_x) + (self.y-point_y)*(self.y-point_y));
return dis;
import pygame;
class Core:
explosions = [];
colliders = []
explosionImages = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
offset_x = 0;
offset_y = 0;
@classmethod
def getTiles(self,file,width,height):
image = pygame.image.load(file);
imageWidth, imageHeight = image.get_size();
colunas = imageWidth / width;
linhas = imageHeight / height;
tilesArray = [];
tilesArray.append(pygame.image.load('img/cannonLeft0.png').convert_alpha());
for l in range(linhas):
for c in range(colunas):
x = c*width;
y = l*height;
subimagem = image.subsurface(x,y,width,height);
tilesArray.append(subimagem);
return tilesArray;
@classmethod
def signExplosion(self, obj):
self.explosions.append(obj);
@classmethod
def unsignExplosion(self,obj):
self.explosions.remove(obj);
@classmethod
def signCollider(self,obj):
self.colliders.append(obj);
@classmethod
def unsignCollider(self,obj):
self.colliders.remove(obj);
@classmethod
def loadExplosions(self):
explosion1 = [];
for i in range(7):
auxImg = pygame.image.load('img/explosion1/simple_' + str(i) + ".png").convert_alpha();
explosion1.append(pygame.transform.scale(auxImg,(16,16)));
self.explosion1_frames = 7;
self.explosionImages[1] = explosion1;
self.explosionImages[3] = self.getTiles('img/explosion3.png', 64, 64);
self.explosionImages[4] = self.getTiles('img/explosion4.png', 256, 256);
self.explosionImages[5] = self.getTiles('img/explosion5.png', 64, 64);
self.explosionImages[6] = self.getTiles('img/explosion6.png', 128, 128);
@classmethod
def explosionStep(self):
for lol in self.explosions:
lol.step();
@classmethod
def move(self,x,y):
self.offset_x += x;
self.offset_y += y;
for kkk in self.colliders:
kkk.gotoXY(kkk.x + x, kkk.y + y);
import pygame,math;
from core import Core;
from collider import Collider;
class EnemyPointer:
def __init__(self,screen,distance,width,height):
self.arrows = [];
self.util = Collider(width/2,height/2.3,0,0,False);
self.screen = screen;
self.d = distance;
def step(self,trooperSpawner1,trooperSpawner2,player):
util = player.collider;
for dude in trooperSpawner1.troop:
if(self.util.pointDistance(dude.collider.x,dude.collider.y) > self.d*1.1):
self.draw(-self.util.pointAngle(dude.collider.x, dude.collider.y),dude.stats.getHP());
for dude in trooperSpawner2.troop:
if(self.util.pointDistance(dude.collider.x,dude.collider.y) > self.d*3):
self.draw(-self.util.pointAngle(dude.collider.x, dude.collider.y),dude.stats.getHP());
def draw(self,angle,health):
angle += 180;
pygame.draw.polygon(self.screen,(255,255,255),(
(self.util.x + self.util.getOrbitX(angle, self.d),self.util.y + self.util.getOrbitY(angle, self.d)),
(self.util.x + self.util.getOrbitX(angle+1.5, self.d-12),self.util.y + self.util.getOrbitY(angle+1.5, self.d-12)),
(self.util.x + self.util.getOrbitX(angle-1.5, self.d-12),self.util.y + self.util.getOrbitY(angle-1.5, self.d-12))), 0);
pygame.draw.polygon(self.screen,(255-health*2.5,2.5*health,2.5*health),(
(self.util.x + self.util.getOrbitX(angle, self.d+4.2),self.util.y + self.util.getOrbitY(angle, self.d+4.2)),
(self.util.x + self.util.getOrbitX(angle+3.1, self.d-15),self.util.y + self.util.getOrbitY(angle+3.1, self.d-15)),
(self.util.x + self.util.getOrbitX(angle-3.1, self.d-15),self.util.y + self.util.getOrbitY(angle-3.1, self.d-15))), 2);
import pygame, math;
from collider import *;
from core import Core;
class Explosion:
collider = Collider(0,0,0,0);
def __init__(self,screen,collider,explosionNumber):
self.collider = collider;
Core.signExplosion(self);
self.screen = screen;
self.frames = Core.explosion1_frames;
self.images = Core.explosionImages[explosionNumber];
self.currentFrame = 0;
def draw(self):
bodyx = self.collider.x-self.collider.w;
bodyy = self.collider.y-self.collider.h;
self.screen.blit(self.images[self.currentFrame],(bodyx,bodyy));
def step(self):
self.currentFrame += 1;
if (self.currentFrame >= self.frames):
self.die();
self.currentFrame = 0;
self.draw();
def die(self):
Core.unsignExplosion(self);
import pygame,math;
class HUD:
def setChargeBars(self,leftBar,rightBar):
self.leftChargeAmount = leftBar;
self.rightChargeAmount = rightBar;
self.maxCharge = 100;
def setHealth(self,health,shield,ammo):
self.health = health;
self.shield = shield;
self.ammo = ammo;
def __init__(self,screen,width,height,lifebarSize,maxHealth,maxAmmo):
self.screen = screen;
self.width = width;
self.height = height;
self.lifeBarSize = lifebarSize;
self.maxHealth = maxHealth;
self.maxAmmo = maxAmmo;
def draw(self,thickness):
middle_x = self.width / 2;
lifeBarBegin = middle_x - self.lifeBarSize/2;
lifeBarEnd = lifeBarBegin + self.lifeBarSize;
self.hpSize = (self.lifeBarSize * self.health)/self.maxHealth;
chargeBulbs_space = (self.width-self.lifeBarSize);
clotSize = (chargeBulbs_space - (chargeBulbs_space*0.6))/4;
containerMargin = thickness/32;
leftEnergyBegin = chargeBulbs_space/2 - clotSize;
leftEnergyEnd = clotSize;
rightEnergyBegin = self.width - chargeBulbs_space/2 + clotSize;
rightEnergyEnd = self.width - clotSize;
energySize = chargeBulbs_space/2 - clotSize*2;
leftEnergySize = (self.leftChargeAmount*energySize)/self.maxCharge;
rightEnergySize = (self.rightChargeAmount*energySize)/self.maxCharge;
sep="=========================================================================<<<";
# hp
pygame.draw.rect(self.screen, (20,230,20), ((lifeBarBegin,self.height-thickness*2),(self.hpSize,thickness)), 0);
#shield
#pygame.draw.rect(self.screen, (20,20,230), ((lifeBarBegin,self.height-thickness/2),(self.hpSize,thickness/2)), 0);
# hp container
pygame.draw.rect(self.screen, (30,30,30), ((lifeBarBegin,self.height-thickness*2),(self.lifeBarSize,thickness)), 2);
#chargebar clots
pygame.draw.rect(self.screen, (60,30,60), ((0,self.height-thickness*2),(clotSize,thickness*2)),0);
pygame.draw.rect(self.screen, (60,30,60), ((chargeBulbs_space/2-clotSize,self.height-thickness*2),(clotSize,thickness*2)),0);
pygame.draw.rect(self.screen, (60,30,60), ((self.width-clotSize,self.height-thickness*2),(clotSize,thickness*2)),0);
pygame.draw.rect(self.screen, (60,30,60), ((self.width-chargeBulbs_space/2,self.height-thickness*2),(clotSize,thickness*2)),0);
#chargebar Container
pygame.draw.rect(self.screen, (30,30,30), ((clotSize,self.height+containerMargin-thickness*2),(chargeBulbs_space/2-clotSize*2,thickness*2-containerMargin*2)), 2);
pygame.draw.rect(self.screen, (30,30,30), ((self.width-chargeBulbs_space/2+clotSize,self.height+containerMargin-thickness*2),(chargeBulbs_space/2-clotSize*2,thickness*2-containerMargin*2)), 2);
#charge Amount
pygame.draw.rect(self.screen, (20,20,230), ((leftEnergyBegin-leftEnergySize,self.height-thickness*2+containerMargin*2),(leftEnergySize,thickness*2-containerMargin*4)), 0);
pygame.draw.rect(self.screen, (20,20,230), ((rightEnergyBegin,self.height-thickness*2+containerMargin*2),(rightEnergySize,thickness*2-containerMargin*4)), 0);
#bullets
bulletSize = self.lifeBarSize/self.maxAmmo;
for i in range(self.ammo):
pygame.draw.polygon(self.screen,(230,230,20),
((lifeBarBegin+(i*bulletSize), self.height),
(lifeBarBegin+(i*bulletSize), self.height-3*(thickness/4)),
(lifeBarBegin+(i*bulletSize)+bulletSize/2, self.height-thickness),
(lifeBarBegin+(i*bulletSize)+bulletSize, self.height-3*(thickness/4)),
(lifeBarBegin+(i*bulletSize)+bulletSize, self.height)),0);
pygame.draw.rect(self.screen, (200,200,25), ((lifeBarBegin+(i*bulletSize),self.height-thickness/4),(bulletSize,thickness/4)),0);
import pygame, sys, random, time, math;
from pygame.locals import *;
#from stats import stats;
#from button import button;
from HUD import HUD;
from tank import Tank;
from trooper import Trooper;
from trooperSpawner import TrooperSpawner;
from core import Core;
from collider import Collider;
from enemyPointer import EnemyPointer;
width = 640
height = 480
pygame.init();
screen=pygame.display.set_mode((width, height),0,32);
charge_left = 0;
charge_right = 0;
key_w = False;
key_a = False;
key_s = False;
key_d = False;
mouseRight = False;
mouseLeft = False;
l_shift = False;
mouse_x = 0;
mouse_y = 0;
totalThickness = 60;
iu = Tank(screen,100,240,30,30,10,4,100);
iu.initStats(50.0, 6.0, 30, 680, 40);
iu.stats.apply_shield(1000, 10000);
bg = pygame.image.load("img/fatec_mapa.png").convert_alpha();
bgpos = Collider(-500,-500,2000,2000);
bg = pygame.transform.scale(bg,(bgpos.w,bgpos.h));
enemyPointer = EnemyPointer(screen, 180,width,height);
viewWideness = 30 + iu.collider.w;
VW_LX = viewWideness;
VW_TY = viewWideness;
VW_RX = width - viewWideness;
VW_BY = height - viewWideness - totalThickness;
Core.loadExplosions();
trooper1 = Trooper(screen, 300, 240, 10, 10, 3, 30, 4);
trooper1.initStats(5, 0, 9, 280, 0);
trooper2 = Trooper(screen, 330, 240, 10, 10, 3, 30, 4);
trooper2.initStats(5, 0, 9, 280, 0);
trooper3 = Trooper(screen, 360, 240, 10, 10, 3, 30, 4);
trooper3.initStats(5, 0, 9, 280, 0);
trooper4 = Trooper(screen, 390, 240, 10, 10, 3, 30, 4);
trooper4.initStats(5, 0, 9, 280, 0);
trooper5 = Trooper(screen, 420, 240, 10, 10, 3, 30, 4);
trooper5.initStats(5, 0, 9, 280, 0);
trooper6 = Trooper(screen, 450, 240, 10, 10, 3, 30, 4);
trooper6.initStats(5, 0, 9, 280, 0);
trooper7 = Trooper(screen, 480, 240, 10, 10, 3, 30, 4);
trooper7.initStats(5, 0, 9, 280, 0);
trooper8 = Trooper(screen, 510, 240, 10, 10, 3, 30, 4);
trooper8.initStats(5, 0, 9, 280, 0);
trooper9 = Trooper(screen, 540, 240, 10, 10, 3, 30, 4);
trooper9.initStats(5, 0, 9, 280, 0);
trooper10 = Trooper(screen, 570, 240, 10, 10, 3, 30, 4);
trooper10.initStats(5, 0, 9, 280, 0);
trooper11 = Trooper(screen, 570, 240, 10, 10, 3, 30, 4);
trooper11.initStats(85, 0, 9, 1000, 0);
trooperSpawner = TrooperSpawner(screen, 20, 50, width, height-totalThickness, 20);
trooperSpawner2 = TrooperSpawner(screen, 20, 150, width, height-totalThickness, 20);
hud = HUD(screen,width,height,300,680,iu.ammo);
def drawWrite(x,y,word):
font = pygame.font.SysFont("Arial",17);
label = font.render(word,1,(220,220,220));
screen.blit(label, (x,y));
while(True):
screen.fill((0, 0, 0));
clock = pygame.time.Clock();
clock.tick(30);
for e in pygame.event.get():
if(e.type == QUIT):
exit();
if(e.type == MOUSEBUTTONDOWN):
mouse_x = e.pos[0];
mouse_y = e.pos[1];
iu.setHeadDirection(mouse_x, mouse_y);
if(e.button == 3):
mouseRight = True;
iu.shoot("right",False);
if(e.button == 1):
mouseLeft = True;
iu.shoot("left",False);
if(e.type == MOUSEBUTTONUP):
mouse_x = e.pos[0];
mouse_y = e.pos[1];
iu.setHeadDirection(mouse_x, mouse_y);
if(e.button == 3):
mouseRight = False;
iu.shoot("right",True);
if(e.button == 1):
mouseLeft = False;
iu.shoot("left",True);
if(e.type == KEYDOWN):
if(e.key == K_LSHIFT): l_shift = True;
if(e.key == K_w): key_w = True;
if(e.key == K_a): key_a = True;
if(e.key == K_s): key_s = True;
if(e.key == K_d): key_d = True;
if(e.type == KEYUP):
if(e.key == K_LSHIFT): l_shift = False;
if(e.key == K_w): key_w = False;
if(e.key == K_a): key_a = False;
if(e.key == K_s): key_s = False;
if(e.key == K_d): key_d = False;
if(e.type == MOUSEMOTION):
mouse_x = e.pos[0];
mouse_y = e.pos[1];
if (mouseRight):
if(charge_right < 100):
charge_right += 2;
else:
charge_right = 0;
if (mouseLeft):
if(charge_left < 100):
charge_left += 2;
else:
charge_left = 0;
#====================================================
screen.blit(bg,(bgpos.x,bgpos.y));
#trooper1.step(iu);
#trooper2.step(iu);
#trooper3.step(iu);
#trooper4.step(iu);
#trooper5.step(iu);
#trooper6.step(iu);
#trooper7.step(iu);
#trooper8.step(iu);
#trooper9.step(iu);
#trooper10.step(iu);
#trooper11.step(iu);
trooperSpawner.step(iu);
trooperSpawner2.step(iu);
if (iu.collider.x <= VW_LX):
Core.move(abs(iu.speed), 0);
iu.collider.x = VW_LX;
if (iu.collider.x >= VW_RX):
Core.move(-abs(iu.speed), 0);
iu.collider.x= VW_RX;
if (iu.collider.y <= VW_TY):
Core.move(0, abs(iu.speed));
iu.collider.y = VW_TY;
if (iu.collider.y >= VW_BY):
Core.move(0, -abs(iu.speed));
iu.collider.y = VW_BY;
iu.step(key_w,key_s,key_a,key_d,mouseLeft, mouseRight,l_shift,mouse_x,mouse_y);
iu.setCharge(charge_left, charge_right);
Core.explosionStep();
enemyPointer.step(trooperSpawner, trooperSpawner2, iu);
hud.setChargeBars(charge_left, charge_right);
hud.setHealth(iu.stats.constitution, iu.stats.shield, iu.ammo);
hud.draw(30);
drawWrite(10, 390,"Def: " + str(iu.stats.defense ));
drawWrite(160, 390,"Pwr: " + str(iu.stats.power));
drawWrite(310, 390,"Speed: " + str(iu.stats.speed));
drawWrite(460, 390,"Shield: " + str(iu.stats.shield));
pygame.display.update();
import pygame,math;
class miniHP:
def draw(self,who,sizeMultiplier,offset_y,offset_x):
sm = sizeMultiplier
ofx = offset_x;
ofy = offset_y;
rectWho = who.collider;
hp = who.stats.getHP();
maxhp = who.stats.base_constitution;
x = rectWho.x - (rectWho.w/2)*sm + ofx;
y = rectWho.y + ofy;
w = (rectWho.w)*sm;
h = (rectWho.h/4)*sm;
hpsize = (w * hp)/maxhp;
pygame.draw.rect(who.screen,(0,200,0),((x,y),(hpsize,h)),0);
pygame.draw.rect(who.screen,(200,200,200),((x,y),(w,h)),1);
import pygame, math;
from stats import *;
from collider import *;
from explosion import Explosion;
class Shot:
collider = Collider(0,0,0,0);
stats = stats(0, 0, 1, 0, 0);
direction = 0;
speed = 0;
fullspeedTime = 0
ammo = 0
def __init__(self,screen,image,x,y,w,z,speed,direction,lifetime,power):
self.collider = Collider(x,y,w,z);
self.screen = screen;
self.direction = direction;
self.speed = speed;
self.lifetime = lifetime;
self.stats = stats(0,0,power + 0.0,0,0)
self.headImage = pygame.image.load(image).convert_alpha();
self.headImage = pygame.transform.scale(self.headImage,(self.collider.w*2,self.collider.h*2));
self.headImage = pygame.transform.rotate(self.headImage,-self.direction);
def draw(self):
bodyx = self.collider.x-self.collider.w-abs(self.collider.getOrbitX(self.direction*2, self.collider.w/2));
bodyy = self.collider.y-self.collider.h-abs(self.collider.getOrbitX(self.direction*2, self.collider.w/2));
self.screen.blit(self.headImage,(bodyx,bodyy));
def step(self):
if (self.lifetime > 0):
self.lifetime -= 1;
self.collider.move(-self.direction, -self.speed)
self.draw();
class Shot_Trooper(Shot):
def die(self):
blow = Explosion(self.screen, self.collider,1);
class Shot_Iu_HalfCharge(Shot):
def die(self):
col = Collider(self.collider.x, self.collider.y,self.collider.w*2,self.collider.h*2);
blow = Explosion(self.screen, col,6);
class Shot_Iu_FullCharge(Shot):
def die(self):
col = Collider(self.collider.x + self.collider.w/2, self.collider.y + self.collider.h/2,self.collider.w,self.collider.h);
blow = Explosion(self.screen, col,5);
class Shot_Iu_NoCharge(Shot):
def die(self):
col = Collider(self.collider.x + self.collider.w/2, self.collider.y + self.collider.h/2,self.collider.w,self.collider.h);
blow = Explosion(self.screen, col,1);
class stats:
def __init__(self,defense,speed,power,constitution,b_power):
self.defense = defense;
self.base_defense = defense;
self.speed = speed;
self.base_speed = speed;
self.power = power;
self.base_power = power;
self.constitution = constitution;
self.base_constitution = constitution;
self.b_power = b_power;
self.base_b_power = b_power;
self.shield = 0;
self.defenseChange_duration = [];
self.defenseChange_amount = [];
self.speedChange_duration = [];
self.speedChange_amount = [];
self.powerChange_duration = [];
self.powerChange_amount = [];
self.DOT_duration = [];
self.DOT_amount = [];
self.shield_amount = [];
self.shield_duration = [];
def apply_damageOverTime(self,amount,duration):
self.DOT_amount.append(amount);
self.DOT_duration.append(duration);
def apply_defLessen(self,amount,duration):
self.defenseChange_amount.append(-amount);
self.defenseChange_duration.append(duration);
def apply_defRaise(self,amount,duration):
self.defenseChange_amount.append(amount);
self.defenseChange_duration.append(duration);
def apply_slow(self,amount,duration):
self.speedChange_amount.append(-amount);
self.speedChange_duration.append(duration);
def apply_speedRaise(self,amount,duration):
self.speedChange_amount.append(amount);
self.speedChange_duration.append(duration);
def apply_snare(self,duration):
self.speedChange_amount.append(-100);
self.speedChange_duration.append(duration);
def apply_powerLessen(self,amount,duration):
self.powerChange_amount.append(-amount);
self.powerChange_duration.append(duration);
def apply_powerRaise(self,amount,duration):
self.powerChange_amount.append(amount);
self.powerChange_duration.append(duration);
def apply_shield(self,amount,duration):
self.shield += amount;
self.shield_amount.append(amount);
self.shield_duration.append(duration);
def clear_debuffs(self):
for i in range(len(self.powerChange_amount)):
if (self.powerChange_amount[i] < 0): self.powerChange_duration[i] = -1;
for i in range(len(self.speedChange_amount)):
if (self.speedChange_amount[i] < 0): self.speedChange_duration[i] = -1;
for i in range(len(self.defenseChange_amount)):
if (self.defenseChange_amount[i] < 0): self.defenseChange_duration[i] = -1;
for i in range(len(self.DOT_amount)):
self.DOT_duration[i] = -1;
def step(self):
self.power = self.base_power;
self.speed = self.base_speed;
self.defense = self.base_defense;
for i in range(len(self.powerChange_amount)):
if(self.powerChange_duration[i] > 0):
self.powerChange_duration[i] -= 1;
self.power += self.powerChange_amount[i];
for i in range(len(self.speedChange_amount)):
if(self.speedChange_duration[i] > 0):
self.speedChange_duration[i] -= 1;
self.speed += (self.speed + (self.speedChange_amount[i]/100));
for i in range(len(self.defenseChange_amount)):
if(self.defenseChange_duration[i] > 0):
self.defenseChange_duration[i] -= 1;
self.defense += self.defenseChange_amount[i];
for i in range(len(self.DOT_amount)):
if(self.DOT_duration[i] > 0):
self.DOT_duration[i] -= 1
if (self.shield > 0):
self.shield -= self.DOT_amount[i];
else:
self.constitution -= self.DOT_amount[i];
for i in range(len(self.shield_duration)):
if(self.shield_duration[i] > 0): self.shield_duration[i] -= 1;
if(self.shield_duration[i] == 0):
self.shield_duration[i] = -1;
self.shield -= self.shield_amount[i];
if (self.shield < 0): self.shield = 0;
def apply_damage(self,amount):
print(amount);
if (self.shield > 0):
self.shield -= amount * (100 / (100 + self.defense));
else:
self.constitution -= amount * (100 / (100 + self.defense));
if (self.constitution <= 0): return True;
else: return False;
def getHP(self):
return self.constitution + self.shield;
import pygame, math;
from stats import *;
from collider import *;
from shot import *;
from shot import Shot
class Tank:
collider = Collider(0,0,0,0);
direction = 0;
speed = 0;
fullspeedTime = 0
ammo = 0
def __init__(self,screen,x,y,w,z,fsT,turnSpeed,ammo):
self.collider = Collider(x,y,w,z);
self.fullspeedTime = fsT;
self.turnSpeed = turnSpeed;
self.ammo = ammo;
self.screen = screen;
self.head_direction = 0;
self.distance = 0;
self.headImage = pygame.image.load("img/head.png").convert_alpha();
self.headImage = pygame.transform.scale(self.headImage,(self.collider.w*2,self.collider.h*2));
self.bodyImage = pygame.image.load("img/body.png").convert_alpha();
self.bodyImage = pygame.transform.scale(self.bodyImage,(self.collider.w*2,self.collider.h*2));
self.img_cannonLeft = [];
self.img_cannonLeftIndex = 0;
self.img_cannonRight = [];
self.img_cannonRightIndex = 0;
#self.img_cannonLeftSemi = [];
#self.img_cannonLeftSemi = 0;
#self.img_cannonRightSemi = [];
#self.img_cannonRightSemi = 0;
# empty frame
#auxImg = pygame.transform.scale(pygame.image.load("img/cannonLeft0.png").convert_alpha(),(self.collider.w*4,self.collider.h*4));
#self.img_cannonLeftSemi.append(auxImg);
#self.img_cannonRightSemi.append(auxImg);
#other frames
for i in range(15):
# normal shots
auxImg = pygame.transform.scale(pygame.image.load("img/cannonLeft" + str(i) + ".png").convert_alpha(),(self.collider.w*4,self.collider.h*4));
self.img_cannonLeft.append(auxImg);
auxImg = pygame.transform.flip(auxImg,True,False);
self.img_cannonRight.append(auxImg);
# semi charged shots
#auxImg = pygame.transform.scale(pygame.image.load("img/cannonLeft" + str(i+5) + ".png").convert_alpha(),(self.collider.w*4,self.collider.h*4));
#self.img_cannonLeftSemi.append(auxImg);
#auxImg = pygame.transform.flip(auxImg,True,False);
#self.img_cannonLeftSemi.append(auxImg);
self.shots = [];
def initStats(self,defense,speed,power,constitution,b_power):
defense += 0.0;
speed += 0.0;
power += 0.0;
constitution += 0.0;
b_power += 0.0;
self.stats = stats(defense,speed,power,constitution,b_power);
def setCharge(self,cL,cR):
self.chargeLeft = cL;
self.chargeRight = cR;
def draw(self):
bodyx = self.collider.x-self.collider.w-abs(self.collider.getOrbitX(self.direction*2, self.collider.w/2));
bodyy = self.collider.y-self.collider.h-abs(self.collider.getOrbitX(self.direction*2, self.collider.w/2));
headx = self.collider.x-self.collider.w-abs(self.collider.getOrbitX(self.head_direction*2, self.collider.w/2));
heady = self.collider.y-self.collider.h-abs(self.collider.getOrbitX(self.head_direction*2, self.collider.w/2));
headLightx = self.collider.x-2*self.collider.w-abs(self.collider.getOrbitX(self.head_direction*2, self.collider.w));
headLighty = self.collider.y-2*self.collider.h-abs(self.collider.getOrbitX(self.head_direction*2, self.collider.w));
bodyImage_show = pygame.transform.rotate(self.bodyImage, self.direction);
self.screen.blit(bodyImage_show,(bodyx,bodyy));
headImage_show = pygame.transform.rotate(self.headImage,-self.head_direction);
self.screen.blit(headImage_show,(headx,heady));
cannonLeft = pygame.transform.rotate(self.img_cannonLeft[self.img_cannonLeftIndex],-self.head_direction);
self.screen.blit(cannonLeft,(headLightx,headLighty));
cannonRight = pygame.transform.rotate(self.img_cannonRight[self.img_cannonRightIndex],-self.head_direction);
self.screen.blit(cannonRight,(headLightx,headLighty));
def setHeadDirection(self,mouse_x,mouse_y):
self.head_direction = self.collider.pointAngle(mouse_x, mouse_y);
self.distance = self.collider.pointDistance(mouse_x, mouse_y);
def shoot(self,cannon,isKeyUp):
originxl = self.collider.x + self.collider.getOrbitX(-self.head_direction+30, -self.collider.w);
originyl = self.collider.y + self.collider.getOrbitY(-self.head_direction+30, -self.collider.h);
originxr = self.collider.x + self.collider.getOrbitX(-self.head_direction-30, -self.collider.w);
originyr = self.collider.y + self.collider.getOrbitY(-self.head_direction-30, -self.collider.h);
if(cannon == "left"):
if(self.chargeLeft <= 20):
if(not isKeyUp):
self.img_cannonLeftIndex = 1;
auxShot = Shot_Iu_NoCharge(self.screen,"img/shot1.png", originxl, originyl, self.collider.w, self.collider.h, 25, self.head_direction,30,self.stats.power);
self.shots.append(auxShot);
elif((self.chargeLeft > 20) & (self.chargeLeft <= 80)):
self.img_cannonLeftIndex = 5;
auxShot = Shot_Iu_HalfCharge(self.screen,"img/shot2.png", originxl, originyl, self.collider.w/2+(self.chargeLeft/2), self.collider.h/2+(self.chargeLeft/2), 32, self.head_direction,50+(self.chargeLeft),self.stats.power+(self.chargeLeft/3));
self.shots.append(auxShot);
elif(self.chargeLeft > 80):
self.img_cannonLeftIndex = 10;
auxShot = Shot_Iu_FullCharge(self.screen,"img/shot4.png", originxl, originyl, self.collider.w*2, self.collider.h*2, 37, self.head_direction,400,self.stats.power*4);
self.shots.append(auxShot);
if(cannon == "right"):
if(self.chargeRight <= 20):
if(not isKeyUp):
self.img_cannonRightIndex = 1;
auxShot = Shot_Iu_NoCharge(self.screen,"img/shot1.png", originxr, originyr, self.collider.w, self.collider.h, 25, self.head_direction,30,self.stats.power);
self.shots.append(auxShot);
elif((self.chargeRight > 20) & (self.chargeRight <= 80)):
self.img_cannonRightIndex = 5;
auxShot = Shot_Iu_HalfCharge(self.screen,"img/shot2.png", originxr, originyr, self.collider.w/2+(self.chargeRight/2), self.collider.h/2+(self.chargeRight/2), 32, self.head_direction,50+(self.chargeLeft),self.stats.power+(self.chargeLeft/3));
self.shots.append(auxShot);
elif(self.chargeRight > 80):
self.img_cannonRightIndex = 10;
auxShot = Shot_Iu_FullCharge(self.screen,"img/shot4.png", originxr, originyr, self.collider.w*2, self.collider.h*2, 37, self.head_direction,400,self.stats.power*4);
self.shots.append(auxShot);
def step(self,upKey,dnKey,ldKey,rdKey,mouseL,mouseR,alter,mouse_x,mouse_y):
speed_threshold = 0.1;
if(upKey):
if(self.speed < self.stats.speed):
self.speed += self.stats.speed/self.fullspeedTime;
if(dnKey):
if(self.speed > - self.stats.speed):
self.speed -= self.stats.speed/self.fullspeedTime;
if ((not upKey) & (not dnKey)):
if(self.speed > 0): self.speed -= self.stats.speed/self.fullspeedTime;
if(self.speed < 0): self.speed += self.stats.speed/self.fullspeedTime;
if((self.speed < speed_threshold) & (self.speed > - speed_threshold)):
self.speed = 0;
if(ldKey):
self.direction += self.turnSpeed;
if(rdKey):
self.direction -= self.turnSpeed;
if(mouseL):
self.setHeadDirection(mouse_x, mouse_y);
if(mouseR):
self.setHeadDirection(mouse_x, mouse_y);
if(alter):
if (self.ammo > 0): self.ammo -= 1;
if(self.img_cannonLeftIndex >= 1):
self.img_cannonLeftIndex+=1;
if(self.img_cannonLeftIndex == 4):
self.img_cannonLeftIndex = 0;
if(self.img_cannonLeftIndex == 9):
self.img_cannonLeftIndex = 0;
if(self.img_cannonLeftIndex == 14):
self.img_cannonLeftIndex = 0;
if(self.img_cannonRightIndex >= 1):
self.img_cannonRightIndex+=1;
if(self.img_cannonRightIndex == 4):
self.img_cannonRightIndex = 0;
if(self.img_cannonRightIndex == 9):
self.img_cannonRightIndex = 0;
if(self.img_cannonRightIndex == 14):
self.img_cannonRightIndex = 0;
if (self.speed > self.stats.speed): self.speed -= self.stats.speed/self.fullspeedTime;
self.collider.move(self.direction, self.speed)
self.stats.step();
self.draw();
for shot in self.shots:
shot.step();
if (shot.lifetime <= 0):
shot.die();
self.shots.remove(shot);
import pygame, math, random;
from stats import *;
from collider import *;
from shot import *;
from shot import Shot
from miniHP import *;
class Trooper:
collider = Collider(0,0,0,0);
direction = 0;
speed = 0;
fullspeedTime = 0
ammo = 0
isAlive = False;
miniHP = miniHP();
def __init__(self,screen,x,y,w,z,burst = 3,delay = 60,firespeed = 8):
self.collider = Collider(x,y,w,z);
self.screen = screen;
self.head_direction = 0;
self.distance = 0;
self.direction = 0;
self.isAlive = True;
if(burst < 1): burst = 1;
if(firespeed < 4): firespeed = 4;
if(delay < 1): delay = 1;
self.shotsPerBurst = burst;
self.shootDelay = delay + (burst * firespeed);
self.firespeed = firespeed #actual delay between each shot
self.currentShootDelay = random.randint(2,self.shootDelay);
self.currentShotBurst = 0;
self.currentDelayFirespeed = 0;
self.sprite = [];
self.spriteIndex = 0;
for i in range(3):
auxImg = pygame.transform.scale(pygame.image.load("img/trooper" + str(i+1) + ".png").convert_alpha(),(self.collider.w*4,self.collider.h*4));
auxImg = pygame.transform.flip(auxImg,False,True);
self.sprite.append(auxImg);
self.shots = [];
def initStats(self,defense,speed,power,constitution,b_power):
defense += 0.0;
speed += 0.0;
power += 0.0;
constitution += 0.0;
b_power += 0.0;
self.stats = stats(defense,speed,power,constitution,b_power);
def draw(self):
bodyx = self.collider.x-self.collider.w-abs(self.collider.getOrbitX(self.direction*2, self.collider.w/2));
bodyy = self.collider.y-self.collider.h-abs(self.collider.getOrbitX(self.direction*2, self.collider.w/2));
drawImg = pygame.transform.rotate(self.sprite[self.spriteIndex],-self.direction);
self.screen.blit(drawImg,(bodyx,bodyy));
def setDirection(self,view_x,view_y):
self.direction = self.collider.pointAngle(view_x, view_y);
def shoot(self):
self.spriteIndex = 1;
offsetx = self.collider.x + self.collider.w + self.collider.getOrbitX(-self.direction+37, -self.collider.w);
offsety = self.collider.y + self.collider.h + self.collider.getOrbitY(-self.direction+37, -self.collider.h);
auxShot = Shot_Trooper(self.screen,"img/shot1.png", offsetx, offsety, self.collider.w, self.collider.h, 20, self.direction,100,self.stats.power);
self.shots.append(auxShot);
def step(self,player):
if(self.isAlive):
if(self.spriteIndex > 0):
self.spriteIndex += 1
if(self.spriteIndex >= 3):
self.spriteIndex = 0;
self.currentShootDelay -= 1;
if(self.currentShootDelay == 0):
self.currentShootDelay = (self.shotsPerBurst * self.firespeed) + random.randint(1,self.shootDelay+(self.shotsPerBurst * self.firespeed));
self.currentShotBurst = self.shotsPerBurst;
self.currentDelayFirespeed = self.firespeed;
self.shoot();
if(self.currentShotBurst > 1):
self.currentDelayFirespeed -= 1;
if(self.currentDelayFirespeed == 0):
self.currentDelayFirespeed = self.firespeed;
self.currentShotBurst -= 1;
self.shoot();
self.setDirection(player.collider.x, player.collider.y);
self.stats.step();
self.draw();
if(self.collider.hitTest(player.collider)):
self.stats.apply_damage(10);
for shot in player.shots: #collide with Player shots
if(shot.collider.hitTest(self.collider)):
shot.lifetime -= 31;
self.stats.apply_damage(shot.stats.power);
if(self.stats.constitution <= 0):
self.die();
self.miniHP.draw(self, 3, self.collider.h*3, 10);
for shot in self.shots: #step own shots
shot.step();
for othershot in player.shots: #collide own shots with player shots
if(shot.collider.hitTest(othershot.collider)):
shot.lifetime = 0;
othershot.lifetime -= 10;
player.stats.apply_shield(5,200);
if (shot.lifetime <= 0):
shot.die();
self.shots.remove(shot);
if(shot.collider.hitTest(player.collider)): #collide own shots with player itself
player.stats.apply_damage(shot.stats.power);
shot.lifetime = 0;
def die(self):
self.isAlive = False;
blow = Explosion(self.screen, self.collider,3);
import pygame,math,random;
from trooper import *;
class TrooperSpawner:
def __init__(self,screen,minDelay,maxDelay,screenWidth,screenHeight,threshold):
self.minDelay = minDelay;
self.maxDelay = maxDelay;
self.currentDelay = random.randint(minDelay,maxDelay);
self.screenWidth = screenWidth;
self.screenHeight = screenHeight;
self.screen = screen;
self.troop = [];
self.threshold = threshold;
def step(self,player):
for dude in self.troop:
dude.step(player);
if(dude.isAlive == False): self.troop.remove(dude);
if (self.currentDelay > 0):
self.currentDelay-=1;
if (self.currentDelay <= 0):
self.currentDelay = random.randint(self.minDelay,self.maxDelay);
self.create();
def create(self):
x = random.randint(self.threshold,self.screenWidth-self.threshold);
y = random.randint(self.threshold,self.screenHeight-self.threshold);
trooper1 = Trooper(self.screen, x, y, 12, 12, 3, 30, 4);
trooper1.initStats(5, 0, 9, 80, 0);
self.troop.append(trooper1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment