def main():
    enemyYaml = open('./testEnemies.yaml').read()
    towerYaml = open('./testTowers.yaml').read()

    gameState1 = GameState(5)

    enemiesDict = YAMLInstancer.get_multiple(enemyYaml, Enemy)
    towersDict = YAMLInstancer.get_multiple(towerYaml, Tower)

    for enemyStr in enemiesDict:
        gameState1.enemyAdd(enemiesDict[enemyStr])

    for towerStr in towersDict:
        gameState1.towerAdd(towersDict[towerStr])

    pygame.init()

    displaySurf = pygame.display.set_mode((50 * len(gameState1.gameEnv.board), 50 * len(gameState1.gameEnv.board[0])))

    while True:

        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()

        displaySurf.blit(gameState1.bgSurf, (0, 0))
        for entSurf in gameState1.getEntitiesSurface():
            displaySurf.blit(entSurf, (0, 0))

        pygame.display.update()
示例#2
0
def main():
    global DISPLAYSURF
    clock = pygame.time.Clock()
    for tower1 in tows:
        tester.board[tower1.xpos][tower1.ypos].hasTower = True
    for enemyStart in baddies:
        tester.board[enemyStart.xpos][enemyStart.ypos].hasEnemy = True
    while True:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
        for tower1 in tows:
            towerChecks(tower1)
        if len(baddies) == 0:
            test_yaml = open('./EnemyTest.yaml').read()
            baddiesStr = YAMLInstancer.get_multiple(test_yaml, Enemy)
            for enemyStr in baddiesStr:
                baddies.append(baddiesStr[enemyStr])
        for x in range(0, len(tester.board)):
            for y in range(0, len(tester.board[0])):
                temp = pygame.Surface((50, 50))
                if tester.board[x][y].getPath() == True:
                    temp.fill(LIGHTBROWN)
                if tester.board[x][y].getPath() != True and tester.board[x][y].hasEnd != True:
                    temp.fill(BROWN)
                if tester.board[x][y].hasEnd == True:
                    temp.fill(BLACK)
                if tester.board[x][y].hasEnemy == True:
                    temp.blit(EnemyImage, (0,0))
                if tester.board[x][y].hasTower == True:
                    temp.blit(TowerImage, (0,0))
                DISPLAYSURF.blit(temp, (x * 50, y * 50))
        for badGuy in baddies:
            enemyMove(badGuy)
            if badGuy.health <= 0:
                baddies.remove(badGuy)
                print(badGuy)
                tester.board[badGuy.xpos][badGuy.ypos].hasEnemy = False
        for proj1 in projs:
            proj1.xpos, proj1.ypos = int(proj1.realX / 50),  int(proj1.realY / 50)
            target1 = proj1.enemy
            ok = False
            for alive in baddies:
                ok = ok or alive == target1
            if not ok:
                projs.remove(proj1)
                print(proj1)
            elif proj1.xpos == proj1.enemy.xpos and proj1.ypos == proj1.enemy.ypos:
                proj1.impact()
                proj1.damage = 0
                proj1.hittimer += 1
                if proj1.hittimer >= 9:
                    projs.remove(proj1)
                    print(proj1)
            else:
                projMove(proj1)
        pygame.display.update()
import pygame, sys

from game.common.yaml_parsing import YAMLInstancer
from game.subsystems.environment import *
from game.subsystems.entities import *
from pygame.locals import *
from game.common.math import *

BROWN = (150, 75, 0)
LIGHTBROWN = (181, 101, 29)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

env = Environment()
DISPLAYSURF = pygame.display.set_mode(
    (100 * len(env.board), 100 * len(env.board[0])))

test_yaml = open('./towerTest.yaml').read()
test_yaml2 = open('./towerTestEnemy.yaml').read()
tower1 = YAMLInstancer.get_single(test_yaml, Tower)
enemy1 = YAMLInstancer.get_single(test_yaml2, Enemy)
proj1 = tower1.fire([enemy1])
print(enemy1)
if not proj1 is None:
    proj1.impact()
print(enemy1)
示例#4
0

class otherClass:
    REQ_ATTRS = ['how_much_scary']
    DEFAULT_ATTRS = {'scary_threshold': 50}
    TYPE_ATTRS = {'how_much_scary': int}

    def __str__(self):
        return 'it has %d scary but it will only be scary after %d' % (
            self.how_much_scary, self.scary_threshold)


example_test = open("./test.yaml").read()  # read the yaml file

# test #1
object_dictionary = YAMLInstancer.instance_multiple_to_dict(
    example_test, testClass)
print(object_dictionary['instance1'])
print(object_dictionary['scary_guy'])
# test #2
YAMLInstancer.get_variables(globals(), example_test,
                            testClass)  # put the objects out into global scope
print(instance1.property1)
print(instance2.property4['hi'])
"""
    this is WEIRD, because INSTANCE1 was declared nowhere...
    but since it was handed the global scope variables
    it was able to create 'instance1' outside this scope, using the class testClass
    
    TL;DR instance1 is an instance of testClass but it thinks it's an error
    python is scary
    
示例#5
0
      y: 0
      height: 0.5 # these are in the parent's "field space" so that 0.5 means 50% of the parent panel
      width: 1.0
    - bottom_panel: # set name by making it an object with a key
        x: 0
        y: 0.5
        width: 1.0
        height: 0.5
        inner_panels:
        - bottom_left_panel: {x: 0, y: 0, width: 0.5, height: 1.0} # object in scrunched form
        - {x: 0.5, y: 0, width: 0.5, height: 1.0} # no name property anywhere; reverts to default
"""
# yaml_layout = open("../../config/layouts.yaml").read() # start menu layout

# with or without using the class notation, get the instance
panel = YAMLInstancer.get_single(yaml_layout, Panel)
# using a method to get a flattened version of all the panels from tree form (most likely you won't be doing this)
inner_panels = panel.get_all_inner()
print('flattened layout:')
print(';\n'.join([str(p) for p in inner_panels])) # some debug (print the panels from yaml)

# create a layout to hold the panels
layout = Layout.fromPanelList(inner_panels)
# setup some pygame stuff
WIDTH, HEIGHT = 400, 400
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))
DISPLAY.fill((0, 0, 0))
# map the panels onto the screen
screen_panels = layout.getPanelsOnRect((0, 0, WIDTH, HEIGHT))

# draw stuff
示例#6
0
from game.common.yaml_parsing import YAMLInstancer
from game.subsystems.ui.graphical_user_interface import GUI
from game.subsystems.ui.layout.layout import Layout
from game.subsystems.ui.layout.layoutmanager import LayoutManager
from game.subsystems.ui.layout.panel import Panel
from game.common.text import Text
import pygame, sys
from game.subsystems.ui.ui_handler import ElementsHandler

pygame.init()
Text.DEFAULT_TEXT = Text("../common/verdana.ttf")
WIDTH, HEIGHT = 400, 400
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))

yaml_layout = open("big_layout_test.yaml").read() # start menu layout
panel_layouts = YAMLInstancer.get_multiple(yaml_layout, Panel, debug=False)
layout_manager = LayoutManager()
for panel_name, panel_layout in panel_layouts.items():
    layout = Layout.layoutFromPanel(panel_layout)
    layout_manager.addLayout(layout, panel_name, set_current=True)

screen_panels = layout.getPanelsOnRect((0, 0, WIDTH, HEIGHT))
elements_handler = ElementsHandler.from_panel_list(screen_panels)
print('all elements: %s'%elements_handler.get_elements())
gui = GUI(elements_handler, layout_manager)

# update / handle exiting
mouse_position = Vector(0, 0)
mouse_down = False
mouse_pressed = False
while True:
示例#7
0
# this is a more detailed testing of yaml, hopefully with nested classes as anonymous instances
from game.common.yaml_parsing import YAMLInstancer


def print_all_attributes(obj):
    return 'instance of %s: {\n%s\n}' % (obj.__ if hasattr(
        obj, '__') else obj.__class__, '\n'.join(
            ["%s: %s" % (k, v) for k, v in vars(obj).items() if k != "__"]))


# open and read test yaml file
test_yaml = open('./test_2.yaml').read()

# get the first (or only) object from the yaml file
o = YAMLInstancer.get_single(test_yaml)
print(print_all_attributes(o))

print('--------------------------------')

# get multiple objects from yaml file, as a dict like {object1name: value1, object2name: value2, . . . }
o = YAMLInstancer.get_multiple(test_yaml)
for k, v in o.items():
    print(print_all_attributes(v))
from game.subsystems.ui.layout.layout import Layout
from game.subsystems.ui.layout.layoutmanager import LayoutManager
from game.subsystems.ui.layout.panel import Panel
from game.common.text import Text
from game.subsystems.gameState import *
import pygame, sys
from game.subsystems.ui.ui_handler import ElementsHandler

pygame.init()
Text.DEFAULT_TEXT = Text("../common/verdana.ttf")
WIDTH, HEIGHT = 800, 600
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))

# SETUP ENTITIES
enemies = list(
    YAMLInstancer.get_multiple(open('EnemyTest.yaml').read(), Enemy).values())

yaml_layout = open("Shoptest.yaml").read()  # start menu layout
panel_layouts = YAMLInstancer.get_multiple(
    yaml_layout, Panel, debug=False)  # get a dictionary with all the layouts
layout_manager = LayoutManager()
for panel_name, panel_layout in panel_layouts.items():
    layout = Layout.layoutFromPanel(panel_layout)
    layout_manager.addLayout(layout, panel_name, set_current=True)
layout = layout_manager.getLayout()
screen_panels = layout.getPanelsOnRect((0, 0, WIDTH, HEIGHT))
elements_handler = ElementsHandler.from_panel_list(screen_panels)
print('all elements: %s' % elements_handler.get_elements())
gui = GUI(elements_handler, layout_manager)

daGame = GameState("TestMap2.txt")
示例#9
0
from game.subsystems.entities import *
from pygame.locals import *
pygame.init()
tester = Environment()
DISPLAYSURF = pygame.display.set_mode((50 * len(tester.board), 50 * len(tester.board[0])))
temp = pygame.image.load('Tower.png')
TowerImage = pygame.transform.scale(temp, (50,50))
temp2 = pygame.image.load('Enemy.png')
EnemyImage = pygame.transform.scale(temp2, (50,50))
BROWN = (150, 75, 0)
LIGHTBROWN = (181, 101, 29)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,00)
test_yaml = open('./EnemyTest.yaml').read()
baddiesStr = YAMLInstancer.get_multiple(test_yaml, Enemy)
towertest_yaml = open('./basictower.yaml').read()
tower1 = YAMLInstancer.get_multiple(towertest_yaml, Tower)
baddies = []
tows = []
projs = []
for enemyStr in baddiesStr:
    baddies.append(baddiesStr[enemyStr])
for towerStr in tower1:
    tows.append(tower1[towerStr])

def main():
    global DISPLAYSURF
    clock = pygame.time.Clock()
    for tower1 in tows:
        tester.board[tower1.xpos][tower1.ypos].hasTower = True
示例#10
0
import pygame, sys
from game.subsystems.ui.ui_handler import ElementsHandler

pygame.init()
Text.DEFAULT_TEXT = Text("../common/verdana.ttf")
WIDTH, HEIGHT = 800, 600
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))

# config
CONFIG_DIR = "../../config/"
GET_CONFIG = lambda filename: open(CONFIG_DIR + filename, 'r')
GET_YAML = lambda filename: GET_CONFIG(filename + '.yaml').read()

# SETUP ENTITIES
enemies = list(
    YAMLInstancer.get_multiple(GET_YAML('entities/enemies'), Enemy).values())

# make the layout manager
layout_manager = LayoutManager()
# get the layouts
panel_layouts = YAMLInstancer.get_multiple(
    GET_YAML('layouts'), Panel,
    debug=False)  # get a dictionary with all the layouts
for pn, p in panel_layouts.items():
    p: Panel = p  # tell it that p is a panel
    #print('panels: %s'%p.get_all_inner())
    layout = Layout.layoutFromPanel(p)  # get the layout
    #print('layout gotten: %s, its panels: %s'%(layout, layout.panels))
    screen_panels = layout.getPanelsOnRect(
        (0, 0, WIDTH,
         HEIGHT))  # transform panels so that they're on the screen size