def __init__(self, surface, dimensions, debug): self.debug = debug self.surface = surface self.width = dimensions[0] self.length = dimensions[1] self.boundary = [self.width/2, self.length/2] self.focus = (0,0) self.mode = 0 #Free camera self.colour = (0,0,0) self.path = filePath.getRootFolder(FILERETRACTS) self.locationFont = pygame.font.Font(filePath.setPath(self.path,["assets","fonts","Kingdom_Hearts_Font.ttf"]), 50) self.mainFont = pygame.font.Font(filePath.setPath(self.path,["assets","fonts","Coda-Regular.ttf"]), 30)
def startUpAnimation(self): #Startup animation startupImage = pygame.image.load(filePath.setPath(self.path,["assets","title screen","logo.png"])).convert() startupImage = pygame.transform.scale(startupImage, self.dimensions) startupRect = startupImage.get_rect() startupRect.center = (round(self.dimensions[0]/2), round(self.dimensions[1]/2)) for i in range (152): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() self.surface.fill((0,0,0)) #1.5 seconds if i < 75: startupImage.set_alpha(i*4+1) self.surface.blit(startupImage, startupRect) pygame.display.update() pygame.time.delay(16) #Transition for i in range(60): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() gradient = pygame.Surface(self.dimensions, pygame.SRCALPHA) gradient.fill((0,0,0,i*4+1)) self.surface.blit(gradient, (0,0)) pygame.display.update() pygame.time.delay(16)
def loadSaves(self): saves = [] for i in range(6): #6 = maximum number of saves try: #Load save files #File formate = [difficulty, level, money, shipType, emblems, items, current chapter] saveFile = open(filePath.setPath(self.path,["data","saves","save"+str((i+1))+".txt"]), "r").readlines() try: #Check if the save is interperatble save = 0 savedData = json.loads(saveFile[0]) if 0 <= savedData[0] and savedData[0] <= 2: #difficulty if 0 <= savedData[1] and savedData[1] <= 260770: #exp if 0 <= savedData[2] and savedData[2] <= 99999: #money if -1 <= savedData[3] and savedData[3] <= 2: #shipType if -1 <= savedData[4][0] and savedData[4][0] <= 2 and -1 <= savedData[4][1] and savedData[4][1] <= 2: #emblems if 0 <= savedData[6] and savedData[6] <= 21: #current chapter saves.append(savedData) save = 1 if save == 0: saves.append("corrupted") except: #Returned save corrupted saves.append("corrupted") except: #Skip if save not found saves.append(0) if self.debug: for i in range(len(saves)): print(saves[i]) print("") return saves
def open(self, surface, networkProtocol, debug=False): #Load assets self.surface = surface self.dimensions = self.surface.get_size() self.debug = debug self.dt = 16 #milliseconds per frame self.waitingTime = self.dt / self.targetDeltaTime self.stage = "connecting to server" self.clock = 0 self.closing = False self.networkProtocol = networkProtocol self.joiningGame = False self.controls = { "up": [pygame.K_w], "left": [pygame.K_a], "right": [pygame.K_d], "down": [pygame.K_s], "SQUARE": [pygame.K_j], "CIRCLE": [pygame.K_l, 1], "CROSS": [pygame.K_k, 0], "TRIANGLE": [pygame.K_i, 3], "start": [pygame.K_p, 7], "home": [pygame.K_ESCAPE, 6], "L1": [pygame.K_q, 4], "R1": [pygame.K_e, 5] } self.entities = {} self.path = filePath.getRootFolder(FILERETRACTS) self.mainFont = pygame.font.Font( filePath.setPath(self.path, ["assets", "fonts", "Coda-Regular.ttf"]), 30) self.open = True
timeElapsed = pygame.time.get_ticks() - startTime wait = max(self.targetDeltaTime - timeElapsed, 0) / 1000 self.dt = max(self.targetDeltaTime, timeElapsed) if self.dt > 16: print(self.dt) reactor.callLater(wait, self.runClient) def setState(self, state, stateName, params=None): self.gameState = state(self.targetDeltaTime) self.stateName = stateName if params != None: self.gameState.open(self.surface, params, debug=self.debug) else: self.gameState.open(self.surface, debug=self.debug) FILERETRACTS = 2 config = filePath.loadConfig( filePath.setPath(filePath.getRootFolder(FILERETRACTS), ["config.txt"])) SERVER_IP = config["SERVER_IP"] SERVER_PORT = config["SERVER_PORT"] RESOLUTION = config["RESOLUTION"] FPS = config["FPS"] GRAPHICS = config["GRAPHICS"] os.environ['SDL_VIDEO_CENTERED'] = '1' debug = True gameClient(RESOLUTION, GRAPHICS, debug)
def loadAbilities(): contentFolder = filePath.setPath(fileRootPath, ["data", "content", "abilities.txt"]) contentData = filePath.loadList(contentFolder) return contentData
def loadProjectileData(): contentFolder = filePath.setPath(fileRootPath, ["data", "content", "projectiles.txt"]) contentData = filePath.loadConfig(contentFolder) return contentData
def loadEnemyData(): contentFolder = filePath.setPath(fileRootPath, ["data", "content", "enemies.txt"]) contentData = filePath.loadConfig(contentFolder) return contentData
def loadImages(self): #PRE-CALCULATIONS self.hudMap = ( (33 * WIDTHSCALE, 603 * LENGTHSCALE), (1065 * WIDTHSCALE, 10 * LENGTHSCALE) ) #Calculated positions of where the hud images are place rootFolder = filePath.getRootFolder(2) #LOAD HUD TEMPLATE self.hud = filePath.setPath(rootFolder, ["assets", "player", "hud.png"]) self.hud = pygame.image.load(self.hud).convert_alpha() #LOAD ABILITY BOX self.abilityBox = ( (206, 233, 245), (20 * WIDTHSCALE, 595 * LENGTHSCALE, round(320 * WIDTHSCALE), round(110 * LENGTHSCALE))) #Colour, rect #LOAD ABILITY ICONS self.abilityIcons = ( ((52, 106, 129), (33 * WIDTHSCALE, 630 * LENGTHSCALE, round(64 * WIDTHSCALE), round(64 * LENGTHSCALE))), ((52, 106, 129), (110 * WIDTHSCALE, 630 * LENGTHSCALE, round(64 * WIDTHSCALE), round(64 * LENGTHSCALE))), ((52, 106, 129), (187 * WIDTHSCALE, 630 * LENGTHSCALE, round(64 * WIDTHSCALE), round(64 * LENGTHSCALE))), ((52, 106, 129), (264 * WIDTHSCALE, 630 * LENGTHSCALE, round(64 * WIDTHSCALE), round(64 * LENGTHSCALE)))) #(Colour, rect), (Colour, rect), ... #LOAD HP BAR self.hpBar = ( ((30, 58, 93), (1112 * WIDTHSCALE, 673 * LENGTHSCALE, round(163 * WIDTHSCALE), round(12 * LENGTHSCALE)), round(2 * WIDTHSCALE)), ((77, 161, 24), (1112 * WIDTHSCALE, 673 * LENGTHSCALE, round(163 * WIDTHSCALE), round(12 * LENGTHSCALE))) ) #Border (Colour, rect), Fill (Colour, rect) #LOAD MP BAR self.mpBar = ( ((30, 58, 93), (1154 * WIDTHSCALE, 688 * LENGTHSCALE, round(120 * WIDTHSCALE), round(12 * LENGTHSCALE)), round(2 * WIDTHSCALE)), ((47, 105, 203), (1154 * WIDTHSCALE, 688 * LENGTHSCALE, round(120 * WIDTHSCALE), round(12 * LENGTHSCALE))) ) #Border (Colour, rect), Fill (Colour, rect) #LOAD LEVEL BAR ANIMATION self.levelBar = [] levelBarImage = filePath.setPath( rootFolder, ["assets", "player", "level bars.png"]) levelBarImage = pygame.image.load(levelBarImage).convert() for i in range(9): levelBar = pygame.Surface((1180, 88)) levelBar.blit(levelBarImage, (0, 0), (0, i * 88, 1180, 88)) levelBar = pygame.transform.smoothscale( levelBar, (round(295 * WIDTHSCALE), round(22 * LENGTHSCALE))) self.levelBar.append(levelBar) #LOAD ATTACK STYLE ICON self.basicAttacks = [ filePath.setPath(rootFolder, ["assets", "player", "basic surge.png"]), filePath.setPath(rootFolder, ["assets", "player", "basic spray.png"]), filePath.setPath(rootFolder, ["assets", "player", "basic hyper.png"]) ] for i in range(3): image = pygame.image.load(self.basicAttacks[i]).convert_alpha() width, length = image.get_rect()[2], image.get_rect()[3] targetLength = round(41 * LENGTHSCALE) targetWidth = round(width * 41 * WIDTHSCALE / length) image = pygame.transform.smoothscale(image, (targetWidth, targetLength)) position = (1240 * WIDTHSCALE - targetWidth, 620 * LENGTHSCALE) self.basicAttacks[i] = (image, position) #LOAD MINIMAP self.minimap = filePath.setPath(rootFolder, ["assets", "player", "minimap.png"]) self.minimap = pygame.image.load(self.minimap).convert_alpha() self.minimap = pygame.transform.smoothscale( self.minimap, (round(205 * WIDTHSCALE), round(187 * LENGTHSCALE))) #SET ANIMATION SETTINGS self.animations = [[0, 8, 0, 50] ] #Current frame, Max frame, time elapsed, duration
import pygame, os, math, json import socket, _thread from lib import LIMITobjects, contentLoader, filePath import collision #experimental SERVER_IP = socket.gethostbyname(socket.gethostname()) SERVER_PORT = 8080 os.environ['SDL_VIDEO_CENTERED'] = '1' config = filePath.loadConfig( filePath.setPath(filePath.getRootFolder(2), ["config.txt"])) RESOLUTION = config["RESOLUTION"] DEBUG = config["DEBUG"] WIDTHSCALE, LENGTHSCALE = RESOLUTION[0] / 1280, RESOLUTION[ 1] / 720 #Scale game according to default 1280x720 dimensions networkPacker = { "moveForward": 0, "turnLeft": 1, "turnRight": 2, "primaryFire": 3, } networkUnPacker = { 0: "moveForward", 1: "turnLeft", 2: "turnRight", 3: "primaryFire", } class server:
def mainMenu(self): background = pygame.image.load(filePath.setPath(self.path,["assets","title screen","overlay.png"])) background = pygame.transform.scale(background, self.dimensions) cursor = pygame.image.load(filePath.setPath(self.path,["assets","background","cursor.png"])) cursor = pygame.transform.scale(cursor, (40,40)) self.cursorTouching = [None] self.instance = "main screen" #Controls which UI is loaded self.savedData = self.loadSaves() #Calculations needed for graph coordinates boxSpace = self.dimensions[1]/3 - 110 boxShift = (boxSpace - 3*35)/2 +self.dimensions[1]/3 +70 boxShift2 = (boxSpace - 2*35)/2 +self.dimensions[1]/3 +70 #i*45+boxShift #Box Widths = 168, 242, 119 graph = { "NEW GAME":{"up":"CONFIG","down":"LOAD","value":(self.dimensions[0]-200, self.dimensions[1]-4.6*self.dimensions[1]/20 +5),"next":"Normal","previous":""}, #Controls where the controller should take the cursor "LOAD":{"up":"NEW GAME","down":"CONFIG","value":(self.dimensions[0]-200, self.dimensions[1]-3.2*self.dimensions[1]/20 +5),"next":"save1","previous":""}, "CONFIG":{"up":"LOAD","down":"NEW GAME","value":(self.dimensions[0]-200, self.dimensions[1]-1.8*self.dimensions[1]/20 +5),"next":"Sound","previous":""}, "Normal":{"up":"Extreme","down":"Hard","value":(self.dimensions[0]*0.25+75, boxShift),"next":"save1","previous":"NEW GAME"}, "Hard":{"up":"Normal","down":"Extreme","value":(self.dimensions[0]*0.25+75, 45+boxShift),"next":"save1","previous":"NEW GAME"}, "Extreme":{"up":"Hard","down":"Normal","value":(self.dimensions[0]*0.25+75, 90+boxShift),"next":"save1","previous":"NEW GAME"}, "save1":{"up":"save6","down":"save2","value":(self.dimensions[0]*0.12, self.dimensions[1]*0.25),"next":"Yes","previous":""}, "save2":{"up":"save1","down":"save3","value":(self.dimensions[0]*0.12, self.dimensions[1]*0.35),"next":"Yes","previous":""}, "save3":{"up":"save2","down":"save4","value":(self.dimensions[0]*0.12, self.dimensions[1]*0.45),"next":"Yes","previous":""}, "save4":{"up":"save3","down":"save5","value":(self.dimensions[0]*0.12, self.dimensions[1]*0.55),"next":"Yes","previous":""}, "save5":{"up":"save4","down":"save6","value":(self.dimensions[0]*0.12, self.dimensions[1]*0.65),"next":"Yes","previous":""}, "save6":{"up":"save5","down":"save1","value":(self.dimensions[0]*0.12, self.dimensions[1]*0.75),"next":"Yes","previous":""}, "Sound":{"up":"Custom Cursor","down":"Music","value":(self.dimensions[0]*0.25+112, boxShift),"next":"Sound","previous":"CONFIG"}, "Music":{"up":"Sound","down":"Custom Cursor","value":(self.dimensions[0]*0.25+112, 45+boxShift),"next":"Music","previous":"CONFIG"}, "Custom Cursor":{"up":"Music","down":"Sound","value":(self.dimensions[0]*0.25+112, 90+boxShift),"next":"Custom Cursor","previous":"CONFIG"}, "Yes":{"up":"No","down":"No","value":(self.dimensions[0]*0.25+56.5, boxShift2),"next":"","previous":""}, "No":{"up":"Yes","down":"Yes","value":(self.dimensions[0]*0.25+56.5, 45+boxShift2),"next":"","previous":""}, } currentNode = "NEW GAME" #Transition for i in range(63): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() self.drawMainOption(4.6, (0,0), "NEW GAME") self.drawMainOption(3.2, (0,0), "LOAD") self.drawMainOption(1.8, (0,0), "CONFIG") self.surface.blit(background, (0,0)) gradient = pygame.Surface(self.dimensions, pygame.SRCALPHA) gradient.fill((255,255,255,249-i*3)) self.surface.blit(gradient, (0,0)) pygame.display.update() pygame.time.delay(16) if self.customCursor == 1: pygame.mouse.set_visible(False) else: pygame.mouse.set_visible(True) #Test the gamepad self.gamePads = [] for i in range(0, pygame.joystick.get_count()): self.gamePads.append(pygame.joystick.Joystick(i)) self.gamePads[-1].init() if self.debug: print("Detected gamepad '",self.gamePads[-1].get_name(),"'") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() #Test the gamepad if event.type == pygame.JOYBUTTONDOWN: if self.debug: print("button:",event.button) if event.button == 0: #Advance button destination = graph[currentNode]["next"] if destination != "": previousNode = currentNode if previousNode != destination: #Prevents loops currentNode = destination graph[currentNode]["previous"] = previousNode startNode = graph[currentNode]["down"] while startNode != currentNode: #Set all the options previous settings graph[startNode]["previous"] = previousNode startNode = graph[startNode]["down"] if previousNode == "LOAD": for i in range(6): graph["save"+str(i+1)]["next"] = "" elif graph[previousNode]["previous"] == "NEW GAME": for i in range(6): graph["save"+str(i+1)]["next"] = "Yes" elif currentNode == "No": currentNode = graph[currentNode]["previous"] self.handleInput() if self.debug: print("option:",currentNode) if event.button == 1: if graph[currentNode]["previous"] != "": x,y = 0,0 currentNode = graph[currentNode]["previous"] if currentNode == "NEW GAME" or currentNode == "LOAD" or currentNode == "CONFIG": self.cursorTouching[0] = "0" elif graph[currentNode]["next"][:2] == "sa": self.cursorTouching[0] = "1" elif currentNode[:2] == "sa": self.cursorTouching = ["CREATEFILE", "No"] else: self.cursorTouching[0] = "0" self.handleInput() else: pygame.mixer.Sound.play(self.menuSounds[1]) #back if self.debug: print("option:",currentNode) if event.type == pygame.JOYHATMOTION: dpad = self.gamePads[0].get_hat(0) if self.debug: print("dpad:",dpad) if dpad[1] == 1: currentNode = graph[currentNode]["up"] elif dpad[1] == -1: currentNode = graph[currentNode]["down"] if event.type == pygame.MOUSEBUTTONDOWN: if len(self.gamePads) == 0: self.handleInput() if self.instance == "startGame": break self.surface.fill((255,255,255)) #Make screen blank (white) self.surface.blit(background, (0,0)) #Title if len(self.gamePads) > 0: x,y = graph[currentNode]["value"] y +=4 # move y down slightlty else: x,y = pygame.mouse.get_pos() #Get mouse coordinates if self.instance == "main screen": self.drawMainOption(4.6, (x,y), "NEW GAME") self.drawMainOption(3.2, (x,y), "LOAD") self.drawMainOption(1.8, (x,y), "CONFIG") else: #Render the gui but prevent selection and collision self.drawMainOption(4.6, (0,0), "NEW GAME") self.drawMainOption(3.2, (0,0), "LOAD") self.drawMainOption(1.8, (0,0), "CONFIG") #Instances if self.instance == "newGame": self.drawUIBOX((x,y), "Select a game difficulty level.", "(The difficulty can not be changed.)", [ ("Normal","Standard difficulty, recommended for new players."), ("Hard","Enemy stats are increased to x1.3."), ("Extreme","Enemy stats are increased to x1.5, damage increased to x1.25, EXP gain decreased to x0.8.") ]) elif self.instance == "createFile": self.loadMenu((x,y), 1) elif self.instance == "load": self.loadMenu((x,y)) elif self.instance == "confirm": self.drawUIBOX((x,y), "CREATE A NEW SAVE", self.createTextOption, [ ("Yes","Begin your new save with "+self.fileType.upper()+" difficulty."), ("No","Return to save menu."), ]) elif self.instance == "config": self.drawUIBOX((x,y), "Configure your options.", "(These can be changed in-game.)", [ ("Sound","Toggle if sound effects are played."), ("Music","Toggle if music is played."), ("Custom Cursor","Toggle between custom cursor and your device's cursor.") ], [ ("TOGGLE", self.playSounds, "playSounds"), ("TOGGLE", self.playMusic, "playMusic"), ("TOGGLE", self.customCursor, "customCursor"), ]) if self.customCursor: #Render custom mouse if the option is on cursorRect = cursor.get_rect() cursorRect.center = (x,y) self.surface.blit(cursor, cursorRect) pygame.display.update() pygame.time.delay(16) self.launchGame() #Launch the game once the main menu has been exited.
def handleInput(self): if self.cursorTouching[0] == "0": #close the ui self.instance = "main screen" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[1]) #back elif self.cursorTouching[0] == "1": #go to self.instance new game self.instance = "newGame" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[1]) #back elif self.cursorTouching[0] == "NEW GAME": self.instance = "newGame" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[2]) #open elif self.cursorTouching[0] == "CREATEFILE": if self.instance != "confirm": self.instance = "createFile" self.fileType = self.cursorTouching[1] if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[4]) #select elif self.instance == "confirm": if self.cursorTouching[1] == "Yes": newSave = open(filePath.setPath(self.path,["data","saves","save"+str((self.saveSlot+1))+".txt"]), "w+") #Create new save or overwrite file if self.fileType == "Normal": difficulty = "0" elif self.fileType == "Hard": difficulty = "1" else: difficulty = "2" self.playerData = [int(difficulty), 0, 0, -1, [-1,-1], [], 0, {"commands":["Surge"]}] #Store the player's save data to be used on launch newSave.write(json.dumps(self.playerData)) newSave.close() #difficulty, level, money, shipType, emblems, items, current chapter self.instance = "startGame" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[4]) #select elif self.cursorTouching[1] == "No": self.instance = "createFile" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[1]) #back elif self.cursorTouching[0] == "LOAD": self.instance = "load" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[2]) #open elif self.cursorTouching[0] == "SAVE": if self.instance == "createFile": if self.savedData[self.cursorTouching[1]] == 0: #If an empty save self.createTextOption = "(Start a new save and begin your adventure.)" else: self.createTextOption = "(Are you sure you want to overwrite your save file?)" self.instance = "confirm" self.saveSlot = self.cursorTouching[1] if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[4]) #select elif self.instance == "load": self.playerData = self.savedData[self.cursorTouching[1]] if not(self.playerData == 0 or self.playerData == "corrupted"):#check if the save is loadable self.instance = "startGame" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[4]) #select else: if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[0]) #error elif self.cursorTouching[0] == "CONFIG": self.instance = "config" if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[2]) #open elif self.cursorTouching[0] == "TOGGLE": if self.cursorTouching[1] == "playSounds": if self.playSounds == 1: self.playSounds = 0 else: self.playSounds = 1 if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[4]) #select elif self.cursorTouching[1] == "playMusic": if self.playMusic == 1: self.playMusic = 0 pygame.mixer.music.pause() else: self.playMusic = 1 pygame.mixer.music.unpause() if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[4]) #select elif self.cursorTouching[1] == "customCursor": if len(self.gamePads) == 0: if self.customCursor == 1: self.customCursor = 0 pygame.mouse.set_visible(True) else: self.customCursor = 1 pygame.mouse.set_visible(False) if self.playSounds: pygame.mixer.Sound.play(self.menuSounds[4]) #select else: pygame.mixer.Sound.play(self.menuSounds[0]) #error
def __init__(self, dimensions, config, debug=False): self.debug = debug pygame.mixer.pre_init(44100, -16, 1, 512) pygame.init() pygame.mouse.set_visible(False) self.dimensions = dimensions self.surface = pygame.display.set_mode(dimensions) pygame.display.set_caption("Kingdom Space Battle") self.path = filePath.getRootFolder(FILERETRACTS) self.fontMain = pygame.font.Font(filePath.setPath(self.path,["assets","fonts","Coda-Regular.ttf"]), 24) self.fontSub = pygame.font.Font(filePath.setPath(self.path,["assets","fonts","Coda-Regular.ttf"]), 20) self.menuSounds = [ pygame.mixer.Sound(filePath.setPath(self.path,["assets","sounds","system sound effects","error.wav"])), pygame.mixer.Sound(filePath.setPath(self.path,["assets","sounds","system sound effects","menu back.wav"])), pygame.mixer.Sound(filePath.setPath(self.path,["assets","sounds","system sound effects","menu open.wav"])), pygame.mixer.Sound(filePath.setPath(self.path,["assets","sounds","system sound effects","menu scroll.wav"])), pygame.mixer.Sound(filePath.setPath(self.path,["assets","sounds","system sound effects","select.wav"])), ] menuMusic = pygame.mixer.music.load(filePath.setPath(self.path,["assets","sounds","music","Dearly Beloved.wav"])) pygame.mixer.music.play(-1) icon = pygame.image.load(filePath.setPath(self.path,["assets","title screen","icon.png"])) pygame.display.set_icon(icon) self.emblems = [ pygame.image.load(filePath.setPath(self.path,["assets","player","emblems","vanguard.png"])), pygame.image.load(filePath.setPath(self.path,["assets","player","emblems","engineer.png"])), pygame.image.load(filePath.setPath(self.path,["assets","player","emblems","stealth.png"])), ] for i in range(len(self.emblems)): #Scale images self.emblems[i] = pygame.transform.scale(self.emblems[i], self.dimensions) self.controls = {"up":[pygame.K_w], "left":[pygame.K_a], "right":[pygame.K_d], "down":[pygame.K_s], "SQUARE":[pygame.K_j], "CIRCLE":[pygame.K_l,1], "CROSS":[pygame.K_k,0], "TRIANGLE":[pygame.K_i,3], "start":[pygame.K_p,7], "home":[pygame.K_ESCAPE,6], "L1":[pygame.K_q,4], "R1":[pygame.K_e,5]} self.playSounds = 1 self.playMusic = 1 self.customCursor = 1 self.startUpAnimation() self.mainMenu()
def __init__(self, surface, dimensions, controls, config, debug=False): if debug: print("Game Engine initiating.") #Set attributes self.SERVER_IP = config["SERVER_IP"] self.SERVER_PORT = config["SERVER_PORT"] self.debug = debug self.surface = surface self.dimensions = dimensions self.entities = [] self.projectiles = [] self.party = [] self.instance = "map" self.baracksAnimation = [0, 0, 0] self.pausedInstances = ("paused", "interaction", "baracks") self.controls = controls self.path = filePath.getRootFolder(FILERETRACTS) self.entityGroups = { "enemy": ("square", "triangle", "entity"), "ally": ("player", ), "object": ("location", "npc") } self.FPS = config["FPS"] self.currentFPS = config[ "FPS"] # Assume it can initially run at the target FPS self.deltaMin = round(1000 / self.FPS) self.frameCount = 0 self.fpsTimer = 0 self.newFPS = config[ "FPS"] # Assume it can initially run at the target FPS # Load assets self.mainFont = pygame.font.Font( filePath.setPath(self.path, ["assets", "fonts", "Coda-Regular.ttf"]), 30) # Load required parts of the engine self.player = None self.selectedCommand = 0 self.commandMenu = [0] self.focus = [0, 0] self.freeFocus = [0, 0] # The focus of the camera in free camera mode self.cameraSpeed = 500 # The speed of the free camera in metres per second self.cameraMode = 0 #0 = free camera self.camera = camera(surface, dimensions, debug) self.hud = LIMITinterface.display(surface, dimensions, debug) # Start the network socket if self.debug: self.networkType = "Host" self.currentImage = [] else: self.networkType = "Peer" self.lastFrame = [] self.networkSocket = None self.connectedAddresses = [] self.activeAddresses = [] self.peerInputs = [] if self.networkType == "Host": self.networkSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.SERVER_IP = socket.gethostbyname(socket.gethostname()) self.networkSocket.bind((self.SERVER_IP, self.SERVER_PORT)) if self.debug: print("SERVER OPENED ON", self.SERVER_IP, ":", self.SERVER_PORT) _thread.start_new_thread(self.peerGateway, ()) else: self.currentFrame = [] self.frameSkipped = [] self.networkSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.networkSocket.connect((self.SERVER_IP, self.SERVER_PORT)) _thread.start_new_thread(self.peerConnection, ())
def open(self, surface, debug=False): #Load assets self.surface = surface self.dimensions = self.surface.get_size() self.debug = debug self.dt = 16 #milliseconds per frame self.waitingTime = self.dt / self.targetDeltaTime pygame.mixer.pre_init(44100, -16, 1, 512) pygame.mouse.set_visible(False) self.path = filePath.getRootFolder(FILERETRACTS) self.fontMain = pygame.font.Font( filePath.setPath(self.path, ["assets", "fonts", "Coda-Regular.ttf"]), 24) self.fontSub = pygame.font.Font( filePath.setPath(self.path, ["assets", "fonts", "Coda-Regular.ttf"]), 20) self.menuSounds = [ pygame.mixer.Sound( filePath.setPath( self.path, ["assets", "sounds", "system sound effects", "error.wav" ])), pygame.mixer.Sound( filePath.setPath(self.path, [ "assets", "sounds", "system sound effects", "menu back.wav" ])), pygame.mixer.Sound( filePath.setPath(self.path, [ "assets", "sounds", "system sound effects", "menu open.wav" ])), pygame.mixer.Sound( filePath.setPath(self.path, [ "assets", "sounds", "system sound effects", "menu scroll.wav" ])), pygame.mixer.Sound( filePath.setPath( self.path, ["assets", "sounds", "system sound effects", "select.wav" ])), ] menuMusic = pygame.mixer.music.load( filePath.setPath( self.path, ["assets", "sounds", "music", "kh3-Dearly Beloved.mp3"])) self.musicStarted = False self.emblems = [ pygame.image.load( filePath.setPath( self.path, ["assets", "player", "emblems", "vanguard.png" ])).convert_alpha(), pygame.image.load( filePath.setPath( self.path, ["assets", "player", "emblems", "engineer.png" ])).convert_alpha(), pygame.image.load( filePath.setPath( self.path, ["assets", "player", "emblems", "stealth.png" ])).convert_alpha(), ] for i in range(len(self.emblems)): #Scale images self.emblems[i] = pygame.transform.scale(self.emblems[i], self.dimensions) self.controls = { "up": [pygame.K_w], "left": [pygame.K_a], "right": [pygame.K_d], "down": [pygame.K_s], "SQUARE": [pygame.K_j], "CIRCLE": [pygame.K_l, 1], "CROSS": [pygame.K_k, 0], "TRIANGLE": [pygame.K_i, 3], "start": [pygame.K_p, 7], "home": [pygame.K_ESCAPE, 6], "L1": [pygame.K_q, 4], "R1": [pygame.K_e, 5] } self.playSounds = 1 self.playMusic = 1 self.customCursor = 1 #Start up animation self.startupImage = pygame.image.load( filePath.setPath( self.path, ["assets", "title screen", "logo.png"])).convert_alpha() self.startupImage = pygame.transform.scale(self.startupImage, self.dimensions) self.startupRect = self.startupImage.get_rect() self.startupRect.center = (round(self.dimensions[0] / 2), round(self.dimensions[1] / 2)) #Title screen self.background = pygame.image.load( filePath.setPath( self.path, ["assets", "title screen", "overlay.png"])).convert_alpha() self.background = pygame.transform.scale(self.background, self.dimensions) self.cursor = pygame.image.load( filePath.setPath(self.path, ["assets", "background", "cursor.png"])) self.cursor = pygame.transform.scale(self.cursor, (40, 40)) self.cursorTouching = [None] self.instance = "main screen" #Controls which UI is loaded self.savedData = self.loadSaves() #Calculations needed for graph coordinates boxSpace = self.dimensions[1] / 3 - 110 boxShift = (boxSpace - 3 * 35) / 2 + self.dimensions[1] / 3 + 70 boxShift2 = (boxSpace - 2 * 35) / 2 + self.dimensions[1] / 3 + 70 #i*45+boxShift #Box Widths = 168, 242, 119 self.graph = { "NEW GAME": { "up": "CONFIG", "down": "LOAD", "value": (self.dimensions[0] - 200, self.dimensions[1] - 4.6 * self.dimensions[1] / 20 + 5), "next": "Normal", "previous": "" }, #Controls where the controller should take the cursor "LOAD": { "up": "NEW GAME", "down": "CONFIG", "value": (self.dimensions[0] - 200, self.dimensions[1] - 3.2 * self.dimensions[1] / 20 + 5), "next": "save1", "previous": "" }, "CONFIG": { "up": "LOAD", "down": "NEW GAME", "value": (self.dimensions[0] - 200, self.dimensions[1] - 1.8 * self.dimensions[1] / 20 + 5), "next": "Sound", "previous": "" }, "Normal": { "up": "Extreme", "down": "Hard", "value": (self.dimensions[0] * 0.25 + 75, boxShift), "next": "save1", "previous": "NEW GAME" }, "Hard": { "up": "Normal", "down": "Extreme", "value": (self.dimensions[0] * 0.25 + 75, 45 + boxShift), "next": "save1", "previous": "NEW GAME" }, "Extreme": { "up": "Hard", "down": "Normal", "value": (self.dimensions[0] * 0.25 + 75, 90 + boxShift), "next": "save1", "previous": "NEW GAME" }, "save1": { "up": "save6", "down": "save2", "value": (self.dimensions[0] * 0.12, self.dimensions[1] * 0.25), "next": "Yes", "previous": "" }, "save2": { "up": "save1", "down": "save3", "value": (self.dimensions[0] * 0.12, self.dimensions[1] * 0.35), "next": "Yes", "previous": "" }, "save3": { "up": "save2", "down": "save4", "value": (self.dimensions[0] * 0.12, self.dimensions[1] * 0.45), "next": "Yes", "previous": "" }, "save4": { "up": "save3", "down": "save5", "value": (self.dimensions[0] * 0.12, self.dimensions[1] * 0.55), "next": "Yes", "previous": "" }, "save5": { "up": "save4", "down": "save6", "value": (self.dimensions[0] * 0.12, self.dimensions[1] * 0.65), "next": "Yes", "previous": "" }, "save6": { "up": "save5", "down": "save1", "value": (self.dimensions[0] * 0.12, self.dimensions[1] * 0.75), "next": "Yes", "previous": "" }, "Sound": { "up": "Custom Cursor", "down": "Music", "value": (self.dimensions[0] * 0.25 + 112, boxShift), "next": "Sound", "previous": "CONFIG" }, "Music": { "up": "Sound", "down": "Custom Cursor", "value": (self.dimensions[0] * 0.25 + 112, 45 + boxShift), "next": "Music", "previous": "CONFIG" }, "Custom Cursor": { "up": "Music", "down": "Sound", "value": (self.dimensions[0] * 0.25 + 112, 90 + boxShift), "next": "Custom Cursor", "previous": "CONFIG" }, "Yes": { "up": "No", "down": "No", "value": (self.dimensions[0] * 0.25 + 56.5, boxShift2), "next": "", "previous": "" }, "No": { "up": "Yes", "down": "Yes", "value": (self.dimensions[0] * 0.25 + 56.5, 45 + boxShift2), "next": "", "previous": "" }, } self.currentNode = "NEW GAME" #Gradients self.gradient = pygame.Surface(self.dimensions, pygame.SRCALPHA) optionsRect = (self.dimensions[0] * 0.4, self.dimensions[1] / 20 ) #Constant for ui sizes self.rectangleSurface = pygame.Surface(optionsRect, pygame.SRCALPHA) self.squareSurface = pygame.Surface((optionsRect[1], optionsRect[1]), pygame.SRCALPHA) self.highlightSurface = pygame.Surface( (optionsRect[1] * 0.6, optionsRect[1] * 0.6), pygame.SRCALPHA) self.lineSurface = pygame.Surface((self.dimensions[0] - 30, 3), pygame.SRCALPHA) self.uiBox = pygame.Surface( (self.dimensions[0], self.dimensions[1] / 3), pygame.SRCALPHA) self.uiBox.fill((0, 0, 0, 170)) self.saveImage = pygame.Surface( (self.dimensions[0] * 0.8, self.dimensions[1] * 0.1), pygame.SRCALPHA) self.saveImageLine = pygame.Surface((self.dimensions[0] * 0.8, 3), pygame.SRCALPHA) #draw main option pre-calculations self.yCalculations = [ self.dimensions[1] - 1.8 * optionsRect[1], self.dimensions[1] - 3.2 * optionsRect[1], self.dimensions[1] - 4.6 * optionsRect[1] ] self.mainOptionCalculations = [ ((self.dimensions[0] + 1 - optionsRect[0], self.yCalculations[0]), (30, self.yCalculations[0]), (30 + optionsRect[1] * 0.2, self.yCalculations[0] + optionsRect[1] * 0.2), (30, self.yCalculations[0] + optionsRect[1] - 3)), ((self.dimensions[0] + 1 - optionsRect[0], self.yCalculations[1]), (30, self.yCalculations[1]), (30 + optionsRect[1] * 0.2, self.yCalculations[1] + optionsRect[1] * 0.2), (30, self.yCalculations[1] + optionsRect[1] - 3)), ((self.dimensions[0] + 1 - optionsRect[0], self.yCalculations[2]), (30, self.yCalculations[2]), (30 + optionsRect[1] * 0.2, self.yCalculations[2] + optionsRect[1] * 0.2), (30, self.yCalculations[2] + optionsRect[1] - 3)) ] #draw ui box pre-calculations self.uiBoxCalculations = [ (0, self.dimensions[1] / 3), ((0, self.dimensions[1] / 3 - 5), (self.dimensions[0], self.dimensions[1] / 3 - 5)), ((0, 2 * self.dimensions[1] / 3), (self.dimensions[0], 2 * self.dimensions[1] / 3)), (self.dimensions[0] / 2, 2 * self.dimensions[1] / 3 - 25), (self.dimensions[0] / 2, self.dimensions[1] / 3 + 30), self.dimensions[1] / 3 - 110, self.dimensions[1] / 3 + 70, self.dimensions[0] * 0.25, ] #load menu pre-calculations self.loadMenuCalculations = [ pygame.Rect(self.dimensions[0] * 0.1, self.dimensions[1] * 0.2, self.dimensions[0] * 0.8, self.dimensions[1] * 0.6), self.dimensions[1] * 0.1, pygame.Rect(self.dimensions[0] * 0.1, self.dimensions[1] * 0.2, self.dimensions[0] * 0.8, self.dimensions[1] * 0.1), pygame.Rect(self.dimensions[0] * 0.1, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.1, self.dimensions[0] * 0.8, self.dimensions[1] * 0.1), pygame.Rect(self.dimensions[0] * 0.1, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.2, self.dimensions[0] * 0.8, self.dimensions[1] * 0.1), pygame.Rect(self.dimensions[0] * 0.1, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.3, self.dimensions[0] * 0.8, self.dimensions[1] * 0.1), pygame.Rect(self.dimensions[0] * 0.1, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.4, self.dimensions[0] * 0.8, self.dimensions[1] * 0.1), pygame.Rect(self.dimensions[0] * 0.1, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.5, self.dimensions[0] * 0.8, self.dimensions[1] * 0.1), ] self.loadMenuYCalculations = [ self.dimensions[1] * 0.2, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.1, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.2, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.3, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.4, self.dimensions[1] * 0.2 + self.dimensions[1] * 0.5, ] self.stage = "startUp1" self.clock = 0 self.closing = False self.open = True