From 61b3e77c07f4db10b80eef991d51d114755c59af Mon Sep 17 00:00:00 2001 From: Zergling_man Date: Sun, 15 Dec 2024 12:30:05 +1100 Subject: [PATCH] I should have made this a repo a while ago --- enemy.py | 10 +++++++ game.py | 41 ++++++++++++++++++++++++++++ grid.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ring.py | 34 +++++++++++++++++++++++ towers.py | 45 ++++++++++++++++++++++++++++++ utils.py | 12 ++++++++ workers.py | 28 +++++++++++++++++++ 7 files changed, 250 insertions(+) create mode 100644 enemy.py create mode 100644 game.py create mode 100644 grid.py create mode 100644 ring.py create mode 100644 towers.py create mode 100644 utils.py create mode 100644 workers.py diff --git a/enemy.py b/enemy.py new file mode 100644 index 0000000..dbdf2ee --- /dev/null +++ b/enemy.py @@ -0,0 +1,10 @@ +import pygame + +class Enemy(): + def __init__(self,x,y): + self.x,self.y=x,y + + def draw(self,screen): + pygame.draw.circle(screen,'black',(self.x,self.y),8) + + def update(self,events): pass \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..3f2df90 --- /dev/null +++ b/game.py @@ -0,0 +1,41 @@ +import pygame +pygame.init() +import grid as grod +import workers +import ring + +running=False +objs=[] + +def main(): + screen=pygame.display.set_mode((1280,720)) + clock=pygame.time.Clock() + global running; running=True + act=0 + grid=grod.Grid(8,8,64,12,11) + grid.add_worker(workers.behaviours.Cursor) + grid.add_worker(workers.behaviours.Average) + grid.add_tower(5,5) + grid.add_tower(3,4,'cannon',2) + objs.append(grid) + while running: + update() + draw(screen) + clock.tick(60) + pygame.quit() + +def update(): + events=pygame.event.get() + for event in events: + if event.type==pygame.QUIT: global running; running=False + if event.type==pygame.KEYDOWN: print(event) + for obj in objs: + obj.update(events) + +def draw(screen): + screen.fill((64,128,64)) + for obj in objs: + obj.draw(screen) + pygame.display.flip() + +main() \ No newline at end of file diff --git a/grid.py b/grid.py new file mode 100644 index 0000000..1bd0cd9 --- /dev/null +++ b/grid.py @@ -0,0 +1,80 @@ +import pygame +import workers, towers, ring +import random + +boxcols=[(90,90,90),(128,0,0),(0,128,0),(0,0,128)] +class Grid(): + def __init__(self,x,y,spacing,w,h): + self.x,self.y,self.spacing,self.w,self.h=x,y,spacing,w,h + self.xe,self.ye=x+spacing*w,y+spacing*h + self.highlight=None + self.act=0 + self.menu=None + self.mx,self.my=0,0 + self.towers=[] # I'll make this a property later + self.workers=[] + + def cell(self,x,y): + sx,sy,s,w,h=self.x,self.y,self.spacing,self.w,self.h + if not (sx-1: self.actions[self.selected](self.x,self.y) \ No newline at end of file diff --git a/towers.py b/towers.py new file mode 100644 index 0000000..ce08439 --- /dev/null +++ b/towers.py @@ -0,0 +1,45 @@ +import pygame + +class Tower(): + def __init__(self,x,y,grid,ttype='null',elem=0): + self.x,self.y,self.grid=x,y,grid + self.type=ttype + self.elem=elem + self.target=Mouse() + + def draw(self,screen): + x,y,s=self.grid.cell_to_real(self.x,self.y) + sprites.get(self.type,'null')(screen,x,y,s,self) + + def update(self,events): + self.target.update(events) + if self.target: self.tx,self.ty=self.target.x,self.target.y + +class Mouse(): + def update(self,events): + self.x,self.y=pygame.mouse.get_pos() + +shots={} +def mkshot(funky): + shots[funky.__name__]=funky + return funky + +sprites={} +def mksprite(funky): + sprites[funky.__name__]=funky + return funky + +@mksprite +def null(screen,x,y,s,t): + pygame.draw.rect(screen, (90,90,90), (x,y,s,s)) + +@mkshot +def null(*args): pass + +@mksprite +def cannon(screen,x,y,s,t): + pygame.draw.rect(screen, (90+165*(t.elem==0),90+165*(t.elem==1),90+165*(t.elem==2)), (x,y,s,s)) + pygame.draw.line(screen, (255,0,0), (x+s/2,y+s/2),(t.tx,t.ty)) + +@mkshot +def cannon(*args): pass \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..429350d --- /dev/null +++ b/utils.py @@ -0,0 +1,12 @@ +import math + +# DIRECTIONS GO BACKWARDS IN THIS GAME +# WHY NOT +def get_direction(x1,y1,x2,y2): + dx,dy=x2-x1,y2-y1 + if dx==0: return math.pi*(2-sign(dy))/2 + t=math.atan(dy/dx) + return t+math.pi*(sign(dx)<0) + +def sign(num): + return (num>=0)*2-1-(num==0) \ No newline at end of file diff --git a/workers.py b/workers.py new file mode 100644 index 0000000..98113ab --- /dev/null +++ b/workers.py @@ -0,0 +1,28 @@ +import pygame +import enum +import math +import random +import utils + +behaviours=enum.Enum('behaviours',['Cursor','Average']) +class Worker(): + def __init__(self,x,y,behaviour,grid): + self.x,self.y,self.behaviour,self.grid=x,y,behaviour,grid + self.direction=0 + + def target(self): + if self.behaviour==behaviours.Cursor: + return pygame.mouse.get_pos() + if self.behaviour==behaviours.Average: + return self.grid.mx,self.grid.my + + def draw(self,screen): + pygame.draw.circle(screen,'grey',(self.x,self.y),8) + pygame.draw.circle(screen,'black',(self.x+math.cos(self.dir)*5,self.y+math.sin(self.dir)*5),3) + + def update(self,events): + mx,my=self.target() + self.dir=utils.get_direction(self.x,self.y,mx,my) + speed=min(5,((mx-self.x)**2+(my-self.y)**2)**0.5*0.1) + self.x+=math.cos(self.dir)*speed + self.y+=math.sin(self.dir)*speed \ No newline at end of file