2023-05-17 12:42:21 +00:00
|
|
|
import uuid
|
|
|
|
|
2023-05-17 13:05:04 +00:00
|
|
|
from globals import *
|
2023-05-17 12:09:48 +00:00
|
|
|
|
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
|
|
|
|
self.sprite = sprite
|
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),
|
2023-05-18 03:39:12 +00:00
|
|
|
"p": lambda: print("pull"),
|
2023-05-17 11:04:10 +00:00
|
|
|
}
|
|
|
|
self.commands = []
|
|
|
|
for key in keys:
|
|
|
|
self.commands.append([key[0], commands[key[1]], 0, 0])
|
|
|
|
|
|
|
|
def useKeys(self, keys):
|
|
|
|
try:
|
|
|
|
for command in self.commands:
|
|
|
|
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
|
|
|
|
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-17 12:42:21 +00:00
|
|
|
if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0:
|
2023-05-18 03:39:12 +00:00
|
|
|
raise Exception("Cannot move off edge!")
|
2023-05-17 13:05:04 +00:00
|
|
|
if gameMap[self.x + x][self.y + y]:
|
|
|
|
gameMap[self.x + x][self.y + y].pushed(x, y, self, self)
|
|
|
|
gameMap[self.x][self.y] = 0
|
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-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):
|
|
|
|
del players[self.id]
|