From c80f919382e35eebc02c1518200a8da5c304f93d Mon Sep 17 00:00:00 2001 From: PAlexanderFranklin Date: Wed, 17 May 2023 21:34:16 -0700 Subject: [PATCH] Add pulling --- blocks.py | 15 ++++++++++++++- enemies.py | 6 +++--- mapgen.py | 4 ++-- player.py | 22 +++++++++++++++++++--- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/blocks.py b/blocks.py index aa2235d..da942c0 100644 --- a/blocks.py +++ b/blocks.py @@ -2,6 +2,7 @@ import pygame import uuid from globals import * +from enemies import Enemy class Block: def __init__(self, id, x, y): @@ -16,7 +17,19 @@ class Block: raise Exception("Cannot push off edge!") if gameMap[self.x + x][self.y + y]: gameMap[self.x + x][self.y + y].pushed(x, y, self, pusher) - gameMap[self.x][self.y] = 0 + gameMap[self.x][self.y] = False + self.x += x + self.sprite.x = (self.x*tile)+2 + self.y += y + self.sprite.y = (self.y*tile)+2 + gameMap[self.x][self.y] = self + + def pulled(self, x, y): + for i in range(-1,2): + for j in range(-1,2): + if isinstance(gameMap[self.x + i][self.y + j], Enemy): + raise Exception("Cannot pull a block that is touching an enemy") + gameMap[self.x][self.y] = False self.x += x self.sprite.x = (self.x*tile)+2 self.y += y diff --git a/enemies.py b/enemies.py index 9b3008b..14fd6aa 100644 --- a/enemies.py +++ b/enemies.py @@ -3,8 +3,8 @@ import uuid import random from globals import * -from player import * -from blocks import * +from player import Player +from blocks import Block def findClosestPlayer(x, y): closestPlayerPosition = {"difference": (1,1), "distance": 9999} @@ -44,7 +44,7 @@ class Enemy: gameMap[self.x + x][self.y + y].die() else: raise Exception("Path is blocked.") - gameMap[self.x][self.y] = 0 + gameMap[self.x][self.y] = False self.x += x self.y += y gameMap[self.x][self.y] = self diff --git a/mapgen.py b/mapgen.py index 0e11809..24ad3d6 100644 --- a/mapgen.py +++ b/mapgen.py @@ -37,8 +37,8 @@ def generateMap(): for id, player in players.items(): maxTries = 5 - for i in range(maxTries): - if i == maxTries - 1: + for i in range(maxTries + 1): + if i == maxTries: raise Exception("No spots found for player!") try: spot = [random.randint(0, tileCountx - 1), random.randint(0, tileCounty - 1)] diff --git a/player.py b/player.py index fe7e7f7..7eb26ff 100644 --- a/player.py +++ b/player.py @@ -1,6 +1,7 @@ import uuid from globals import * +from blocks import Block class Player: def __init__(self, id, x, y, sprite, keys): @@ -8,6 +9,7 @@ class Player: self.x = x self.y = y self.sprite = sprite + self.pull = False commands = { "n": lambda: self.move(0,-1), @@ -18,14 +20,20 @@ class Player: "sw": lambda: self.move(-1,1), "w": lambda: self.move(-1,0), "nw": lambda: self.move(-1,-1), - "p": lambda: print("pull"), } self.commands = [] for key in keys: - self.commands.append([key[0], commands[key[1]], 0, 0]) + if key[1] == "p": + self.pullKey = key[0] + else: + self.commands.append([key[0], commands[key[1]], 0, 0]) def useKeys(self, keys): try: + if keys[self.pullKey]: + self.pull = True + else: + self.pull = False for command in self.commands: if keys[command[0]]: if command[2] == 0 or (command[2] > 10 and command[3] > 2): @@ -43,12 +51,20 @@ class Player: try: if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0: raise Exception("Cannot move off edge!") + pulling = self.pull and isinstance(gameMap[self.x - x][self.y - y], Block) if gameMap[self.x + x][self.y + y]: + if pulling: + raise Exception("Cannot push and pull at the same time") gameMap[self.x + x][self.y + y].pushed(x, y, self, self) - gameMap[self.x][self.y] = 0 + gameMap[self.x][self.y] = False self.x += x self.y += y gameMap[self.x][self.y] = self + try: + if pulling: + gameMap[self.x - 2*x][self.y - 2*y].pulled(x, y) + except: + pass except Exception as error: raise error