Create repo

This commit is contained in:
2023-04-10 00:48:44 +02:00
commit 75e0a1f679
79 changed files with 7029 additions and 0 deletions

BIN
Battles/Battles - Game.rar Normal file

Binary file not shown.

133
Battles/Code/Battles.py Normal file
View File

@@ -0,0 +1,133 @@
"""
~~~~~~~~~~~~~~
Battles.py
~~~~~~~~~~~~~~
By - JATIN KUMAR MANDAV
BATTLES is inspired by mobile strategy video game Clash of Clans
which is developed and published by Finnish game developer Supercell.
This game is developed using PYGAME library of PYTHON 2.7
Lines - 127
"""
import pygame
import sys
from math import *
from GUI import *
from Maps import *
from Structures import *
pygame.init()
width = 1260
height = 720
margin = 25
groundW = width - 150 - 2*margin
groundH = height - 2*margin
display = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
clock = pygame.time.Clock()
initGUI(display, (width, height))
initStruct(display, (groundW, groundH), margin)
initMaps(display, (width, height), margin, 100)
white = (255, 255, 255)
black = (0, 0, 0)
yellow = (241, 196, 15)
red = (203, 67, 53)
green = (30, 132, 73)
blue = (36, 113, 163)
gray = (113, 125, 126)
activeButton = 0
def drawBorder():
pygame.draw.rect(display, black, (margin, margin, groundW, groundH), 2)
def createMaps(arg=0):
maps = Maps()
maps.createMap()
def adventure(arg=0):
maps = Maps()
maps.openMap([0, "adventure"])
def customMap(arg=0):
maps = Maps()
maps.openMap([0, "custom"])
def close(arg=0):
pygame.quit()
sys.exit()
def game():
loop = True
font = pygame.font.SysFont("Agency FB", 50)
font.set_bold(True)
mandav = font.render("MANDAV", True, (71, 71, 71))
font2 = pygame.font.SysFont("Agency FB", 200)
font2.set_bold(True)
title = font2.render("BATTLES", True, (200, 200, 200))
titlePos = title.get_rect()
titlePos.center = [width/2, height/2 - 200]
tank = pygame.image.load("Images/Tank.png")
tankPos = tank.get_rect()
tankPos.center = [width/2, height/2 + 40]
heli = pygame.image.load("Images/Helicopter.png")
heliPos = heli.get_rect()
heliPos.center = [width/2 - 70, height/2 - 100]
quit = Button(width - 300 - 10, height/2 + 200, 300, 100, blue, blue, close)
quit.addText("QUIT", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
create = Button(width - 600 - 20, height/2 + 200, 300, 100, blue, blue, createMaps)
create.addText("CREATE MAPS", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
adven = Button(width - 900 - 30, height/2 + 200, 300, 100, blue, blue, adventure)
adven.addText("ADVENTURE", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
custom = Button(width - 1200 - 40, height/2 + 200, 300, 100, blue, blue, customMap)
custom.addText("CUSTOM BASES", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.type == pygame.MOUSEBUTTONDOWN:
quit.select()
create.select()
adven.select()
custom.select()
display.fill((51,51,51))
display.blit(title, titlePos)
display.blit(heli, heliPos)
display.blit(tank, tankPos)
display.blit(mandav, (width - 200, height - 60))
quit.draw()
create.draw()
adven.draw()
custom.draw()
pygame.display.update()
clock.tick(60)
game()

View File

@@ -0,0 +1,2 @@
364.0,341.0,4/747.0,360.0,4/541.0,237.0,4/542.0,443.0,4/|648.0,349.0,3/441.0,345.0,3/|473.0,281.0,4/615.0,280.0,4/608.0,411.0,4/463.0,413.0,4/|557,354,5/|431,135/281,218/269,358/327,483/433,533/589,543/723,503/851,379/806,250/678,144/
391.0,229.0,4/493.0,229.0,4/579.0,230.0,4/667.0,231.0,4/|429.0,340.0,3/608.0,343.0,3/|382.0,433.0,4/498.0,431.0,4/589.0,433.0,4/673.0,433.0,4/|529,343,5/|293,325/771,331/297,148/465,129/602,129/738,143/287,499/423,507/607,517/767,529/

View File

@@ -0,0 +1 @@
8

24
Battles/Code/Data/maps Normal file
View File

@@ -0,0 +1,24 @@
465.0,271.0,1/|||565,352,1/|637,425/
451.0,270.0,1/613.0,452.0,1/|||525,361,1/|659,309/448,421/
386.0,327.0,1/517.0,453.0,1/||605.0,297.0,1/|513,351,1/|676,387/463,229/394,441/
434.0,265.0,1/633.0,454.0,1/||647.0,261.0,1/|538,346,1/|413,361/505,476/770,355/531,165/
453.0,253.0,1/657.0,255.0,1/||553.0,451.0,1/|553,339,2/|710,389/437,381/
437.0,453.0,1/635.0,458.0,1/||533.0,531.0,1/|541,359,2/|429,306/542,241/655,336/
671.0,447.0,1/445.0,448.0,1/||450.0,255.0,1/654.0,260.0,1/|543,348,2/|833,335/397,339/566,164/558,472/
429.0,456.0,1/649.0,257.0,1/||619.0,410.0,1/453.0,273.0,1/|536,343,2/|708,327/569,448/353,350/557,206/803,516/285,200/259,497/884,198/
429.0,281.0,2/656.0,276.0,1/600.0,432.0,2/|429.0,434.0,1/|544.0,236.0,1/524.0,493.0,1/|519,363,3/|755,377/715,464/569,529/335,483/309,327/365,153/947,162/
455.0,273.0,2/613.0,452.0,2/699.0,335.0,1/|617.0,305.0,1/|423.0,376.0,1/556.0,225.0,2/|551,365,3/|
421.0,261.0,2/603.0,245.0,2/627.0,375.0,2/|437.0,429.0,1/|345.0,375.0,2/534.0,489.0,2/337.0,535.0,1/|520,337,3/|749,542/809,364/284,163/209,310/195,489/531,581/
458.0,281.0,2/612.0,256.0,2/623.0,380.0,2/|475.0,383.0,1/|378.0,345.0,2/392.0,452.0,2/546.0,467.0,2/|545,348,3/|710,479/807,361/731,205/488,165/328,215/277,429/499,569/892,543/
434.0,245.0,3/645.0,362.0,3/701.0,261.0,2/432.0,466.0,1/|601.0,241.0,1/|422.0,371.0,2/523.0,431.0,2/529.0,179.0,3/|536,329,4/|775,397/703,510/552,525/295,479/307,312/338,168/709,156/838,271/
465.0,234.0,3/615.0,221.0,3/675.0,336.0,3/559.0,432.0,2/|457.0,367.0,2/|379.0,279.0,2/693.0,436.0,2/391.0,432.0,3/|545,337,4/|879,478/649,526/461,525/267,466/268,345/290,161/753,187/822,346/
434.0,257.0,3/663.0,261.0,3/663.0,433.0,3/435.0,419.0,2/|698.0,343.0,2/|549.0,211.0,2/544.0,527.0,3/347.0,326.0,3/|536,354,4/|775,514/875,359/759,207/293,135/131,168/167,412/188,547/367,534/
446.0,253.0,3/630.0,243.0,3/629.0,427.0,3/451.0,422.0,3/|691.0,333.0,2/|536.0,170.0,3/543.0,489.0,3/377.0,331.0,3/|547,343,4/|707,509/859,357/769,219/401,133/207,275/245,431/347,515/534,578/675,127/
478.0,255.0,4/695.0,367.0,4/467.0,437.0,3/666.0,230.0,3/|574.0,475.0,3/579.0,259.0,1/|672.0,473.0,3/380.0,329.0,3/473.0,533.0,4/569.0,180.0,2/|560,363,5/|790,545/891,371/807,195/329,136/257,415/347,537/218,254/
405.0,213.0,4/662.0,513.0,4/719.0,217.0,4/399.0,495.0,2/|559.0,221.0,2/651.0,356.0,3/|357.0,359.0,3/529.0,519.0,4/753.0,407.0,4/775.0,271.0,4/|529,361,5/|824,542/919,398/921,209/265,160/215,287/211,441/282,550/953,515/
228.0,195.0,4/878.0,183.0,4/877.0,539.0,4/230.0,519.0,4/|672.0,362.0,3/449.0,358.0,3/|546.0,231.0,4/545.0,483.0,4/355.0,354.0,3/785.0,369.0,3/|548,366,5/|705,192/411,181/195,325/389,523/684,526/919,362/
535.0,193.0,4/537.0,503.0,4/762.0,349.0,4/298.0,347.0,4/|637.0,355.0,3/413.0,349.0,3/|231.0,184.0,4/842.0,186.0,4/847.0,542.0,4/217.0,531.0,4/|532,357,5/|681,179/402,197/193,282/175,383/357,541/697,559/899,455/896,290/
543.0,221.0,4/324.0,346.0,4/749.0,348.0,4/553.0,499.0,4/|431.0,366.0,3/661.0,367.0,3/|363.0,227.0,4/356.0,465.0,4/721.0,477.0,4/705.0,218.0,4/|542,372,5/|887,543/925,417/907,261/815,146/545,133/269,131/191,237/169,397/245,541/561,583/
351.0,430.0,4/461.0,493.0,4/628.0,492.0,4/719.0,403.0,4/|480.0,377.0,3/587.0,375.0,3/|362.0,275.0,4/423.0,177.0,4/551.0,159.0,4/679.0,247.0,4/|540,295,5/|735,149/817,249/858,383/835,490/753,579/301,135/203,229/207,349/202,471/311,545/
364.0,341.0,4/747.0,360.0,4/541.0,237.0,4/542.0,443.0,4/|648.0,349.0,3/441.0,345.0,3/|473.0,281.0,4/615.0,280.0,4/608.0,411.0,4/463.0,413.0,4/|557,354,5/|431,135/281,218/269,358/327,483/433,533/589,543/723,503/851,379/806,250/678,144/
391.0,229.0,4/493.0,229.0,4/579.0,230.0,4/667.0,231.0,4/|429.0,340.0,3/608.0,343.0,3/|382.0,433.0,4/498.0,431.0,4/589.0,433.0,4/673.0,433.0,4/|529,343,5/|293,325/771,331/297,148/465,129/602,129/738,143/287,499/423,507/607,517/767,529/

135
Battles/Code/GUI.py Normal file
View File

@@ -0,0 +1,135 @@
"""
~~~~~~
GUI.py
~~~~~~
By - JATIN KUMAR MANDAV
A small library for Button widget for the game
This is completely coded using PYGAME library of PYTHON 2.7
Lines - 136
"""
# Imports
import pygame
import sys
# Initialize Pygame
pygame.init()
width = 0
height = 0
display = None
# Initialize display, width, margin, and height of pygame window
def initGUI(screen, size):
global display, width, height
display = screen
width = size[0]
height = size[1]
# Button Class
class Button:
def __init__(self, x, y, w, h, color, activeColor, action=None, border=None, borderWidth = 2):
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
self.activeColor = activeColor
self.arg = 0
self.borderWidth = borderWidth
self.action = action
if border:
self.border = border
else:
self.border = color
self.image = None
self.imagePos = None
self.text = None
self.textPos = None
self.align = "center"
self.font = "Times New Roman"
self.fontSize = 11
self.fontColor = (0, 0, 0)
self.image = None
self.imagePos = None
def select(self):
if isActive(self.x, self.y, self.w, self.h) and self.action != None:
self.action(self.arg)
def draw(self, index=0):
if index == self.arg:
color = self.activeColor
else:
color = self.color
pygame.draw.rect(display, color, (self.x, self.y, self.w, self.h))
pygame.draw.rect(display, self.border, (self.x, self.y, self.w, self.h), self.borderWidth)
if (self.text):
display.blit(self.text, self.textPos)
if not (self.image == None):
display.blit(self.image, self.imagePos)
def addImage(self, image, pos):
size = image.get_size()
self.image = image
self.imagePos = (self.x + pos[0] - size[0]/2, self.y + pos[1] - size[1]/2)
def addText(self, text, pos, size=25, font="Times New Roman", color=(0, 0 ,0), align="center"):
self.font = font
self.fontSize = size
self.fontColor = color
self.align = align
font = pygame.font.SysFont(font, size)
self.text = font.render(text, True, color)
self.textPos = getRect(self.text)
if align == "center":
self.textPos.center = (self.x + self.w/2 + pos[0], self.y + self.h/2 + pos[1])
else:
self.textPos = (self.x + 10 + pos[0], self.y + pos[1])
def updateText(self, text, pos):
font = pygame.font.SysFont(self.font, self.fontSize)
self.text = font.render(text, True, self.fontColor)
self.textPos = getRect(self.text)
if self.align == "center":
self.textPos.center = (self.x + self.w/2 + pos[0], self.y + self.h/2 + pos[1])
else:
self.textPos = (self.x + 10 + pos[0], self.y + pos[1])
def getRect(font):
return font.get_rect()
# Check if mouse is over the button
def isActive(x, y, w, h):
pos = pygame.mouse.get_pos()
if (x < pos[0] < x + w) and (y < pos[1] < y + h):
return True
return False
if __name__ == "__main__":
screen = pygame.display.set_mode((200, 100))
initGUI(screen, (200, 100))
newButton = Button(50, 25, 100, 50, (255, 255, 255))
newButton.addText("Text", (0, 0))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
display.fill(0)
newButton.draw()
pygame.display.update()

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

805
Battles/Code/Maps.py Normal file
View File

@@ -0,0 +1,805 @@
"""
~~~~~~~
Maps.py
~~~~~~~
By - JATIN KUMAR MANDAV
This library contains the MAPS class which contains major;y two function, openMap() and createMap()
The user can open the predefined or their saved custom maps using openMap() or
Can save their oen designed custom maps using createMap()
This is completely coded using PYGAME library of PYTHON 2.7
Lines - 809
"""
# Imports
import pygame
import sys
from math import *
from GUI import *
from Structures import *
from Troops import *
width = 0
height = 0
display = None
margin = 0
clock = pygame.time.Clock()
groundW = 0
groundH = 0
validW = 0
validH = 0
margin2 = 0
white = (255, 255, 255)
black = (0, 0, 0)
yellow = (241, 196, 15)
red = (203, 67, 53)
green = (30, 132, 73)
blue = (36, 113, 163)
gray = (113, 125, 126)
darkGray = (23, 32, 42)
activeButton = 1
presentLevel = 1
victory = False
stars = 0
canInfo = {"w":10, "h":33, "size":33, "range":200, "baseR":25, "shotR":7,
"shotSpeed":5}
mortarInfo = {"w": 25, "h":25, "r":50, "range":300, "shotR":7, "shotSpeed":7}
towerInfo = {"w": 5, "h":30, "size":20, "range":300, "baseR":25, "shotR":7,
"shotSpeed":10}
hqInfo = {"r":50}
def initMaps(screen, size, border=0, border2=0):
global width, height, display, margin, groundW, groundH, validW, validH, margin2
display = screen
width = size[0]
height = size[1]
margin = border
groundW = width - 150 - 2*margin
groundH = height - 2*margin
margin2 = border2
validW = width - 2*margin2 - 150
validH = height - 2*margin2
initGUI(display, (width, height))
initStruct(display, (groundW, groundH), margin)
initTroops(display, (groundW, groundH), margin)
def active(num):
global activeButton
activeButton = num
def levelActive(num):
global presentLevel
presentLevel = num
# Saves the custom maps created by user or "guest"
def saveMap(defenceStruct, user="guest"):
global structure, noCannon, noMortar, noTower, noHquart, noResource
if len(defenceStruct) > 0:
noCannon = 0
noMortar = 0
noTower = 0
noHquart = 0
noResource = 0
cannons = []
mortars = []
towers = []
headquarter = []
resources = []
for struct in defenceStruct:
if struct.type == "CANNON":
cannons.append(struct)
elif struct.type == "MORTAR":
mortars.append(struct)
elif struct.type == "TOWER":
towers.append(struct)
elif struct.type == "HEADQUARTERS":
headquarter.append(struct)
elif struct.type == "RESOURCE":
resources.append(struct)
if user == "admin":
fmap = open("Data/maps", "a")
else:
fmap = open("Data/customMaps", "a")
for struct in cannons:
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level) + "/")
fmap.write("|")
for struct in mortars:
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level) + "/")
fmap.write("|")
for struct in towers:
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level) + "/")
fmap.write("|")
for struct in headquarter:
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level - 1) + "/")
fmap.write("|")
for struct in resources:
fmap.write(str(struct.x) + "," + str(struct.y) + "/")
fmap.write("\n")
fmap.close()
structure = []
# Button to show the damage progress and stars earned
damageButton = Button(10, 10, 300, 130, (51, 51, 51), (51, 51, 51))
star1 = pygame.image.load("Images/1-stars.png")
size = star1.get_size()
star01 = pygame.transform.scale(star1, (size[0]/5, size[1]/5))
star2 = pygame.image.load("Images/2-stars.png")
size = star2.get_size()
star02 = pygame.transform.scale(star2, (size[0]/5, size[1]/5))
star3 = pygame.image.load("Images/3-stars.png")
size = star3.get_size()
star03 = pygame.transform.scale(star3, (size[0]/5, size[1]/5))
def damageDone(fraction, structs):
global victory, stars
hqThere = False
for struct in structs:
if struct.type == "HEADQUARTERS":
hqThere = True
break
percent = int(float(fraction)*100)
if percent == 100:
damageButton.addImage(star03, (damageButton.w/2, damageButton.h/2 - 30))
victory = True
stars = 3
elif not hqThere and percent >= 50:
damageButton.addImage(star02, (damageButton.w/2, damageButton.h/2 - 30))
victory = True
stars = 2
elif (percent >= 50) or (not hqThere and percent < 50):
damageButton.addImage(star01, (damageButton.w/2, damageButton.h/2 - 30))
victory = True
stars = 1
elif percent < 50 and hqThere:
damageButton.image = None
victory = False
stars = 0
damageButton.draw()
pygame.draw.rect(display, (208, 211, 212), (damageButton.x + 50, damageButton.y + damageButton.h - 50, 200, 30))
w = 2*percent
pygame.draw.rect(display, (142, 68, 173), (damageButton.x + 50 + 1, damageButton.y + damageButton.h - 50 + 1, w - 2, 30 - 2))
def drawBorder():
pygame.draw.rect(display, black, (margin, margin, groundW, groundH), 2)
def close():
pygame.quit()
sys.exit()
# Pause the Game
def pause():
font = pygame.font.SysFont("Showcard Gothic", 70)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_ESCAPE:
return
if event.key == pygame.K_m:
global loop
loop = False
return
pauseText = font.render("PAUSED", True, (255, 255, 200))
resume = font.render("\'ESCAPE\' to RESUME", True, (140, 200, 50))
main = font.render("\'M\' to MAIN MENU", True, (200, 200, 255))
quit = font.render("\'Q\' to QUIT", True, (200, 255, 200))
display.blit(pauseText, (200, height/2 - 150))
display.blit(resume, (200, height/2 - 50))
display.blit(main, (200, height/2 + 50))
display.blit(quit, (200, height/2 + 150))
pygame.display.update()
clock.tick(60)
# Maps class
class Maps:
def __init__(self):
fmap = open("Data/maps", "r")
data = fmap.read().split("\n")
fmap .close()
data.pop(-1)
for i in range(len(data)):
data[i] = data[i].split("|")
for j in range(len(data[i])):
data[i][j] = data[i][j].split("/")
if len(data[i][j]) > 1:
data[i][j].pop(-1)
if not (data[i][j] == ['']):
for k in range(len(data[i][j])):
data[i][j][k] = [int(float(x)) for x in data[i][j][k].split(",")]
self.maps = data[:]
fp = open("Data/mapReached", "rb")
self.mapNum = int(float(fp.read()))
fp.close()
fmap = open("Data/customMaps", "r")
data = fmap.read().split("\n")
fmap.close()
data.pop(-1)
for i in range(len(data)):
data[i] = data[i].split("|")
for j in range(len(data[i])):
data[i][j] = data[i][j].split("/")
if len(data[i][j]) > 1:
data[i][j].pop(-1)
if not (data[i][j] == ['']):
for k in range(len(data[i][j])):
data[i][j][k] = [int(float(x)) for x in data[i][j][k].split(",")]
self.customMaps = data[:]
self.mode = "adventure"
self.font = pygame.font.SysFont("Agency FB", 100)
def createMap(self):
global presentLevel, loop
global structure, noCannon, noMortar, noTower , noHquart, noResource
loop = True
maxCannon = 4
maxMortar = 2
maxTower = 4
maxHquart = 1
maxResource = 10
noCannon = 0
noMortar = 0
noTower = 0
noHquart = 0
noResource = 0
canMaxLevel = 4
morMaxLevel = 3
towMaxLevel = 4
hqMaxLevel = 5
structure = []
level = []
for i in range(5):
h = 40
w = 40
x = 100*(i + 1) + w*i
y = margin + 20
newButton = Button(x, y, w, h, blue, (93, 173, 226), levelActive)
newButton.arg = i + 1
newButton.addText(str(i+1), (0, 0), 20)
level.append(newButton)
inventory = []
for i in range(5):
h = 100
x = width - 140
y = 10*(i + 1) + h*i
newButton = Button(x, y, 130, h, blue, (93, 173, 226), active)
inventory.append(newButton)
save = Button(width - 140, height - 70, 130, 50, blue, (93, 173, 226), saveMap)
save.addText("SAVE MAP", (0, 0), 20)
inventory[0].addText("CANNONS" + ": " + str(maxCannon), (0, 0), 20)
inventory[1].addText("MORTARS" + ": " + str(maxMortar), (0, 0), 20)
inventory[2].addText("TOWERS" + ": " + str(maxTower), (0, 0), 20)
inventory[3].addText("HEADQUARTERS" + ": " + str(maxHquart), (0, 0), 20)
inventory[4].addText("RESOURCE" + ": " + str(maxResource), (0, 0), 20)
inventory[0].arg = 1
inventory[1].arg = 2
inventory[2].arg = 3
inventory[3].arg = 4
inventory[4].arg = 5
while loop:
inventory[0].updateText("CANNONS" + ": " + str(maxCannon - noCannon), (0, 0))
inventory[1].updateText("MORTARS" + ": " + str(maxMortar - noMortar), (0, 0))
inventory[2].updateText("TOWERS" + ": " + str(maxTower - noTower), (0, 0))
inventory[3].updateText("H.Q." + ": " + str(maxHquart - noHquart), (0, 0))
inventory[4].updateText("RESOURCE" + ": " + str(maxResource - noResource), (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_ESCAPE or event.key == pygame.K_SPACE or pygame.key == pygame.K_p:
pause()
if event.key == pygame.K_z and len(structure):
type = structure[len(structure) - 1].type
if type == "CANNON":
structure.pop(-1)
noCannon -= 1
elif type == "MORTAR":
structure.pop(-1)
noMortar -= 1
elif type == "TOWER":
structure.pop(-1)
noTower -= 1
elif type == "H.Q. ":
structure.pop(-1)
noHquart -= 1
elif type == "RESOURCE":
structure.pop(-1)
noResource -= 1
if event.type == pygame.MOUSEBUTTONDOWN:
inventory[0].select()
inventory[1].select()
inventory[2].select()
inventory[3].select()
inventory[4].select()
level[0].select()
level[1].select()
level[2].select()
level[3].select()
level[4].select()
save.arg = structure
save.select()
pos = pygame.mouse.get_pos()
if (margin < pos[0] < groundW + margin) and (margin < pos[1] < groundH + margin):
if (margin2 < pos[0] < validW + margin2) and (margin2 < pos[1] < validH + margin2):
if activeButton == 1 and noCannon < maxCannon:
if presentLevel > canMaxLevel:
presentLevel = canMaxLevel
newCannon = Cannon(pos[0], pos[1], canInfo["w"], canInfo["h"], canInfo["size"], canInfo["range"], canInfo["baseR"],
presentLevel, canInfo["shotR"], canInfo["shotSpeed"])
structure.append(newCannon)
noCannon += 1
if activeButton == 2 and noMortar < maxMortar:
if presentLevel > morMaxLevel:
presentLevel = morMaxLevel
newMortar = Mortar(pos[0], pos[1], mortarInfo["w"], mortarInfo["h"], mortarInfo["r"], mortarInfo["range"],
presentLevel, mortarInfo["shotR"],
mortarInfo["shotSpeed"])
structure.append(newMortar)
noMortar += 1
if activeButton == 3 and noTower < maxTower:
if presentLevel > towMaxLevel:
presentLevel = towMaxLevel
newTower = Tower(pos[0], pos[1], towerInfo["w"], towerInfo["h"], towerInfo["size"], towerInfo["range"],
towerInfo["baseR"], presentLevel, towerInfo["shotR"], towerInfo["shotSpeed"])
structure.append(newTower)
noTower += 1
if activeButton == 4 and noHquart < maxHquart:
if presentLevel > hqMaxLevel:
presentLevel = hqMaxLevel
newHQ = HeadQuarters(pos[0], pos[1], hqInfo["r"], presentLevel)
structure.append(newHQ)
noHquart += 1
if activeButton == 5 and noResource < maxResource:
newResource = Resource(pos[0], pos[1])
structure.append(newResource)
noResource += 1
display.fill((154,102,64))
pygame.draw.rect(display, (195,151,98), (margin, margin, groundW, groundH))
pygame.draw.rect(display, (154,102,64), (margin2, margin2, validW, validH))
drawBorder()
pygame.draw.rect(display, gray, (width - 150, 0, 150, height))
for struct in structure:
if struct.type == "HEADQUARTERS" or struct.type == "RESOURCE":
struct.draw()
else:
struct.draw([])
struct.shoot()
for item in inventory:
item.draw(activeButton)
if activeButton == 1:
for i in range(canMaxLevel):
level[i].draw(presentLevel)
elif activeButton == 2:
for i in range(morMaxLevel):
level[i].draw(presentLevel)
elif activeButton == 3:
for i in range(towMaxLevel):
level[i].draw(presentLevel)
elif activeButton == 4:
for i in range(hqMaxLevel):
level[i].draw(presentLevel)
save.draw()
pygame.display.update()
clock.tick(60)
def showResult(self):
def quitGame(event=0):
global loop
loop = False
if victory:
if self.mode == "adventure":
fp = open("Data/mapReached", "wb")
fp.write(str(self.mapNum + 1))
fp.close()
fp = open("Data/mapReached", "rb")
print(fp.read())
fp.close()
text = self.font.render("VICTORY", True, (255, 255, 255))
else:
text = self.font.render("LOSS", True, (255, 255, 255))
last = False
if self.mode == "adventure":
if victory and self.mapNum == len(self.maps) - 1:
endReached = self.font.render("All Bases Conquered!", True, (200, 200, 200))
pos2 = endReached.get_rect()
pos2.center = [width/2, 200]
last = True
if stars == 1:
pos = star1.get_rect()
pos.center = [width/2, height/2]
display.blit(star1, pos)
elif stars == 2:
pos = star2.get_rect()
pos.center = [width/2, height/2]
display.blit(star2, pos)
elif stars == 3:
pos = star3.get_rect()
pos.center = [width/2, height/2]
display.blit(star3, pos)
pos = text.get_rect()
pos.center = [width/2, height/2]
if victory:
next = Button(width/2 - 100 - 150, height/2 + 200, 200, 100, blue, blue, self.openMap)
if last:
next.addText("START AGAIN", (0, 0), 30)
else:
next.addText("NEXT MAP", (0, 0), 30)
next.arg = [1, self.mode]
else:
next = Button(width/2 - 100 - 150, height/2 + 200, 200, 100, blue, blue, self.openMap)
next.addText("ATTACK AGAIN", (0, 0), 30)
next.arg = [0, self.mode]
quit = Button(width/2 - 100 + 150, height/2 + 200, 200, 100, blue, blue, quitGame)
quit.addText("QUIT", (0, 0), 30)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.type == pygame.MOUSEBUTTONDOWN:
next.select()
quit.select()
display.blit(text, pos)
if last:
display.blit(endReached, pos2)
next.draw()
quit.draw()
if not loop:
return
clock.tick(60)
pygame.display.update()
def openMap(self, arg):
global loop
self.mode = arg[1]
loop = True
global presentLevel
defenceStruct = []
if self.mode == "adventure":
self.mapNum = (self.mapNum + arg[0])%len(self.maps)
else:
self.mapNum = (self.mapNum + arg[0])%len(self.customMaps)
if self.mode == "adventure":
map = self.maps[self.mapNum]
else:
map = self.customMaps[self.mapNum]
cannonCoord = map[0]
mortarCoord = map[1]
towerCoord = map[2]
hqCoord = map[3]
resourceCoord = map[4]
if not (cannonCoord == [""]):
for pos in cannonCoord:
newCannon = Cannon(pos[0], pos[1], canInfo["w"], canInfo["h"], canInfo["size"],
canInfo["range"], canInfo["baseR"], pos[2],
canInfo["shotR"], canInfo["shotSpeed"])
defenceStruct.append(newCannon)
if not (mortarCoord == [""]):
for pos in mortarCoord:
newMortar = Mortar(pos[0], pos[1], mortarInfo["w"], mortarInfo["h"], mortarInfo["r"],
mortarInfo["range"], pos[2], mortarInfo["shotR"],
mortarInfo["shotSpeed"])
defenceStruct.append(newMortar)
if not (towerCoord == [""]):
for pos in towerCoord:
newTower = Tower(pos[0], pos[1], towerInfo["w"], towerInfo["h"], towerInfo["size"],
towerInfo["range"], towerInfo["baseR"], pos[2],
towerInfo["shotR"], towerInfo["shotSpeed"])
defenceStruct.append(newTower)
if not (hqCoord == [""]):
for pos in hqCoord:
newHQ = HeadQuarters(pos[0], pos[1], hqInfo["r"], pos[2])
defenceStruct.append(newHQ)
if not (resourceCoord == [""]):
for pos in resourceCoord:
newResource = Resource(pos[0], pos[1])
defenceStruct.append(newResource)
if self.mode == "custom":
level = []
for i in range(5):
h = 40
w = 40
x = width - 75 - 20
y = height/2 + (h + 10)*i
newButton = Button(x, y, w, h, blue, (93, 173, 226), levelActive)
newButton.arg = i + 1
newButton.addText(str(i+1), (0, 0), 20)
level.append(newButton)
troops = []
inventory = []
for i in range(3):
h = 100
x = width - 140
y = 10*(i + 1) + h*i
newButton = Button(x, y, 130, h, blue, (93, 173, 226), active)
inventory.append(newButton)
if self.mode == "custom":
maxShooter = 20
maxTank = 5
maxHeli = 3
shooterMaxLevel = 5
tankMaxLevel = 4
heliMaxLevel = 3
else:
if self.mapNum < 4:
maxShooter = 12
maxTank = 0
maxHeli = 0
shooterMaxLevel = 1
tankMaxLevel = 0
heliMaxLevel = 0
elif self.mapNum < 8:
maxShooter = 15
maxTank = 1
maxHeli = 0
shooterMaxLevel = 2
tankMaxLevel = 1
heliMaxLevel = 0
elif self.mapNum < 12:
maxShooter = 15
maxTank = 2
maxHeli = 1
shooterMaxLevel = 3
tankMaxLevel = 2
heliMaxLevel = 1
elif self.mapNum < 16:
maxShooter = 18
maxTank = 3
maxHeli = 2
shooterMaxLevel = 4
tankMaxLevel = 3
heliMaxLevel = 2
else:
maxShooter = 20
maxTank = 5
maxHeli = 3
shooterMaxLevel = 5
tankMaxLevel = 4
heliMaxLevel = 3
if self.mode == "custom":
next = Button(width - 130, height - 100, 100, 40, blue, blue, self.openMap)
next.addText("NEXT MAP", (0, 0), 20)
next.arg = [1, self.mode]
noShooter = 0
noTank = 0
noHeli = 0
inventory[0].addText("SHOOTER" + ": " + str(maxShooter), (0, 0), 20)
inventory[1].addText("TANK" + ": " + str(maxTank), (0, 0), 20)
inventory[2].addText("HELICOPTER" + ": " + str(maxHeli), (0, 0), 20)
inventory[0].arg = 1
inventory[1].arg = 2
inventory[2].arg = 3
initStruct = len(defenceStruct)
while loop:
inventory[0].updateText("SHOOTER" + ": " + str(maxShooter - noShooter), (0, 0))
inventory[1].updateText("TANK" + ": " + str(maxTank - noTank), (0, 0))
inventory[2].updateText("HELICOPTER" + ": " + str(maxHeli - noHeli), (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_ESCAPE or event.key == pygame.K_SPACE or pygame.key == pygame.K_p:
pause()
if event.type == pygame.MOUSEBUTTONDOWN:
inventory[0].select()
inventory[1].select()
inventory[2].select()
if self.mode == "custom":
level[0].select()
level[1].select()
level[2].select()
level[3].select()
level[4].select()
next.select()
pos = pygame.mouse.get_pos()
if (margin < pos[0] < groundW + margin) and (margin < pos[1] < groundH + margin):
if not ((margin2 < pos[0] < margin2 + validW) and (margin2 < pos[1] < margin2 + validH)):
if activeButton == 1 and noShooter < maxShooter:
if self.mode == "custom":
if presentLevel > shooterMaxLevel:
presentLevel = shooterMaxLevel
newShooter = Shooter(pos[0], pos[1], 5, 1.2, 180, presentLevel, 5, 2)
else:
newShooter = Shooter(pos[0], pos[1], 5, 1.2, 180, shooterMaxLevel, 5, 2)
troops.insert(0, newShooter)
noShooter += 1
if activeButton == 2 and noTank < maxTank:
if self.mode == "custom":
if presentLevel > tankMaxLevel:
presentLevel = tankMaxLevel
newTank = Tank(pos[0], pos[1], 20, 1, 200, presentLevel, 6, 2)
else:
newTank = Tank(pos[0], pos[1], 20, 1, 200, tankMaxLevel, 6, 2)
troops.insert(0, newTank)
noTank += 1
if activeButton == 3 and noHeli < maxHeli:
if self.mode == "custom":
if presentLevel > heliMaxLevel:
presentLevel = heliMaxLevel
newHeli = Helicopter(pos[0], pos[1], 30, 0.7, 200, presentLevel, 7, 10)
else:
newHeli = Helicopter(pos[0], pos[1], 30, 0.7, 200, heliMaxLevel, 7, 10)
troops.append(newHeli)
noHeli += 1
display.fill((154,102,64))
pygame.draw.rect(display, (195,151,98), (margin, margin, groundW, groundH))
pygame.draw.rect(display, (154,102,64), (margin2, margin2, validW, validH))
drawBorder()
pygame.draw.rect(display, gray, (width - 150, 0, 150, height))
for item in inventory:
item.draw(activeButton)
for troop in troops:
troop.move(defenceStruct)
troop.draw()
troop.shoot()
troop.updateHealth(defenceStruct)
for struct in defenceStruct:
if not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE"):
struct.removeHit()
for struct in defenceStruct:
if struct.type == "HEADQUARTERS":
struct.draw()
elif struct.type == "RESOURCE":
struct.draw()
else:
struct.draw(troops)
struct.shoot()
struct.updateHealth(troops)
for troop in troops:
troop.removeHit()
for struct in defenceStruct:
if struct.health <= 0:
defenceStruct.remove(struct)
for troop in troops:
if troop.health <= 0:
troops.remove(troop)
if self.mode == "custom":
if activeButton == 1:
for i in range(shooterMaxLevel):
level[i].draw(presentLevel)
elif activeButton == 2:
for i in range(tankMaxLevel):
level[i].draw(presentLevel)
if activeButton == 3 :
for i in range(heliMaxLevel):
level[i].draw(presentLevel)
if self.mode == "custom" and noShooter == 0 and noTank == 0 and noHeli == 0:
next.draw()
damageDone(1.0 - float(len(defenceStruct))/initStruct, defenceStruct)
if (defenceStruct == []) or (noShooter == maxShooter and noTank == maxTank and noHeli == maxHeli and troops == []):
self.showResult()
pygame.display.update()
clock.tick(90)
if __name__ == "__main__":
width = 1260
height = 720
screen = pygame.display.set_mode((width, height))
initMaps(screen, (width, height), 25, 100)
newMap = Maps()
newMap.openMap([0, "adventure"])
#newMap.createMap()
close()

716
Battles/Code/Structures.py Normal file
View File

@@ -0,0 +1,716 @@
"""
~~~~~~~~~~~~~
Structures.py
~~~~~~~~~~~~~
By - JATIN KUMAR MANDAV
This is a library for all structures used in Game
Structures: 1. CANNONS
2. MORTARS
3. TOWERS
4. HEADQUARTERS
5. RESOURCE BUILDINGS
This is completely coded using PYGAME library of PYTHON 2.7
Lines - 717
"""
# Imports
import pygame
import sys
from math import *
import random
pygame.init()
display = None
width = 0
height = 0
margin = 0
clock = pygame.time.Clock()
def initStruct(screen, size, border=0):
global display, width, height, margin
display = screen
width = size[0]
height = size[1]
margin = border
def getCoordPoly(x, y, r, n, rotate=0, anchor=(0, 0)):
coords = []
for i in range(n):
coords.append([x - anchor[0] + r*cos(2*pi*i/n + rotate), y - anchor[1] + r*sin(2*pi*i/n + rotate)])
return coords[:]
class Shots:
def __init__(self, x, y, r1, speed, angle, color=(0, 0, 0), type="circle", r2=0):
self.x = x
self.y = y
self.r1 = r1
if r2 == 0:
self.r2 = r1
else:
self.r2 = r2
self.type = type
self.speed = speed
self.angle = angle
self.color = color
def draw(self):
if self.type == "circle":
pygame.draw.ellipse(display, self.color, (self.x - self.r1, self.y - self.r2, self.r1*2, self.r2*2))
elif self.type == "line":
x2 = self.x + self.r1*cos(self.angle)
y2 = self.y + self.r1*sin(self.angle)
pygame.draw.line(display, self.color, (self.x, self.y), (x2, y2), 2)
def move(self):
self.x += self.speed*cos(self.angle)
self.y += self.speed*sin(self.angle)
class Cannon:
def __init__(self, x, y, w, h, size, range, baseR, level, radius, speed, colorBarrel=(0, 0, 0), colorHead=(0, 0, 0), colorBase=(255, 255, 255)):
self.x = x
self.y = y
self.xOld = self.x
self.yOld = self.y
self.angle = 0
self.r = size
self.w = w
self.h = h
self.type = "CANNON"
self.attackType = "GROUND"
self.baseR = baseR
self.radius = radius
self.speed = speed
self.range = range
self.coord = getCoordPoly(self.x, self.y, self.r, 4)
self.rectCoord = []
self.shots = []
self.recoil = 3
self.rebound = 0.1
self.level = level
if self.level == 1:
self.colorBarrel = (23, 32, 42)
self.colorHead = (39, 55, 70)
self.colorBase = (123, 125, 125)
self.hitPoint = 420
self.damage = 8
elif self.level == 2:
self.colorBarrel = (11, 83, 69)
self.colorHead = (30, 132, 73)
self.colorBase = (23, 32, 42)
self.hitPoint = 640
self.damage = 33
self.headCover = (69, 179, 157)
elif self.level == 3:
self.colorBarrel = (21, 67, 96)
self.colorHead = (40, 116, 166)
self.colorBase = (144, 148, 151)
self.hitPoint = 830
self.damage = 32
self.rectCoord2 = []
elif self.level == 4:
self.colorBarrel = (23, 32, 42)
self.colorHead = (46, 64, 83)
self.colorBase = (144, 148, 151)
self.hitPoint = 1200
self.damage = 54
self.rectCoord2 = []
self.headCover = (133, 146, 158)
self.health = self.hitPoint
self.nearestPos = pygame.mouse.get_pos()
self.font = pygame.font.SysFont("Agency FB", 20)
def updateHealth(self, troops):
error = self.baseR
for troop in troops:
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
for shot in troop.shots:
if troop.isHit((shot.x, shot.y)):
self.health -= troop.damage
def isHit(self, coord):
error = self.baseR
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
if dist < error:
return True
return False
def removeHit(self):
tempList = self.shots[:]
for shot in self.shots:
if self.isHit((shot.x, shot.y)):
tempList.remove(shot)
self.shots = tempList[:]
def rotate(self, coord, angle, anchor=(0, 0)):
corr = 270
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
def translate(self, coord):
return [coord[0] + self.x, coord[1] + self.y]
def shoot(self):
error = 0.05
if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)):
self.x += self.rebound*cos(self.angle)
self.y += self.rebound*sin(self.angle)
else:
pos = self.nearestPos
#pos = pygame.mouse.get_pos()
dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld)
if dist < self.range/2:
shot = Shots((self.rectCoord[1][0] + self.rectCoord[2][0])/2, (self.rectCoord[1][1] + self.rectCoord[2][1])/2, self.radius, self.speed, self.angle, (0, 0, 0), "line")
self.shots.append(shot)
if self.level >= 3:
shot = Shots((self.rectCoord2[1][0] + self.rectCoord2[2][0])/2, (self.rectCoord2[1][1] + self.rectCoord2[2][1])/2, self.radius, self.speed, self.angle, (0, 0, 0), "line")
self.shots.append(shot)
self.x -= self.recoil*cos(self.angle)
self.y -= self.recoil*sin(self.angle)
tempList = self.shots[:]
for shot in self.shots:
shot.move()
shot.draw()
if not ((margin < shot.x < width) and (margin < shot.y < height)):
tempList.remove(shot)
self.shots = tempList[:]
def draw(self, troops):
if len(troops):
nearestPos = 0
lowestDist = float("inf")
for struct in troops:
if struct.type == "GROUND":
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
if lowestDist > dist:
self.nearestPos = [struct.x, struct.y]
lowestDist = dist
else:
self.nearestPos = pygame.mouse.get_pos()
pos = self.nearestPos
#pos = pygame.mouse.get_pos()
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
points = [(0, 0), (0, self.h), (self.w, self.h), (self.w, 0)]
self.coord = getCoordPoly(self.x, self.y, self.r/2, 6, rotate)
if self.level <= 3:
self.rectCoord = []
for point in points:
self.rectCoord.append(self.translate(self.rotate(point, rotate, (self.w/2, 0))))
if self.level >= 3:
w = self.w/2
h = self.h
self.rectCoord = []
self.rectCoord2 = []
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2 + w, 0))))
self.rectCoord2.append(self.translate(self.rotate(point, rotate, (w/2 - w, 0))))
baseCoord = getCoordPoly(self.xOld, self.yOld, self.baseR, 4, radians(45))
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.baseR*2, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.baseR*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
pygame.draw.polygon(display, self.colorBase, baseCoord)
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord)
if self.level >= 3:
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord2)
pygame.draw.polygon(display, self.colorHead, self.coord)
if self.level == 2 or self.level == 4:
pygame.draw.ellipse(display, self.headCover, (self.x - self.baseR/3, self.y - self.baseR/3, self.baseR*2/3, self.baseR*2/3))
#pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1)
class Tower:
def __init__(self, x, y, w, h, size, range, baseR, level, radius, speed, colorBarrel=(0, 0, 0), colorHead=(0, 0, 0), colorBase=(255, 0, 0)):
self.x = x
self.y = y
self.xOld = self.x
self.yOld = self.y
self.angle = 0
self.r = size
self.w = w
self.h = h
self.type = "TOWER"
self.baseR = baseR
self.radius = radius
self.speed = speed
self.range = range
self.coord = getCoordPoly(self.x, self.y, self.r, 4)
self.rectCoord = []
self.attackType = "GROUND AND AIR"
self.shots = []
self.recoil = 5
self.rebound = 0.25
self.level = level
if self.level == 1:
self.colorBarrel = (100, 30, 22)
self.colorHead = (100, 30, 22)
self.colorBase = (26, 82, 118)
self.hitPoint = 380
self.damage = 10
elif self.level == 2:
self.colorBarrel = (100, 30, 22)
self.colorHead = (100, 30, 22)
self.colorBase = (125, 102, 8)
self.hitPoint = 580
self.damage = 30
elif self.level == 3:
self.colorBarrel = (183, 149, 11)
self.colorHead = (185, 119, 14)
self.colorBase = (11, 83, 69)
self.colorBase2 = (39, 174, 96)
self.hitPoint = 810
self.damage = 60
elif self.level == 4:
self.colorBarrel = (23, 32, 42)
self.colorHead = (52, 73, 94)
self.colorBase = (21, 67, 96)
self.colorBase2 = (98, 101, 103)
self.hitPoint = 1230
self.damage = 90
self.health = self.hitPoint
self.nearestPos = pygame.mouse.get_pos()
self.font = pygame.font.SysFont("Agency FB", 20)
def updateHealth(self, troops):
error = self.r
for troop in troops:
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
for shot in troop.shots:
if troop.isHit((shot.x, shot.y)):
self.health -= troop.damage
def isHit(self, coord):
error = self.r
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
if dist < error:
return True
return False
def removeHit(self):
tempList = self.shots[:]
for shot in self.shots:
if self.isHit((shot.x, shot.y)):
tempList.remove(shot)
self.shots = tempList[:]
def rotate(self, coord, angle, anchor=(0, 0)):
corr = 270
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
def translate(self, coord):
return [coord[0] + self.x, coord[1] + self.y]
def shoot(self):
error = 0.1
if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)):
self.x += self.rebound*cos(self.angle)
self.y += self.rebound*sin(self.angle)
else:
#pos = pygame.mouse.get_pos()
pos = self.nearestPos
dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld)
if dist < self.range/2:
shot = Shots((self.rectCoord[1][0] + self.rectCoord[2][0])/2, (self.rectCoord[1][1] + self.rectCoord[2][1])/2, self.radius, self.speed, self.angle, (0, 0, 0), "line")
self.shots.append(shot)
self.x -= self.recoil*cos(self.angle)
self.y -= self.recoil*sin(self.angle)
tempList = self.shots[:]
for shot in self.shots:
shot.move()
shot.draw()
if not ((margin < shot.x < width) and (margin < shot.y < height)):
tempList.remove(shot)
self.shots = tempList[:]
def draw(self, troops):
if len(troops):
nearestPos = 0
lowestDist = float("inf")
for struct in troops:
if struct.type == "AIR" or struct.type == "GROUND":
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
if lowestDist > dist:
self.nearestPos = [struct.x, struct.y]
lowestDist = dist
else:
self.nearestPos = pygame.mouse.get_pos()
else:
self.nearestPos = pygame.mouse.get_pos()
#pos = pygame.mouse.get_pos()
pos = self.nearestPos
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
if self.level == 4:
w = self.w*2
h = self.h
points = [(0, 0), (0, h), (w, h), (w, 0)]
self.rectCoord = []
for point in points:
self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0))))
else:
w = self.w
h = self.h
points = [(0, 0), (0, h), (w, h), (w, 0)]
self.rectCoord = []
for point in points:
self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0))))
baseCoord = getCoordPoly(self.xOld, self.yOld, self.baseR, 6)
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.baseR*2, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.baseR*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
pygame.draw.polygon(display, self.colorBase, baseCoord)
if self.level >= 3:
baseCoord2 = getCoordPoly(self.xOld, self.yOld, self.baseR*3/4, 6)
pygame.draw.polygon(display, self.colorBase2, baseCoord2)
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord)
#pygame.draw.polygon(display, self.colorHead, self.coord)
pygame.draw.ellipse(display, self.colorHead, (self.x - self.r/2, self.y - self.r/2, self.r, self.r ))
#pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1)
class Mortar:
def __init__(self, x, y, w, h, r, range, level, radius, speed, colorBarrel=(0, 0, 0), colorHead=(0, 0, 0), colorBase=(255, 0, 0)):
self.x = x
self.y = y
self.xOld = self.x
self.yOld = self.y
self.w = w
self.h = h
self.r = r
self.level = level
if self.level == 1:
self.colorBarrel = (95, 106, 106)
self.colorHead = (46, 64, 83)
self.colorBase = (146, 43, 33)
self.range = range
self.hitPoint = 400
self.damage = 37
elif self.level == 2:
self.colorBarrel = (125, 102, 8)
self.colorHead = (11, 83, 69)
self.colorBase = (166, 172, 175)
self.colorHeadCover = (34, 153, 84)
self.range = range
self.hitPoint = 650
self.damage = 70
elif self.level == 3:
self.colorBarrel = (21, 67, 96)
self.colorHead = (36, 113, 163)
self.colorBase = (113, 125, 126)
self.colorBarrelHead = (46, 64, 83)
self.range = range
self.hitPoint = 850
self.damage = 130
self.radius = radius
self.speed = speed
self.type = "MORTAR"
self.attackType = "GROUND"
self.coord = []
self.rectCoord = []
self.shots = []
self.angle = 0
self.recoil = 7
self.rebound = 0.1
self.health = self.hitPoint
self.font = pygame.font.SysFont("Agency FB", 20)
self.nearestPos = pygame.mouse.get_pos()
def updateHealth(self, troops):
error = 15
for troop in troops:
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
for shot in troop.shots:
if troop.isHit((shot.x, shot.y)):
self.health -= troop.damage
def isHit(self, coord):
error = 50
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
if dist < self.r:
return True
return False
def removeHit(self):
tempList = self.shots[:]
for shot in self.shots:
if self.isHit((shot.x, shot.y)):
tempList.remove(shot)
self.shots = tempList[:]
def shoot(self):
error = 0.05
if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)):
self.x += self.rebound*cos(self.angle)
self.y += self.rebound*sin(self.angle)
else:
#pos = pygame.mouse.get_pos()
pos = self.nearestPos
dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld)
if dist < self.range/2:
self.x -= self.recoil*cos(self.angle)
self.y -= self.recoil*sin(self.angle)
shot = Shots((self.rectCoord[1][0] + self.rectCoord[2][0])/2, (self.rectCoord[1][1] + self.rectCoord[2][1])/2, self.radius, self.speed, self.angle)
self.shots.append(shot)
tempList = self.shots[:]
for shot in self.shots:
shot.move()
shot.draw()
if not ((margin < shot.x < width) and (margin < shot.y < height)):
tempList.remove(shot)
self.shots = tempList[:]
def rotate(self, coord, angle, anchor=(0, 0)):
corr = 270
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
def translate(self, coord):
return [coord[0] + self.x, coord[1] + self.y]
def draw(self, troops):
if len(troops):
nearestPos = 0
lowestDist = float("inf")
for struct in troops:
if struct.type == "GROUND":
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
if lowestDist > dist:
self.nearestPos = [struct.x, struct.y]
lowestDist = dist
else:
self.neasestPos = pygame.mouse.get_pos()
else:
self.nearestPos = pygame.mouse.get_pos()
pos = self.nearestPos
#pos = pygame.mouse.get_pos()
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
points = [(0, 0), (0, self.h), (self.w, self.h), (self.w, 0)]
self.coord = getCoordPoly(self.x, self.y, self.w*5/8, 8, rotate)
self.rectCoord = []
for point in points:
self.rectCoord.append(self.translate(self.rotate(point, rotate, (self.w/2, 0))))
baseCoord = getCoordPoly(self.xOld, self.yOld, self.r*2/5, 10)
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
pygame.draw.polygon(display, self.colorBase, baseCoord)
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord)
pygame.draw.polygon(display, self.colorHead, self.coord)
if self.level == 2:
coord = getCoordPoly(self.x, self.y, self.w*3/8, 8, rotate)
pygame.draw.polygon(display, self.colorHeadCover, coord)
if self.level == 3:
coord = []
w = self.w*1.2
h = self.h/5
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
coord.append(self.translate(self.rotate(point, rotate, (w/2, h/2 - self.h))))
pygame.draw.polygon(display, self.colorBarrelHead, coord)
#pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1)
class HeadQuarters:
def __init__(self, x, y, r, level, color1=(0, 0, 0), color2=(255, 255, 0)):
self.x = x
self.y = y
self.r = r
self.type = "HEADQUARTERS"
self.color1 = (23, 32, 42)
self.color2 = (113, 125, 126)
self.level = level + 1
if self.level == 2:
self.hitPoint = 570
elif self.level == 3:
self.hitPoint = 1500
elif self.level == 4:
self.hitPoint = 3000
elif self.level == 5:
self.hitPoint = 4500
elif self.level == 6:
self.hitPoint = 6000
self.health = self.hitPoint
self.font = pygame.font.SysFont("Agency FB", 20)
def updateHealth(self, troops):
error = 15
for troop in troops:
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
for shot in troop.shots:
if troop.isHit((shot.x, shot.y)):
self.health -= troop.damage
def draw(self):
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*1.5, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*1.5 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
outer = []
inner = []
for i in range(self.level):
coord = getCoordPoly(self.x, self.y, self.r - (2.0/3)*self.r*(i)/(self.level), 5)
if i == 0:
outer = coord
elif i == self.level - 1:
inner = coord
if i%2 == 0:
pygame.draw.polygon(display, self.color1, coord)
else:
pygame.draw.polygon(display, self.color2, coord)
for i in range(5):
pygame.draw.line(display, self.color2, outer[i], inner[i], 3)
gray = (121, 125, 127)
darkGray = (28, 40, 51)
yellow = (212, 172, 13)
red = (203, 67, 53)
orange = (202, 111, 30)
blue = (36, 113, 163)
green = (17, 122, 101)
violet = (125, 60, 152)
class Resource:
def __init__(self, x, y):
self.x = x
self.y = y
self.r = random.randrange(20, 40)
self.type = "RESOURCE"
colors = [gray, darkGray, yellow, red, orange, blue, green, violet]
self.color1 = random.choice(colors)
self.color2 = random.choice(colors)
self.sides = random.randrange(4, 10)
self.hitPoint = random.randrange(250, 700)
self.health = self.hitPoint
self.font = pygame.font.SysFont("Agency FB", 20)
def updateHealth(self, troops):
error = 15
for troop in troops:
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
for shot in troop.shots:
if troop.isHit((shot.x, shot.y)):
self.health -= troop.damage
def draw(self):
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*1.5, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*1.5 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
outer = []
inner = []
level = self.sides
for i in range(level):
coord = getCoordPoly(self.x, self.y, self.r - (2.0/3)*self.r*(i)/(self.sides), self.sides)
if i == 0:
outer = coord
elif i == level - 1:
inner = coord
if i%2 == 0:
pygame.draw.polygon(display, self.color1, coord)
else:
pygame.draw.polygon(display, self.color2, coord)
for i in range(level):
pygame.draw.line(display, self.color2, outer[i], inner[i], 3)

776
Battles/Code/Troops.py Normal file
View File

@@ -0,0 +1,776 @@
"""
~~~~~~~~~
Troops.py
~~~~~~~~~
By - JATIN KUMAR MANDAV
This library contains different Troops classes for the game
Troops: 1. SHOOTERS
2. TANKS
3. HELICOPTERS
Also contains funcions to draw different shapes
This is completely coded in using PYGAME library of PYTHON 2.7
Lines - 777
"""
# Imports
import pygame
import sys
from math import *
# Initialize PyGame
pygame.init()
margin = 0
width = 400
height = 400
display = None
def initTroops(screen, size, m):
global display, width, height, margin
display = screen
width = size[0]
height = size[1]
margin = m
def getCoordPoly(x, y, r, n, rotate=0, anchor=(0, 0)):
coords = []
for i in range(n):
coords.append([x - anchor[0] + r*cos(2*pi*i/n + rotate), y - anchor[1] + r*sin(2*pi*i/n + rotate)])
#pygame.draw.polygon(display, black, coords, 1)
return coords[:]
class Shots:
def __init__(self, x, y, r1, speed, angle, color=(0, 0, 0), type="circle", r2=0):
self.x = x
self.y = y
self.r1 = r1
if r2 == 0:
self.r2 = r1
else:
self.r2 = r2
self.type = type
self.speed = speed
self.angle = angle
self.color = color
def draw(self):
if self.type == "circle":
pygame.draw.ellipse(display, self.color, (self.x - self.r1, self.y - self.r2, self.r1*2, self.r2*2))
elif self.type == "line":
x2 = self.x + self.r1*cos(self.angle)
y2 = self.y + self.r1*sin(self.angle)
pygame.draw.line(display, self.color, (self.x, self.y), (x2, y2), 2)
def move(self):
self.x += self.speed*cos(self.angle)
self.y += self.speed*sin(self.angle)
class Shooter:
def __init__(self, x, y, r, speed, range, level, shotSpeed, radius):
self.x = x
self.y = y
self.range = range
self.shotSpeed = shotSpeed
self.radius = radius
self.r = r
self.speed = speed
self.speedMemory = speed
self.level = level
self.slow = 20
if self.level == 1:
self.colorBase = (125, 102, 8)
self.colorHead = (212, 172, 13)
self.colorGun = (40, 55, 71)
self.hitPoint = 45
self.health = self.hitPoint
self.damage = 8
elif self.level == 2:
self.colorBase = (19, 141, 117)
self.colorHead = (11, 83, 69)
self.colorGun = (52, 73, 94)
self.hitPoint = 60
self.health = self.hitPoint
self.damage = 14
elif self.level == 3:
self.colorBase = (46, 134, 193)
self.colorHead = (21, 67, 96)
self.colorGun = (33, 47, 60)
self.hitPoint = 80
self.health = self.hitPoint
self.damage = 23
elif self.level == 4:
self.colorBase = (26, 82, 118)
self.colorHead = (20, 143, 119)
self.colorGun = (23, 32, 42)
self.hitPoint = 100
self.health = self.hitPoint
self.damage = 25
self.slow = 30
elif self.level == 5:
self.colorBase = (69, 179, 157)
self.colorHead = (244, 208, 63)
self.colorGun = (23, 32, 42)
self.hitPoint = 120
self.health = self.hitPoint
self.damage = 35
self.slow = 30
self.angle = 0
self.delay = 0
self.gunCoord = []
self.gunCoord2 = []
self.shots = []
self.nearestPos = None
self.type = "GROUND"
self.font = pygame.font.SysFont("Agency FB", 15)
def updateHealth(self, defenceStruct):
error = self.r
for struct in defenceStruct:
if not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE") and (struct.attackType == "GROUND" or struct.attackType == "GROUND AND AIR"):
if (abs(struct.nearestPos[0] - self.x) < error) and (abs(struct.nearestPos[1] - self.y) < error):
for shot in struct.shots:
if struct.isHit((shot.x, shot.y)):
self.health -= struct.damage
def isHit(self, coord):
error = 30
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
if dist < error:
return True
return False
def removeHit(self):
tempList = self.shots[:]
for shot in self.shots:
if self.isHit((shot.x, shot.y)):
tempList.remove(shot)
self.shots = tempList[:]
def shoot(self):
self.delay = (self.delay + 1)%100
if self.delay%self.slow == 0:
pos = self.nearestPos
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
if dist < self.range/2:
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle)
self.shots.append(shot)
if self.level >= 4:
shot = Shots((self.gunCoord2[1][0] + self.gunCoord2[2][0])/2, (self.gunCoord2[1][1] + self.gunCoord2[2][1])/2, self.radius, self.shotSpeed, self.angle)
self.shots.append(shot)
tempList = self.shots[:]
for shot in self.shots:
shot.move()
shot.draw()
if not ((margin < shot.x < width) and (margin < shot.y < height)):
tempList.remove(shot)
self.shots = tempList[:]
def rotate(self, coord, angle, anchor=(0, 0), corr=270):
corr = corr
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
def translate(self, coord):
return [coord[0] + self.x, coord[1] + self.y]
def draw(self):
w = self.r*4/5
h = self.r*4
#pos = pygame.mouse.get_pos()
pos = self.nearestPos
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
rectCoord = [(0, 0), (0, h), (w, h), (w, 0)]
baseCoord = []
for point in rectCoord:
baseCoord.append(self.translate(self.rotate(point, rotate, (w/2, h/2), 180)))
w = self.r/2
h = self.r*2
rectCoord = [(0, 0), (0, h), (w, h), (w, 0)]
self.gunCoord = []
shift = w/2
if self.level >= 4:
shift = 2*w
self.gunCoord2 = []
for point in rectCoord:
self.gunCoord2.append(self.translate(self.rotate(point, rotate, (-2*w, 0), 270)))
for point in rectCoord:
self.gunCoord.append(self.translate(self.rotate(point, rotate, (shift, 0), 270)))
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*5, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*5 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
if self.level >= 4:
pygame.draw.polygon(display, self.colorGun, self.gunCoord2)
pygame.draw.polygon(display, self.colorGun, self.gunCoord)
pygame.draw.polygon(display, self.colorBase, baseCoord)
pygame.draw.ellipse(display, self.colorHead, (self.x - self.r, self.y - self.r, self.r*2, self.r*2))
#pygame.draw.ellipse(display, (123, 125, 125), (self.x - self.range/2, self.y - self.range/2, self.range, self.range), 1)
def move(self, defenceStruct):
if len(defenceStruct):
nearestPos = 0
lowestDist = float("inf")
for struct in defenceStruct:
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
if lowestDist > dist:
self.nearestPos = [struct.x, struct.y]
lowestDist = dist
else:
self.nearestPos = pygame.mouse.get_pos()
angle = atan2(self.nearestPos[1] - self.y, self.nearestPos[0] - self.x)
self.x += self.speed*cos(angle)
self.y += self.speed*sin(angle)
dist = abs(self.nearestPos[1] - self.y) + abs(self.nearestPos[0] - self.x)
if dist < self.range/2:
self.speed = 0
else:
self.speed = self.speedMemory
class Tank:
def __init__(self, x, y, r, speed, range, level, shotSpeed, radius):
self.x = x
self.y = y
self.barrelX = x
self.barrelY = y
self.range = range
self.shotSpeed = shotSpeed
self.radius = radius
self.r = r
self.speed = speed
self.speedMemory = speed
self.level = level
if self.level == 1:
self.colorBase = (125, 102, 8)
self.colorWheel = (11, 83, 69)
self.colorGun = (14, 98, 81)
self.colorHead = (20, 90, 50)
self.hitPoint = 300
self.damage = 5
elif self.level == 2:
self.colorBase = (19, 141, 117)
self.colorWheel = (14, 98, 81)
self.colorGun = (11, 83, 69)
self.colorHead = (14, 98, 81)
self.hitPoint = 430
self.damage = 8
elif self.level == 3:
self.colorBase = (41, 128, 185)
self.colorWheel = (21, 67, 96)
self.colorGun = (36, 113, 163)
self.colorHead = (21, 67, 96)
self.hitPoint = 570
self.damage = 12
elif self.level == 4:
self.colorBase = (52, 73, 94)
self.colorWheel = (33, 47, 61)
self.colorGun = (23, 32, 42)
self.colorHead = (33, 47, 61)
self.hitPoint = 730
self.damage = 13
self.gunCoord2 = []
self.angle = 0
self.health = self.hitPoint
self.gunCoord = []
self.shots = []
self.type = "GROUND"
self.nearestPos = None
self.recoil = r/8
self.rebound = 0.2
self.font = pygame.font.SysFont("Agency FB", 15)
def updateHealth(self, defenceStruct):
error = self.r
for struct in defenceStruct:
if (not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE")) and (struct.attackType == "GROUND" or struct.attackType == "GROUND AND AIR"):
if (abs(struct.nearestPos[0] - self.x) < error) and (abs(struct.nearestPos[1] - self.y) < error):
for shot in struct.shots:
if struct.isHit((shot.x, shot.y)):
self.health -= struct.damage
def isHit(self, coord):
error = self.r
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
if dist < error:
return True
return False
def removeHit(self):
tempList = self.shots[:]
for shot in self.shots:
if self.isHit((shot.x, shot.y)):
tempList.remove(shot)
self.shots = tempList[:]
def shoot(self):
error = 0.5
if not ((abs(self.barrelX - self.x) < error) and (abs(self.barrelY - self.y) < error)):
self.barrelX += self.rebound*cos(self.angle)
self.barrelY += self.rebound*sin(self.angle)
else:
pos = self.nearestPos
#pos = pygame.mouse.get_pos()
dist = ((self.barrelX - pos[0])**2 + (self.barrelY - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.barrelY, pos[0] - self.barrelX)
if dist < self.range/2:
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle)
self.shots.append(shot)
if self.level == 4:
shot = Shots((self.gunCoord2[1][0] + self.gunCoord2[2][0])/2, (self.gunCoord2[1][1] + self.gunCoord2[2][1])/2, self.radius, self.shotSpeed, self.angle)
self.shots.append(shot)
self.barrelX -= self.recoil*cos(self.angle)
self.barrelY -= self.recoil*sin(self.angle)
tempList = self.shots[:]
for shot in self.shots:
shot.move()
shot.draw()
if not ((margin < shot.x < width) and (margin < shot.y < height)):
tempList.remove(shot)
self.shots = tempList[:]
def rotate(self, coord, angle, anchor=(0, 0), corr=270):
corr = corr
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
def translate(self, coord, point=-1):
if point == -1:
return [coord[0] + self.x, coord[1] + self.y]
else:
return [coord[0] + point[0], coord[1] + point[1]]
def draw(self):
#pos = pygame.mouse.get_pos()
pos = self.nearestPos
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
baseCoord = []
w = self.r
h = self.r*7/4
base = [(0, 0), (0, h), (w, h), (w, 0)]
for point in base:
baseCoord.append(self.translate(self.rotate(point, rotate, (w/2, h/2))))
w = self.r/3
h = self.r*2
wheels = [(0, 0), (0, h), (w, h), (w, 0)]
wheel1 = []
wheel2 = []
for point in wheels:
wheel1.append(self.translate(self.rotate(point, rotate, (w/2 - self.r/2 - w/2, h/2))))
wheel2.append(self.translate(self.rotate(point, rotate, (w/2 + self.r/2 + w/2, h/2))))
sides = 5
if self.level >= 3:
sides = 8
head = getCoordPoly(self.barrelX, self.barrelY , self.r/2, sides, rotate)
w = self.r/4
h = self.r*1.2
coord = [(0, 0), (0, h), (w, h), (w, 0)]
self.gunCoord = []
self.gunCoord2 = []
if self.level == 4:
for point in coord:
self.gunCoord.append(self.translate(self.rotate(point, rotate, (w/2 + w, 0)), (self.barrelX, self.barrelY)))
self.gunCoord2.append(self.translate(self.rotate(point, rotate, (w/2 - w, 0)), (self.barrelX, self.barrelY)))
else:
for point in coord:
self.gunCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0)), (self.barrelX, self.barrelY)))
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*2, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
pygame.draw.polygon(display, self.colorBase, baseCoord)
pygame.draw.polygon(display, self.colorWheel, wheel1)
pygame.draw.polygon(display, self.colorWheel, wheel2)
pygame.draw.polygon(display, self.colorGun, self.gunCoord)
if self.level == 4:
pygame.draw.polygon(display, self.colorGun, self.gunCoord2)
pygame.draw.polygon(display, self.colorHead, head)
if self.level == 2:
pygame.draw.ellipse(display, self.colorBase, (self.barrelX - self.r/4, self.barrelY - self.r/4, self.r/2, self.r/2))
if self.level >= 3:
coord = getCoordPoly(self.barrelX, self.barrelY, self.r/3, 3, rotate)
pygame.draw.polygon(display, self.colorBase, coord)
#pygame.draw.ellipse(display, (123, 125, 125), (self.x - self.range/2, self.y - self.range/2, self.range, self.range), 1)
def move(self, defenceStruct):
if len(defenceStruct):
nearestPos = 0
lowestDist = float("inf")
for struct in defenceStruct:
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
if lowestDist > dist:
self.nearestPos = [struct.x, struct.y]
lowestDist = dist
else:
self.nearestPos = pygame.mouse.get_pos()
#self.nearestPos = pygame.mouse.get_pos()
angle = atan2(self.nearestPos[1] - self.y, self.nearestPos[0] - self.x)
self.x += self.speed*cos(angle)
self.y += self.speed*sin(angle)
self.barrelX += self.speed*cos(angle)
self.barrelY += self.speed*sin(angle)
dist = abs(self.nearestPos[1] - self.y) + abs(self.nearestPos[0] - self.x)
if dist < self.range/2:
self.speed = 0
else:
self.speed = self.speedMemory
class Helicopter:
def __init__(self, x, y, r, speed, range, level, shotSpeed=5, radius=2):
self.x = x
self.y = y
self.barrelX = x
self.barrelY = y
self.range = range
self.shotSpeed = shotSpeed
self.radius = radius
self.r = r
self.speed = speed
self.speedMemory = speed
self.level = level
if self.level == 1:
self.colorFront = (26, 82, 118)
self.colorBody = (26, 82, 118)
self.colorTail = (26, 82, 118)
self.colorTailHead = (26, 82, 118)
self.colorFan = (23, 32, 42)
self.colorGun = (23, 32, 42)
self.hitPoint = 700
self.damage = 30
elif self.level == 2:
self.colorFront = (11, 83, 69)
self.colorBody = (22, 160, 133)
self.colorTail = (11, 83, 69)
self.colorTailHead = (11, 83, 69)
self.colorFan = (23, 32, 42)
self.colorGun = (23, 32, 42)
self.hitPoint = 900
self.damage = 50
elif self.level == 3:
self.colorFront = (28, 40, 51)
self.colorBody = (21, 67, 96)
self.colorTail = (28, 40, 51)
self.colorTailHead = (28, 40, 51)
self.colorFan = (23, 32, 42)
self.colorGun = (23, 32, 42)
self.hitPoint = 1300
self.damage = 52
self.angle = 0
self.rotAngle = 0
self.fanAngle = 0
self.type = "AIR"
self.gunCoord = []
self.gunCoord2 = []
self.shots = []
self.nearestPos = None
self.health = self.hitPoint
self.delay = 0
self.font = pygame.font.SysFont("Agency FB", 15)
def updateHealth(self, defenceStruct):
error = self.r
for struct in defenceStruct:
if not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE") and (struct.attackType == "AIR" or struct.attackType == "GROUND AND AIR"):
if (abs(struct.nearestPos[0] - self.x) < error) and (abs(struct.nearestPos[1] - self.y) < error):
for shot in struct.shots:
if struct.isHit((shot.x, shot.y)):
self.health -= struct.damage
def isHit(self, coord):
error = self.r
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
if dist < error:
return True
return False
def removeHit(self):
tempList = self.shots[:]
for shot in self.shots:
if self.isHit((shot.x, shot.y)):
tempList.remove(shot)
self.shots = tempList[:]
def shoot(self):
self.delay = (self.delay + 1)%100
if self.level == 1:
if self.speed == 0 and self.rotAngle == -90 and self.delay%7 == 0:
pos = self.nearestPos
#pos = pygame.mouse.get_pos()
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
if dist < self.range/2:
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
self.shots.append(shot)
if self.level == 2:
if self.delay%10 == 0:
pos = self.nearestPos
#pos = pygame.mouse.get_pos()
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
if dist < self.range/2:
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
self.shots.append(shot)
elif self.level == 3:
if self.delay%10 == 0:
pos = self.nearestPos
#pos = pygame.mouse.get_pos()
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
if dist < self.range/2:
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
self.shots.append(shot)
shot = Shots((self.gunCoord2[1][0] + self.gunCoord2[2][0])/2, (self.gunCoord2[1][1] + self.gunCoord2[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
self.shots.append(shot)
tempList = self.shots[:]
for shot in self.shots:
shot.move()
shot.draw()
if not ((margin < shot.x < width) and (margin < shot.y < height)):
tempList.remove(shot)
self.shots = tempList[:]
def rotate(self, coord, angle, anchor=(0, 0), corr=270):
corr = corr
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
def translate(self, coord, point=-1):
if point == -1:
return [coord[0] + self.x, coord[1] + self.y]
else:
return [coord[0] + point[0], coord[1] + point[1]]
def draw(self):
#pos = pygame.mouse.get_pos()
pos = self.nearestPos
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
self.fanAngle = (self.fanAngle + 46)%360
len = (self.r*2)/(3**0.5)
centre = (self.x, self.y)
top = (self.x + len*cos(radians(self.fanAngle)), self.y + len*sin(radians(self.fanAngle)))
left = (self.x + len*cos(radians(self.fanAngle - 120)), self.y + len*sin(radians(self.fanAngle - 120)))
right = (self.x + len*cos(radians(self.fanAngle + 120)), self.y + len*sin(radians(self.fanAngle + 120)))
w = self.r/2
h = self.r/2
if self.level >= 2:
w = self.r/4
h = self.r/2
points = [(0, 0), (0, h), (w, h), (w, 0)]
base = []
if self.speed == 0 and self.rotAngle > -90 and self.level == 1:
self.rotAngle -= 3
for point in points:
base.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2, h/2))))
front = getCoordPoly((base[1][0] + base[2][0])/2, (base[1][1] + base[2][1])/2, w/2, 8, rotate + radians(self.rotAngle))
if self.level >= 2:
back = getCoordPoly((base[0][0] + base[3][0])/2, (base[0][1] + base[3][1])/2, w, 8, rotate + radians(self.rotAngle))
else:
back = getCoordPoly((base[0][0] + base[3][0])/2, (base[0][1] + base[3][1])/2, w/2, 8, rotate + radians(self.rotAngle))
tail = []
if self.level >= 2:
w = self.r/3
h = self.r/2
points = [(0, 0), (0, h), (w, h), (w, 0)]
base2 = []
for point in points:
base2.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2, h*2/3))))
flap1 = []
flap2 = []
w = self.r/3
h = self.r/4
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
flap1.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2 + w, h))))
flap2.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2 - w, h))))
w = self.r/8
h = self.r*1.2
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
tail.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2, h))))
if self.level == 1:
self.gunCoord = []
w = self.r/15
h = self.r/2.5
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
self.gunCoord.append(self.translate(self.rotate(point, rotate + radians(90) + radians(self.rotAngle), (w/2, 0))))
elif self.level == 2:
self.gunCoord = []
w = self.r/15
h = self.r/2.5
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
self.gunCoord.append(self.translate(self.rotate(point, rotate + radians(180) + radians(self.rotAngle), (w/2, h*3/2))))
elif self.level == 3:
self.gunCoord = []
self.gunCoord2 = []
w = self.r/15
h = self.r/2.5
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
self.gunCoord.append(self.translate(self.rotate(point, rotate + radians(180) + radians(self.rotAngle), (w/2 + 5*w, h/2))))
self.gunCoord2.append(self.translate(self.rotate(point, rotate + radians(180) + radians(self.rotAngle), (w/2 - 5*w, h/2))))
tailHead = []
w = self.r/10
h = self.r/3
points = [(0, 0), (0, h), (w, h), (w, 0)]
for point in points:
tailHead.append(self.translate(self.rotate(point, rotate + radians(90) + radians(self.rotAngle), (self.r + w, h/2))))
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*2, 40, 8))
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
pygame.draw.polygon(display, self.colorFront, front)
pygame.draw.polygon(display, self.colorGun, self.gunCoord)
pygame.draw.polygon(display, self.colorBody, back)
pygame.draw.polygon(display, self.colorBody, base)
if self.level >= 2:
pygame.draw.polygon(display, self.colorBody, base2)
pygame.draw.polygon(display, self.colorFront, flap1)
pygame.draw.polygon(display, self.colorFront, flap2)
if self.level == 3:
pygame.draw.polygon(display, self.colorGun, self.gunCoord2)
pygame.draw.polygon(display, self.colorTail, tail)
pygame.draw.polygon(display, self.colorTailHead, tailHead)
pygame.draw.line(display, self.colorFan, top, centre, 5)
pygame.draw.line(display, self.colorFan, left, centre, 5)
pygame.draw.line(display, self.colorFan, right, centre, 5)
def move(self, defenceStruct):
if len(defenceStruct):
nearestPos = 0
lowestDist = float("inf")
for struct in defenceStruct:
if struct.type == "HEADQUARTERS" or struct.type == "RESOURCE":
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
else:
dist = (self.x - struct.xOld)**2 + (self.y - struct.yOld)**2
if lowestDist > dist:
if struct.type == "HEADQUARTERS" or struct.type == "RESOURCE":
self.nearestPos = [struct.x, struct.y]
else:
self.nearestPos = [struct.xOld, struct.yOld]
lowestDist = dist
else:
self.nearestPos = pygame.mouse.get_pos()
#self.nearestPos = pygame.mouse.get_pos()
angle = atan2(self.nearestPos[1] - self.y, self.nearestPos[0] - self.x)
self.x += self.speed*cos(angle)
self.y += self.speed*sin(angle)
dist = abs(self.nearestPos[1] - self.y) + abs(self.nearestPos[0] - self.x)
if dist < self.range/2:
self.speed = 0
else:
self.speed = self.speedMemory
self.rotAngle = 0

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.