def makeMap(screen, maze, MyShield): line = "" allLines = [] deltaX = 0 deltaY = 0 file = open("pacmanportalmaze.txt", "r") if file.mode == 'r': contents = file.read() for chars in contents: if chars != '\n': line += chars else: allLines.append(line) line = "" for line in allLines: for char in line: if char == 'X': newBlock = Blocks(screen) newBlock.rect.x, newBlock.rect.y = deltaX, deltaY maze.add(newBlock) elif char == 'o': newShield = Shield(screen) newShield.rect.x, newShield.rect.y = deltaX, (deltaY) MyShield.add(newShield) deltaX += 13 deltaX = 0 deltaY += 13
def give_shield(self, shield): data = Shields.array[shield] # Manage Enchantment + brand spawned_enchantment = data[5] if d(10) + (1.5 * self.tier) > 10 and shield not in Weapons.legendaries: spawned_enchantment += d(int(max(1, self.tier / 2))) - 1 try: brand = data[6] except: brand = None # Create Shield Object self.wielding.append(Shield(self.game,shield, data[0], data[1], data[2], data[3], data[4],spawned_enchantment, None, brand))
def place_shield(self, armor, loc, enchantment=0, brand=None): data = Shields.array[armor] # Manage Enchantment spawned_enchantment = data[5] + enchantment try: brand = data[6] except IndexError: pass # Create Shield Object self.game.items.append( Shield(self.game, armor, data[0], data[1], data[2], data[3], data[4], spawned_enchantment, loc, brand))
def __init__(self, speed, size, width, height, fps): images = [] images.append(pygame.image.load('res/image/player.png')) images.append(pygame.image.load('res/image/player2.png')) super().__init__(size[0]/2 - width/2, size[1] - height, width, height, images, fps, True) self.lives = 3 self.isShield = False self.shield = Shield(0, 0, 70, 70, 1) self.timer = 0 self.speed = speed self.shieldExpended = False self.parentSize = size self.isMoveLeft = False self.isMoveRight = False self.isMoveUp = False self.isMoveDown = False images2 = [] images2.append(pygame.image.load('res/image/exp1.png')) images2.append(pygame.image.load('res/image/exp2.png')) images2.append(pygame.image.load('res/image/exp3.png')) images2.append(pygame.image.load('res/image/exp4.png')) images2.append(pygame.image.load('res/image/exp5.png')) images2.append(pygame.image.load('res/image/exp6.png')) images2.append(pygame.image.load('res/image/exp7.png')) images2.append(pygame.image.load('res/image/exp8.png')) images2.append(pygame.image.load('res/image/exp9.png')) images2.append(pygame.image.load('res/image/exp10.png')) images2.append(pygame.image.load('res/image/exp11.png')) images2.append(pygame.image.load('res/image/exp12.png')) images2.append(pygame.image.load('res/image/exp13.png')) images2.append(pygame.image.load('res/image/exp14.png')) images2.append(pygame.image.load('res/image/exp15.png')) images2.append(pygame.image.load('res/image/exp16.png')) images2.append(pygame.image.load('res/image/exp17.png')) self.explosion = Sprite(images2, 15, False)
class Player(GameObject): def __init__(self, speed, size, width, height, fps): images = [] images.append(pygame.image.load('res/image/player.png')) images.append(pygame.image.load('res/image/player2.png')) super().__init__(size[0]/2 - width/2, size[1] - height, width, height, images, fps, True) self.lives = 3 self.isShield = False self.shield = Shield(0, 0, 70, 70, 1) self.timer = 0 self.speed = speed self.shieldExpended = False self.parentSize = size self.isMoveLeft = False self.isMoveRight = False self.isMoveUp = False self.isMoveDown = False images2 = [] images2.append(pygame.image.load('res/image/exp1.png')) images2.append(pygame.image.load('res/image/exp2.png')) images2.append(pygame.image.load('res/image/exp3.png')) images2.append(pygame.image.load('res/image/exp4.png')) images2.append(pygame.image.load('res/image/exp5.png')) images2.append(pygame.image.load('res/image/exp6.png')) images2.append(pygame.image.load('res/image/exp7.png')) images2.append(pygame.image.load('res/image/exp8.png')) images2.append(pygame.image.load('res/image/exp9.png')) images2.append(pygame.image.load('res/image/exp10.png')) images2.append(pygame.image.load('res/image/exp11.png')) images2.append(pygame.image.load('res/image/exp12.png')) images2.append(pygame.image.load('res/image/exp13.png')) images2.append(pygame.image.load('res/image/exp14.png')) images2.append(pygame.image.load('res/image/exp15.png')) images2.append(pygame.image.load('res/image/exp16.png')) images2.append(pygame.image.load('res/image/exp17.png')) self.explosion = Sprite(images2, 15, False) def update(self, gameTime): super().update(gameTime) if self.isMoveUp and self.y > 0: self.y -= self.speed elif self.isMoveDown and self.y + self.height < self.parentSize[1]: self.y += self.speed if self.isMoveLeft and self.x > 0: self.x -= self.speed elif self.isMoveRight and self.x + self.width < self.parentSize[0]: self.x += self.speed if self.isShield: self.shield.x = self.x - 3 self.shield.y = self.y - 3 self.timer += 1 if self.timer > self.shield.timeLimit: self.isShield = False self.shieldExpended = True def draw(self, surface): super().draw(surface) if self.isShield: surface.blit(self.shield.getImage(), self.shield.getRect()) def explode(self): self.setSprite(self.explosion) def moveUp(self, move): if move: self.isMoveUp = True self.isMoveDown = False else: self.isMoveUp = False def moveDown(self, move): if move: self.isMoveUp = False self.isMoveDown = True else: self.isMoveDown = False def moveLeft(self, move): if move: self.isMoveRight = False self.isMoveLeft = True else: self.isMoveLeft = False def moveRight(self, move): if move: self.isMoveRight = True self.isMoveLeft = False else: self.isMoveRight = False
def gameLoop(self): #game loop while self.runningGame: #if the game is in the main menu if self.inMainMenu: #read in the controls for the start menu self.readMainMenuControls() #controls menu access from main menu if self.inControlMenu: self.controlMenu() else: self.mainMenu() #if the game is paused elif self.pausedGame: self.pauseMenu() self.readGameControls() #if you lose the game - display game over screen elif self.gameOver: self.gameOverMenu() self.readGameOverControls() #whilst the game is not paused or in the main menu or on the game over screen if (not self.pausedGame and not self.inMainMenu and not self.gameOver): #read controls for the player ship self.readGameControls() #if the player runs out of lives - game over if self.player.lives == -1: self.gameOver = True self.gameOverSound.play() #if there are <=5 enemies left, spawn more if (self.enemiesRemaining <= 5): self.spawnEnemies() #adjust the current coords of a shield, if the player has it if self.hasShield: self.shield.updateLocation( (self.player.rect.x + 15, self.player.rect.y - 2)) #move the background image - yTranslation if self.changeFrame: self.yScaler += 1 self.changeFrame = False #moves every other frame else: self.changeFrame = True if self.yScaler == 1400: self.yScaler = 0 #sprite collision detection between sprites and projectiles for projectile in self.projectileList: #for every collision, remove the sprite and increase player score self.hitList = pygame.sprite.spritecollide( projectile, self.enemyList, True) #if the projectile goes out of bounds - remove it if projectile.rect.y <= -20 or projectile.rect.x <= -10 or projectile.rect.x >= 400: projectile.kill() #for every projectile that hits an enemy for hits in self.hitList: #killing enemy has a 1/20 chance of producing a power up self.tokenChance = random.randrange(0, 20) if (self.tokenChance == 0): self.levelUpToken = PowerUp( projectile.rect.center, random.randrange(1, 6)) self.tokenList.add(self.levelUpToken) self.spriteList.add(self.levelUpToken) #remove projectile, reduce enemy count and increase score projectile.kill() self.playerScore.increase() self.playerScoreShadow.increase() self.shipExplosion.play() self.enemiesRemaining -= 1 #enemy random fire pattern - unpredictable for enemyShip in self.enemyList: #if they reach the bottom of the map - remove enemy if enemyShip.rect.y >= 700: enemyShip.kill() self.enemiesRemaining -= 1 #each enemy will have a 1/300 chance of firing a projectile at the player self.number = random.randrange(0, 300) if (self.number == 5): self.enemyProjectile = EnemyProjectile( (enemyShip.rect.x, enemyShip.rect.y)) self.spriteList.add(self.enemyProjectile) self.enemyProjectileList.add(self.enemyProjectile) #hit detection of enemy projectiles with player self.enemyProjectileCollideList = pygame.sprite.spritecollide( self.player, self.enemyProjectileList, True) #reduce health if hit self.player.takeHit(5 * len(self.enemyProjectileCollideList)) self.enemiesRemaining -= len(self.enemyProjectileCollideList) self.healthBar.newHealth(self.player.health) self.healthBarShadow.newHealth(self.player.health) self.playerLives.newLives(self.player.lives) self.playerLivesShadow.newLives(self.player.lives) #if enemy ship collides with player ship self.enemyShipCollideList = pygame.sprite.spritecollide( self.player, self.enemyList, True) #reduce player health if collides with enemy ship self.player.takeHit(5 * len(self.enemyShipCollideList)) self.healthBar.newHealth(self.player.health) self.healthBarShadow.newHealth(self.player.health) self.playerLives.newLives(self.player.lives) self.playerLivesShadow.newLives(self.player.lives) #collision of enemy projectiles and shields for enemyProjectile in self.enemyProjectileList: #if enemy projectile goes out of range remove it if enemyProjectile.rect.y >= 610: enemyProjectile.kill() #remove shield if it is hit by a projectile if self.hasShield and pygame.sprite.collide_rect( self.shield, enemyProjectile): enemyProjectile.kill() self.shield.kill() self.hasShield = not self.hasShield #hit detection of tokens with player ship self.tokenCollideList = pygame.sprite.spritecollide( self.player, self.tokenList, True) for token in self.tokenCollideList: #each token in the token list will grant the player a bonus #increase playerLives by one if (token.tokenType == 1): self.player.addLives(1) self.playerLives.newLives(self.player.lives) self.playerLivesShadow.newLives(self.player.lives) self.oneUp.play() #increase playerHealth by 20 elif (token.tokenType == 2): self.addHealth.play() self.player.addHealth(20) #spawn 3 projectiles that travel in 3 directions elif (token.tokenType == 3): self.triProjectileSound.play() projectile = TriProjectile( (self.player.rect.x, self.player.rect.y), 0) projectile1 = TriProjectile( (self.player.rect.x, self.player.rect.y), 1) projectile2 = TriProjectile( (self.player.rect.x, self.player.rect.y), 2) self.projectileList.add(projectile) self.projectileList.add(projectile1) self.projectileList.add(projectile2) self.spriteList.add(projectile) self.spriteList.add(projectile1) self.spriteList.add(projectile2) #spawns a shield for the player, if they don't already have one elif (token.tokenType == 4 and not self.hasShield): self.shieldSound.play() self.shield = Shield( (self.player.rect.x + 15, self.player.rect.y - 2)) self.spriteList.add(self.shield) self.hasShield = True #drops a bomb that destroys all enemy ships elif (token.tokenType == 5): self.enemiesRemaining = 0 self.playerScore.increase2(25) self.playerScoreShadow.increase2(25) self.bomb.play() for enemy in self.enemyList: enemy.remove([self.spriteList, self.enemyList]) #animate the wallpaper on screen self.window.blit(self.backgroundImage, (0, self.yScaler)) self.window.blit(self.backgroundImage, (0, self.yScaler - 1400)) #animate the sprites self.spriteList.update() self.spriteList.draw(self.window) pygame.display.flip() self.spriteList.clear(self.window, self.screen) self.gameClock.tick(120)
from warrior import Warrior from Weapon import Weapon from Shield import Shield import random name1 = "The best of the best" name2 = "Superhero" warrior1 = Warrior(name1, 100, Weapon("hero's weapon", 50, 50), Shield("hero's shield", 70, (10, 20))) warrior2 = Warrior(name2, 100, Weapon("superhero's weapon", 50, 50), Shield("superhero's shield", 70, (10, 20))) anyoneAlive = True while anyoneAlive: prHp = warrior2.GetHp() anyoneAlive = not warrior1.Attack(warrior2) nowHp = warrior2.GetHp() print(name1 + " нанес удар в размере " + str(prHp - nowHp)) if (not anyoneAlive): break prHp = warrior1.GetHp() anyoneAlive = not warrior2.Attack(warrior1) nowHp = warrior1.GetHp() print(name2 + " нанес удар в размере " + str(prHp - nowHp)) if (warrior1.GetHp() <= 0): print(name2 + " победил!") else: print(name1 + " победил!")