Refactor mapgen, add border

This commit is contained in:
PAlexanderFranklin 2023-05-17 05:09:48 -07:00
parent b679393534
commit 1817c7165d
6 changed files with 54 additions and 34 deletions

View File

@ -3,6 +3,7 @@ import sys
import random
from constants import *
import mapgen
from player import *
from blocks import *
from enemies import *
@ -71,39 +72,7 @@ players.append(Player(
gameMap,
))
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(spot[0], spot[1], gameMap)
except Exception as error:
print(error)
for i in range(15):
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(spot[0], spot[1], gameMap)
except Exception as error:
print(error)
for player in players:
for i 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]] = player
player.x = spot[0]
player.y = spot[1]
break
except Exception as error:
print(error)
if i > 45:
pygame.quit()
sys.exit()
mapgen.generateMap(gameMap, players)
sprite_sheet_image = pygame.image.load('assets/penguin.png').convert_alpha()

View File

@ -10,6 +10,8 @@ class Block:
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:
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

View File

@ -8,7 +8,7 @@ black = pygame.Color("black")
tile = 34
tileCountx = 50
tileCounty = 30
tileCounty = 25
screen_width = tile * tileCountx
screen_height = tile * tileCounty

View File

@ -9,6 +9,8 @@ class Enemy:
self.gameMap = gameMap
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

43
mapgen.py Normal file
View File

@ -0,0 +1,43 @@
import random
from constants import *
from player import *
from blocks import *
from enemies import *
def generateMap(gameMap, players):
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(spot[0], spot[1], gameMap)
except Exception as error:
continue
for i in range(50):
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(spot[0], spot[1], gameMap)
break
except Exception as error:
continue
for player in players:
maxTries = 5
for i in range(maxTries):
if i == maxTries - 1:
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]] = player
player.x = spot[0]
player.y = spot[1]
break
except Exception as error:
continue

View File

@ -1,3 +1,5 @@
from constants import *
class Player:
def __init__(self, x, y, sprite, keys, gameMap):
self.x = x
@ -35,6 +37,8 @@ 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:
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