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:
Hannah-Dagemark 2024-05-27 14:31:11 +02:00
commit 081a8a948a
6 changed files with 127 additions and 19 deletions

81
Main.py
View file

@ -1,5 +1,6 @@
import pygame
import sys
import random
from threading import Thread
sys.path.append('./Map')
@ -242,11 +243,72 @@ class uButtons:
class uCommunicate:
def claimState():
if GameMap.paintedTile:
ip = GameRealms.getPlayer("p1")
pt = GameMap.paintedTile
print(f"Claiming {pt.Id} for {ip.name}")
def __init__(self):
self.globalGameState = 0 #0 = preround, 1 = active
self.turnHolder = 0
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():
uTexter.write("State",(1000,0))
uButtoner.addButton("Start Game", 850, 500, 200, 50, "darkgray", "gray", "GameRealms.load(map)", True)
uButtoner.addButton("Claim",850,600,110,40,"darkgray","gray","uCommunicate.claimState()")
uButtoner.addButton("Start Game", 850, 500, 200, 50, "darkgray", "gray", "uCommunicator.startGame()", True)
uButtoner.addButton("Claim",850,600,110,40,"darkgray","gray","uCommunicator.claimState()")
uButtoner.addButton("ReRender", 1000, 600, 180, 50, "gray", "blue","GameMap.re_load()")
prerenderGraphics()
@ -270,8 +332,8 @@ GameMap = MapManager.MapManager()
GameMap.load(map)
t = Thread(target=GameMap.populate, args=(map,))
t.start()
t1 = Thread(target=GameMap.populate, args=(map,))
t1.start()
time2 = pygame.time.get_ticks()
@ -323,4 +385,5 @@ while running:
if timeY % 1000 == 0:
print(f"{timeY-timeX}ms per tick\n{1000/(timeY-timeX)} Ticks per second")
timeX = pygame.time.get_ticks()
uCommunicator.globalGameState = 0
pygame.quit()

View file

@ -159,6 +159,12 @@ class MapManager:
def re_load(self):
print("To Be Implomented")
def tIsClaimed(self,id):
if self.tiles[f"{id}"].owner == "Unclaimed":
return False
else:
return True
def findTileAt(self, pos):
for x in self.tiles.keys():
@ -173,13 +179,24 @@ class MapManager:
print(f"Could not find tile at position: {pos[0]},{pos[1]}")
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):
info = {}
thistile = self.tiles[f"{id}"]
info["id"] = id
info["position"] = thistile.Position
info["pixels"] = thistile.getPixels()
info["terrain"] = thistile.getTerrain()
info["border"] = thistile.getBorderPixels()
info["owner"] = thistile.owner
info["gameplay"] = thistile.getGameplayInfo()
return info
@ -196,7 +213,16 @@ class MapManager:
self.paintedTile = None
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:
@ -205,7 +231,7 @@ class Tile:
self.Id = Id
self.Position = Pos
self.pixels = []
self.colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
self.colour = (0,0,0)
self.terrain = ""
self.resources = {}
self.buildings = []
@ -213,6 +239,7 @@ class Tile:
self.owner = "Unclaimed"
self.pop = 0
self.borderPixels = []
self.empireBorderPixels = []
self.borderPainted = False
def getId(self):
@ -249,10 +276,11 @@ class Tile:
map.set_at((pixel[0],pixel[1]), (255,255,255,255))
self.borderPainted = True
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:
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):
for pixel in self.pixels:
#check above

View file

@ -11,10 +11,15 @@ class RealmManager:
print (self.compnames)
self.compnames = self.compnames.split("\n")
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):
if x == 0:
self.players[0] = Player("p1")
self.players[x] = Player("c", x, self.compnames)
self.players[0] = Player("p1", self.colournames)
self.players[x] = Player("c", self.colournames, x, self.compnames)
for x in self.players:
p = self.players[x]
print(f"Player {p.id}/{p.name} with colour {p.borderColour}")
@ -23,14 +28,18 @@ class RealmManager:
if detail == "p1":
return self.players[0]
else:
print("oopsie")
return self.players[int(detail)]
class Player:
def __init__(self, controller, id=0, names=None):
def __init__(self, controller, colour, id=0, names=None,):
self.id = id
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":
self.name = names[random.randint(0,len(names)-1)]

View 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