Commit 27/05-24
Computer functionality! 6 players can now start a round, one is the human player and 5 are computer players. They can't really play, they just randomly select tiles and claim them.
This commit is contained in:
parent
64261170d5
commit
081a8a948a
6 changed files with 127 additions and 19 deletions
81
Main.py
81
Main.py
|
|
@ -1,5 +1,6 @@
|
||||||
import pygame
|
import pygame
|
||||||
import sys
|
import sys
|
||||||
|
import random
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
sys.path.append('./Map')
|
sys.path.append('./Map')
|
||||||
|
|
@ -242,11 +243,72 @@ class uButtons:
|
||||||
|
|
||||||
class uCommunicate:
|
class uCommunicate:
|
||||||
|
|
||||||
def claimState():
|
def __init__(self):
|
||||||
if GameMap.paintedTile:
|
self.globalGameState = 0 #0 = preround, 1 = active
|
||||||
ip = GameRealms.getPlayer("p1")
|
self.turnHolder = 0
|
||||||
pt = GameMap.paintedTile
|
|
||||||
print(f"Claiming {pt.Id} for {ip.name}")
|
def startGame(self):
|
||||||
|
GameRealms.load(map)
|
||||||
|
self.globalGameState = 1
|
||||||
|
t2 = Thread(target=uCommunicator.gameLoop)
|
||||||
|
t2.start()
|
||||||
|
|
||||||
|
def gameLoop(self):
|
||||||
|
while self.globalGameState == 1:
|
||||||
|
for x in range(GameRealms.amount):
|
||||||
|
if self.turnHolder != 0:
|
||||||
|
self.turnHolder = GameRealms.players[x].id
|
||||||
|
print(f"Turn being played by: {self.turnHolder}/{GameRealms.players[x].name}")
|
||||||
|
self.expand_cccs(self.turnHolder)
|
||||||
|
else:
|
||||||
|
while self.turnHolder == 0:
|
||||||
|
y = 1
|
||||||
|
|
||||||
|
def claimState(self):
|
||||||
|
if GameMap.paintedTile and self.turnHolder == 0:
|
||||||
|
if not GameMap.tIsClaimed(GameMap.paintedTile.Id):
|
||||||
|
ip = GameRealms.getPlayer(f"p1")
|
||||||
|
pt = GameMap.paintedTile
|
||||||
|
print(f"Claiming {pt.Id} for {ip.name}")
|
||||||
|
GameMap.claimTileFor(pt.Id,ip)
|
||||||
|
self.turnHolder += 1
|
||||||
|
|
||||||
|
def expand_cccs(self, ccc):
|
||||||
|
print(f"Using ccc = {ccc}")
|
||||||
|
expandTile = None
|
||||||
|
curPlayer = GameRealms.getPlayer(f"{ccc}")
|
||||||
|
if len(curPlayer.heldTiles) == 0:
|
||||||
|
while expandTile == None:
|
||||||
|
tryTile = GameMap.findTileAt((random.randint(0,800),random.randint(0,550)))
|
||||||
|
if tryTile != None:
|
||||||
|
if not GameMap.tIsClaimed(tryTile):
|
||||||
|
expandTile = tryTile
|
||||||
|
GameMap.claimTileFor(expandTile,GameRealms.getPlayer(f"{ccc}"),map)
|
||||||
|
curPlayer.heldTiles.append(expandTile)
|
||||||
|
else:
|
||||||
|
while expandTile == None:
|
||||||
|
trytile = int(curPlayer.heldTiles[random.randint(0,len(curPlayer.heldTiles)-1)])
|
||||||
|
daPos = GameMap.getTileInfo(trytile)["position"]
|
||||||
|
ranX = random.randint(0,3)
|
||||||
|
if ranX == 0:
|
||||||
|
if GameMap.getTileInfo(trytile+1)["owner"] == "Unclaimed":
|
||||||
|
expandTile = trytile+1
|
||||||
|
elif ranX == 1:
|
||||||
|
if GameMap.getTileInfo(trytile-1)["owner"] == "Unclaimed":
|
||||||
|
expandTile = trytile-1
|
||||||
|
elif ranX == 2:
|
||||||
|
daPos = (daPos[0],daPos[1]+1)
|
||||||
|
if GameMap.findTileAtRel(daPos) != None:
|
||||||
|
if GameMap.getTileInfo(GameMap.findTileAtRel(daPos))["owner"] == "Unclaimed":
|
||||||
|
expandTile = GameMap.findTileAtRel(daPos)
|
||||||
|
elif ranX == 3:
|
||||||
|
daPos = (daPos[0],daPos[1]-1)
|
||||||
|
if GameMap.findTileAtRel(daPos) != None:
|
||||||
|
if GameMap.getTileInfo(GameMap.findTileAtRel(daPos))["owner"] == "Unclaimed":
|
||||||
|
expandTile = GameMap.findTileAtRel(daPos)
|
||||||
|
|
||||||
|
GameMap.claimTileFor(expandTile,GameRealms.getPlayer(f"{ccc}"),map)
|
||||||
|
curPlayer.heldTiles.append(expandTile)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -256,8 +318,8 @@ uCommunicator = uCommunicate()
|
||||||
|
|
||||||
def prerenderGraphics():
|
def prerenderGraphics():
|
||||||
uTexter.write("State",(1000,0))
|
uTexter.write("State",(1000,0))
|
||||||
uButtoner.addButton("Start Game", 850, 500, 200, 50, "darkgray", "gray", "GameRealms.load(map)", True)
|
uButtoner.addButton("Start Game", 850, 500, 200, 50, "darkgray", "gray", "uCommunicator.startGame()", True)
|
||||||
uButtoner.addButton("Claim",850,600,110,40,"darkgray","gray","uCommunicate.claimState()")
|
uButtoner.addButton("Claim",850,600,110,40,"darkgray","gray","uCommunicator.claimState()")
|
||||||
uButtoner.addButton("ReRender", 1000, 600, 180, 50, "gray", "blue","GameMap.re_load()")
|
uButtoner.addButton("ReRender", 1000, 600, 180, 50, "gray", "blue","GameMap.re_load()")
|
||||||
prerenderGraphics()
|
prerenderGraphics()
|
||||||
|
|
||||||
|
|
@ -270,8 +332,8 @@ GameMap = MapManager.MapManager()
|
||||||
|
|
||||||
GameMap.load(map)
|
GameMap.load(map)
|
||||||
|
|
||||||
t = Thread(target=GameMap.populate, args=(map,))
|
t1 = Thread(target=GameMap.populate, args=(map,))
|
||||||
t.start()
|
t1.start()
|
||||||
|
|
||||||
time2 = pygame.time.get_ticks()
|
time2 = pygame.time.get_ticks()
|
||||||
|
|
||||||
|
|
@ -323,4 +385,5 @@ while running:
|
||||||
if timeY % 1000 == 0:
|
if timeY % 1000 == 0:
|
||||||
print(f"{timeY-timeX}ms per tick\n{1000/(timeY-timeX)} Ticks per second")
|
print(f"{timeY-timeX}ms per tick\n{1000/(timeY-timeX)} Ticks per second")
|
||||||
timeX = pygame.time.get_ticks()
|
timeX = pygame.time.get_ticks()
|
||||||
|
uCommunicator.globalGameState = 0
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
@ -160,6 +160,12 @@ class MapManager:
|
||||||
def re_load(self):
|
def re_load(self):
|
||||||
print("To Be Implomented")
|
print("To Be Implomented")
|
||||||
|
|
||||||
|
def tIsClaimed(self,id):
|
||||||
|
if self.tiles[f"{id}"].owner == "Unclaimed":
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
def findTileAt(self, pos):
|
def findTileAt(self, pos):
|
||||||
for x in self.tiles.keys():
|
for x in self.tiles.keys():
|
||||||
tile = self.tiles[f"{x}"]
|
tile = self.tiles[f"{x}"]
|
||||||
|
|
@ -173,13 +179,24 @@ class MapManager:
|
||||||
print(f"Could not find tile at position: {pos[0]},{pos[1]}")
|
print(f"Could not find tile at position: {pos[0]},{pos[1]}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def findTileAtRel(self, pos):
|
||||||
|
for x in self.tiles.keys():
|
||||||
|
tile = self.tiles[f"{x}"]
|
||||||
|
if tile.Position == pos:
|
||||||
|
print(f"x declared as {x} for {pos} in FindTileAtRel")
|
||||||
|
return x
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def getTileInfo(self, id):
|
def getTileInfo(self, id):
|
||||||
info = {}
|
info = {}
|
||||||
thistile = self.tiles[f"{id}"]
|
thistile = self.tiles[f"{id}"]
|
||||||
info["id"] = id
|
info["id"] = id
|
||||||
|
info["position"] = thistile.Position
|
||||||
info["pixels"] = thistile.getPixels()
|
info["pixels"] = thistile.getPixels()
|
||||||
info["terrain"] = thistile.getTerrain()
|
info["terrain"] = thistile.getTerrain()
|
||||||
info["border"] = thistile.getBorderPixels()
|
info["border"] = thistile.getBorderPixels()
|
||||||
|
info["owner"] = thistile.owner
|
||||||
info["gameplay"] = thistile.getGameplayInfo()
|
info["gameplay"] = thistile.getGameplayInfo()
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
@ -196,7 +213,16 @@ class MapManager:
|
||||||
self.paintedTile = None
|
self.paintedTile = None
|
||||||
print("Cleared previous tile")
|
print("Cleared previous tile")
|
||||||
|
|
||||||
def claimTileFor(self, tileID):
|
def claimTileFor(self, tileID, Claimant, mapObject=None):
|
||||||
|
tile = self.tiles[f"{tileID}"]
|
||||||
|
tile.owner = Claimant.name
|
||||||
|
tile.empireBorderPixels += tile.borderPixels
|
||||||
|
tile.colour = Claimant.borderColour
|
||||||
|
tile.pop += 1
|
||||||
|
if Claimant.id != 0:
|
||||||
|
tile.borderPainted = True
|
||||||
|
tile.paint_border_pixels(mapObject)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Tile:
|
class Tile:
|
||||||
|
|
@ -205,7 +231,7 @@ class Tile:
|
||||||
self.Id = Id
|
self.Id = Id
|
||||||
self.Position = Pos
|
self.Position = Pos
|
||||||
self.pixels = []
|
self.pixels = []
|
||||||
self.colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
|
self.colour = (0,0,0)
|
||||||
self.terrain = ""
|
self.terrain = ""
|
||||||
self.resources = {}
|
self.resources = {}
|
||||||
self.buildings = []
|
self.buildings = []
|
||||||
|
|
@ -213,6 +239,7 @@ class Tile:
|
||||||
self.owner = "Unclaimed"
|
self.owner = "Unclaimed"
|
||||||
self.pop = 0
|
self.pop = 0
|
||||||
self.borderPixels = []
|
self.borderPixels = []
|
||||||
|
self.empireBorderPixels = []
|
||||||
self.borderPainted = False
|
self.borderPainted = False
|
||||||
|
|
||||||
def getId(self):
|
def getId(self):
|
||||||
|
|
@ -249,9 +276,10 @@ class Tile:
|
||||||
map.set_at((pixel[0],pixel[1]), (255,255,255,255))
|
map.set_at((pixel[0],pixel[1]), (255,255,255,255))
|
||||||
self.borderPainted = True
|
self.borderPainted = True
|
||||||
else:
|
else:
|
||||||
print(f"Clearing border for tile {self.Id}")
|
print(f"Clearing border for tile {self.Id} using colour arguments {self.colour}. Default would be {(0,0,0,255)}")
|
||||||
for pixel in self.borderPixels:
|
for pixel in self.borderPixels:
|
||||||
map.set_at((pixel[0],pixel[1]), (0,0,0,255))
|
map.set_at((pixel[0],pixel[1]), (self.colour[0], self.colour[1], self.colour[2],255))
|
||||||
|
self.borderPainted = False
|
||||||
|
|
||||||
def findBorder(self, map):
|
def findBorder(self, map):
|
||||||
for pixel in self.pixels:
|
for pixel in self.pixels:
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -11,10 +11,15 @@ class RealmManager:
|
||||||
print (self.compnames)
|
print (self.compnames)
|
||||||
self.compnames = self.compnames.split("\n")
|
self.compnames = self.compnames.split("\n")
|
||||||
temptext.close()
|
temptext.close()
|
||||||
|
temptext = open("./Realms/valid_border_colours.txt", "r")
|
||||||
|
self.colournames = temptext.read()
|
||||||
|
print (self.colournames)
|
||||||
|
self.colournames = self.colournames.split("\n")
|
||||||
|
temptext.close()
|
||||||
for x in range(self.amount):
|
for x in range(self.amount):
|
||||||
if x == 0:
|
if x == 0:
|
||||||
self.players[0] = Player("p1")
|
self.players[0] = Player("p1", self.colournames)
|
||||||
self.players[x] = Player("c", x, self.compnames)
|
self.players[x] = Player("c", self.colournames, x, self.compnames)
|
||||||
for x in self.players:
|
for x in self.players:
|
||||||
p = self.players[x]
|
p = self.players[x]
|
||||||
print(f"Player {p.id}/{p.name} with colour {p.borderColour}")
|
print(f"Player {p.id}/{p.name} with colour {p.borderColour}")
|
||||||
|
|
@ -23,14 +28,18 @@ class RealmManager:
|
||||||
if detail == "p1":
|
if detail == "p1":
|
||||||
return self.players[0]
|
return self.players[0]
|
||||||
else:
|
else:
|
||||||
print("oopsie")
|
return self.players[int(detail)]
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
|
|
||||||
def __init__(self, controller, id=0, names=None):
|
def __init__(self, controller, colour, id=0, names=None,):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.controller = controller
|
self.controller = controller
|
||||||
self.borderColour = (random.randint(10,240),random.randint(10,240),random.randint(10,240))
|
self.heldTiles = []
|
||||||
|
colour = colour[random.randint(0,len(colour)-1)]
|
||||||
|
colour = colour.split(",")
|
||||||
|
print (colour)
|
||||||
|
self.borderColour = (int(colour[0]),int(colour[1]),int(colour[2]))
|
||||||
if controller == "c":
|
if controller == "c":
|
||||||
self.name = names[random.randint(0,len(names)-1)]
|
self.name = names[random.randint(0,len(names)-1)]
|
||||||
Binary file not shown.
8
Realms/valid_border_colours.txt
Normal file
8
Realms/valid_border_colours.txt
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
255,0,0
|
||||||
|
255,255,0
|
||||||
|
255,0,255
|
||||||
|
0,0,0
|
||||||
|
160,160,160
|
||||||
|
0,100,0
|
||||||
|
255,100,255
|
||||||
|
180,0,180
|
||||||
Loading…
Add table
Add a link
Reference in a new issue