Add crushing and ids

This commit is contained in:
PAlexanderFranklin 2023-05-17 05:42:21 -07:00
parent 1817c7165d
commit d99f991080
5 changed files with 36 additions and 21 deletions

View File

@ -1,6 +1,7 @@
import pygame
import sys
import random
import uuid
from constants import *
import mapgen
@ -22,8 +23,9 @@ for i in range(tileCountx):
column.append(False)
gameMap.append(column)
players = []
players.append(Player(
players = {}
players["1"] = (Player(
"1",
5,
5,
red,
@ -39,7 +41,8 @@ players.append(Player(
],
gameMap,
))
players.append(Player(
players["2"] = (Player(
"2",
3,
3,
green,
@ -55,7 +58,8 @@ players.append(Player(
],
gameMap,
))
players.append(Player(
players["3"] = (Player(
"3",
5,
5,
red,
@ -96,7 +100,7 @@ while True:
keys = pygame.key.get_pressed()
for player in players:
for id, player in players.items():
player.useKeys(keys)
# Rendering

View File

@ -1,16 +1,18 @@
import pygame
import uuid
from constants import *
class Block:
def __init__(self, x, y, gameMap):
def __init__(self, id, x, y, gameMap):
self.id = id
self.x = x
self.y = y
self.gameMap = gameMap
self.sprite = pygame.Rect((x*tile) + 2, (y*tile) + 2, tile - 2, tile - 2)
def pushed(self, x, y, caller, pusher):
if self.x + x > tileCountx - 1 or self.y + y > tileCounty or self.x + x < 0 or self.y + y < 0:
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 self.gameMap[self.x + x][self.y + y]:
self.gameMap[self.x + x][self.y + y].pushed(x, y, self, pusher)

View File

@ -1,19 +1,24 @@
import pygame
import uuid
from constants import *
from player import *
from blocks import *
class Enemy:
def __init__(self, x, y, gameMap):
def __init__(self, id, x, y, gameMap):
self.id = id
self.x = x
self.y = y
self.gameMap = gameMap
def die(self):
pass
def pushed(self, x, y, caller, pusher):
if self.x + x > tileCountx - 1 or self.y + y > tileCounty or self.x + x < 0 or self.y + y < 0:
raise Exception("Cannot push off edge!")
if self.gameMap[self.x + x][self.y + y]:
self.gameMap[self.x + x][self.y + y].pushed(x, y, self, pusher)
self.gameMap[self.x][self.y] = 0
self.x += x
self.y += y
self.gameMap[self.x][self.y] = self
if isinstance(pusher, Player) and isinstance(caller, 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 isinstance(self.gameMap[self.x + x][self.y + y], Block):
self.die()
return
raise Exception("Not crushing enemy!")

View File

@ -1,4 +1,5 @@
import random
import uuid
from constants import *
from player import *
@ -11,7 +12,7 @@ def generateMap(gameMap, players):
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]] = Block(spot[0], spot[1], gameMap)
gameMap[spot[0]][spot[1]] = Block(uuid.uuid4(), spot[0], spot[1], gameMap)
except Exception as error:
continue
@ -21,12 +22,12 @@ def generateMap(gameMap, players):
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]] = Enemy(spot[0], spot[1], gameMap)
gameMap[spot[0]][spot[1]] = Enemy(uuid.uuid4(), spot[0], spot[1], gameMap)
break
except Exception as error:
continue
for player in players:
for id, player in players.items():
maxTries = 5
for i in range(maxTries):
if i == maxTries - 1:

View File

@ -1,7 +1,10 @@
import uuid
from constants import *
class Player:
def __init__(self, x, y, sprite, keys, gameMap):
def __init__(self, id, x, y, sprite, keys, gameMap):
self.id = id
self.x = x
self.y = y
self.sprite = sprite
@ -37,7 +40,7 @@ class Player:
def move(self, x, y):
try:
if self.x + x > tileCountx - 1 or self.y + y > tileCounty or self.x + x < 0 or self.y + y < 0:
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 self.gameMap[self.x + x][self.y + y]:
self.gameMap[self.x + x][self.y + y].pushed(x, y, self, self)