def __init__(self, title, width, height): self.Title = title self.Height = height self.Width = width self.Screen = pygame.display.set_mode((self.Width, self.Height)) #This is to initialize the framerate (fps) self.Clock = pygame.time.Clock() #This color is a refrence to the color object, in there all different colors are defined self.Color = clr.Color() self.DefaultFont = pygame.font.SysFont(None, 30) #Initialize text messages self.StartText = Text.Text( self.Color.Black, self.DefaultFont, "Make the player move on the screen, up, down, left and right!") self.NextLine = Text.Text( self.Color.Black, self.DefaultFont, "Why do the same thing over and over again when it allready exists!!!" ) #Create all game characters here self.Player1 = c.Component(c.Position(400, 100), "enemy.png") self.Apple = c.Component(c.Position(0, 0), "apple.png") self.AppleCounter = 0 self.Score = Text.Text(self.Color.Black, self.DefaultFont, "Apple Counter: " + str(self.AppleCounter))
def init(self): scenebase.SceneBase.init(self) bg = create_entity(self.world, "IntroBG.png", pygame.Rect(640, 360, 1280, 720)) self.world.add_component(bg, components.Background()) player = get_player(self.world) saddle = create_entity(self.world, "saddle.png", pygame.Rect(300, 560, 80, 80)) self.world.add_component(saddle, components.Velocity()) self.world.add_component(saddle, components.Audio("light")) def dragon_hit(): self.world.component_for_entity(dragon, components.Animation).frame = 1 damage = notify(self.world, self.font, "Dragon Tamed", self, text.TextScene("Sadly, the dragon refused to be tamed. Instead, though, it granted NaN the power of fireballs! Let's see how this turns out...",SceneEight())) image = self.font.render("Dragon Tamed", False, (255, 128, 0)) self.world.component_for_entity(damage, components.Image).image = image self.world.component_for_entity(damage, components.Position).x = 960 self.world.component_for_entity(damage, components.Position).y = 320 self.world.component_for_entity(damage, components.Size).width = image.get_width() self.world.component_for_entity(damage, components.Size).height = image.get_height() pygame.mixer.Sound(os.path.join(components.get_base_path(), 'audio', 'dragon.ogg')).play() dragon = self.world.create_entity() self.world.add_component(dragon, components.Position(1000, 500)) self.world.add_component(dragon, components.Velocity(0, 0)) self.world.add_component(dragon, components.Animation("dragon.png", splitx=640, frame=0)) self.world.add_component(dragon, components.Size(640, 640)) self.world.add_component(dragon, components.Touch(saddle, rect=pygame.Rect(0, -400, -320, 0), touch=dragon_hit)) def move_up(entity): self.world.add_component(entity, components.ChangePosition((0, -40), 1, interpolation.Smooth(), True, move_down, entity)) def move_down(entity): self.world.add_component(entity, components.ChangePosition((0, 40), 1, interpolation.Smooth(), True, move_up, entity)) tutorial = self.world.create_entity() image = self.font.render("'put' the saddle on the dragon", False, (32, 255, 128)) self.world.add_component(tutorial, components.Position(640, 680)) self.world.add_component(tutorial, components.Image(image=image)) self.world.add_component(tutorial, components.Size(image.get_width(), image.get_height())) move_up(tutorial) clouds = [] for i in [1,2,3,4,5,6]: cloud = create_entity(self.world, "cloud.png", pygame.Rect(random.randrange(100, 1180), random.randrange(75, 200), 160, 80)) self.world.add_component(cloud, components.Background()) self.world.add_component(cloud, components.Hang()) clouds.append(cloud) self.world.add_processor(processors.RenderProcessor()) self.world.add_processor(processors.InputProcessor(), priority=10) self.world.add_processor(processors.PhysicsProcessor(600), priority=5) self.world.add_processor(processors.AnimationProcessor(), priority=5) self.world.add_processor(processors.PlayerProcessor(player, 100), priority=25) self.world.add_processor(processors.DragonSceneProcessor(player, tutorial, self.font), priority=20)
def __init__(self, title, width, height): #The basic parameters wich make up the game self.Title = title self.Height = height self.Width = width self.Screen = pygame.display.set_mode((self.Width, self.Height)) #This is to initialize the framerate (fps) self.Clock = pygame.time.Clock() #This color is a refrence to the color object, in there all different colors are defined self.Color = clr.Color() self.DefaultFont = pygame.font.SysFont(None, 30) self.InfoText = Text.Text( self.Color.Black, self.DefaultFont, "Score 3 points to go to the next level, press space to fly!") #Create all game characters here self.Player1 = c.Component(c.Position(400, 250), "enemy.png") #Empty list of enemies, this will be filled during the game #This is a special kind of list called an array, each element in the array has an unique Index #indeces start from 0, by calling Enemies[3] you call enemy in position number 3 #!!!!BECAREFULL WITH THE BOUNDS: When the index does not exist you will get an error, always check if the IndexError #exists before calling it. self.Enemies = []
def create_stuff(ID, x, y): name,typ,mat,val,fgcol,hp,kg,solid,push,script = STUFF[ID] world = rog.world() if fgcol == "random": fgcol = random.choice(list(COL.keys())) ## tt = thing.Thing(x,y, _type=typ,name=name,color=COL[fgcol]) ## tt.mass = kg ## tt.material=mat ## if lo: _hp(tt, lo) ## tt.isSolid = solid ## if push: rog.make(tt, CANPUSH) ## #_applyResistancesFromMaterial(tt, mat) ## return tt ent = world.create_entity( cmp.Name(name), cmp.Position(x,y), cmp.Draw(typ, fgcol=fgcol), cmp.Form(mass=kg, mat=mat, val=val), cmp.BasicStats(hp=hp,mp=hp), ) if solid: rog.make(ent, ISSOLID) if push: rog.add_component(ent, cmp.Pushable()) script(ent) return ent
def update(self): #Get keyboard input, checks if a certain key is pressed or not and get mouse Position keys = pygame.key.get_pressed() #Player opperations self.Player1.gravity(self.Height, 10) if keys[pygame.K_SPACE] and self.Player1.ImageRect.y > 0: self.Player1.jump(20, self.Height) #Enemy opperations if len(self.Enemies) < 1: self.Enemies.append( c.Component(c.Position(self.Width - 100, 300), "knight.png")) for enemy in self.Enemies: enemy.gravity(self.Height, 10) enemy.update(-25, 0) if enemy.ImageRect.x < 0: self.Enemies.remove(enemy) if enemy.intersection(self.Player1.ImageRect.x, self.Player1.ImageRect.y, self.Player1.ImageRect.height, self.Player1.ImageRect.width): self.Player1.Score -= 1 self.Player1.jump(200, self.Height) if enemy.ImageRect.x == self.Player1.ImageRect.x: self.Player1.Score += 1
def potion_healing(self, x, y): self.log.debug("Adding healing potion at (" + str(x) + ", " + str(y) + ")") item = self.world.create_entity( components.Position(x, y), components.Render("!", libtcod.light_red), components.Optics(transparent=True, lit=True), components.Item(), components.Text( noun="healing potion", pronoun=pronoun_from_name("healing potion"), description= "A bubbling red potion with miraculous healing powers."), components.HealingEffect(hit_points=5, duration=3000, interval=1000)) drop_action = components.DropAction(item) self.world.add_component(item, drop_action) drink_action = components.DrinkAction(item) self.world.add_component(item, drink_action) throw_action = components.ThrowAction(item, distance=4) self.world.add_component(item, throw_action) return item
def __init__(self, title, width, height): self.Title = title self.Height = height self.Width = width self.Screen = pygame.display.set_mode((self.Width, self.Height)) #This is to initialize the framerate (fps) self.Clock = pygame.time.Clock() #This color is a refrence to the color object, in there all different colors are defined self.Color = clr.Color() self.DefaultFont = pygame.font.SysFont(None, 30) #Initialize text messages self.StartText = Text.Text(self.Color.Black, self.DefaultFont, "Welcom to my pygame template!!!") #Initialize the soundprovider self.BackgroundMusic = sp.SoundProvider( "9th_Symphony_Finale_by_Beethoven.mp3") #Set the background music to play self.BackgroundMusic.Play(5) #Create all game characters here self.Player1 = c.Component(c.Position(400, 100), "enemy.png") #Create a button self.ExitButton = button.Button( 300, 250, 50, 200, self.Color.Red, Text.Text(self.Color.Black, self.DefaultFont, "Exit"), lambda: sys.exit()) self.StartButton = button.Button( 600, 250, 50, 200, self.Color.Green, Text.Text(self.Color.Black, self.DefaultFont, "Start"))
def update(self): #Your update logic here keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.Car.update(-10, 0) if keys[pygame.K_RIGHT]: self.Car.update(10, 0) self.Car.horizontal_screen_wrap(self.Width) if len(self.Obstacles) < 1: self.Obstacles.append( c.Component(c.Position(random.randint(20, self.Width - 40), 0), "truck.jpg")) for obstacle in self.Obstacles: obstacle.update(0, 20) if obstacle.ImageRect.y > self.Height: self.Obstacles.remove(obstacle) if self.Car.intersection(obstacle.ImageRect.x, obstacle.ImageRect.y, obstacle.ImageRect.height, obstacle.ImageRect.height): print("Game Over") sys.exit()
def notify(world, font, message, from_scene, to_scene): entity = world.create_entity() world.add_component(entity, components.Position(640, 500)) world.add_component(entity, components.Image("speech.png")) util.drawText(world.component_for_entity(entity, components.Image).image, message, (255, 255, 255), pygame.Rect(30, 20, 246, 134), font) world.add_component(entity, components.Size(307, 173)) world.add_component(entity, components.Delay(2, fade_up, world, entity, from_scene, to_scene)) return entity
def add_entities(dungeon, registry): p_room = choice(dungeon.rooms) y_coord, x_coord = p_room.coords random_y = randint(y_coord, y_coord + p_room.dims[0] - 1) random_x = randint(x_coord, x_coord + p_room.dims[1] - 1) player = registry.create_entity( components.Position(y=random_y, x=random_x), components.Player(), components.Icon("@")) dungeon.board[random_y][random_x] = player
def Assemble_Chunk(sName, sType, attribDict): entity = Entity(sEntityName, sEntityType, {}) entity._Add_Component(components.Mesh()) entity._Add_Component( components.Position(attribDict['WindowPos'].split(','))) entity._Add_Component( components.Position(attribDict['WorldPos'].split(','))) entity._Add_Component(components.Flag("Is Empty", True)) entity._Add_Component(components.Flag("Is Loaded", False)) #Here a list comprehension is used to construct a list of lists of lists of Tile components. entity._Add_Component([[[components.Tile() for depth in xrange(config.CHUNK_LAYERS)] \ for col in xrange(config.CHUNK_TILES_WIDE)] \ for row in xrange(config.CHUNK_TILES_HIGH)]) return entity
def wall(self, x, y): return self.world.create_entity( components.Position(x, y), components.Render("#", libtcod.dark_gray), components.Tangible(True), components.Optics(transparent=False), components.Terrain(), components.Text("wall", "a", description="A smooth stone wall."), components.Essence(0), )
def troll(self, x, y): self.log.debug("Adding troll at (" + str(x) + ", " + str(y) + ")") return self.world.create_entity( components.Actor(), components.Position(x, y), components.Render("T", libtcod.darker_green), components.Tangible(True), components.Optics(transparent=True), components.Creature(), components.Text("troll", description="A slimy green troll"), components.Health(20), components.Fighter(5, 1), components.Essence(40), components.Time(), components.Inventory())
def orc(self, x, y): self.log.debug("Adding orc at (" + str(x) + ", " + str(y) + ")") return self.world.create_entity( components.Actor(), components.Position(x, y), components.Render("o", libtcod.desaturated_green), components.Tangible(True), components.Optics(transparent=True), components.Creature(), components.Text("orc", description="A massive snarling orc"), components.Health(10), components.Fighter(3, 0), components.Essence(20), components.Time(), components.Inventory())
def __init__(self, title, width, height): self.Title = title self.Height = height self.Width = width self.Screen = pygame.display.set_mode((self.Width, self.Height)) self.DefaultFont = pygame.font.SysFont(None, 30) #This color is a refrence to the color object, in there all different colors are defined self.Color = clr.Color() #Initlialize sound provider self.CrashSound = sp.SoundProvider("Crash.mp3") #Create all game characters here self.Player1 = c.Component(c.Position(200, 250), "knight.png") #This is a list/array of all enemies, at initialization there is only one enemy in the list self.Enemies = [] self.Enemies.append(c.Component(c.Position(50, 250), "enemy.png"))
def player(self, x, y): player = self.world.create_entity( components.Actor(initial=0), components.Position(x, y), components.Render("@", libtcod.turquoise), components.Tangible(False), components.Optics(transparent=True), components.Player(), components.Text("ghost", "a", "A creepy old ghost..."), components.Possessor(2.0), components.Essence(100), components.EssenceAbsorber(1, 10), components.EssenceDrain(0.1, 1000), components.Time()) return player
def Assemble_Player(sEntityName, sEntityType, attribDict): entity = Entity(sEntityName, sEntityType, {}) entity._Add_Component( components.Animated_Sprite( '0', iFrameWidth, iFrameHeight, { sAnimation: [texture for sAnimation, texture in dTextureStrips.items()] })) entity._Add_Component(components.Position('0', (0, 0))) return entity
def init(self): scenebase.SceneBase.init(self) bg = create_entity(self.world, "HouseScene3BG.png", pygame.Rect(640, 360, 1280, 720)) self.world.add_component(bg, components.Background()) player = get_player(self.world) floor = self.world.create_entity() self.world.add_component(floor, components.Position(640, 560)) self.world.add_component(floor, components.Size(1280, 100)) #self.world.add_component(floor, components.Image("box1.png")) def spider_tally(): self.count += 1 if self.count == 3: puzzle_complete() for i in [(200, 320), (280, 240), (420, 230), (440, 400), (590, 300), (680, 170), (760, 390), (830, 230)]: spider = create_entity(self.world, "Cobweb.png", pygame.Rect(i[0], i[1], 80, 80)) self.world.add_component(spider, components.Hang()) self.world.add_component(spider, components.Flammable()) self.world.add_component(spider, components.Audio("light")) self.world.add_component(spider, components.Touch(floor, touch=spider_tally)) box = create_entity(self.world, "box1.png", pygame.Rect(520, 560, 80, 80)) self.world.add_component(box, components.Velocity()) self.world.add_component(box, components.Flammable(True)) self.world.add_component(box, components.Audio("light")) guy = create_entity(self.world, "NPC3.png", pygame.Rect(210, 560, 80, 80)) self.world.add_component(guy, components.Velocity()) self.world.add_component(guy, components.Flammable()) self.world.add_component(guy, components.Audio("grunt")) def puzzle_complete(): notify(self.world, self.small_font, "Now get out of my barn!", self, text.TextScene("Lack of real adventuring work not only wore on NaN, but it dulled him. His body atrophied as his mind numbed, and he realized he could no longer perform the same feats he'd grown accustomed to.", SceneFive())) cat = create_entity(self.world, "Cat.png", pygame.Rect(1190, 560, 80, 80)) self.world.add_component(cat, components.Velocity(0,0)) self.world.add_component(cat, components.Flammable()) self.world.add_component(cat, components.Audio("light")) bubble = create_entity(self.world, "speech.png", pygame.Rect(1000, 100, 307, 173)) self.world.add_component(bubble, components.Hang()) image = self.world.component_for_entity(bubble, components.Image).image util.drawText(image, "My barn is dusty and full of cobwebs. NaN! Come here and do this for me.", (255, 255, 255), pygame.Rect(30, 20, 246, 134), self.small_font) self.world.add_processor(processors.RenderProcessor()) self.world.add_processor(processors.InputProcessor(), priority=10) self.world.add_processor(processors.PhysicsProcessor(600), priority=5) self.world.add_processor(processors.AnimationProcessor(), priority=5) self.world.add_processor(processors.PlayerProcessor(player, 75), priority=25)
def __init__(self, title, width, height): self.Title = title self.Height = height self.Width = width self.Screen = pygame.display.set_mode((self.Width, self.Height)) self.DefaultFont = pygame.font.SysFont(None, 30) #This color is a refrence to the color object, in there all different colors are defined self.Color = clr.Color() #Create all game characters here self.Player1 = c.Component(c.Position(200, 250), "giphy.gif")
def get_player(world): player = world.create_entity() image = components.Image("playerIdle.png") animation = components.Animation("playerSimple.png", splitx=80, framelength=.1) carry_image = components.Image("playerCarryIdle.png") carry_animation = components.Animation("playerCarrySimple.png", splitx=80, framelength=.1) jump = components.Audio("jump.ogg") throw = components.Audio("throw") world.add_component(player, components.Position(100, 560)) world.add_component(player, components.Velocity()) world.add_component(player, image) world.add_component(player, components.Size(80, 80)) world.add_component(player, components.Player(image, animation, carry_image, carry_animation, jump, throw)) return player
def corpse(self, x, y, creature_type): self.log.debug("Adding corpse at (" + str(x) + ", " + str(y) + ")") item = self.world.create_entity( components.Position(x, y), components.Render("%", libtcod.darker_red), components.Optics(transparent=True, lit=True), components.Item(), components.Text(noun=creature_type.lower() + " corpse", pronoun=pronoun_from_name(creature_type), description="A mutilated " + creature_type + " corpse.")) drop_action = components.DropAction(item) self.world.add_component(item, drop_action) return item
def __init__(self, title, width, height): self.Title = title self.Height = height self.Width = width self.Screen = pygame.display.set_mode((self.Width, self.Height)) #This is to initialize the framerate (fps) self.Clock = pygame.time.Clock() #This color is a refrence to the color object, in there all different colors are defined self.Color = clr.Color() self.DefaultFont = pygame.font.SysFont(None, 30) #Images self.Car = c.Component(c.Position(500, 500), "car.jpg") self.Obstacles = []
def __init__(self, world, sprite, x=0, y=0): self.position = components.Position(x, y) self.sprite = sprite self.sprite.position = x, y
def init(self): scenebase.SceneBase.init(self) bg = create_entity(self.world, "IntroBG.png", pygame.Rect(640, 360, 1280, 720)) self.world.add_component(bg, components.Background()) player = get_player(self.world) sword = create_entity(self.world, "sword.png", pygame.Rect(300, 560, 80, 80)) self.world.add_component(sword, components.Velocity()) self.world.add_component(sword, components.Audio("light")) def dragon_hit(): self.world.component_for_entity(dragon, components.Animation).frame = 1 damage = notify( self.world, self.font, "999,999,999,999,999,999", self, text.TextScene( "And thusly the skilled adventurer NaN took out yet another dragon. Dragon killing was no longer a dangerous quest but rather routine cleaning. With the dragon population dwindling NaN had time to help more people with their non life threatening problems.", SceneTwo())) image = self.font.render("999,999,999,999,999,999", False, (255, 128, 0)) self.world.component_for_entity(damage, components.Image).image = image self.world.component_for_entity(damage, components.Position).x = 960 self.world.component_for_entity(damage, components.Position).y = 320 self.world.component_for_entity( damage, components.Size).width = image.get_width() self.world.component_for_entity( damage, components.Size).height = image.get_height() pygame.mixer.Sound( os.path.join(components.get_base_path(), 'audio', 'dragon.ogg')).play() dragon = self.world.create_entity() self.world.add_component(dragon, components.Position(1000, 500)) self.world.add_component(dragon, components.Velocity(0, 0)) self.world.add_component( dragon, components.Animation("dragon.png", splitx=640, frame=0)) self.world.add_component(dragon, components.Size(640, 640)) self.world.add_component( dragon, components.Touch(sword, rect=pygame.Rect(0, -400, -320, 0), touch=dragon_hit)) def move_up(entity): self.world.add_component( entity, components.ChangePosition((0, -40), 1, interpolation.Smooth(), True, move_down, entity)) def move_down(entity): self.world.add_component( entity, components.ChangePosition((0, 40), 1, interpolation.Smooth(), True, move_up, entity)) tutorial = self.world.create_entity() image = self.font.render("use arrow keys or WASD to move", False, (32, 255, 128)) self.world.add_component(tutorial, components.Position(640, 680)) self.world.add_component(tutorial, components.Image(image=image)) self.world.add_component( tutorial, components.Size(image.get_width(), image.get_height())) move_up(tutorial) clouds = [] for i in [1, 2, 3, 4, 5, 6]: cloud = create_entity( self.world, "cloud.png", pygame.Rect(random.randrange(100, 1180), random.randrange(75, 200), 160, 80)) self.world.add_component(cloud, components.Background()) self.world.add_component(cloud, components.Hang()) clouds.append(cloud) self.world.add_processor(processors.RenderProcessor()) self.world.add_processor(processors.InputProcessor(), priority=10) self.world.add_processor(processors.PhysicsProcessor(600), priority=5) self.world.add_processor(processors.AnimationProcessor(), priority=5) self.world.add_processor(processors.PlayerProcessor(player, 100), priority=25) self.world.add_processor(processors.Scene1Processor( player, tutorial, self.font), priority=20)
def create_entity(world, image, rect): entity = world.create_entity() world.add_component(entity, components.Image(image)) world.add_component(entity, components.Position(rect.x, rect.y)) world.add_component(entity, components.Size(rect.width, rect.height)) return entity
def process(self, filtered_events, pressed_keys, dt, screen): v = self.world.component_for_entity(self.player, components.Velocity) p = self.world.component_for_entity(self.player, components.Player) s = self.world.component_for_entity(self.player, components.Size) pos = self.world.component_for_entity(self.player, components.Position) v.x = 0 if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]: v.x -= 3 * self.vitality self.player_is_facing_right = False self.player_is_facing_left = True if pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]: v.x += 3 * self.vitality self.player_is_facing_right = True p.holdingfireball = False if (p.facing_right and v.x < 0) or (not p.facing_right and v.x > 0): p.facing_right = not p.facing_right self.player_is_facing_right = False p.image.image = pygame.transform.flip(p.image.image, True, False) p.animation.image = pygame.transform.flip(p.animation.image, True, False) p.carry_image.image = pygame.transform.flip( p.carry_image.image, True, False) p.carry_animation.image = pygame.transform.flip( p.carry_animation.image, True, False) if v.x == 0 and self.world.has_component(self.player, components.Animation): self.world.remove_component(self.player, components.Animation) if p.holding: self.world.add_component(self.player, p.carry_image) else: self.world.add_component(self.player, p.image) elif v.x != 0 and self.world.has_component(self.player, components.Image): self.world.remove_component(self.player, components.Image) if p.holding: self.world.add_component(self.player, p.carry_animation) else: self.world.add_component(self.player, p.animation) for event in filtered_events: if event.type == pygame.KEYDOWN: if (event.key == pygame.K_UP or event.key == pygame.K_w or event.key == pygame.K_SPACE) and v.y == 0: v.y -= 6 * self.vitality p.jump.sound.play() elif event.key == pygame.K_f: if self.MessageShown == False: game.FireballMessage(self.world) self.MessageShown = True global fireball self.fireball = game.CreateFireball( self.world, pos.x, pos.y) self.world.add_component(self.fireball, components.Position(pos.x, pos.y)) self.world.add_component(self.fireball, components.Velocity()) self.world.add_component(self.fireball, components.Flammable(True)) v = self.world.component_for_entity( self.fireball, components.Velocity) if self.player_is_facing_right == True: while not v.x > 1300: v.x += 3 elif self.player_is_facing_right == False: while not v.x == 0: v.x -= 3 elif event.key == pygame.K_e: if p.holding: if self.world.has_component(self.player, components.Image): self.world.remove_component( self.player, components.Image) self.world.add_component(self.player, p.image) elif self.world.has_component(self.player, components.Animation): self.world.remove_component( self.player, components.Animation) self.world.add_component(self.player, p.animation) i = self.world.component_for_entity( p.holding, components.Image) s = self.world.component_for_entity( p.holding, components.Size) i.image = pygame.transform.rotate(i.image, -90) tmp = s.width s.width = s.height s.height = tmp self.world.add_component(p.holding, components.Velocity(0, 0)) p.holding = None else: rect = pygame.Rect( pos.x, pos.y - s.height * s.scale / 2, s.width * s.scale * (1 if p.facing_right else -1), s.height * s.scale) rect.normalize() for ent, (p2, i, s, v) in self.world.get_components( components.Position, components.Image, components.Size, components.Velocity): if self.world.has_component( ent, components.Player): continue if rect.collidepoint(p2.x, p2.y): p.holding = ent if self.world.has_component( self.player, components.Image): self.world.remove_component( self.player, components.Image) self.world.add_component( self.player, p.carry_image) elif self.world.has_component( self.player, components.Animation): self.world.remove_component( self.player, components.Animation) self.world.add_component( self.player, p.carry_animation) if self.world.has_component( ent, components.RotationalVelocity): r = self.world.component_for_entity( ent, components.RotationalVelocity) i.image = r.image s.width = r.width s.height = r.height self.world.remove_component( ent, components.RotationalVelocity) i.image = pygame.transform.rotate(i.image, 90) tmp = s.width s.width = s.height s.height = tmp self.world.remove_component( ent, components.Velocity) if self.world.has_component( ent, components.Audio): self.world.component_for_entity( ent, components.Audio).sound.play() break elif event.type == pygame.MOUSEBUTTONDOWN and p.holding: if self.world.has_component(self.player, components.Image): self.world.remove_component(self.player, components.Image) self.world.add_component(self.player, p.image) elif self.world.has_component(self.player, components.Animation): self.world.remove_component(self.player, components.Animation) self.world.add_component(self.player, p.animation) i = self.world.component_for_entity(p.holding, components.Image) s = self.world.component_for_entity(p.holding, components.Size) i.image = pygame.transform.rotate(i.image, -90) tmp = s.width s.width = s.height s.height = tmp x = event.pos[1] * 1280 / pygame.display.get_surface( ).get_width() y = event.pos[0] * 720 / pygame.display.get_surface( ).get_height() angle = math.atan2(x - pos.y, y - pos.x) % (2 * math.pi) self.world.add_component( p.holding, components.Velocity( math.cos(angle) * 16 * self.vitality, math.sin(angle) * 12 * self.vitality)) self.world.add_component(p.holding, components.RotationalVelocity(320)) p.holding = None p.throw.sound.play() elif event.type == pygame.MOUSEBUTTONDOWN and p.holdingfireball == True: v = self.world.component_for_entity(self.fireball, component.Velocity) while not v.x == 800: v.x = v.x + 1 v = self.world.component_for_entity( self.fireball, component.Velocity) if p.holding: p3 = self.world.component_for_entity(p.holding, components.Position) p3.x = pos.x p3.y = pos.y - s.height + 10 def BossBattleFireballLoop(): while True: if fireball: return fireball
def process(self, filtered_events, pressed_keys, dt, screen): for ent, (p, s, v) in self.world.get_components(components.Position, components.Size, components.Velocity): if v.y == 0 and self.world.has_component( ent, components.RotationalVelocity): r = self.world.component_for_entity( ent, components.RotationalVelocity) i = self.world.component_for_entity(ent, components.Image) i.image = r.image s.width = r.width s.height = r.height self.world.remove_component(ent, components.RotationalVelocity) v.y = min(v.y + 9.81 * 100 * dt, 53 * 100) # terminal velocity if not self.world.has_component(ent, components.Player): v.x *= .98 p.x = max(min(p.x + v.x * dt, 1280), 0) if self.ground != -1: p.y = min(p.y + v.y * dt, self.ground - s.height * s.scale / 2) if p.y >= self.ground - s.height * s.scale / 2 and v.y > 0: v.y = 0 else: p.y -= v.y * dt #Touch Physics for ent, (t, p, s) in self.world.get_components(components.Touch, components.Position, components.Size): rect = pygame.Rect(p.x + t.rect.x, p.y + s.height / 2 + t.rect.y, s.width + t.rect.width, s.height + t.rect.height) tp = self.world.component_for_entity(t.target, components.Position) ts = self.world.component_for_entity(t.target, components.Size) if rect.colliderect(pygame.Rect(tp.x, tp.y, ts.width, ts.height)): if not t.active: t.touch(*t.args) if t.multi: t.active = True else: self.world.remove_component(ent, components.Touch) else: t.active = False #Hanging Object Physics for hangEnt, (h, p, s) in self.world.get_components(components.Hang, components.Position, components.Size): rect = pygame.Rect(p.x, p.y, s.width, s.height) for ent, (v, tp, ts) in self.world.get_components(components.Velocity, components.Position, components.Size): if not self.world.has_component(ent, components.Player): if rect.colliderect( pygame.Rect(tp.x, tp.y, ts.width, ts.height)): if self.world.has_component(hangEnt, components.Hang): self.world.remove_component( hangEnt, components.Hang) self.world.add_component(hangEnt, components.Velocity(0, 0)) break #Platform physics for platEnt, (tl, box, pf) in self.world.get_components(components.Position, components.Size, components.Platform): for ent, (p, s, v) in self.world.get_components(components.Position, components.Size, components.Velocity): if (tl.x - box.width / 2) < p.x < (tl.x + box.width / 2) and ( tl.y - box.height) < (p.y + s.height / 2) - 20 < tl.y: if v.y > 0: p.y = min(((tl.y - box.height) - s.height / 2) + 20, p.y) v.y = min(0, v.y) #Flamable stuff for flamEnt, (f, p, s) in self.world.get_components(components.Flammable, components.Position, components.Size): if f.lit: if not self.world.has_component(flamEnt, components.Delay): flame = self.world.create_entity() self.world.add_component( flame, components.Position( p.x - s.width / 2 + random.random() * s.width, p.y - s.height / 2 + random.random() * s.height)) self.world.add_component(flame, components.Size(60, 60)) size = int(random.random() * 20) self.world.add_component( flame, components.Rect((255, random.random() * 255, 0), pygame.Rect(0, 0, size, size))) self.world.add_component( flame, components.ChangePosition( (-25 + random.random() * 50, -50 - random.random() * 100), 1, interpolation.PowIn(2), True, self.remove_entity, flame)) self.world.add_component(flamEnt, components.Delay(.03)) for ent, (f2, tp, ts) in self.world.get_components( components.Flammable, components.Position, components.Size): if not f2.lit: rect = pygame.Rect(p.x, p.y, s.width, s.height) if rect.colliderect( pygame.Rect(tp.x, tp.y, ts.width, ts.height)): f2.lit = True
def create_weapon(name, x,y): world = rog.world() ent = world.create_entity() data = WEAPONS[name] _type = get_weapon_type(data) value = get_weapon_value(data) mass = get_weapon_mass(data) hpmax = get_weapon_hpmax(data) capacity = get_weapon_capacity(data) reloadTime = get_weapon_rt(data) jamChance = get_weapon_jam(data) material = get_weapon_mat(data) rng = get_weapon_range(data) atk = get_weapon_atk(data) dmg = get_weapon_dmg(data) _pow = get_weapon_pow(data) dfn = get_weapon_dfn(data) arm = get_weapon_arm(data) asp = get_weapon_asp(data) msp = get_weapon_msp(data) elem = get_weapon_elem(data) ammo = get_weapon_ammo(data) flags = get_weapon_flags(data) script = get_weapon_script(data) physDmg = dmg if elem == ELEM_PHYS else 0 fgcol = COL['accent'] bgcol = COL['deep'] world.add_component(ent, cmp.Name(name)) world.add_component(ent, cmp.Position(x, y)) world.add_component(ent, cmp.Draw( char=_type, fgcol=fgcol, bgcol=bgcol )) world.add_component(ent, cmp.Form( mass=mass, material=material, value=value )) world.add_component(ent, cmp.BasicStats( hp=hpmax,mp=hpmax, #resfire=resfire,resbio=resbio,reselec=reselec,resphys=resphys )) ammoDict={} mainhandDict={} elementalDict={} if ammoType: ammoDict.update({'ammoType':ammoType}) if capacity: ammoDict.update({'capacity':capacity}) if reloadTime: ammoDict.update({'reloadTime':reloadTime}) if jamChance: ammoDict.update({'jamChance':jamChance}) if rng: mainhandDict.update({'rng':rng}) if atk: mainhandDict.update({'atk':atk}) if dmg: mainhandDict.update({'dmg':dmg}) if _pow: mainhandDict.update({'pow':_pow}) if dfn: mainhandDict.update({'dfn':dfn}) if arm: mainhandDict.update({'arm':arm}) if asp: mainhandDict.update({'asp':asp}) if msp: mainhandDict.update({'msp':msp}) if elem: elementalDict.update({'element':elem}) modDict={} if not ammoDict == {}: modDict.update({cmp.Ammo : ammoDict}) if not mainhandDict == {}: modDict.update({cmp.CombatStats : mainhandDict}) world.add_component(ent, cmp.CanEquipInMainhand(modDict)) if not elementalDict == {}: world.add_component(ent, cmp.ElementalDamage(elementalDict)) for flag in flags: rog.make(ent, flag) if script: script(weap) return weap
def create_gear(name,x,y,quality): world = rog.world() ent = world.create_entity() gData = GEAR[name] _type = get_gear_type(gData) value = get_gear_value(gData) mass = get_gear_mass(gData) hpmax = get_gear_hpmax(gData) material = get_gear_material(gData) dfn = get_gear_dfn(gData) arm = get_gear_arm(gData) msp = get_gear_msp(gData) sight = get_gear_sight(gData) resbio = get_gear_resbio(gData) resfire = get_gear_resfire(gData) reselec = get_gear_reselec(gData) resphys = get_gear_resphys(gData) script = get_gear_script(gData) fgcol = COL['accent'] bgcol = COL['deep'] world.add_component(ent, cmp.Name(name)) world.add_component(ent, cmp.Position(x, y)) world.add_component(ent, cmp.Draw(char=_type,fgcol=fgcol,bgcol=bgcol) ) world.add_component(ent, cmp.Form(mass=mass, material=material, value=value)) world.add_component(ent, cmp.BasicStats( hp=hpmax,mp=hpmax, resfire=resfire,resbio=resbio,reselec=reselec,resphys=resphys ) ) #stat mod dictionaries #{component : {var : modf}} basicStatsDict = {} combatDict = {} sightDict = {} if resbio: basicStatsDict.update({"resbio":resbio}) if resfire: basicStatsDict.update({"resfire":resfire}) if reselec: basicStatsDict.update({"reselec":reselec}) if resphys: basicStatsDict.update({"resphys":resphys}) if dfn: combatDict.update({"dfn":dfn}) if arm: combatDict.update({"arm":arm}) if msp: combatDict.update({"msp":msp}) if sight: sightDict.update({"sight":sight}) modDict = {} if not basicStatsDict == {}: modDict.update({cmp.BasicStats : basicStatsDict}) if not combatDict == {}: modDict.update({cmp.CombatStats : combatDict}) if not sightDict == {}: modDict.update({cmp.SenseSight : sightDict}) if _type == ARMR: world.add_component(ent, cmp.CanEquipInBodySlot(modDict)) elif _type == HELM: world.add_component(ent, cmp.CanEquipInHeadSlot(modDict)) elif _type == BACK: world.add_component(ent, cmp.CanEquipInBackSlot(modDict)) #item resistances based on material??? How to do this? if script: script(ent) return ent
def create_fluid(x,y,ID,volume): ent = world.create_entity(cmp.Position(x,y),cmp.FluidContainer()) ## fluid.add(ID, volume) return ent