Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 34.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 39.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 40.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 41.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 42.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 43.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 51.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 52.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 53.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 54.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 55.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 56.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 57.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 58.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 59.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 61.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 62.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 63.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions Enemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import math
import pygame

pygame.init()


class Enem:
def __init__(self, x, y, vel):
self.live = True
self.health = 5000
self.max_health = 5000
self.vel = vel
self.path = [(76, 80), (138, 56), (193, 80), (683, 79), (756, 152), (716, 241), (505, 265), (452, 309),
(450, 555), (450, 600)]
self.x = x
self.y = y
self.path_pos = 0
self.i = 0
self.img_left = []
self.img_right = []
for i in range(1, 12):
self.img_left.append(pygame.image.load("run_left (" + str(i) + ").png"))
self.img_right.append(pygame.image.load("run_right (" + str(i) + ").png"))
for i in range(len(self.img_right)):
self.img_right[i] = pygame.transform.scale(self.img_right[i], (94, 80))
for i in range(len(self.img_left)):
self.img_left[i] = pygame.transform.scale(self.img_left[i], (94, 80))
self.width = self.img_right[0].get_width()
self.height = self.img_right[0].get_height()

def move(self):
if (self.path[self.path_pos][0] - self.x) <= 5 and (self.path[self.path_pos][1] - self.y) <= 5:
self.path_pos += 1
x1, y1 = self.x, self.y
x2, y2 = self.path[self.path_pos]
hypothenuse = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
self.x += (x2 - x1) / hypothenuse * self.vel
self.y += (y2 - y1) / hypothenuse * self.vel

def draw(self, win):
if self.path[self.path_pos][0] - self.x < 0:
win.blit(self.img_left[self.i], (self.x - self.width // 2, self.y - self.height // 2))
else:
win.blit(self.img_right[self.i], (self.x - self.width // 2, self.y - self.height // 2))
self.i += 1
if self.i >= len(self.img_right):
self.i = 0
self.health_bar(win)

def set_health(self, damage):
self.health -= damage
if self.health <= 0:
self.live = False
else:
self.live = True

def health_bar(self, win):
length = 75
move_by = length / self.max_health
health_bar = move_by * self.health
red =(255, 0, 0)
green = (0, 255, 0)
rect_size_red = (self.x - self.width // 2, self.y - self.height // 2, length, 10)
rect_size_green = (self.x - self.width // 2, self.y - self.height // 2, health_bar, 10)
pygame.draw.rect(win, red, rect_size_red, 0)
pygame.draw.rect(win, green, rect_size_green, 0)

class Troll(Enem):
def __init__(self, x, y, vel):
self.live = True
self.health = 14000
self.max_health = 14000
self.vel = vel
self.path = [(76, 80), (138, 56), (193, 80), (683, 79), (756, 152), (716, 241), (505, 265), (452, 309),
(450, 555), (450, 600)]
self.x = x
self.y = y
self.path_pos = 0
self.i = 0
self.img_left = []
self.img_right = []
for i in range(1, 21):
self.img_left.append(pygame.image.load("run_troll_left (" + str(i) + ").png"))
self.img_right.append(pygame.image.load("run_troll_right (" + str(i) + ").png"))
for i in range(len(self.img_right)):
self.img_right[i] = pygame.transform.scale(self.img_right[i], (94, 80))
for i in range(len(self.img_left)):
self.img_left[i] = pygame.transform.scale(self.img_left[i], (94, 80))
self.width = self.img_right[0].get_width()
self.height = self.img_right[0].get_height()
192 changes: 192 additions & 0 deletions Game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import pygame
import Menu
import Towers
import Enemy
import time

pygame.init()

white = (255, 255, 255)
size_for_img = (50, 50)


class Graphics:
def __init__(self):
self.star_img = pygame.image.load("star.png")
self.star_img.set_colorkey(white)
self.star_img = pygame.transform.scale(self.star_img, size_for_img)
self.health_img = pygame.image.load("health.png")
self.health_img.set_colorkey(white)
self.health_img = pygame.transform.scale(self.health_img, size_for_img)

def draw(self, win, money, health):
win.blit(self.star_img, (20, 500))
text = pygame.font.SysFont("arial", 50)
text = text.render(str(money), 1, white)
win.blit(text, (75, 500))
win.blit(self.health_img, (20, 450))
text = pygame.font.SysFont("arial", 50)
text = text.render(str(health), 1, white)
win.blit(text, (75, 450))


class Music:
def __init__(self):
pygame.mixer_music.load("song.mp3")
pygame.mixer_music.play(-1)

def off(self):
pygame.mixer.music.pause()

def con(self):
pygame.mixer.music.unpause()


def gen(n, vel):
temp = []
x = 0
y = 80
if n >= 4:
temp.append(Enemy.Troll(x, y, vel - 0.3))
x -= 50
if n >= 8:
temp.append(Enemy.Troll(x, y, vel - 0.3))
x -= 50
for i in range(n):
temp.append(Enemy.Enem(x, y, vel))
x -= 50
return temp


class Game:
def __init__(self):
self.width = 1000
self.height = 562
self.win = pygame.display.set_mode((self.width, self.height))
self.bg = pygame.image.load("bg.png")
self.vm = Menu.VerticalMenu(945, 280)
self.vm.all_buttons()
self.mus = Music()
self.money = 500
self.towers = []
self.waves = [gen(2, 1), gen(4, 1.1), gen(6, 1.2), gen(7, 1.3), gen(8, 1.4), gen(10, 1.7)]
self.waves_i = 0
self.enemies = self.waves[self.waves_i]
self.health = 10
self.graph = Graphics()
self.wn = False
self.tower_place = [[281, 113], [447, 112], [656, 126],
[546, 301], [270, 347],
[363, 474], [539, 475]]
for i in self.tower_place:
i.append(False)

def run(self):
r = True
FPS = 60
clock = pygame.time.Clock()
while r:
clock.tick(FPS)
if self.health <= 0:
r = False
if not self.enemies:
self.waves_i += 1
if self.waves_i >= len(self.waves):
self.wn = True
r = False
else:
self.enemies = self.waves[self.waves_i]
for events in pygame.event.get():
if events.type == pygame.QUIT:
r = False
elif events.type == pygame.KEYUP:
if events.key == pygame.K_1:
self.mus.off()
elif events.key == pygame.K_2:
self.mus.con()
for buttons in self.vm.but:
if events.type == pygame.MOUSEBUTTONDOWN and buttons.click() and self.money >= buttons.cost:
for but in self.vm.but:
but.selected = False
buttons.selected = True
bo = True
for but in self.vm.but:
if but.click():
bo = False
if bo and buttons.selected and events.type == pygame.MOUSEBUTTONDOWN:
"""
Tower будет получать координаты и ставиться на это место
"""
if buttons.ty == "ArcherTower":
temp = Towers.ArcherTower(0, 0)
if self.money >= temp.price:
pos = pygame.mouse.get_pos()
for i in range(len(self.tower_place)):
if abs(self.tower_place[i][0] - pos[0]) <= 25 and abs(
self.tower_place[i][1] - pos[1]) <= 20 and not self.tower_place[i][2]:
pos1 = self.tower_place[i][0]
pos2 = self.tower_place[i][1]
temp = Towers.ArcherTower(pos1, pos2)
self.towers.append(temp)
self.money -= temp.price
self.tower_place[i][2] = True
if buttons.ty == "ArcherTower1":
temp = Towers.ArcherTower1(0, 0)
if self.money >= temp.price:
pos = pygame.mouse.get_pos()
for i in range(len(self.tower_place)):
if abs(self.tower_place[i][0] - pos[0]) <= 25 and abs(
self.tower_place[i][1] - pos[1]) <= 20 and not self.tower_place[i][2]:
pos1 = self.tower_place[i][0]
pos2 = self.tower_place[i][1]
temp = Towers.ArcherTower1(pos1, pos2)
self.towers.append(temp)
self.money -= temp.price
self.tower_place[i][2] = True
buttons.selected = False
pygame.mouse.set_visible(True)
for i in range(len(self.enemies)):
if self.enemies[i].path_pos == len(self.enemies[i].path) - 1:
self.enemies.pop(i)
self.health -= 1
break
self.enemies[i].move()
if not self.enemies[i].live:
self.enemies.pop(i)
self.money += 50
break
for t in self.towers:
t.fire(self.enemies)
self.draw()

def draw(self):
self.win.blit(self.bg, (0, 0))
self.vm.draw(self.win)
for en in self.enemies:
en.draw(self.win)
for tow in self.towers:
tow.draw(self.win)
for buttons in self.vm.but:
if buttons.selected:
pygame.mouse.set_visible(False)
pos = pygame.mouse.get_pos()
if buttons.ty == "ArcherTower":
temp = Towers.ArcherTower(*pygame.mouse.get_pos())
if buttons.ty == "ArcherTower1":
temp = Towers.ArcherTower1(*pygame.mouse.get_pos())
self.win.blit(temp.img,
(pos[0] - temp.img.get_width() // 2, pos[1] - temp.img.get_height() // 2))
for tmp in self.tower_place:
if not tmp[2]:
pygame.draw.circle(self.win, (255, 0, 0), (tmp[0], tmp[1] + 40), 10)
self.graph.draw(self.win, self.money, self.health)
if self.wn:
img = pygame.transform.scale(pygame.image.load("win.png"), (243, 100))
self.win.blit(img, (self.width // 2 - img.get_width() // 2, self.height // 2 - img.get_height() // 2))
pygame.display.update()
if self.wn:
time.sleep(1)


g = Game()
g.run()
74 changes: 74 additions & 0 deletions Menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pygame

pygame.init()


class Button:
def click(self):
pos = pygame.mouse.get_pos()
if pos[0] >= self.x - self.width // 2 and pos[0] <= self.x + self.width // 2 and pos[
1] <= self.y + self.height // 2 and pos[1] >= self.y - self.height // 2:
return True
return False

def draw(self, win):
win.blit(self.img, (self.x - self.width // 2, self.y - self.height // 2))


class VerticalButton(Button):
def __init__(self, img, cost, ty):
self.cost = cost
self.img = img
self.width = self.img.get_width()
self.height = self.img.get_height()
self.x = None
self.y = None
self.selected = False
self.ty = ty

def draw(self, win):
win.blit(self.img, (self.x - self.width // 2, self.y - self.height // 2))


class Menu:
def all_buttons(self):
img_button_32 = pygame.image.load("32.png")
img_button_32 = pygame.transform.scale(img_button_32, (70, 60))
button_32 = VerticalButton(img_button_32, 250, "ArcherTower")
self.add_button(button_32)
img_button_34 = pygame.image.load("34.png")
img_button_34 = pygame.transform.scale(img_button_34, (70, 60))
button_34 = VerticalButton(img_button_34, 300, "ArcherTower1")
self.add_button(button_34)

def add_button(self, button):
button.x = self.x
button.y = self.y - self.height // 2 + self.sizes[-1][0] + 10
self.sizes[-1][1] = self.sizes[-1][0] + button.height + 10
self.sizes.append([self.sizes[-1][1] + 30, None])
self.but.append(button)

def draw(self, win):
win.blit(self.img, (self.x - self.width // 2, self.y - self.height // 2))
for button in self.but:
button.draw(win)
win.blit(self.star_img, (button.x - 35, 7 + button.y + button.height // 2))
f = pygame.font.SysFont('arial', 25)
text = f.render(str(button.cost), 1, (255, 255, 255))
win.blit(text, (button.x, button.y + button.height // 2 + 5))


class VerticalMenu(Menu):
def __init__(self, x, y):
self.x = x
self.y = y
self.sizes = [[45, None]]
self.but = []
"""Graphics"""
self.star_img = pygame.image.load("star.png")
self.star_img.set_colorkey((255, 255, 255))
self.star_img = pygame.transform.scale(self.star_img, (20, 20))
self.img = pygame.image.load("table.png")
self.img = pygame.transform.scale(self.img, (115, 430))
self.width = self.img.get_width()
self.height = self.img.get_height()
Loading