def __init__(self, speed, starting_position, texture, window_rectangle, collision_manager): self.window_rectangle = window_rectangle self.starting_position = starting_position self.speed = speed self.collision_manager = collision_manager self.direction = 1 if speed > 0 else -1 # Plane fly_anim = Animation() fly_anim.texture = texture fly_anim.add_frame(sf.Rectangle((0, 0), (88, 73))) fly_anim.add_frame(sf.Rectangle((88, 0), (88, 73))) fly_anim.add_frame(sf.Rectangle((176, 0), (88, 73))) fly_anim.add_frame(sf.Rectangle((88, 0), (88, 73))) self.plane = AnimatedSprite(sf.seconds(0.2), False, True) self.plane.play(fly_anim) self.plane.size = sf.Vector2(self.plane.global_bounds.width / 2, self.plane.global_bounds.height / 2) self.plane.origin = self.plane.global_bounds.width / 2.0, self.plane.global_bounds.height / 2.0 self.plane.scale((self.direction * 0.5, 0.5)) self.plane.position = self.starting_position self.plane_speed = sf.Vector2(speed, 0) self.is_dead = False self.jump_time = None self.plane_jumped = False self.immortal = None self.bullets = set() SoundManager.play_player_appear_sound()
def display_list(self): self.win.clear(self.backgnd) states = sf.RenderStates() if self.use_shader: states.shader = self.cycle.shader self.win.draw(self.screen_spr, states) if self.boxzoom == 1: if self.clock.elapsed_time.milliseconds > 1000.0: self.boxzoom = 2 elif self.boxzoom == 2: fac = (self.clock.elapsed_time.milliseconds - 1000.0) / 1000.0 fac2 = math.sin((fac * 0.5 * math.pi)) x, y = self.endpos * fac2 w, h = self.endsize + (self.startsize - self.endsize) * (1 - fac2) self.view.reset(sf.Rectangle((x, y), (w, h))) self.win.view = self.view self.win.display() if self.cycle_switch: self.cycle.update(self.step) if self.clock.elapsed_time > sf.seconds(2): self.clock.restart() self.view_index += 1 if self.view_index == len(self.view_list): self.view_index = 0 self.mode = "init_display_list"
def move_randomly(self, dt): if self.direction_timer.elapsed_time > sf.seconds(5) \ or not MAP_RECT.contains(self.position + (self.direction * self.speed * 10)): self.direction = random_unit_vector() self.direction_timer.restart() self.move(self.direction * self.speed, dt)
def create_animation(self, action): action, direction, duration = self.current_action texture, frames = self.data.mobs[self.mob][action, direction] body = AnimationSprite(texture, frames) return MobAnimation(body, sf.seconds(1))
def run(self): clock = sf.Clock() time_since_last_update = sf.seconds(0) for i in range(settings.INITIAL_ENTITIES): self.entities.append(Entity(sf.Vector2( random.randrange(width), random.randrange(height)))) while self.window.is_open: dt = clock.restart() time_since_last_update += dt while time_since_last_update > time_per_update: time_since_last_update -= time_per_update t = clock.elapsed_time self.update(time_per_update) t = clock.elapsed_time - t self.statistics.t_update.append(t.microseconds) self.process_events() self.statistics.update_texts(dt) t = clock.elapsed_time self.render() self.statistics.num_frames += 1 t = clock.elapsed_time - t self.statistics.t_render.append(t.microseconds)
def update(self, elapsed_time): for bullet in self.bullets: bullet.update(elapsed_time) self.check_bounds() if self.is_dead: return if not self.plane_jumped and (self.plane.rotation <= 60 or self.plane.rotation >= 300): self.plane.rotate(1.25 * self.direction) if self.immortal and self.immortal.elapsed_time.seconds > IMMORTAL_TIME: self.immortal = None self.plane.color = sf.Color(255, 255, 255, 255) if self.plane_jumped: self.plane_speed.y = -200.0 if self.jump_time.elapsed_time.seconds < 0.25: self.plane.rotate(-2.5 * self.direction) else: self.plane_jumped = False self.jump_time = None if self.plane.rotation % 300 > 60: self.plane.rotation = (300, 60)[self.plane.rotation * self.direction > 300] if self.plane_speed.y <= 50 * GRAVITY: self.plane_speed += sf.Vector2(0.0, GRAVITY) self.plane.move(self.plane_speed * elapsed_time) self.plane.update(sf.seconds(elapsed_time))
def __init__(self, framestime, name): self.framestime = sf.seconds(framestime) self.clock = sf.Clock() self.currentframe = 0 self.name = name self.frames = [] self._itersCount = 0
def update(self, elapsed_time): for bullet in self.bullets: bullet.update(elapsed_time) self.check_bounds() if self.is_dead: return if not self.plane_jumped and (self.plane.rotation <= 60 or self.plane.rotation >= 300): self.plane.rotate(1.25 * self.direction) if self.immortal and self.immortal.elapsed_time.seconds > IMMORTAL_TIME: self.immortal = None self.plane.color = sf.Color(255, 255, 255, 255) if self.plane_jumped: self.plane_speed.y = -200.0 if self.jump_time.elapsed_time.seconds < 0.25: self.plane.rotate(-2.5 * self.direction) else: self.plane_jumped = False self.jump_time = None if self.plane.rotation % 300 > 60: self.plane.rotation = ( 300, 60)[self.plane.rotation * self.direction > 300] if self.plane_speed.y <= 50 * GRAVITY: self.plane_speed += sf.Vector2(0.0, GRAVITY) self.plane.move(self.plane_speed * elapsed_time) self.plane.update(sf.seconds(elapsed_time))
def __init__(self, gameSize): self.computerPaddle = sf.RectangleShape() self.computerPaddle.size = sf.Vector2(98, 23) self.computerPaddle.origin = self.computerPaddle.size / 2 self.computerPaddle.fill_color = sf.Color(252, 216, 72) self.computerPaddle.position = gameSize.x / 2, 25 self.computerPaddle.outline_thickness = 2 self.computerPaddle.outline_color = sf.Color.WHITE self.paddleSpeed = 0 self.ai_timer = sf.Clock() self.ai_time = sf.seconds(0.1)
def update(self, elapsed_time): # Backgrounds self.backgrounds[0].move( sf.Vector2(-GROUND_SPEED / 10.0, 0.0) * elapsed_time) self.backgrounds[1].position = self.backgrounds[0].position + \ sf.Vector2(self.backgrounds[0].global_bounds.width, 0) if self.backgrounds[0].position.x <= -WWIDTH: self.backgrounds[0], self.backgrounds[1] = self.backgrounds[ 1], self.backgrounds[0] # Grounds self.grounds[0].move(sf.Vector2(-GROUND_SPEED, 0) * elapsed_time) self.grounds[1].position = self.grounds[0].position + sf.Vector2( self.grounds[0].global_bounds.width, 0) if self.grounds[0].position.x <= -WWIDTH: self.grounds[0], self.grounds[1] = self.grounds[1], self.grounds[0] # Plane if not self.plane_jumped and (self.plane.rotation <= 60 or self.plane.rotation >= 300): self.plane.rotate(1.25) if self.plane_jumped: self.plane_speed = sf.Vector2(0.0, -150.0) if self.jump_time.elapsed_time.seconds < 0.25: self.plane.rotate(-2.5) else: self.plane_jumped = False self.jump_time = None if self.plane.rotation % 300 > 60: self.plane.rotation = (300, 60)[self.plane.rotation > 300] if self.plane_speed.y <= 50 * GRAVITY: self.plane_speed += sf.Vector2(0.0, GRAVITY) self.plane.move(self.plane_speed * elapsed_time) self.plane.update(sf.seconds(elapsed_time)) # Rocks if len(self.rocks) > 0: self.rocks[0].move(sf.Vector2(-GROUND_SPEED, 0.0) * elapsed_time) self.rocks[1].position = sf.Vector2( self.rocks[0].position.x + 300, WHEIGHT - self.rocks[0].global_bounds.height) if self.rocks[1].position.x <= -self.rocks[1].global_bounds.width: for i in xrange(2): self.rocks.pop(0) self.spawn_rocks()
def __init__(self, font): sf.Drawable.__init__(self) self.time_per_text_update = sf.seconds(1 / UPDATES_PER_SECOND) self.update_time = sf.seconds(0) self.num_frames = 0 self.fps = deque([], UPDATES_PER_SECOND) self.t_update = deque([], UPDATES_PER_SECOND) self.t_render = deque([], UPDATES_PER_SECOND) self.text = sf.Text() self.text.font = font self.text.position = (5, 5) self.text.character_size = 14 self.text.color = sf.Color(220, 220, 100, 220) self.settings_text = sf.Text() self.settings_text.font = font self.settings_text.position = (5, settings.HEIGHT - 65) self.settings_text.character_size = 14 self.settings_text.color = sf.Color(220, 220, 100, 220) self.num_entities = 0 self.collision_checks = 0 self.help_text = sf.Text() self.help_text.font = font self.help_text.position = (settings.WIDTH - 300, 5) self.help_text.character_size = 14 self.help_text.color = sf.Color(220, 220, 100, 220) r = re.compile(r'(.+=) *sf\.Keyboard\.(.*)') with open("settings.py") as s: t = r.findall(s.read()) t = '\n'.join(map(' '.join, t)) self.help_text.string = "KEYBINDINGS:\n" + "------------\n" + t self.help = settings.HELP_ON
def __init__(self, font): sf.Drawable.__init__(self) self.time_per_text_update = sf.seconds(1/UPDATES_PER_SECOND) self.update_time = sf.seconds(0) self.num_frames = 0 self.fps = deque([], UPDATES_PER_SECOND) self.t_update = deque([], UPDATES_PER_SECOND) self.t_render = deque([], UPDATES_PER_SECOND) self.text = sf.Text() self.text.font = font self.text.position = (5, 5) self.text.character_size = 14 self.text.color = sf.Color(220, 220, 100, 220) self.settings_text = sf.Text() self.settings_text.font = font self.settings_text.position = (5, settings.HEIGHT-65) self.settings_text.character_size = 14 self.settings_text.color = sf.Color(220, 220, 100, 220) self.num_entities = 0 self.collision_checks = 0 self.help_text = sf.Text() self.help_text.font = font self.help_text.position = (settings.WIDTH-300, 5) self.help_text.character_size = 14 self.help_text.color = sf.Color(220, 220, 100, 220) r = re.compile(r'(.+=) *sf\.Keyboard\.(.*)') with open("settings.py") as s: t = r.findall(s.read()) t = '\n'.join(map(' '.join, t)) self.help_text.string = "KEYBINDINGS:\n" + "------------\n" + t self.help = settings.HELP_ON
def __init__(self, frametime=sf.seconds(0.2), paused=False, looped=True): super(AnimatedSprite, self).__init__() self.animation = None self.frametime = frametime self.paused = paused self.looped = looped self.current_time = None self.current_frame = 0 self.texture = None self.vertices = sf.VertexArray(sf.PrimitiveType.QUADS, 4)
def __init__(self, body, underwear, hair=None, duration=sf.seconds(1)): Animation.__init__(self, body.size(), duration) self._body = body self._underwear = underwear self._hair = hair self._mantle = None self._shoes = None self._legging = None self._hauberk = None self._armor = None self._weapon = None self._shield = None
def update(self, elapsed_time): # Backgrounds self.backgrounds[0].move(sf.Vector2(-GROUND_SPEED / 10.0, 0.0) * elapsed_time) self.backgrounds[1].position = self.backgrounds[0].position + \ sf.Vector2(self.backgrounds[0].global_bounds.width, 0) if self.backgrounds[0].position.x <= -WWIDTH: self.backgrounds[0], self.backgrounds[1] = self.backgrounds[1], self.backgrounds[0] # Grounds self.grounds[0].move(sf.Vector2(-GROUND_SPEED, 0) * elapsed_time) self.grounds[1].position = self.grounds[0].position + sf.Vector2(self.grounds[0].global_bounds.width, 0) if self.grounds[0].position.x <= -WWIDTH: self.grounds[0], self.grounds[1] = self.grounds[1], self.grounds[0] # Plane if not self.plane_jumped and (self.plane.rotation <= 60 or self.plane.rotation >= 300): self.plane.rotate(1.25) if self.plane_jumped: self.plane_speed = sf.Vector2(0.0, -150.0) if self.jump_time.elapsed_time.seconds < 0.25: self.plane.rotate(-2.5) else: self.plane_jumped = False self.jump_time = None if self.plane.rotation % 300 > 60: self.plane.rotation = (300, 60)[self.plane.rotation > 300] if self.plane_speed.y <= 50 * GRAVITY: self.plane_speed += sf.Vector2(0.0, GRAVITY) self.plane.move(self.plane_speed * elapsed_time) self.plane.update(sf.seconds(elapsed_time)) # Rocks if len(self.rocks) > 0: self.rocks[0].move(sf.Vector2(-GROUND_SPEED, 0.0) * elapsed_time) self.rocks[1].position = sf.Vector2(self.rocks[0].position.x + 300, WHEIGHT - self.rocks[0].global_bounds.height) if self.rocks[1].position.x <= -self.rocks[1].global_bounds.width: for i in xrange(2): self.rocks.pop(0) self.spawn_rocks()
def __init__(self): # Window self.window = sf.RenderWindow(sf.VideoMode(WWIDTH, WHEIGHT), WTITLE, sf.Style.CLOSE | sf.Style.TITLEBAR, SETTINGS) self.window.framerate_limit = 60 # Clock self.clock = sf.Clock() # View self.view = sf.View(sf.Rectangle((0, 0), (WWIDTH, WHEIGHT))) self.window.view = self.view # Loading assets self.load_assets() self.backgrounds = [sf.Sprite(self.bg_texture) for i in xrange(2)] self.grounds = [sf.Sprite(self.ground_texture) for i in xrange(2)] self.grounds[0].position = 0, 409 self.grounds[1].position = 0, 409 # Plane fly_anim = Animation() fly_anim.texture = self.plane_sheet fly_anim.add_frame(sf.Rectangle((0, 0), (88, 73))) fly_anim.add_frame(sf.Rectangle((88, 0), (88, 73))) fly_anim.add_frame(sf.Rectangle((176, 0), (88, 73))) fly_anim.add_frame(sf.Rectangle((88, 0), (88, 73))) self.plane = AnimatedSprite(sf.seconds(0.2), False, True) self.plane.play(fly_anim) self.plane.origin = self.plane.global_bounds.width / 2.0, self.plane.global_bounds.height / 2.0 self.plane.position = sf.Vector2(150, 200) self.plane_speed = sf.Vector2(0.0, 0.0) self.jump_time = None self.plane_jumped = False # Rocks self.rocks = [] self.spawn_rocks()
def create_animation(self, action): gender = self.gender skin_color = self.skin_color underwear_color = self.underwear_color mantle = self.mantle action, direction, duration = self.current_action texture, frames = self.data.human[gender, skin_color, action, direction] body = AnimationSprite(texture, frames) texture, frames = self.data.underwear[gender, action, direction, underwear_color] underwear = AnimationSprite(texture, frames) animation = HumanAnimation(body, underwear, None, sf.seconds(1)) if mantle: texture, frames = self.data.mantles[gender, mantle.appearance, action, direction] mantle = AnimationSprite(texture, frames) animation.mantle = mantle return animation
def run(self): if not self.load_assets(): sys.exit(-1) self.bird_animation = Animation() self.bird_animation.texture = self.bird_texture self.bird_animation.add_frame(sf.Rectangle((0, 0), (34, 24))) self.bird_animation.add_frame(sf.Rectangle((34, 0), (34, 24))) self.bird_animation.add_frame(sf.Rectangle((68, 0), (34, 24))) self.bird_sprite = AnimatedSprite(sf.seconds(0.2), False, True) self.bird_sprite.position = sf.Vector2(50, 50) while self.window.is_open: for e in self.window.events: self.event_handler(e) elapsed_time = self.clock.restart() self.update(elapsed_time) self.render()
def step(self, dt): #self.debug = [] #self.debug.append("(dt=%i/16 ms)" % dt) if not self.has_treasure and self.treasure_time.elapsed_time >= sf.seconds(120): self.treasure = Treasure(*random_point_not_near(self.player.position)) self.boss = Boss(*random_point_not_near(self.player.position)) self.creatures.append(self.boss) sound.boss1.play() #print("Treasure spawned at %s" % self.treasure.position) self.has_treasure = True if self.has_treasure and self.treasure.win_condition(self.player): self.has_ended = True self.next_state = GameWonState for c in self.creatures: if c.collides_with(self.player): self.creatures.remove(c) c.bite(self.player) if self.player.health <= 0: self.has_ended = True self.next_state = GameOverState for h in self.lives: if h.collides_with(self.player): self.lives.remove(h) h.heal(self.player) def exhaust(): self.is_running = False self.player.stamina -= 1 if self.player.stamina <= 0: sound.exhausted.play() self.stamina_regeneration_timer.restart() for event in self.window.events: if type(event) is sf.CloseEvent \ or (type(event) is sf.KeyEvent \ and event.pressed and event.code == sf.Keyboard.ESCAPE): self.next_state = ExitState self.has_ended = True elif type(event) is sf.KeyEvent and event.pressed \ and event.code == sf.Keyboard.X: self.is_dark = not self.is_dark elif type(event) is sf.KeyEvent and event.code == sf.Keyboard.L_SHIFT: if event.pressed and not self.is_running: self.is_running = True self.run_timer.restart() elif event.released and self.is_running: exhaust() if self.is_running and self.run_timer.elapsed_time >= sf.seconds(1): exhaust() #if self.is_running: # self.debug.append("sprint (" + str(self.run_timer.elapsed_time.seconds) + ")") if self.player.stamina > 0: delta = self.player_movement_vector(self.player) else: delta = sf.Vector2() view_delta = sf.Vector2() if self.player.position.x > WIDTH / 2 \ and self.player.position.x < MAP_WIDTH - WIDTH / 2: view_delta += (delta.x, 0) if self.player.position.y > HEIGHT / 2 \ and self.player.position.y < MAP_HEIGHT - HEIGHT / 2: view_delta += (0, delta.y) #self.debug.append("dr: %s" % delta) self.player.move(delta, dt) sf.Listener.set_position((self.player.position.x, self.player.position.y, 0)) self.view.move(view_delta.x * dt, view_delta.y * dt) #self.debug.append("Pos: %s" % self.player.position) #Monster movement for creature in self.creatures: creature.step(self.player, self.is_dark, dt) creature.sound_tick() if self.stamina_regeneration_timer.elapsed_time >= sf.seconds(5) \ and self.player.stamina < self.player.max_stamina: if self.player.stamina < 0: self.player.stamina = 0 self.player.stamina += 1 self.stamina_regeneration_timer.restart() self.life_point_display.points = self.player.health self.stamina_display.points = self.player.stamina
def main(): window = sf.RenderWindow(sf.VideoMode(800, 600), "PySFML Animation") window.framerate_limit = 60 texture = sf.Texture.from_file("assets/images/player.png") walking_down = Animation() walking_down.texture = texture walking_down.add_frame(sf.Rectangle((32, 0), (32, 32))) walking_down.add_frame(sf.Rectangle((64, 0), (32, 32))) walking_down.add_frame(sf.Rectangle((32, 0), (32, 32))) walking_down.add_frame(sf.Rectangle((0, 0), (32, 32))) walking_up = Animation() walking_up.texture = texture walking_up.add_frame(sf.Rectangle((32, 96), (32, 32))) walking_up.add_frame(sf.Rectangle((64, 96), (32, 32))) walking_up.add_frame(sf.Rectangle((32, 96), (32, 32))) walking_up.add_frame(sf.Rectangle((0, 96), (32, 32))) walking_left = Animation() walking_left.texture = texture walking_left.add_frame(sf.Rectangle((32, 32), (32, 32))) walking_left.add_frame(sf.Rectangle((64, 32), (32, 32))) walking_left.add_frame(sf.Rectangle((32, 32), (32, 32))) walking_left.add_frame(sf.Rectangle((0, 32), (32, 32))) walking_right = Animation() walking_right.texture = texture walking_right.add_frame(sf.Rectangle((32, 64), (32, 32))) walking_right.add_frame(sf.Rectangle((64, 64), (32, 32))) walking_right.add_frame(sf.Rectangle((32, 64), (32, 32))) walking_right.add_frame(sf.Rectangle((0, 64), (32, 32))) current_anim = walking_down anim_sprite = AnimatedSprite(sf.seconds(0.2), True, False) anim_sprite.position = sf.Vector2(400, 300) frame_clock = sf.Clock() speed = 80.0 no_key_pressed = True while window.is_open: for event in window.events: if type(event) is sf.CloseEvent: window.close() delta = frame_clock.elapsed_time frame_clock.restart() movement = sf.Vector2(0.0, 0.0) if sf.Keyboard.is_key_pressed(sf.Keyboard.DOWN): current_anim = walking_down movement += sf.Vector2(0.0, speed) no_key_pressed = False if sf.Keyboard.is_key_pressed(sf.Keyboard.UP): current_anim = walking_up movement += sf.Vector2(0.0, -speed) no_key_pressed = False if sf.Keyboard.is_key_pressed(sf.Keyboard.LEFT): current_anim = walking_left movement += sf.Vector2(-speed, 0.0) no_key_pressed = False if sf.Keyboard.is_key_pressed(sf.Keyboard.RIGHT): current_anim = walking_right movement += sf.Vector2(speed, 0.0) no_key_pressed = False anim_sprite.play(current_anim) anim_sprite.move(movement * delta.seconds) if no_key_pressed: anim_sprite.stop() no_key_pressed = True anim_sprite.update(delta) window.clear() window.draw(anim_sprite) window.display()
pos_y = ret[1] # load textures textureEven = load_texture(textureEven, "Player.png") textureOdd = load_texture(textureOdd, "Player2.png") textureSwim = load_texture(textureSwim, "PlayerSwim.png") texture_fight = load_texture(texture_fight, "fight.png") font = sf.Font.from_file("arial.ttf") vDatoInt = 0 # Asigno teclas segun si es par o impar texture = define_keyboard(keyboard, ident) animated_sprite = AnimatedSprite(sf.seconds(0.2), True, False) ret = set_animations(texture) walking_down = ret[0] walking_left = ret[1] walking_right = ret[2] walking_up = ret[3] current_animation = walking_down animated_sprite.texture = texture animated_sprite.position = (320, 320) final_tile = map_j.final_tile # TODO map_j.get_final_tile() frame_clock = sf.Clock()
import sfml as sf # STATIC VALUES TIME_PER_UPDATE = sf.seconds(1/60) WIDTH = 1280 HEIGHT = 720 CELL_SIZE = 60 ENTITY_SIZE = 20 ENTITY_MAX_SPEED = 100 INITIAL_ENTITIES = 50 HELP_ON = True # DEFAULT FLOCKING PARAMETERS attractive_mouse = False scary_mouse = False boid_sight_radius = 60 desired_separation = 50 max_steering_force = 80 separation = 0.2 alignment = 0.1 cohesion = 0.1
ball.origin = (ball_radius / 2, ball_radius / 2) # load the font font = sf.Font.from_file("data/sansation.ttf") # initialize the pause message pause_message = sf.Text() pause_message.font = font pause_message.character_size = 40 pause_message.position = (170, 150) pause_message.color = sf.Color.WHITE pause_message.string = "Welcome to pySFML pong!\nPress space to start the game" # define the paddles properties ai_timer = sf.Clock() ai_time = sf.seconds(0.1) paddle_speed = 400. right_paddle_speed = 0. ball_speed = 400. ball_angle = 0. # to be changed later clock = sf.Clock() is_playing = False while window.is_open: # handle events for event in window.events: # window closed or escape key pressed: exit if type(event) is sf.CloseEvent: window.close()
import sfml as sf # STATIC VALUES TIME_PER_UPDATE = sf.seconds(1 / 60) WIDTH = 1280 HEIGHT = 720 CELL_SIZE = 60 ENTITY_SIZE = 20 ENTITY_MAX_SPEED = 100 INITIAL_ENTITIES = 50 HELP_ON = True # DEFAULT FLOCKING PARAMETERS attractive_mouse = False scary_mouse = False boid_sight_radius = 60 desired_separation = 50 max_steering_force = 80 separation = 0.2 alignment = 0.1 cohesion = 0.1 # KEYBINDINGS toggle_help = sf.Keyboard.F1
def __update(self): for event in self.window.events: ### ### Process window events #### if type(event) is sf.CloseEvent: self.running = False ### ### Kill signal exit program... #### if type(event) is sf.ResizeEvent: self.window.size(event.size) ### ### Resize viewport if user changed the window dimensions... ### (Note: User can not do this.) #### ### ### Left Click Mouse Event... ### Prevent placement of a node to close to other nodes. #### if type(event) is sf.MouseButtonEvent and event.button is sf.Mouse.LEFT and event.pressed: ### ### This is for the placement of nodes... #### isOverNode = False for node in self.nodeList: if event.position.x > (node.x+20)-self.nodePaddingPlacement and\ event.position.x < (node.x+20)+self.nodePaddingPlacement and\ event.position.y > (node.y+20)-self.nodePaddingPlacement and\ event.position.y < (node.y+20)+self.nodePaddingPlacement: isOverNode = True if isOverNode == False and event.position.y < (self.window_height - 200) and\ event.position.y > 100 and event.position.x > 40 and\ event.position.x < (self.window_width - 40): tmp = Node() tmp.x = (event.position.x - 20) tmp.y = (event.position.y - 20) guiListItems = ApplicationInterface() listFromGui = guiListItems.getInventory() if len(listFromGui) > 0: tmp.setIteams(listFromGui) tmp.setNumber(self.NodeCount) self.nodeList.append(tmp) self.NodeCount += 1 else: self.infoText = sf.Text("*** You can not make an empty Node ***", self.font, 18) self.infoText.position = 20, 40 ### ### this is for the help box... #### if self.isOverHelp == True: myHelp = ApplicationHelp() ### ### Right Click Mouse Event... ### Find the Node under the cursor and gets its Inventory. #### if type(event) is sf.MouseButtonEvent and event.button is sf.Mouse.RIGHT and event.pressed: for node in self.nodeList: node.isCurrunt = False if event.position.x > (node.x+20)-self.nodePadding and\ event.position.x < (node.x+20)+self.nodePadding and\ event.position.y > (node.y+20)-self.nodePadding and\ event.position.y < (node.y+20)+self.nodePadding: tmpstr = str(node.getIteams()) tmpstr = re.sub('[(){}<>\'\']', '', tmpstr) tmpstr = re.sub('[,]', '\n', tmpstr) tmpstr = re.sub('[:]', ': ', tmpstr) self.listString = sf.Text("Inventory:\n"+ " " + str(tmpstr), self.font, 20) self.listString.position = 20, self.window_height - 180 node.isCurrunt = True ### ### Middle Click Mouse Event... ### Place the pybot under the cursor location. #### if type(event) is sf.MouseButtonEvent and event.button is sf.Mouse.MIDDLE and event.pressed: tmp = Node() tmp.x = (event.position.x - 25) tmp.y = (event.position.y - 30) self.pyBot = tmp ### ### Esc key pressed exit program... #### if sf.Keyboard.is_key_pressed(sf.Keyboard.ESCAPE): self.running = False ### ### E keyboard key pressed start pybot... #### if sf.Keyboard.is_key_pressed(sf.Keyboard.E): if self.NodeCount > 1: self.waitForNextRun = False self.gui_find = ApplicationInterfaceLookFor() self.TimeToWait = sf.seconds(self.gui_find.getTime()) self.Sorting.Find(self.gui_find.getItem()) self.isPathing = True self.Clock.restart() self.clockTriggered = True else: self.infoText = sf.Text("*** You must have more then 2 nodes for pybot to function. ***", self.font, 18) self.infoText.position = 20, 40 ### ### R keyboard key pressed pause pybot... #### if sf.Keyboard.is_key_pressed(sf.Keyboard.R): self.isPathing = False
def __init__(self, body, duration=sf.seconds(1)): Animation.__init__(self, body.size(), duration) self._body = body
def step(self, dt): #self.debug = [] #self.debug.append("(dt=%i/16 ms)" % dt) if not self.has_treasure and self.treasure_time.elapsed_time >= sf.seconds( 120): self.treasure = Treasure( *random_point_not_near(self.player.position)) self.boss = Boss(*random_point_not_near(self.player.position)) self.creatures.append(self.boss) sound.boss1.play() #print("Treasure spawned at %s" % self.treasure.position) self.has_treasure = True if self.has_treasure and self.treasure.win_condition(self.player): self.has_ended = True self.next_state = GameWonState for c in self.creatures: if c.collides_with(self.player): self.creatures.remove(c) c.bite(self.player) if self.player.health <= 0: self.has_ended = True self.next_state = GameOverState for h in self.lives: if h.collides_with(self.player): self.lives.remove(h) h.heal(self.player) def exhaust(): self.is_running = False self.player.stamina -= 1 if self.player.stamina <= 0: sound.exhausted.play() self.stamina_regeneration_timer.restart() for event in self.window.events: if type(event) is sf.CloseEvent \ or (type(event) is sf.KeyEvent \ and event.pressed and event.code == sf.Keyboard.ESCAPE): self.next_state = ExitState self.has_ended = True elif type(event) is sf.KeyEvent and event.pressed \ and event.code == sf.Keyboard.X: self.is_dark = not self.is_dark elif type(event ) is sf.KeyEvent and event.code == sf.Keyboard.L_SHIFT: if event.pressed and not self.is_running: self.is_running = True self.run_timer.restart() elif event.released and self.is_running: exhaust() if self.is_running and self.run_timer.elapsed_time >= sf.seconds(1): exhaust() #if self.is_running: # self.debug.append("sprint (" + str(self.run_timer.elapsed_time.seconds) + ")") if self.player.stamina > 0: delta = self.player_movement_vector(self.player) else: delta = sf.Vector2() view_delta = sf.Vector2() if self.player.position.x > WIDTH / 2 \ and self.player.position.x < MAP_WIDTH - WIDTH / 2: view_delta += (delta.x, 0) if self.player.position.y > HEIGHT / 2 \ and self.player.position.y < MAP_HEIGHT - HEIGHT / 2: view_delta += (0, delta.y) #self.debug.append("dr: %s" % delta) self.player.move(delta, dt) sf.Listener.set_position( (self.player.position.x, self.player.position.y, 0)) self.view.move(view_delta.x * dt, view_delta.y * dt) #self.debug.append("Pos: %s" % self.player.position) #Monster movement for creature in self.creatures: creature.step(self.player, self.is_dark, dt) creature.sound_tick() if self.stamina_regeneration_timer.elapsed_time >= sf.seconds(5) \ and self.player.stamina < self.player.max_stamina: if self.player.stamina < 0: self.player.stamina = 0 self.player.stamina += 1 self.stamina_regeneration_timer.restart() self.life_point_display.points = self.player.health self.stamina_display.points = self.player.stamina
menubar = self.menuBar() file_menu = menubar.addMenu('&File') file_menu.addAction(new_game) file_menu.addAction(close_action) self.setGeometry(500, 500, 500, 500) self.setWindowTitle('pedeRPG') self.show() def main(): app = QtGui.QApplication(sys.argv) menu = Menu() sys.exit(app.exec_()) if __name__ == '__main__': main() RESOLUTION = 1024, 768 WALLPAPER = 'f****t.png' TITLE = "Placeholder for shitfuck" window = sf.RenderWindow(sf.VideoMode(*RESOLUTION), TITLE) texture = sf.Texture.from_file(WALLPAPER) sprite = sf.Sprite(texture) window.draw(sprite) window.display() sf.sleep(sf.seconds(5))
ball.origin = (ball_radius / 2, ball_radius / 2) # load the font font = sf.Font.from_file("data/sansation.ttf") # initialize the pause message pause_message = sf.Text() pause_message.font = font pause_message.character_size = 40 pause_message.position = (170, 150) pause_message.color = sf.Color.WHITE pause_message.string = "Welcome to pySFML pong!\nPress space to start the game" # define the paddles properties ai_timer = sf.Clock() ai_time = sf.seconds(0.1) paddle_speed = 400.0 right_paddle_speed = 0.0 ball_speed = 400.0 ball_angle = 0.0 # to be changed later clock = sf.Clock() is_playing = False while window.is_open: # handle events for event in window.events: # window closed or escape key pressed: exit if type(event) is sf.CloseEvent: window.close()