Ejemplo n.º 1
0
 def loadNewGame(self):
     player = Player(200, 200, self.director)
     # player.setWeapon(
     # WpnBlade("wpns2.png", -1, pygame.Rect(344, 342, 28, 28), "blade_swing.wav", 0.5))
     # WpnBlade("lightsaber.png", -1, pygame.Rect(128, 77, 42, 42), "sthswng1.wav", 0.2))
     # WpnBow("items-1.png", None, pygame.Rect(0, 24, 24, 24)))
     # WpnRifle())
     # WpnLaser())
     # WpnGrenade())
     scene = LevelOneA(self.director, player)
     self.director.setScene(scene)
Ejemplo n.º 2
0
 def setup(self):
     '''
     sets up the game
     '''
     self.currentMap = self.mapList[0].getSpriteLists()
     
     for i in range(self.levelCount):
         levelAdd = Level()
         levelAdd.load(self.currentMap, i)
         self.levels.append(levelAdd)
     
     
     startX = self.currentMap['StartBlock'].sprite_list[0].center_x
     startY = self.currentMap['StartBlock'].sprite_list[0].center_y
     self.playerSprite = Player(startX, startY, 'player_imgs')
     self.playerSprite.laserPool = self.lasers
     self.characterList = self.levels[0].characters
     for char in self.characterList:
         char.target = self.playerSprite
         char.laserPool = self.lasers
     self.characterList.append(self.playerSprite)
     self.physicsEngine = PhysicsEngine(self.characterList, self.levels[self.currentLevel].noWalk)
     self.score = self.playerSprite.score
     self.set_mouse_visible(False)
Ejemplo n.º 3
0
    def __initPlayer(self):

        dictMode = {}  #will contains all information about each game mode
        file = minidom.parse("./XML_Files/InitPlayer.xml")
        modes = file.getElementsByTagName("DifficultyMode")
        for mode in modes:
            data = xmlp.parseAttributes(mode)
            dictMode[data.get('id')] = data

        #Ask the player which mode does he wants to play
        ToPrint = "Difficulty Modes: "
        for mode in dictMode.keys():
            ToPrint = ToPrint + ' ' + mode

        print(ToPrint)

        #Parse the user input
        userMode = ui.userIput("Which mode do you want to play? ",
                               dictMode.keys())
        SelectedMode = dictMode[userMode]

        ## Parse the mode's parameters
        player = Player(SelectedMode.get('hp'))

        armorName = SelectedMode.get('armor')
        if armorName is None:
            player.equipArmor(self.WI.getArmorByCategory("basic"))
        else:
            player.equipArmor(self.WI.getArmor(armorName))

        weaponName = SelectedMode.get('weapon')
        if weaponName is None:
            player.equipWeapon(self.WI.getWeaponByCategory("basic"))
        else:
            player.equipWeapon(self.WI.getWeapon(weaponName))

        potion = self.WI.getPotionByCategory("HP")
        if SelectedMode.get('potion') is not None:
            if cast.strToBool(SelectedMode.get('potion')):
                potion.fill()
        player.addToBag(potion)

        return player
Ejemplo n.º 4
0
Archivo: Game.py Proyecto: vyrval/GYATH
 def __initPlayer(self):
     
     dictMode={} #will contains all information about each game mode
     file = minidom.parse("./XML_Files/InitPlayer.xml")
     modes = file.getElementsByTagName("DifficultyMode")
     for mode in modes:
         data= xmlp.parseAttributes(mode)             
         dictMode[data.get('id')]= data
     
     #Ask the player which mode does he wants to play
     ToPrint="Difficulty Modes: "
     for mode in dictMode.keys():
         ToPrint= ToPrint + ' ' + mode
     
     print(ToPrint)
     
     #Parse the user input
     userMode = ui.userIput("Which mode do you want to play? ", dictMode.keys())
     SelectedMode = dictMode[userMode]
     
     ## Parse the mode's parameters
     player = Player(SelectedMode.get('hp'))
     
     armorName =SelectedMode.get('armor') 
     if armorName is None:
         player.equipArmor(self.WI.getArmorByCategory("basic"))
     else:
         player.equipArmor(self.WI.getArmor(armorName))
         
     weaponName = SelectedMode.get('weapon')
     if weaponName is None:
         player.equipWeapon(self.WI.getWeaponByCategory("basic"))
     else:
         player.equipWeapon(self.WI.getWeapon(weaponName))
         
     potion = self.WI.getPotionByCategory("HP")
     if SelectedMode.get('potion') is not None:
         if cast.strToBool(SelectedMode.get('potion')):  
             potion.fill()
     player.addToBag(potion)
     
     return player
Ejemplo n.º 5
0
class Game(arcade.Window):
    '''
    top level class for the game
    '''
    #constants
    mapcount = 1
    background = arcade.color.BLACK
    viewportMargin = 300
    movementSpeed = 4
    levelCount = 3
    N, NE, E, SE, S, SW, W, NW = 0, 1, 2, 3, 4, 5, 6, 7
    keyToDir = {
                0:None, 1:W, 2:E, 3:None,
                4:S, 5:SW, 6:SE, 7:S, 8:N,
                9:NW, 10:NE, 11:N, 12:None,
                13:W, 14:E, 15:None
                }


    def __init__(self, width = 1440, height = 900, title = 'Game', fullScreen = True):
        '''
        Constructor
        '''
        super().__init__(width, height, title, fullscreen=fullScreen)
        self.mapList = None
        #change later
        self.playerSprite = None
        self.mapList = [Map('map1.tmx')]
        
        self.currentMap = None
        self.currentLevel = 0
        self.levels = []
        self.characterList = None
        self.enemyList = None
        
        self.leftPressed = False
        self.rightPressed = False
        self.upPressed = False
        self.downPressed = False
        
        self.physicsEngine = None
        
        self.directionKey = 0
        
        self.viewLeft, self.viewBottom = 0, 0
        self.lasers = []
        
        self.score = 0
        self.stats = StatsScreen(self)
        
        arcade.set_background_color(self.background)        
        
    def setup(self):
        '''
        sets up the game
        '''
        self.currentMap = self.mapList[0].getSpriteLists()
        
        for i in range(self.levelCount):
            levelAdd = Level()
            levelAdd.load(self.currentMap, i)
            self.levels.append(levelAdd)
        
        
        startX = self.currentMap['StartBlock'].sprite_list[0].center_x
        startY = self.currentMap['StartBlock'].sprite_list[0].center_y
        self.playerSprite = Player(startX, startY, 'player_imgs')
        self.playerSprite.laserPool = self.lasers
        self.characterList = self.levels[0].characters
        for char in self.characterList:
            char.target = self.playerSprite
            char.laserPool = self.lasers
        self.characterList.append(self.playerSprite)
        self.physicsEngine = PhysicsEngine(self.characterList, self.levels[self.currentLevel].noWalk)
        self.score = self.playerSprite.score
        self.set_mouse_visible(False)
           
    def on_draw(self):
        '''
        draws the game elements
        '''
        arcade.start_render()
        self.levels[self.currentLevel].draw()
        for laser in self.lasers:
            laser.draw()
        self.characterList.draw()
        if self.playerSprite.target:
            arcade.draw_circle_outline(self.playerSprite.target.center_x, self.playerSprite.target.center_y, 35, arcade.color.LIME_GREEN, 4)
        self.stats.draw()
            
    def _changeLevel(self, dLevel):
        '''
        called when going up or down stairs. dLevel is -1 for going down, +1 for going up.
        changes the current level and game character list appropriately
        '''
        assert dLevel in (1, -1)
        #clear lasers
        for laser in self.lasers:
            self.lasers.remove(laser)
        #remove the player form the current level's list of characters before changing level
        self.levels[self.currentLevel].characters.remove(self.playerSprite)
        self.currentLevel += dLevel
        self.physicsEngine.walls = self.levels[self.currentLevel].noWalk
        if dLevel == -1:
            self.playerSprite.center_x = self.levels[self.currentLevel].startDownX
            self.playerSprite.center_y = self.levels[self.currentLevel].startDownY
        else:
            self.playerSprite.center_x = self.levels[self.currentLevel].startUpX
            self.playerSprite.center_y = self.levels[self.currentLevel].startUpY
        self.characterList = self.levels[self.currentLevel].characters
        #target all NPCs to player
        for char in self.characterList:
            char.target = self.playerSprite
            char.laserPool = self.lasers
        self.characterList.append(self.playerSprite)
        self.physicsEngine.characters = self.characterList
        
    def _scrollUpdate(self):
        '''
        manages screen scrolling
        '''
        changed = False

        # check scroll left
        left_bndry = self.viewLeft + self.viewportMargin
        if self.playerSprite.left < left_bndry:
            self.viewLeft -= left_bndry - self.playerSprite.left
            changed = True

        # check scroll right
        right_bndry = self.viewLeft + self.width - self.viewportMargin
        if self.playerSprite.right > right_bndry:
            self.viewLeft += self.playerSprite.right - right_bndry
            changed = True

        # check scroll up
        top_bndry = self.viewBottom + self.height - self.viewportMargin
        if self.playerSprite.top > top_bndry:
            self.viewBottom += self.playerSprite.top - top_bndry
            changed = True

        # check scroll down
        bottom_bndry = self.viewBottom + self.viewportMargin
        if self.playerSprite.bottom < bottom_bndry:
            self.viewBottom -= bottom_bndry - self.playerSprite.bottom
            changed = True

        if changed:
            arcade.set_viewport(self.viewLeft,
                                self.width + self.viewLeft,
                                self.viewBottom,
                                self.height + self.viewBottom)
                
    def update(self, delta_time):
        '''
        updates the character list, physics engine, and checks for going up or down stairs
        '''
        self._scrollUpdate()       
        self.playerSprite.isoDirection = self.keyToDir[self.directionKey]
        if self.directionKey == 0:
            self.playerSprite.moving = False
        else:
            self.playerSprite.moving = True
            self._searchTarget()
        
        self.characterList.update()
        self.physicsEngine.update()
        self._manageLasers()
        self.levels[self.currentLevel].update()
              
        # Check stairs
        if self.levels[self.currentLevel].upstairs:
            if arcade.check_for_collision_with_list(self.playerSprite, self.levels[self.currentLevel].upstairs):
                self._changeLevel(1)
        if self.levels[self.currentLevel].downstairs:
            if arcade.check_for_collision_with_list(self.playerSprite, self.levels[self.currentLevel].downstairs):
                self._changeLevel(-1)
            
    def _searchTarget(self):
        closest = None
        minDistance = None
        for char in self.characterList:
            if char != self.playerSprite and char:
                distance = spriteDistance(self.playerSprite, char)
                if closest and minDistance:
                    if distance < minDistance:
                        minDistance = distance
                        closest = char
                else:
                    minDistance = distance
                    closest = char
        self.playerSprite.target = closest
    
    def on_key_press(self, key, modifiers):
        '''
        implements actions associated with key press
        close on esc
        set the direction key using binary encoding on direction keys
        binary encoding is (A3)(A2)(A1)(A0) = (UP)(DOWN)(RIGHT)(LEFT)
        player shoots on space
        '''
        if key == arcade.key.ESCAPE:
            self.close()
            
        if key == arcade.key.UP:
            self.directionKey += 8
        if key == arcade.key.DOWN:
            self.directionKey += 4
        if key == arcade.key.RIGHT:
            self.directionKey += 2
        if key == arcade.key.LEFT:
            self.directionKey += 1
        
        if key == arcade.key.SPACE and self.playerSprite.target:
            self.playerSprite.shoot()
    
    def on_key_release(self, key, modifiers):
        '''
        updates the direction key binary encoding and character shooting
        '''
        if key == arcade.key.UP:
            self.directionKey -= 8
        elif key == arcade.key.DOWN:
            self.directionKey -= 4
        elif key == arcade.key.RIGHT:
            self.directionKey -= 2
        elif key == arcade.key.LEFT:
            self.directionKey -= 1
        
        if key == arcade.key.SPACE:
            self.playerSprite.shootDirection = None
    
    def on_mouse_motion(self, x:float, y:float, dx:float, dy:float):
        pass
    
    def _manageLasers(self):
        for laser in self.lasers:
            points = (laser.center_x, laser.center_y), (laser.center_x + laser.change_x, laser.center_y + laser.change_y)
            for wall in self.levels[self.currentLevel].noWalk:
                if arcade.geometry.are_polygons_intersecting(points, wall.points):
                    try:
                        self.lasers.remove(laser)
                        laser.owner.lasers.remove(laser)
                    except:
                        pass
                    continue
            for char in self.characterList:
                if arcade.geometry.are_polygons_intersecting(points, char.points) and char != laser.owner:
                    try:
                        self.lasers.remove(laser)
                        laser.owner.lasers.remove(laser)
                    except:
                        pass
                    try:
                        self.characterList.remove(char)
                        if char is self.playerSprite:
                            arcade.draw_text('game over', 720, 450, arcade.color.WHITE, font_size=50)
                            arcade.pause(2)
                            self.close()
                        self.score += 1
                    except:
                        pass
Ejemplo n.º 6
0
Archivo: test.py Proyecto: vyrval/GYATH
from Items.PotionObject import PotionObject
from Characters.Player import Player


import xml.dom.minidom as minidom

WI= WorldItems()
pot = WI.getPotionByCategory('HP')


print(pot.data)

val = pot.get('usable')
print(val is None)

P1= Player()
P1.addToBag(pot)

print(P1)
P1.listInventory()

P1.useFromBag("HP_potion")




# 
# 
# file = minidom.parse("./XML_Files/PotionsFile.xml")
# potions = file.getElementsByTagName("Potion")
# 
Ejemplo n.º 7
0
    def main_game_loop(self):

        # Define the entity groups
        character_entities = pygame.sprite.Group()  # All character entities (including Player.py)
        built_entities = pygame.sprite.Group()  # Anything the player has built

        # Build the level
        level = Level()
        level.generate(self.tile_cache)

        # Create the player
        player_sprites = "data/images/player/"
        camera = Camera(main_camera, level.width * self.tile_size, level.height * self.tile_size,
                        self.window_width, self.window_height)
        self.player = Player(32, 32, player_sprites, self.tile_size, self.tile_size, 2, camera)
        character_entities.add(self.player)

        # Create cursor entity for better collisions
        cursor = Cursor()

        game_running = True

        up, down, left, right = 0, 0, 0, 0

        while game_running:

            # Reset game variables
            mouse_clicked = False

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit_game()

                # Key down events
                if event.type == pygame.KEYDOWN:

                    if event.key in (pygame.K_UP, pygame.K_w):
                        up = 1
                    if event.key in (pygame.K_DOWN, pygame.K_s):
                        down = 1
                    if event.key in (pygame.K_LEFT, pygame.K_a):
                        left = 1
                    if event.key in (pygame.K_RIGHT, pygame.K_d):
                        right = 1

                # Key up events
                if event.type == pygame.KEYUP:

                    if event.key in (pygame.K_UP, pygame.K_w):
                        up = 0
                    if event.key in (pygame.K_DOWN, pygame.K_s):
                        down = 0
                    if event.key in (pygame.K_LEFT, pygame.K_a):
                        left = 0
                    if event.key in (pygame.K_RIGHT, pygame.K_d):
                        right = 0

            cursor.update()

            for tile in level.terrain:
                self.screen.blit(tile.image, self.player.camera.apply(tile))

            for tile in level.obstacles:
                self.screen.blit(tile.image, self.player.camera.apply(tile))

            for character in character_entities:
                self.screen.blit(character.image, self.player.camera.apply(character))

            self.player.update(up, down, left, right, level.obstacles)

            pygame.display.flip()

        self.main_menu_loop()
Ejemplo n.º 8
0
class Game:
    tile_dir = "data/images/tiles/"
    ui_dir = "data/images/ui/"

    def __init__(self):

        # Define window properties
        self.window_size = self.window_width, self.window_height = 640, 640
        self.tile_size = 32
        self.depth = 0
        self.flags = 0
        self.fps = 60

        # Initialize pygame
        pygame.init()

        # Define basic pygame properties
        self.screen = pygame.display.set_mode(self.window_size, self.flags, self.depth)
        self.clock = pygame.time.Clock()

        # Load the image cache
        self.tile_cache = ImageCache(Game.tile_dir)
        self.ui_cache = ImageCache(Game.ui_dir)

        # Define the key entities and default to None until we start the main game loop
        self.player = None

    def main_menu_loop(self):
        start_game = False

        # Create buttons with the static callback functions
        new_game_img = self.ui_cache.get_image("new_game_but")
        new_game_img_hov = self.ui_cache.get_image("new_game_but_hover")
        new_game_button = Button(self.get_half_width(), self.get_half_height() - 50,
                                 new_game_img, new_game_img_hov, 200, 50, start_game_cb)

        quit_img = self.ui_cache.get_image("quit_but")
        quit_img_hover = self.ui_cache.get_image("quit_but_hover")
        quit_button = Button(self.get_half_width(), self.get_half_height() + 10,
                             quit_img, quit_img_hover, 200, 50, quit_game)

        # Define sprite group for buttons to easily draw
        button_entities = pygame.sprite.Group()
        button_entities.add((new_game_button, quit_button))

        # Retrieve other UI images from cache
        bg_img = self.ui_cache.get_image("menu_bg")
        credits_img = self.ui_cache.get_image("credits")

        # Only draw background and other static images once to screen
        self.screen.blit(bg_img, (0, 0))
        self.screen.blit(credits_img, (20, self.window_height - 50))

        # Set the screen title
        pygame.display.set_caption("The Forming")

        while not start_game:
            self.clock.tick(self.fps)
            mouse_clicked = False

            # Loop through all events
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_clicked = True
                if event.type == pygame.KEYDOWN:
                    pass

            # Update the buttons. Start game will return True if clicked, exiting the menu loop
            start_game = new_game_button.update(pygame.mouse.get_pos(), mouse_clicked)
            quit_button.update(pygame.mouse.get_pos(), mouse_clicked)

            button_entities.draw(self.screen)

            pygame.display.flip()

        self.main_game_loop()

    def main_game_loop(self):

        # Define the entity groups
        character_entities = pygame.sprite.Group()  # All character entities (including Player.py)
        built_entities = pygame.sprite.Group()  # Anything the player has built

        # Build the level
        level = Level()
        level.generate(self.tile_cache)

        # Create the player
        player_sprites = "data/images/player/"
        camera = Camera(main_camera, level.width * self.tile_size, level.height * self.tile_size,
                        self.window_width, self.window_height)
        self.player = Player(32, 32, player_sprites, self.tile_size, self.tile_size, 2, camera)
        character_entities.add(self.player)

        # Create cursor entity for better collisions
        cursor = Cursor()

        game_running = True

        up, down, left, right = 0, 0, 0, 0

        while game_running:

            # Reset game variables
            mouse_clicked = False

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit_game()

                # Key down events
                if event.type == pygame.KEYDOWN:

                    if event.key in (pygame.K_UP, pygame.K_w):
                        up = 1
                    if event.key in (pygame.K_DOWN, pygame.K_s):
                        down = 1
                    if event.key in (pygame.K_LEFT, pygame.K_a):
                        left = 1
                    if event.key in (pygame.K_RIGHT, pygame.K_d):
                        right = 1

                # Key up events
                if event.type == pygame.KEYUP:

                    if event.key in (pygame.K_UP, pygame.K_w):
                        up = 0
                    if event.key in (pygame.K_DOWN, pygame.K_s):
                        down = 0
                    if event.key in (pygame.K_LEFT, pygame.K_a):
                        left = 0
                    if event.key in (pygame.K_RIGHT, pygame.K_d):
                        right = 0

            cursor.update()

            for tile in level.terrain:
                self.screen.blit(tile.image, self.player.camera.apply(tile))

            for tile in level.obstacles:
                self.screen.blit(tile.image, self.player.camera.apply(tile))

            for character in character_entities:
                self.screen.blit(character.image, self.player.camera.apply(character))

            self.player.update(up, down, left, right, level.obstacles)

            pygame.display.flip()

        self.main_menu_loop()

    def get_half_width(self):
        return self.window_width / 2

    def get_half_height(self):
        return self.window_height / 2
Ejemplo n.º 9
0
Archivo: test.py Proyecto: vyrval/GYATH
from Prefabs.WorldItems import WorldItems
from Items.PotionObject import PotionObject
from Characters.Player import Player

import xml.dom.minidom as minidom

WI = WorldItems()
pot = WI.getPotionByCategory('HP')

print(pot.data)

val = pot.get('usable')
print(val is None)

P1 = Player()
P1.addToBag(pot)

print(P1)
P1.listInventory()

P1.useFromBag("HP_potion")

#
#
# file = minidom.parse("./XML_Files/PotionsFile.xml")
# potions = file.getElementsByTagName("Potion")
#
# for potion in potions:
#     data={}
#     for attrName, attrValue in potion.attributes.items():
#         #attrName= attrName.replace('u\'', '')