Add pulling
This commit is contained in:
parent
a58db98866
commit
c80f919382
15
blocks.py
15
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
|
||||
|
@ -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
|
||||
|
@ -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)]
|
||||
|
22
player.py
22
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user