From a2c171bc27353c1f164ca9c57f99747d42f71ce4 Mon Sep 17 00:00:00 2001 From: Prathmesh Ingale Date: Mon, 7 Mar 2022 14:08:02 +0530 Subject: [PATCH 1/5] Enemy_modified --- enemy_class.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/enemy_class.py b/enemy_class.py index 96164fe..75c05df 100644 --- a/enemy_class.py +++ b/enemy_class.py @@ -1,14 +1,66 @@ import pygame +from settings import * + vec = pygame.math.vector class Enemy: - def __init__(self, app, pos): + def __init__(self, app, pos, number): self.app = app self.grid_pos = pos self.pix_pos = self.get_pix_pos() + self.radius = int(self.app.cell_width//2.3) + self.number = number + self.colour = self.set_colour() + self.direction = vec(1,0) + self.personality = self.set_personality() + + + + + def update(self): + self.pix_pos += self.direction + if self.time_to_move: + self.move() + + def draw(self): + pygame.draw.circle(self.app.screen, self.colour,(int(self.pix_pos.x), int(self.pix_pos.y)), self.radius) + + + def time_to_move(self): + pass + + def move(self): + pass def get_pix_pos(self): return vec((self.grid_pos.x * self.app.cell_width) + TOP_BOTTOM_BUFFER // 2 + self.app.cell_width // 2, (self.grid_pos.y * self.app.cell_height) + TOP_BOTTOM_BUFFER // 2 + self.app.cell_height // 2) - - def draw(self): - pygame.draw.circle(self.app.screen, (255,255,255),(int(self.pix_pos.x), int(self.pix_pos.y)), 15) #konsi screen, colour, position, 15 is size + + def set_colour(self): + if self.number == 0: + return (43, 78, 203) + if self.number == 1: + return (189, 29, 29) + if self.number == 2: + return (197, 200, 27) + if self.number == 3: + return (215, 159, 33) + + + def set_personality(self): + if self.number == 0: + return "speedy" + elif self.number == 1: + return "slow" + elif self.number == 2: + return "random" + else: + return "scared" + + + + + + + + + From dd969cb451f08863c7bfc0738bee05306735446f Mon Sep 17 00:00:00 2001 From: Prathmesh Ingale Date: Tue, 8 Mar 2022 15:19:12 +0530 Subject: [PATCH 2/5] Enemy Movements --- enemy_class.py | 81 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/enemy_class.py b/enemy_class.py index 75c05df..24d839a 100644 --- a/enemy_class.py +++ b/enemy_class.py @@ -3,34 +3,99 @@ vec = pygame.math.vector + + class Enemy: def __init__(self, app, pos, number): self.app = app self.grid_pos = pos + self.starting_pos = [pos.x, pos.y] self.pix_pos = self.get_pix_pos() self.radius = int(self.app.cell_width//2.3) self.number = number self.colour = self.set_colour() - self.direction = vec(1,0) + self.direction = vec(0,0) self.personality = self.set_personality() - - + self.target = None + self.speed = self.set_speed() def update(self): - self.pix_pos += self.direction - if self.time_to_move: - self.move() + self.target = self.set_target() + if self.target != self.grid_pos: + self.pix_pos += self.direction * self.speed + if self.time_to_move(): + self.move() + + # Setting grid position in reference to pix position + self.grid_pos[0] = (self.pix_pos[0]-TOP_BOTTOM_BUFFER + + self.app.cell_width//2)//self.app.cell_width+1 + self.grid_pos[1] = (self.pix_pos[1]-TOP_BOTTOM_BUFFER + + self.app.cell_height//2)//self.app.cell_height+1 + def set_target(self): + if self.personality == "speedy" or self.personality == "slow": + return self.app.player.grid_pos + else: + if self.app.player.grid_pos[0] > COLS//2 and self.app.player.grid_pos[1] > ROWS//2: + return vec(1, 1) + if self.app.player.grid_pos[0] > COLS//2 and self.app.player.grid_pos[1] < ROWS//2: + return vec(1, ROWS-2) + if self.app.player.grid_pos[0] < COLS//2 and self.app.player.grid_pos[1] > ROWS//2: + return vec(COLS-2, 1) + else: + return vec(COLS-2, ROWS-2) + def draw(self): pygame.draw.circle(self.app.screen, self.colour,(int(self.pix_pos.x), int(self.pix_pos.y)), self.radius) - + def set_speed(self): + if self.personality in ["speedy", "scared"]: + speed = 2 + else: + speed = 1 + return speed + + def time_to_move(self): pass def move(self): - pass + pass + + + def BFS(self, start, target): + grid = [[0 for x in range(28)] for x in range(30)] + for cell in self.app.walls: + if cell.x < 28 and cell.y < 30: + grid[int(cell.y)][int(cell.x)] = 1 + queue = [start] + path = [] + visited = [] + while queue: + current = queue[0] + queue.remove(queue[0]) + visited.append(current) + if current == target: + break + else: + neighbours = [[0, -1], [1, 0], [0, 1], [-1, 0]] + for neighbour in neighbours: + if neighbour[0]+current[0] >= 0 and neighbour[0] + current[0] < len(grid[0]): + if neighbour[1]+current[1] >= 0 and neighbour[1] + current[1] < len(grid): + next_cell = [neighbour[0] + current[0], neighbour[1] + current[1]] + if next_cell not in visited: + if grid[next_cell[1]][next_cell[0]] != 1: + queue.append(next_cell) + path.append({"Current": current, "Next": next_cell}) + shortest = [target] + while target != start: + for step in path: + if step["Next"] == target: + target = step["Current"] + shortest.insert(0, step["Current"]) + return shortest + def get_pix_pos(self): return vec((self.grid_pos.x * self.app.cell_width) + TOP_BOTTOM_BUFFER // 2 + self.app.cell_width // 2, (self.grid_pos.y * self.app.cell_height) + TOP_BOTTOM_BUFFER // 2 + self.app.cell_height // 2) From 3260a9ca7b2f80cf04f0ee678d01c7d1d22e492e Mon Sep 17 00:00:00 2001 From: Prathmesh Ingale Date: Tue, 8 Mar 2022 15:34:00 +0530 Subject: [PATCH 3/5] clening --- README.md | 1 - app_class.py | 46 ---------------------------------------------- hello.py | 1 - main.py | 10 ---------- settings.py | 26 -------------------------- 5 files changed, 84 deletions(-) delete mode 100644 README.md delete mode 100644 app_class.py delete mode 100644 hello.py delete mode 100644 main.py delete mode 100644 settings.py diff --git a/README.md b/README.md deleted file mode 100644 index 0c3cc7a..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# pacman-game-in-python \ No newline at end of file diff --git a/app_class.py b/app_class.py deleted file mode 100644 index 875e7a6..0000000 --- a/app_class.py +++ /dev/null @@ -1,46 +0,0 @@ -import imp -from re import L -import pygame as pg -import sys -from settings import * - -pg.init() -vec = pg.math.Vector2 - -class App: - def __init__(self): - self.screen = pg.display.set_mode((WIDTH,HEIGHT)) - self.clock = pg.time.Clock() - self.running = True - self.state = 'intro' - - def run(self): - while self.running: - if self.state == 'intro': - self.intro_events() - self.intro_update() - self.intro_draw() - else : - pass - - self.clock.tick(FPS) - pg.quit() - sys.exit() - - - -######################## INTRO FUNCTIONS ######################## - - - def intro_events(self): - for event in pg.event.get(): - if event.type == pg.QUIT: - self.running = False - - - def intro_update(self): - pass - - - def intro_draw(self): - pg.display.update() \ No newline at end of file diff --git a/hello.py b/hello.py deleted file mode 100644 index 053ff8c..0000000 --- a/hello.py +++ /dev/null @@ -1 +0,0 @@ -print("HELLOO") diff --git a/main.py b/main.py deleted file mode 100644 index fe1c52b..0000000 --- a/main.py +++ /dev/null @@ -1,10 +0,0 @@ -from app_class import * - - - -if __name__ == '__main__': - app=App() - app.run() - - -answer = input('wrong') \ No newline at end of file diff --git a/settings.py b/settings.py deleted file mode 100644 index 226ed00..0000000 --- a/settings.py +++ /dev/null @@ -1,26 +0,0 @@ -from pygame.math import Vector2 as vec - -# screen settings -WIDTH, HEIGHT = 610, 670 -FPS = 60 -TOP_BOTTOM_BUFFER = 50 -MAZE_WIDTH, MAZE_HEIGHT = WIDTH-TOP_BOTTOM_BUFFER, HEIGHT-TOP_BOTTOM_BUFFER - -ROWS = 30 -COLS = 28 - -# colour settings -BLACK = (0, 0, 0) -RED = (208, 22, 22) -GREY = (107, 107, 107) -WHITE = (255, 255, 255) -PLAYER_COLOUR = (190, 194, 15) - -# font settings -START_TEXT_SIZE = 16 -START_FONT = 'arial black' - -# player settings -# PLAYER_START_POS = vec(2, 2) - -# mob settings From dedfd2956060bbc119659f6d5acbaf0f72e2675e Mon Sep 17 00:00:00 2001 From: Prathmesh Ingale Date: Tue, 8 Mar 2022 22:58:46 +0530 Subject: [PATCH 4/5] directions_for_enemy_movement --- enemy_class.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/enemy_class.py b/enemy_class.py index 24d839a..5e098db 100644 --- a/enemy_class.py +++ b/enemy_class.py @@ -58,11 +58,32 @@ def set_speed(self): def time_to_move(self): - pass + if int(self.pix_pos.x+TOP_BOTTOM_BUFFER//2) % self.app.cell_width == 0: + if self.direction == vec(1, 0) or self.direction == vec(-1, 0) or self.direction == vec(0, 0): + return True + if int(self.pix_pos.y+TOP_BOTTOM_BUFFER//2) % self.app.cell_height == 0: + if self.direction == vec(0, 1) or self.direction == vec(0, -1) or self.direction == vec(0, 0): + return True + return False def move(self): - pass + self.direction = self.get_random_direction() + def get_random_direction(self): + while True: + number = random.randint(-2, 1) + if number == -2: + x_dir, y_dir = 1, 0 + elif number == -1: + x_dir, y_dir = 0, 1 + elif number == 0: + x_dir, y_dir = -1, 0 + else: + x_dir, y_dir = 0, -1 + next_pos = vec(self.grid_pos.x + x_dir, self.grid_pos.y + y_dir) + if next_pos not in self.app.walls: + break + return vec(x_dir, y_dir) def BFS(self, start, target): grid = [[0 for x in range(28)] for x in range(30)] @@ -120,12 +141,3 @@ def set_personality(self): return "random" else: return "scared" - - - - - - - - - From 8d7a04d52e4ae6ff7c11f7d94da79714b1be08f9 Mon Sep 17 00:00:00 2001 From: Prathmesh Ingale Date: Wed, 9 Mar 2022 00:30:29 +0530 Subject: [PATCH 5/5] Removing __pycache__ --- __pycache__/app_class.cpython-38.pyc | Bin 1405 -> 0 bytes __pycache__/settings.cpython-38.pyc | Bin 189 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 __pycache__/app_class.cpython-38.pyc delete mode 100644 __pycache__/settings.cpython-38.pyc diff --git a/__pycache__/app_class.cpython-38.pyc b/__pycache__/app_class.cpython-38.pyc deleted file mode 100644 index 2ea3a1dc469b79bad068e029936721cf90791e88..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1405 zcmZuwOK%iM5boDJc6RN6Q6M5mq(}q8D;^0P5CY+VEuj^NB}gry(PX-@XFWSR?(QY5 zWlr!jIINHUOMT5Le}Pk~X0pK+?p1x&U0qvK^;PxjwY7x6x-q@_=NC@M-*`B^z(yW{ z*b#stidICTW)+<>$!KTeQnYu03_$ZL91|^;(J`T*C-I~wlV?N;6?`Tt_{3yi(N`o5 z|AClflu}QhBU~@=e0niBJOZ(I0W^_R5y=!)>@$&EaV0=QR26~_RHS0iq3Wpwbfo(5 zje+Nno2H=f|JB010C@spHvt@(&?R%+iFfRn9+4~nONi;iK2~VyDF&D*0wMWm?1Qp) zW|sCnYj%C4O50TVLdI6RY&ug~25)w@#@jO7{(a}!_E?6tFk07MxN-`YPpVn5?}MUM zk(t+ZS?~J5y4>j#>V?(SyAxv8A-Au0hI_NA9!~SRw0qgF_wN2S+|qX6&6;787rQy! zseEy3m^V#URJpaA&BDi7R@SA=dd{<6JqWI zQbqTfxqd{J3|k7C|EC4&8a;z$=%Qmv$pLZkvWIq;Ec>Wi*4W4d5HlSZ95{oC`9aHK zrr+z@+0|*@D4@Y7-JLS|U#17n61Xe&C4c(jl?n0B;$U7n&+Wo`p+A()3@|Qo*1-?x z#DUz4u(1~a2-Kg@7=DRZ2Rw|B3-CBY1J2z4H2eaGZaj|37I}O5BR!%M4i*7~B<*Mf z2i;h((bF{SQVZIOJm1w0TE1ws6fa-zjN8gnX4W96`3d1{wgxl324b;5Lf4^VhZiu% z(>$F?7H1iy?HLX=a(zNaUF35ZF&p5Nf>^ARoQdBt|6P1^Y~y3LD>AySnxDax^0sOd z!QO{mkn@^hE`h^MkkzNxzm$$C!B(0p2*_7k zP#gLbn=P+600ue1fwKtE`Y_%A5v@HPNM24G&x|%$qzsz{KB%soAJKv@+QH+rQs9G< pVw$@>8U3LPH#4^-f3J%+)Op|b@q?~{hu9&DImOUXoWGc%{0scVAVL5D diff --git a/__pycache__/settings.cpython-38.pyc b/__pycache__/settings.cpython-38.pyc deleted file mode 100644 index 49fc80c9659b1361272d2f89cecc5a6429b03abb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 189 zcmWIL<>g`kf)CkJNohd(F^GcI`j46y!Oesvk44TZEOqpL8 z85lD60M)&;0V?s+WWL23?&%WZaf{8v)zjT0