Compare commits
No commits in common. "7251a271835ae9bc46c37156bb3707de26c4c7d6" and "5786ac0d320e40189a0b604910a370e803f23e9f" have entirely different histories.
7251a27183
...
5786ac0d32
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1 @@
|
||||
__pycache__
|
||||
venv
|
||||
__pycache__
|
@ -17,6 +17,11 @@ def main():
|
||||
pygame.init()
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
screen = pygame.display.set_mode((screen_width,screen_height))
|
||||
pygame.display.set_caption('Arctic Masher')
|
||||
|
||||
bottomBar = pygame.Rect(0, screen_height - bottomBarHeight, screen_width, bottomBarHeight)
|
||||
|
||||
players["1"] = (Player(
|
||||
"1",
|
||||
5,
|
||||
@ -48,7 +53,7 @@ def main():
|
||||
(pygame.K_KP_1, "sw"),
|
||||
(pygame.K_KP_4, "w"),
|
||||
(pygame.K_KP_7, "nw"),
|
||||
(pygame.K_RSHIFT, "p"),
|
||||
(pygame.K_KP_0, "p"),
|
||||
],
|
||||
))
|
||||
# players["3"] = (Player(
|
||||
@ -69,12 +74,6 @@ def main():
|
||||
# ],
|
||||
# ))
|
||||
|
||||
|
||||
gameWindow = pygame.display.set_mode((screen_width,screen_height))
|
||||
pygame.display.set_caption('Arctic Masher')
|
||||
|
||||
bottomBar = pygame.Rect(0, screen_height - bottomBarHeight, screen_width, bottomBarHeight)
|
||||
|
||||
mapgen.generateMap()
|
||||
|
||||
sprite_sheet_image = pygame.image.load('assets/penguin.png').convert_alpha()
|
||||
@ -87,7 +86,6 @@ def main():
|
||||
|
||||
return image
|
||||
frame_0 = get_image(sprite_sheet_image, 0, 0, 32, 32, (tile-2)/32, black)
|
||||
frame_1 = get_image(sprite_sheet_image, 0, 32, 32, 32, (tile-2)/32, black)
|
||||
frame_4 = get_image(sprite_sheet_image, 32, 0, 32, 32, (tile-2)/32, black)
|
||||
|
||||
game_font = pygame.font.Font("freesansbold.ttf",32)
|
||||
@ -107,24 +105,23 @@ def main():
|
||||
enemy.runAI()
|
||||
|
||||
# Rendering
|
||||
gameWindow.fill(bg_color)
|
||||
pygame.draw.rect(gameWindow, bar_color, bottomBar)
|
||||
screen.fill(bg_color)
|
||||
pygame.draw.rect(screen, bar_color, bottomBar)
|
||||
for column in gameMap:
|
||||
for spot in column:
|
||||
if not spot:
|
||||
continue
|
||||
elif isinstance(spot, Block):
|
||||
pygame.draw.rect(gameWindow, spot.color, spot.sprite)
|
||||
pygame.draw.rect(screen, spot.color, spot.sprite)
|
||||
elif isinstance(spot, Enemy):
|
||||
gameWindow.blit(spot.sprite, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
||||
elif isinstance(spot, Player):
|
||||
gameWindow.blit(frame_0, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
||||
scoreSurface = game_font.render(f'player {spot.id} kills: {spot.kills} lives: {spot.lives}', False, black)
|
||||
gameWindow.blit(scoreSurface, (500*int(spot.id) - 400, screen_height - bottomBarHeight + 20))
|
||||
try:
|
||||
screen.blit(frame_4, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
||||
spot.renderPos = ((spot.x - spot.renderPos[0])/3 + spot.renderPos[0], (spot.y - spot.renderPos[1])/3 + spot.renderPos[1])
|
||||
except:
|
||||
pass
|
||||
elif isinstance(spot, Player):
|
||||
screen.blit(frame_0, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
||||
spot.renderPos = ((spot.x - spot.renderPos[0])/3 + spot.renderPos[0], (spot.y - spot.renderPos[1])/3 + spot.renderPos[1])
|
||||
scoreSurface = game_font.render(f'player {spot.id} kills: {spot.kills} lives: {spot.lives}', False, black)
|
||||
screen.blit(scoreSurface, (500*int(spot.id) - 400, screen_height - bottomBarHeight + 20))
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
|
@ -2,7 +2,6 @@ import pygame
|
||||
import uuid
|
||||
|
||||
from globals import *
|
||||
from utilities import *
|
||||
from enemies import *
|
||||
|
||||
class Block:
|
||||
@ -16,7 +15,7 @@ class Block:
|
||||
self.sprite = pygame.Rect((x*tile) + 2, (y*tile) + 2, tile - 2, tile - 2)
|
||||
|
||||
def pushed(self, x, y, caller, pusher):
|
||||
if checkOOBounds((self.x + x, self.y + y)):
|
||||
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 push off edge!")
|
||||
if gameMap[self.x + x][self.y + y]:
|
||||
gameMap[self.x + x][self.y + y].pushed(x, y, self, pusher)
|
||||
@ -30,8 +29,6 @@ class Block:
|
||||
def pulled(self, x, y):
|
||||
for i in range(-1,2):
|
||||
for j in range(-1,2):
|
||||
if checkOOBounds((self.x + i, self.y + j)):
|
||||
continue
|
||||
if hasattr(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
|
||||
|
126
enemies.py
126
enemies.py
@ -3,7 +3,6 @@ import uuid
|
||||
import random
|
||||
|
||||
from globals import *
|
||||
from utilities import *
|
||||
from player import *
|
||||
from blocks import *
|
||||
|
||||
@ -14,28 +13,9 @@ def findClosestPlayer(x, y):
|
||||
distance = max(abs(difference[0]), abs(difference[1]))
|
||||
if distance > closestPlayerPosition["distance"]:
|
||||
continue
|
||||
closestPlayerPosition = {"difference": difference, "distance": distance, "player": player}
|
||||
closestPlayerPosition = {"difference": difference, "distance": distance}
|
||||
return closestPlayerPosition
|
||||
|
||||
def randomizeMovement(weightsx, weightsy, diff, wrongChance):
|
||||
weightsx = [-diff[0], abs(diff[1])+0.1, diff[0]]
|
||||
if weightsx[0] < 0:
|
||||
weightsx[0] = weightsx[2]*wrongChance
|
||||
elif weightsx[2] < 0:
|
||||
weightsx[2] = weightsx[0]*wrongChance
|
||||
else:
|
||||
weightsx[0] = max(weightsx[1]/(15.1-weightsx[1]*0.5), 1)
|
||||
weightsx[2] = weightsx[0]
|
||||
weightsy = [-diff[1], abs(diff[0])+0.1, diff[1]]
|
||||
if weightsy[0] < 0:
|
||||
weightsy[0] = weightsy[2]*wrongChance
|
||||
elif weightsy[2] < 0:
|
||||
weightsy[2] = weightsy[0]*wrongChance
|
||||
else:
|
||||
weightsy[0] = max(weightsy[1]/(15.1-weightsy[1]*0.5), 1)
|
||||
weightsy[2] = weightsy[0]
|
||||
return (weightsx, weightsy)
|
||||
|
||||
class Enemy:
|
||||
def __init__(self, id, x, y):
|
||||
self.id = id
|
||||
@ -43,27 +23,25 @@ class Enemy:
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.renderPos = (x,y)
|
||||
# self.AITime = random.randint(350, 600)
|
||||
self.AITime = random.randint(10, 50)
|
||||
self.deathColor = white
|
||||
self.sprite = pygame.Surface((32,32), pygame.SRCALPHA)
|
||||
pygame.draw.circle(self.sprite, white, (16,16), 10)
|
||||
self.AITime = random.randint(350, 600)
|
||||
|
||||
|
||||
def die(self):
|
||||
del enemies[self.id]
|
||||
|
||||
def pushed(self, x, y, caller, pusher):
|
||||
if hasattr(pusher, "PLAYER") and hasattr(caller, "BLOCK"):
|
||||
if checkOOBounds((self.x + x, self.y + y)) or hasattr(gameMap[self.x + x][self.y + y], "BLOCK"):
|
||||
wallCrush = self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0
|
||||
if wallCrush or hasattr(gameMap[self.x + x][self.y + y], "BLOCK"):
|
||||
pusher.kills += 1
|
||||
caller.color = self.deathColor
|
||||
caller.color = random.choice([grey, green])
|
||||
self.die()
|
||||
return
|
||||
raise Exception("Not crushing enemy!")
|
||||
|
||||
def move(self, x, y):
|
||||
try:
|
||||
if checkOOBounds((self.x + x, self.y + y)):
|
||||
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!")
|
||||
if gameMap[self.x + x][self.y + y]:
|
||||
if hasattr(gameMap[self.x + x][self.y + y], "PLAYER"):
|
||||
@ -81,82 +59,26 @@ class Enemy:
|
||||
self.AITime -= 1
|
||||
if self.AITime < 1:
|
||||
try:
|
||||
diff = findClosestPlayer(self.x, self.y)["difference"]
|
||||
weightsx = [1,1,1]
|
||||
weightsy = [1,1,1]
|
||||
randomWeights = randomizeMovement(weightsx, weightsy, diff, 0.15)
|
||||
weightsx = randomWeights[0]
|
||||
weightsy = randomWeights[1]
|
||||
closestPlayer = findClosestPlayer(self.x, self.y)
|
||||
weightsx = [-closestPlayer["difference"][0], abs(closestPlayer["difference"][1]), closestPlayer["difference"][0]]
|
||||
if weightsx[0] < 0:
|
||||
weightsx[0] = weightsx[2]/6
|
||||
elif weightsx[2] < 0:
|
||||
weightsx[2] = weightsx[0]/6
|
||||
else:
|
||||
weightsx[0] = weightsx[1]/8
|
||||
weightsx[2] = weightsx[1]/8
|
||||
weightsy = [-closestPlayer["difference"][1], abs(closestPlayer["difference"][0]), closestPlayer["difference"][1]]
|
||||
if weightsy[0] < 0:
|
||||
weightsy[0] = weightsy[2]/6
|
||||
elif weightsy[2] < 0:
|
||||
weightsy[2] = weightsy[0]/6
|
||||
else:
|
||||
weightsy[0] = weightsy[1]/8
|
||||
weightsy[2] = weightsy[1]/8
|
||||
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
||||
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
||||
self.move(movementx, movementy)
|
||||
self.AITime = random.randint(150, 200)
|
||||
except Exception as error:
|
||||
pass
|
||||
|
||||
class Smart(Enemy):
|
||||
def __init__(self, id, x, y):
|
||||
Enemy.__init__(self, id, x, y)
|
||||
self.SMART = True
|
||||
self.usesPathMap = True
|
||||
self.deathColor = red
|
||||
self.trackedPlayer = False
|
||||
self.trackingCounter = 0
|
||||
self.sprite = pygame.Surface((32,32), pygame.SRCALPHA)
|
||||
pygame.draw.circle(self.sprite, red, (16,16), 12)
|
||||
|
||||
def runAI(self):
|
||||
self.AITime -= 1
|
||||
if self.AITime < 1:
|
||||
try:
|
||||
if self.trackingCounter < 1:
|
||||
self.AITime = random.randint(150, 250)
|
||||
self.trackingCounter = 9
|
||||
self.trackedPlayer = False
|
||||
elif self.trackedPlayer and self.trackedPlayer["player"].alive:
|
||||
pathMap = self.trackedPlayer["player"].pathMap
|
||||
weightsx = [1,1,1]
|
||||
weightsy = [1,1,1]
|
||||
if pathMap[self.x][self.y]:
|
||||
fastDir = pathMap[self.x][self.y][2]
|
||||
weightsx[1 + fastDir[0]] = 30
|
||||
weightsy[1 + fastDir[1]] = 30
|
||||
else:
|
||||
randomWeights = randomizeMovement(weightsx, weightsy, self.trackedPlayer["difference"], 0.32)
|
||||
weightsx = randomWeights[0]
|
||||
weightsy = randomWeights[1]
|
||||
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
||||
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
||||
self.move(movementx, movementy)
|
||||
self.trackingCounter -= 1
|
||||
self.AITime = random.randint(10, 15)
|
||||
else:
|
||||
closestPlayer = findClosestPlayer(self.x, self.y)
|
||||
self.trackedPlayer = closestPlayer
|
||||
self.AITime = random.randint(10, 15)
|
||||
except Exception as error:
|
||||
pass
|
||||
|
||||
class Mage(Enemy):
|
||||
def __init__(self, id, x, y):
|
||||
Enemy.__init__(self, id, x, y)
|
||||
self.deathColor = purple
|
||||
self.sprite = pygame.Surface((32,32), pygame.SRCALPHA)
|
||||
pygame.draw.circle(self.sprite, purple, (16,16), 6)
|
||||
|
||||
def runAI(self):
|
||||
self.AITime -= 1
|
||||
if self.AITime < 1:
|
||||
try:
|
||||
diff = findClosestPlayer(self.x, self.y)["difference"]
|
||||
weightsx = [1,1,1]
|
||||
weightsy = [1,1,1]
|
||||
randomWeights = randomizeMovement(weightsx, weightsy, diff, 0.15)
|
||||
weightsx = randomWeights[0]
|
||||
weightsy = randomWeights[1]
|
||||
movementx = random.choices([-1, 0, 1], weightsx)[0] * random.randint(1,2)
|
||||
movementy = random.choices([-1, 0, 1], weightsy)[0] * random.randint(1,2)
|
||||
self.move(movementx, movementy)
|
||||
self.AITime = random.randint(150, 200)
|
||||
except Exception as error:
|
||||
pass
|
@ -1,14 +1,12 @@
|
||||
import pygame
|
||||
|
||||
bg_color = pygame.Color('lightsalmon3')
|
||||
bg_color = pygame.Color('cornsilk4')
|
||||
bar_color = pygame.Color('white')
|
||||
red = pygame.Color('brown4')
|
||||
blue = pygame.Color('cadetblue3')
|
||||
green = pygame.Color('green3')
|
||||
grey = pygame.Color("gray26")
|
||||
black = pygame.Color("black")
|
||||
white = pygame.Color("white")
|
||||
purple = pygame.Color("purple")
|
||||
|
||||
tile = 34
|
||||
tileCountx = 50
|
||||
|
33
mapgen.py
33
mapgen.py
@ -6,20 +6,6 @@ from player import *
|
||||
from blocks import *
|
||||
from enemies import *
|
||||
|
||||
def spawnEnemy(type, amount):
|
||||
for i in range(amount):
|
||||
for j in range(50):
|
||||
try:
|
||||
spot = [random.randint(0, tileCountx - 1), random.randint(0, tileCounty - 1)]
|
||||
if gameMap[spot[0]][spot[1]]:
|
||||
raise Exception("spot taken!")
|
||||
newEnemy = type(uuid.uuid4(), spot[0], spot[1])
|
||||
gameMap[spot[0]][spot[1]] = newEnemy
|
||||
enemies[newEnemy.id] = newEnemy
|
||||
break
|
||||
except Exception as error:
|
||||
continue
|
||||
|
||||
def generateMap():
|
||||
gameMap.clear()
|
||||
for i in range(tileCountx):
|
||||
@ -35,10 +21,19 @@ def generateMap():
|
||||
gameMap[spot[0]][spot[1]] = Block(uuid.uuid4(), spot[0], spot[1])
|
||||
except Exception as error:
|
||||
continue
|
||||
|
||||
spawnEnemy(Enemy, 20)
|
||||
spawnEnemy(Smart, 3)
|
||||
spawnEnemy(Mage, 3)
|
||||
|
||||
for i in range(60):
|
||||
for j in range(50):
|
||||
try:
|
||||
spot = [random.randint(0, tileCountx - 1), random.randint(0, tileCounty - 1)]
|
||||
if gameMap[spot[0]][spot[1]]:
|
||||
raise Exception("spot taken!")
|
||||
newEnemy = Enemy(uuid.uuid4(), spot[0], spot[1])
|
||||
gameMap[spot[0]][spot[1]] = newEnemy
|
||||
enemies[newEnemy.id] = newEnemy
|
||||
break
|
||||
except Exception as error:
|
||||
continue
|
||||
|
||||
for id, player in players.items():
|
||||
maxTries = 5
|
||||
@ -52,7 +47,7 @@ def generateMap():
|
||||
gameMap[spot[0]][spot[1]] = player
|
||||
player.x = spot[0]
|
||||
player.y = spot[1]
|
||||
player.buildOwnPathMap()
|
||||
player.pathMap = player.buildOwnPathMap()
|
||||
break
|
||||
except Exception as error:
|
||||
continue
|
69
player.py
69
player.py
@ -5,34 +5,29 @@ from globals import *
|
||||
from blocks import *
|
||||
|
||||
def buildPathMap(x, y):
|
||||
try:
|
||||
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]]
|
||||
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
|
||||
outOfBounds = newX > tileCountx - 1 or newY > tileCounty - 1 or newX < 0 or newY < 0
|
||||
if (i != 0 or j != 0) and not outOfBounds and not pathMap[newX][newY]:
|
||||
pathMap[newX][newY] = (newX,newY,(-i,-j))
|
||||
if not gameMap[newX][newY]:
|
||||
newTiles.add(pathMap[newX][newY])
|
||||
currentTiles = list(newTiles)
|
||||
newTiles = set()
|
||||
while True:
|
||||
for tile in currentTiles:
|
||||
for i in range(-1,2):
|
||||
for j in range(-1,2):
|
||||
newX = tile[0] + i
|
||||
newY = tile[1] + j
|
||||
if (i != 0 or j != 0) and not checkOOBounds((newX,newY)) and not pathMap[newX][newY]:
|
||||
pathMap[newX][newY] = (newX,newY,(-i,-j))
|
||||
if not gameMap[newX][newY] or hasattr(gameMap[newX][newY], "usesPathMap"):
|
||||
newTiles.add(pathMap[newX][newY])
|
||||
if len(currentTiles) < 1:
|
||||
break
|
||||
currentTiles = list(newTiles)
|
||||
newTiles = set()
|
||||
return pathMap
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise error
|
||||
return pathMap
|
||||
|
||||
class Player:
|
||||
def __init__(self, id, x, y, sprite, keys):
|
||||
@ -46,7 +41,6 @@ class Player:
|
||||
self.PLAYER = True
|
||||
self.kills = 0
|
||||
self.lives = 3
|
||||
self.alive = True
|
||||
|
||||
commands = {
|
||||
"n": lambda: self.move(0,-1),
|
||||
@ -74,8 +68,6 @@ class Player:
|
||||
self.pull = True
|
||||
else:
|
||||
self.pull = False
|
||||
if keys[pygame.K_o]:
|
||||
print(self.pathMap)
|
||||
for command in self.commands:
|
||||
try:
|
||||
if keys[command[0]]:
|
||||
@ -95,7 +87,7 @@ class Player:
|
||||
|
||||
def move(self, x, y):
|
||||
try:
|
||||
if checkOOBounds((self.x + x, self.y + y)):
|
||||
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 hasattr(gameMap[self.x - x][self.y - y], "BLOCK")
|
||||
if gameMap[self.x + x][self.y + y]:
|
||||
@ -106,16 +98,16 @@ class Player:
|
||||
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
|
||||
for id, player in players.items():
|
||||
try:
|
||||
player.buildOwnPathMap()
|
||||
except Exception as error:
|
||||
print(error)
|
||||
try:
|
||||
if pulling:
|
||||
gameMap[self.x - 2*x][self.y - 2*y].pulled(x, y)
|
||||
except:
|
||||
pass
|
||||
except Exception as error:
|
||||
raise error
|
||||
|
||||
@ -127,7 +119,6 @@ class Player:
|
||||
for i in range(maxTries + 1):
|
||||
if self.lives < 1:
|
||||
del players[self.id]
|
||||
self.alive = False
|
||||
break
|
||||
if i == maxTries:
|
||||
raise Exception("No spots found for player!")
|
||||
@ -139,10 +130,6 @@ class Player:
|
||||
self.x = spot[0]
|
||||
self.y = spot[1]
|
||||
self.lives -= 1
|
||||
try:
|
||||
self.buildOwnPathMap()
|
||||
except Exception as error:
|
||||
print(error)
|
||||
break
|
||||
except Exception as error:
|
||||
continue
|
@ -1,2 +0,0 @@
|
||||
profilehooks==1.12.0
|
||||
pygame==2.5.2
|
@ -1,4 +0,0 @@
|
||||
from globals import *
|
||||
|
||||
def checkOOBounds(pos):
|
||||
return pos[0] > tileCountx - 1 or pos[1] > tileCounty - 1 or pos[0] < 0 or pos[1] < 0
|
Loading…
x
Reference in New Issue
Block a user