Switch lists to global, add enemies list

This commit is contained in:
PAlexanderFranklin 2023-05-17 06:05:04 -07:00
parent d99f991080
commit 693866c443
6 changed files with 47 additions and 40 deletions

View File

@ -3,7 +3,7 @@ import sys
import random
import uuid
from constants import *
from globals import *
import mapgen
from player import *
from blocks import *
@ -15,15 +15,6 @@ clock = pygame.time.Clock()
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('Arctic Masher')
gameMap = []
for i in range(tileCountx):
column = []
for j in range(tileCounty):
column.append(False)
gameMap.append(column)
players = {}
players["1"] = (Player(
"1",
5,
@ -38,8 +29,8 @@ players["1"] = (Player(
(pygame.K_z, "sw"),
(pygame.K_a, "w"),
(pygame.K_q, "nw"),
(pygame.K_s, "p"),
],
gameMap,
))
players["2"] = (Player(
"2",
@ -55,8 +46,8 @@ players["2"] = (Player(
(pygame.K_KP_1, "sw"),
(pygame.K_KP_4, "w"),
(pygame.K_KP_7, "nw"),
(pygame.K_KP_5, "p"),
],
gameMap,
))
players["3"] = (Player(
"3",
@ -72,11 +63,11 @@ players["3"] = (Player(
(pygame.K_b, "sw"),
(pygame.K_g, "w"),
(pygame.K_t, "nw"),
(pygame.K_h, "p"),
],
gameMap,
))
mapgen.generateMap(gameMap, players)
mapgen.generateMap()
sprite_sheet_image = pygame.image.load('assets/penguin.png').convert_alpha()

View File

@ -1,24 +1,24 @@
import pygame
import uuid
from constants import *
from globals import *
class Block:
def __init__(self, id, x, y, gameMap):
def __init__(self, id, x, y):
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 - 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)
self.gameMap[self.x][self.y] = 0
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
self.x += x
self.sprite.x = (self.x*tile)+2
self.y += y
self.sprite.y = (self.y*tile)+2
self.gameMap[self.x][self.y] = self
gameMap[self.x][self.y] = self

View File

@ -1,24 +1,27 @@
import pygame
import uuid
from constants import *
from globals import *
from player import *
from blocks import *
class Enemy:
def __init__(self, id, x, y, gameMap):
def __init__(self, id, x, y):
self.id = id
self.x = x
self.y = y
self.gameMap = gameMap
def die(self):
pass
del enemies[self.id]
def pushed(self, x, y, caller, pusher):
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):
if wallCrush or isinstance(gameMap[self.x + x][self.y + y], Block):
self.die()
return
raise Exception("Not crushing enemy!")
raise Exception("Not crushing enemy!")
def move(self, x, y):
pass

View File

@ -11,4 +11,8 @@ tileCountx = 50
tileCounty = 25
screen_width = tile * tileCountx
screen_height = tile * tileCounty
screen_height = tile * tileCounty
gameMap = []
players = {}
enemies = {}

View File

@ -1,28 +1,36 @@
import random
import uuid
from constants import *
from globals import *
from player import *
from blocks import *
from enemies import *
def generateMap(gameMap, players):
def generateMap():
gameMap.clear()
for i in range(tileCountx):
column = []
for j in range(tileCounty):
column.append(False)
gameMap.append(column)
for i in range(300):
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]] = Block(uuid.uuid4(), spot[0], spot[1], gameMap)
gameMap[spot[0]][spot[1]] = Block(uuid.uuid4(), spot[0], spot[1])
except Exception as error:
continue
for i in range(50):
for i in range(3):
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!")
gameMap[spot[0]][spot[1]] = Enemy(uuid.uuid4(), spot[0], spot[1], gameMap)
newEnemy = Enemy(uuid.uuid4(), spot[0], spot[1])
gameMap[spot[0]][spot[1]] = newEnemy
enemies[newEnemy.id] = newEnemy
break
except Exception as error:
continue

View File

@ -1,14 +1,14 @@
import uuid
from constants import *
from globals import *
class Player:
def __init__(self, id, x, y, sprite, keys, gameMap):
def __init__(self, id, x, y, sprite, keys):
self.id = id
self.x = x
self.y = y
self.sprite = sprite
self.gameMap = gameMap
commands = {
"n": lambda: self.move(0,-1),
"ne": lambda: self.move(1,-1),
@ -18,6 +18,7 @@ class Player:
"sw": lambda: self.move(-1,1),
"w": lambda: self.move(-1,0),
"nw": lambda: self.move(-1,-1),
"p": lambda: print(enemies),
}
self.commands = []
for key in keys:
@ -42,12 +43,12 @@ 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 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)
self.gameMap[self.x][self.y] = 0
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
self.x += x
self.y += y
self.gameMap[self.x][self.y] = self
gameMap[self.x][self.y] = self
except Exception as error:
raise error