def nextLevel(self): self.level += 1 if self.level != 1: if self.level % 2 == 0: if self.enemyFireChance > 20: self.enemyFireChance -= 2 self.setGrid(16 + self.level / 2, self.level / 2) else: self.setGrid(16 + (self.level - 1) / 2, self.level // 2 + 1) if self.level == 5: self.soundManager.playSound("LevelUp.ogg", 2) for player in range(len(self.playerList)): self.playerList[player].image = "ship" + str( player + 1) + "upgrade2" self.playerList[player].anim = Animate( self.sprites.getSprite(self.playerList[player].image), 1, 1, 32, 32, 2) elif self.level == 10: self.soundManager.playSound("LevelUp.ogg", 2) for player in range(len(self.playerList)): self.playerList[player].image = "ship" + str( player + 1) + "upgrade3" self.playerList[player].anim = Animate( self.sprites.getSprite(self.playerList[player].image), 1, 1, 32, 32, 2) if self.level % 2 == 1: self.playerList[self.clientPlayerNum].missileCap += 1
def nextLevel(self): self.enemyGrid = [] self.level += 1 if self.level == 5: self.soundManager.playSound("LevelUp.ogg") self.player.image = "ship1upgrade2" self.player.anim = Animate( self.sprites.getSprite(self.player.image), 1, 1, 32, 32, 2) elif self.level == 10: self.soundManager.playSound("LevelUp.ogg") self.player.image = "ship1upgrade3" self.player.anim = Animate( self.sprites.getSprite(self.player.image), 1, 1, 32, 32, 2) if self.level % 2 == 0: if self.enemyFireChance > 20: self.enemyFireChance -= 2 self.setGrid(16 + self.level / 2, self.level / 2) else: self.setGrid(16 + (self.level - 1) / 2, self.level // 2 + 1) #for row in range(self.enemyRowCount): # for column in range(self.enemyColumnCount): # if self.level % 2 == 0: # self.enemyGrid[row][column].speed += self.level * 5 # else: # rnum = random.randint(self.level - 1, self.level) # self.enemyGrid[row][column].health = rnum if self.level % 2 == 1: self.player.missileCap += 1
def checkMissiles(self): numMissiles = 0 while numMissiles < len(self.missiles): self.missiles[numMissiles].update() attacker = self.missiles[numMissiles].owner #1 is the player's missile shots if attacker == 1: if ((self.missiles[numMissiles].posy + self.missiles[numMissiles].imageh) < 0): self.missiles.pop(numMissiles) self.player.missileCount -= 1 else: self.checkHit(numMissiles) #-1 is the enemy's missile shots elif attacker == -1: if (self.missiles[numMissiles].collider.colliderect( self.player.collider)): self.player.lives -= 1 self.player.anim = Animate( self.sprites.getSprite(self.player.image + "blink"), 6, 6, 32, 32, 2, False) enemyGridPos = self.missiles.pop(numMissiles).getEnemyPos() self.enemyGrid[enemyGridPos[0]][ enemyGridPos[1]].missileCount -= 1 elif ((self.missiles[numMissiles].posy) > self.screenh): enemyGridPos = self.missiles.pop(numMissiles).getEnemyPos() self.enemyGrid[enemyGridPos[0]][ enemyGridPos[1]].missileCount -= 1 numMissiles += 1
def checkHit(self, numMissiles): for row in range(self.enemyRowCount): for column in range(self.enemyColumnCount): if self.enemyGrid[row][column].health != 0: if self.enemyGrid[row][column].collider.colliderect( self.missiles[numMissiles].collider): attacker = self.missiles.pop(numMissiles).owner self.enemyGrid[row][column].health -= 1 self.player.missileCount -= 1 if self.enemyGrid[row][ column].health == 0 and not self.enemyGrid[ row][column].dead: self.enemyGrid[row][column].dead = True self.enemyGrid[row][column].anim = Animate( self.sprites.getSprite( self.enemyGrid[row][column].type[:6] + "DeathSpriteSheet"), 3, 3, 32, 32, 2, False) self.enemyCount -= 1 if self.enemyGrid[row][ column].type == "Alien4SpriteSheet": self.player.score += (100 * self.level) * 10 elif self.enemyGrid[row][ column].type != "Alien3SpriteSheet": self.player.score += (100 * self.level) * 2 else: self.player.score += 100 * self.level return
def update(self): if self.start: if pygame.time.get_ticks() >= self.startTime + 100: self.soundManager.playSound("Enemy_entrance.ogg") pygame.time.delay(2000) self.soundManager.playNewMusic("ScumInvaders.ogg") self.start = False self.keyUpdate() if not self.paused: self.backgroundUpdate() self.player.anim.update() if self.player.anim.done: self.player.anim = Animate( self.sprites.getSprite(self.player.image), 1, 1, 32, 32, 2) self.state = self.enemyUpdate() if self.checkState(): return self.state self.checkMissiles() self.state = self.checkPlayerLives() if self.checkState(): return self.state self.checkEnemyCount() else: return self.mouseUpdate() return self.state
def __init__(self, screen, screenw, screenh, spriteList, soundManager): self.sprites = spriteList self.screen = screen self.screenw = screenw self.screenh = screenh self.soundManager = soundManager self.player = Player( 1, "ship1", "missile1", (500, 700), 32, 32, Animate(self.sprites.getSprite("ship1"), 1, 1, 32, 32, 10)) #self.player = Player(player, "ship" + str(player), "missile"+ str(player + 1), (200 * player,700), 32, 32) self.paused = False self.start = True self.level = 1 self.background = "GameBackground" self.background2 = "GameBackground" self.enemyGrid = [] self.enemyRowCount = 5 self.enemyColumnCount = 10 self.enemyCount = 50 #self.player.score = 0 self.setGrid() self.missiles = [] self.missileDelay = 100 self.enemyDelay = 100 self.enemyFireChance = 100 self.nextMissile = pygame.time.get_ticks() + self.missileDelay self.nextEnemyMove = pygame.time.get_ticks() + self.enemyDelay self.bgHeight = 1536 self.currentBG1Height = 0 self.currentBG2Height = -self.bgHeight self.state = "Game" self.keyDelay = 500 self.nextKeyInput = pygame.time.get_ticks() self.fontsize = 30 self.font = pygame.font.Font( os.path.join('Fonts', 'nasalization-rg.ttf'), self.fontsize) self.pauseButtons = [] self.pauseButtons.append( Button(screen, self.sprites.getSprite("exit"), self.sprites.getSprite("exitHighlighted"), 368, 330, 281, 68, "Menu", 'Exit.ogg', soundManager)) self.mouseDelay = 100 self.mouseNext = pygame.time.get_ticks() random.seed(datetime.now()) self.startTime = None
def reset(self): self.player = Player( 1, "ship1", "missile2", (500, 700), 32, 32, Animate(self.sprites.getSprite("ship1"), 1, 1, 32, 32, 10)) self.enemyGrid = [] self.missiles = [] self.enemyCount = 50 self.setGrid() self.level = 1 self.player.score = 0 self.state = "Game" self.paused = False self.start = True self.startTime = pygame.time.get_ticks()
def checkHit(self, numMissiles): for row in range(self.enemyRowCount): for column in range(self.enemyColumnCount): if self.enemyGrid[row][column].health > 0: if self.enemyGrid[row][column].collider.colliderect( self.missiles[numMissiles].collider): attacker = self.missiles.pop(numMissiles).owner if attacker == self.clientPlayerNum: self.enemyGrid[row][column].health -= 1 self.socket.send("HIT:" + self.hostName + ":" + "ENEMY:" + str(row) + ":" + str(column)) self.playerList[attacker].missileCount -= 1 if self.enemyGrid[row][ column].health == 0 and not self.enemyGrid[ row][column].dead: self.enemyGrid[row][column].dead = True #self.socket.send("DEATH:" + self.hostName + ":" + "ENEMY:" + str(row) + ":" + str(column)) self.enemyGrid[row][column].anim = Animate( self.sprites.getSprite( self.enemyGrid[row][column].type[:6] + "DeathSpriteSheet"), 3, 3, 32, 32, 2, False) self.enemyCount -= 1 if self.enemyGrid[row][ column].type == "Alien4SpriteSheet": self.playerList[ self.clientPlayerNum].score += ( 100 * self.level) * 10 elif self.enemyGrid[row][ column].type != "Alien3SpriteSheet": self.playerList[ self.clientPlayerNum].score += ( 100 * self.level) * 2 else: self.playerList[ self. clientPlayerNum].score += 100 * self.level return
def setGrid(self, speed=16, health=1): for row in range(self.enemyRowCount): self.enemyGrid.append([]) for column in range(self.enemyColumnCount): rnum = random.randint(1, 100) enemySprite = "Alien1SpriteSheet" if rnum <= 45: enemySprite = "Alien2SpriteSheet" elif rnum >= 90 and rnum < 98: enemySprite = "Alien3SpriteSheet" elif rnum >= 98: enemySprite = "Alien4SpriteSheet" self.enemyGrid[row].append( Enemy((32 + (column * 96), (row * 64) - self.enemyRowCount * 64), 32, 32, Animate(self.sprites.getSprite(enemySprite), 2, 2, 32, 32, 10), health, speed, row, column, enemySprite))
def checkMissiles(self): numMissiles = 0 while numMissiles < len(self.missiles): self.missiles[numMissiles].update() attacker = self.missiles[numMissiles].owner #1 is the player's missile shots if attacker >= 0: if ((self.missiles[numMissiles].posy + self.missiles[numMissiles].imageh) <= 0): #self.socket.send("SHOOT:" + self.hostName + ":" + str(self.clientPlayerNum)) self.missiles.pop(numMissiles) self.playerList[attacker].missileCount -= 1 else: if attacker == self.clientPlayerNum: self.checkHit(numMissiles) #-1 is the enemy's missile shots elif attacker == -1: if (self.missiles[numMissiles].collider.colliderect( self.playerList[self.clientPlayerNum].collider)): self.playerList[self.clientPlayerNum].lives -= 1 self.playerList[self.clientPlayerNum].anim = Animate( self.sprites.getSprite( self.playerList[self.clientPlayerNum].image + "blink"), 6, 6, 32, 32, 2, False) #self.socket.send("HIT:" + str(self.clientPlayerNum)) enemyGridPos = self.missiles.pop(numMissiles).getEnemyPos() self.enemyGrid[enemyGridPos[0]][ enemyGridPos[1]].missileCount -= 1 elif ((self.missiles[numMissiles].posy) >= self.screenh): enemyGridPos = self.missiles.pop(numMissiles).getEnemyPos() self.enemyGrid[enemyGridPos[0]][ enemyGridPos[1]].missileCount -= 1 numMissiles += 1
def setGrid(self, speed=16, health=1): self.enemyGrid = [] self.enemyCount = 50 rNums = self.serverGridInfo["TYPES"] for row in range(self.enemyRowCount): self.enemyGrid.append([]) for column in range(self.enemyColumnCount): rnum = rNums[row][column] enemySprite = "Alien1SpriteSheet" if rnum <= 45: enemySprite = "Alien2SpriteSheet" elif rnum >= 90 and rnum < 98: enemySprite = "Alien3SpriteSheet" elif rnum >= 98: enemySprite = "Alien4SpriteSheet" self.enemyGrid[row].append( Enemy((32 + (column * 96), (row * 64) - self.enemyRowCount * 64), 32, 32, Animate(self.sprites.getSprite(enemySprite), 2, 2, 32, 32, 10), health, speed, row, column, enemySprite))
def update(self): try: if self.serverReady: pass #if self.clientPlayerNum == 0: # self.sendGrid() else: if self.clientPlayerNum == 0: self.socket.send("GETGAMESTART:" + self.hostName) message, serverAddress = self.socket.clientSocket.recvfrom(2048) modifiedMessage = message.decode() #if "ENEMYGRID" in modifiedMessage: # data = json.loads(modifiedMessage[10:]) # self.readGrid(data) modifiedMessage = modifiedMessage.split(":") #rather than print, maybe write to a file to check what's going on since dunno where prints are printing out... print('right before offset code stuff for test') if 'serversendtime' in modifiedMessage: #okay, server sent message but it does not detect print('client received message') #calculate offset clientreceivetime = time.time() serverclientoffset = abs( double(clientreceivetime) - modifiedMessage[modifiedMessage.index('serversendtime') + 1]) #send info back to server to store self.socket.send('serverclientoffset: ' + str(serverclientoffset) + ' clientsendtime: ' + str(time.time())) print('client sent message') #print(modifiedMessage) if modifiedMessage[0] == "GAMESTART": self.serverReady = True if modifiedMessage[0] == "MOV": if int(modifiedMessage[1]) != self.clientPlayerNum: self.playerList[int(modifiedMessage[1])].posx = int( modifiedMessage[2]) if modifiedMessage[0] == "SHOOT": if int(modifiedMessage[3]) != self.clientPlayerNum: self.missiles.append( Missile( int(modifiedMessage[3]), self.playerList[int( modifiedMessage[3])].missileImage, (int(modifiedMessage[1]) + self.playerList[int(modifiedMessage[3])].imagew - 18, int(modifiedMessage[2]) - self.playerList[int(modifiedMessage[3])].imageh), 8, 32) ) #(self.playerList[int(modifiedMessage[3])].posx + (self.playerList[int(modifiedMessage[3])].imagew - 18), self.playerList[int(modifiedMessage[3])].posy - (self.playerList[int(modifiedMessage[3])].imageh)), 8, 32)) if modifiedMessage[0] == "HIT": if modifiedMessage[2] == "ENEMY": self.enemyGrid[int(modifiedMessage[3])][int( modifiedMessage[4])].health -= 1 if self.enemyGrid[int(modifiedMessage[3])][int( modifiedMessage[4] )].health == 0 and not self.enemyGrid[int( modifiedMessage[3])][int(modifiedMessage[4])].dead: self.enemyGrid[int(modifiedMessage[3])][int( modifiedMessage[4])].dead = True #self.socket.send("DEATH:" + self.hostName + ":" + "ENEMY:" + str(row) + ":" + str(column)) self.enemyGrid[int(modifiedMessage[3])][int( modifiedMessage[4])].anim = Animate( self.sprites.getSprite( self.enemyGrid[int(modifiedMessage[3])][ int(modifiedMessage[4])].type[:6] + "DeathSpriteSheet"), 3, 3, 32, 32, 2, False) self.enemyCount -= 1 except Exception as error: if type(error) != BlockingIOError: mod = ' '.join(str(type(error))) with open("Log.txt", "a") as f: f.write(mod) if self.serverReady: if self.start: if pygame.time.get_ticks() >= self.startTime + 100: self.soundManager.playSound("Enemy_entrance.ogg") pygame.time.delay(2000) self.soundManager.playNewMusic( "ScumInvadersTheme(Final).ogg", .2) self.start = False self.state = self.enemyUpdate() if self.checkState(): return self.state self.checkEnemyCount() self.keyUpdate() self.backgroundUpdate() self.checkMissiles() self.state = self.checkPlayerLives() if self.checkState(): return self.state if self.paused: return self.mouseUpdate() return self.state
def checkPlayerLives(self): if (self.player.lives <= 0): self.player.anim = Animate(self.sprites.getSprite("shipexplode"), 3, 3, 32, 32, 2, False) return "Menu" return "Game"
def __init__(self, screen, screenw, screenh, spriteList, soundManager, numPlayers, clientPlayerNum, ip, hostName, socket): self.sprites = spriteList self.hostName = hostName self.clientPlayerNum = clientPlayerNum self.numPlayers = numPlayers self.screen = screen self.screenw = screenw self.screenh = screenh self.soundManager = soundManager self.playerList = [] self.playerList.append( Player(0, "ship1", "missile1", (300, 700), 32, 32, Animate(self.sprites.getSprite("ship1"), 1, 1, 32, 32, 10))) if numPlayers > 1: self.playerList.append( Player( 1, "ship2", "missile2", (400, 700), 32, 32, Animate(self.sprites.getSprite("ship2"), 1, 1, 32, 32, 10))) if numPlayers > 2: self.playerList.append( Player( 2, "ship3", "missile3", (500, 700), 32, 32, Animate(self.sprites.getSprite("ship3"), 1, 1, 32, 32, 10))) if numPlayers > 3: self.playerList.append( Player( 3, "ship4", "missile4", (600, 700), 32, 32, Animate(self.sprites.getSprite("ship4"), 1, 1, 32, 32, 10))) self.paused = False self.start = True self.level = 0 self.background = "GameBackground" self.background2 = "GameBackground" self.enemyGrid = [] self.serverGridInfo = {} self.enemyRowCount = 5 self.aliveButDead = True self.enemyColumnCount = 10 self.enemyCount = 50 #self.player.score = 0 self.missiles = [] self.missileDelay = 100 self.enemyDelay = 100 self.enemyFireChance = 100 self.nextMissile = pygame.time.get_ticks() + self.missileDelay self.nextEnemyMove = pygame.time.get_ticks() + self.enemyDelay self.bgHeight = 1536 self.currentBG1Height = 0 self.currentBG2Height = -self.bgHeight self.state = "multiGame" self.keyDelay = 500 self.nextKeyInput = pygame.time.get_ticks() self.fontsize = 30 self.font = pygame.font.Font( os.path.join('Fonts', 'nasalization-rg.ttf'), self.fontsize) self.pauseButtons = [] self.pauseButtons.append( Button(screen, self.sprites.getSprite("exit"), self.sprites.getSprite("exitHighlighted"), 368, 330, 281, 68, "Lobby", 'Exit.ogg', soundManager)) self.mouseDelay = 100 self.mouseNext = pygame.time.get_ticks() self.serverReady = False random.seed(datetime.now()) self.startTime = pygame.time.get_ticks() #for server self.socket = socket print("IM IN GAME")
def update(self): message = None while message != "": message = self.socket.receive() #if "ENEMYGRID" in modifiedMessage: # data = json.loads(modifiedMessage[10:]) # self.readGrid(data) if message != None: modifiedMessage = message.split(":") #print(modifiedMessage) if message[:4] == "GRID": self.serverGridInfo = json.loads(message[5:]) self.setGrid() print("GOTGRID") if modifiedMessage[0] == "GAMESTART": self.serverReady = True self.nextLevel() print("GAMESTART") elif modifiedMessage[0] == "MOV": #print(modifiedMessage) if int(modifiedMessage[1]) != self.clientPlayerNum: self.playerList[int(modifiedMessage[1])].posx = int( modifiedMessage[2]) elif modifiedMessage[0] == "DEATH": self.playerList[int(modifiedMessage[1])].anim = Animate( self.sprites.getSprite("shipexplode"), 3, 3, 32, 32, 10, False) self.playerList[int(modifiedMessage[1])].alive = False elif modifiedMessage[0] == "SHOOT": if int(modifiedMessage[3]) != self.clientPlayerNum: self.missiles.append( Missile( int(modifiedMessage[3]), self.playerList[int( modifiedMessage[3])].missileImage, (int(modifiedMessage[1]) + self.playerList[int( modifiedMessage[3])].imagew - 18, int(modifiedMessage[2]) - self.playerList[int( modifiedMessage[3])].imageh), 8, 32) ) #(self.playerList[int(modifiedMessage[3])].posx + (self.playerList[int(modifiedMessage[3])].imagew - 18), self.playerList[int(modifiedMessage[3])].posy - (self.playerList[int(modifiedMessage[3])].imageh)), 8, 32)) elif modifiedMessage[0] == "HIT": if modifiedMessage[1] == "ENEMY": #print(modifiedMessage) self.enemyGrid[int(modifiedMessage[2])][int( modifiedMessage[3])].health -= 1 if self.enemyGrid[int(modifiedMessage[2])][int( modifiedMessage[3] )].health == 0 and not self.enemyGrid[int( modifiedMessage[2])][int( modifiedMessage[3])].dead: self.enemyGrid[int(modifiedMessage[2])][int( modifiedMessage[3])].dead = True #self.socket.send("DEATH:" + self.hostName + ":" + "ENEMY:" + str(row) + ":" + str(column)) self.enemyGrid[int(modifiedMessage[2])][int( modifiedMessage[3])].anim = Animate( self.sprites.getSprite( self.enemyGrid[int(modifiedMessage[2])] [int(modifiedMessage[3])].type[:6] + "DeathSpriteSheet"), 3, 3, 32, 32, 2, False) self.enemyCount -= 1 #elif modifiedMessage[0] == "NEXTLEVEL": # self.serverGridInfo = json.loads(message[5:]) if self.serverReady: if self.start: if pygame.time.get_ticks() >= self.startTime + 100: self.soundManager.playSound("Enemy_entrance.ogg") pygame.time.delay(2000) self.soundManager.playNewMusic("ScumInvaders.ogg") self.start = False self.state = self.enemyUpdate() if self.checkState(): return self.state self.checkEnemyCount() for player in self.playerList: if player.alive: player.anim.update() for player in self.playerList: if player.alive: if player.anim.done: player.anim = Animate( self.sprites.getSprite( self.playerList[self.clientPlayerNum].image), 1, 1, 32, 32, 2, False) self.keyUpdate() self.backgroundUpdate() self.checkMissiles() self.state = self.checkPlayerLives() if self.checkState(): return self.state if self.paused: return self.mouseUpdate() return self.state