2023-05-18 07:35:26 +00:00
|
|
|
import pygame
|
2023-05-17 12:42:21 +00:00
|
|
|
import uuid
|
|
|
|
|
2023-05-17 13:05:04 +00:00
|
|
|
from globals import *
|
2023-05-18 05:30:15 +00:00
|
|
|
from blocks import *
|
2023-05-17 12:09:48 +00:00
|
|
|
|
2023-05-18 07:35:26 +00:00
|
|
|
def buildPathMap(x, y):
|
|
|
|
pathMap = []
|
|
|
|
for i in range(tileCountx):
|
|
|
|
column = []
|
|
|
|
for j in range(tileCounty):
|
|
|
|
column.append(False)
|
|
|
|
pathMap.append(column)
|
|
|
|
pathMap[x][y] = (x,y,(0,0))
|
|
|
|
currentTiles = [pathMap[x][y]]
|
|
|
|
newTiles = set()
|
|
|
|
while len(currentTiles) > 0:
|
|
|
|
for tile in currentTiles:
|
|
|
|
for i in range(-1,2):
|
|
|
|
for j in range(-1,2):
|
|
|
|
newX = tile[0] + i
|
|
|
|
newY = tile[1] + j
|
2023-05-18 07:56:24 +00:00
|
|
|
if (i != 0 or j != 0) and not checkOOBounds((newX,newY)) and not pathMap[newX][newY]:
|
2023-05-18 07:35:26 +00:00
|
|
|
pathMap[newX][newY] = (newX,newY,(-i,-j))
|
|
|
|
if not gameMap[newX][newY]:
|
|
|
|
newTiles.add(pathMap[newX][newY])
|
|
|
|
currentTiles = list(newTiles)
|
|
|
|
newTiles = set()
|
|
|
|
return pathMap
|
|
|
|
|
2023-05-17 11:04:10 +00:00
|
|
|
class Player:
|
2023-05-17 13:05:04 +00:00
|
|
|
def __init__(self, id, x, y, sprite, keys):
|
2023-05-17 12:42:21 +00:00
|
|
|
self.id = id
|
2023-05-17 11:04:10 +00:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
2023-05-18 07:35:26 +00:00
|
|
|
self.renderPos = (x,y)
|
|
|
|
self.pathMap = []
|
2023-05-17 11:04:10 +00:00
|
|
|
self.sprite = sprite
|
2023-05-18 04:34:16 +00:00
|
|
|
self.pull = False
|
2023-05-18 05:30:15 +00:00
|
|
|
self.PLAYER = True
|
|
|
|
self.kills = 0
|
2023-05-18 05:45:11 +00:00
|
|
|
self.lives = 3
|
2023-05-17 13:05:04 +00:00
|
|
|
|
2023-05-17 11:04:10 +00:00
|
|
|
commands = {
|
|
|
|
"n": lambda: self.move(0,-1),
|
|
|
|
"ne": lambda: self.move(1,-1),
|
|
|
|
"e": lambda: self.move(1,0),
|
|
|
|
"se": lambda: self.move(1,1),
|
|
|
|
"s": lambda: self.move(0,1),
|
|
|
|
"sw": lambda: self.move(-1,1),
|
|
|
|
"w": lambda: self.move(-1,0),
|
|
|
|
"nw": lambda: self.move(-1,-1),
|
|
|
|
}
|
|
|
|
self.commands = []
|
|
|
|
for key in keys:
|
2023-05-18 04:34:16 +00:00
|
|
|
if key[1] == "p":
|
|
|
|
self.pullKey = key[0]
|
|
|
|
else:
|
|
|
|
self.commands.append([key[0], commands[key[1]], 0, 0])
|
2023-05-18 07:35:26 +00:00
|
|
|
|
|
|
|
def buildOwnPathMap(self):
|
|
|
|
self.pathMap = buildPathMap(self.x, self.y)
|
2023-05-17 11:04:10 +00:00
|
|
|
|
|
|
|
def useKeys(self, keys):
|
|
|
|
try:
|
2023-05-18 04:34:16 +00:00
|
|
|
if keys[self.pullKey]:
|
|
|
|
self.pull = True
|
|
|
|
else:
|
|
|
|
self.pull = False
|
2023-05-17 11:04:10 +00:00
|
|
|
for command in self.commands:
|
2023-05-18 07:35:26 +00:00
|
|
|
try:
|
|
|
|
if keys[command[0]]:
|
|
|
|
if command[2] == 0 or (command[2] > 10 and command[3] > 2):
|
|
|
|
command[1]()
|
|
|
|
command[3] = 0
|
|
|
|
command[2] += 1
|
|
|
|
command[3] += 1
|
|
|
|
else:
|
|
|
|
command[2] = 0
|
2023-05-17 11:04:10 +00:00
|
|
|
command[3] = 0
|
2023-05-18 07:35:26 +00:00
|
|
|
except:
|
2023-05-17 11:04:10 +00:00
|
|
|
command[2] = 0
|
|
|
|
command[3] = 0
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2023-05-17 11:40:57 +00:00
|
|
|
def move(self, x, y):
|
2023-05-17 11:04:10 +00:00
|
|
|
try:
|
2023-05-18 08:03:23 +00:00
|
|
|
if checkOOBounds((self.x + x, self.y + y)):
|
2023-05-18 03:39:12 +00:00
|
|
|
raise Exception("Cannot move off edge!")
|
2023-05-18 05:30:15 +00:00
|
|
|
pulling = self.pull and hasattr(gameMap[self.x - x][self.y - y], "BLOCK")
|
2023-05-17 13:05:04 +00:00
|
|
|
if gameMap[self.x + x][self.y + y]:
|
2023-05-18 04:34:16 +00:00
|
|
|
if pulling:
|
|
|
|
raise Exception("Cannot push and pull at the same time")
|
2023-05-17 13:05:04 +00:00
|
|
|
gameMap[self.x + x][self.y + y].pushed(x, y, self, self)
|
2023-05-18 04:34:16 +00:00
|
|
|
gameMap[self.x][self.y] = False
|
2023-05-17 11:04:10 +00:00
|
|
|
self.x += x
|
|
|
|
self.y += y
|
2023-05-17 13:05:04 +00:00
|
|
|
gameMap[self.x][self.y] = self
|
2023-05-18 07:35:26 +00:00
|
|
|
for id, player in players.items():
|
|
|
|
try:
|
|
|
|
player.buildOwnPathMap()
|
|
|
|
except Exception as error:
|
|
|
|
print(error)
|
2023-05-18 04:34:16 +00:00
|
|
|
try:
|
|
|
|
if pulling:
|
|
|
|
gameMap[self.x - 2*x][self.y - 2*y].pulled(x, y)
|
|
|
|
except:
|
|
|
|
pass
|
2023-05-17 11:04:10 +00:00
|
|
|
except Exception as error:
|
2023-05-17 11:40:57 +00:00
|
|
|
raise error
|
|
|
|
|
|
|
|
def pushed(self, x, y, caller, pusher):
|
2023-05-18 03:39:12 +00:00
|
|
|
raise Exception("Cannot push other players.")
|
|
|
|
|
|
|
|
def die(self):
|
2023-05-18 05:45:11 +00:00
|
|
|
maxTries = 5
|
|
|
|
for i in range(maxTries + 1):
|
|
|
|
if self.lives < 1:
|
|
|
|
del players[self.id]
|
|
|
|
break
|
|
|
|
if i == maxTries:
|
|
|
|
raise Exception("No spots found for player!")
|
|
|
|
try:
|
|
|
|
spot = [random.randint(0, tileCountx - 1), random.randint(0, tileCounty - 1)]
|
|
|
|
if gameMap[spot[0]][spot[1]]:
|
|
|
|
raise Exception("spot taken!")
|
|
|
|
gameMap[spot[0]][spot[1]] = self
|
|
|
|
self.x = spot[0]
|
|
|
|
self.y = spot[1]
|
|
|
|
self.lives -= 1
|
|
|
|
break
|
|
|
|
except Exception as error:
|
|
|
|
continue
|