def __init__(self, soundfile): self.soundfile = soundfile self.sound = sf.Sound() try: buffer = sf.SoundBuffer.from_file(soundfile) except IOError: exit(1) self.sound.buffer = buffer
def load_assets(self): try: buffer = sf.SoundBuffer.from_file('assets/sounds/tone1.ogg') self.ball_sound = sf.Sound(buffer) self.font = sf.Font.from_file( 'assets/fonts/kenvector_future_thin.ttf') except IOError: return False return True
def get(path): try: return SoundManager.sounds[path] except KeyError: try: buffer = sf.SoundBuffer.from_file(path) sound = sf.Sound(buffer) SoundManager.sounds[path] = sound return sound except IOError: print("Could not load sound %s!" % path) return None
def __init__(self, mainScreen, ruleTables, x=0, y=200, width=200, height=75, text="DEEP IQ's Turn"): self.tables = RuleTables(self, mainScreen) self.x = x self.y = y self.width = width self.height = height self.text = text self.message = "DEEP IQ waits idly. " self.coolDown = sf.Clock() self.canBeClicked = True self.mainScreen = mainScreen box = sf.RectangleShape() box.size = (self.width, self.height) box.outline_color = sf.Color.WHITE box.outline_thickness = 3 box.fill_color = sf.Color.TRANSPARENT box.position = (self.x, self.y) self.box = box buffers = [ sf.SoundBuffer.from_file( os.path.join(os.getcwd(), "res", "dice0.ogg")), sf.SoundBuffer.from_file( os.path.join(os.getcwd(), "res", "dice0.ogg")), sf.SoundBuffer.from_file( os.path.join(os.getcwd(), "res", "dice1.ogg")), sf.SoundBuffer.from_file( os.path.join(os.getcwd(), "res", "dice2.ogg")), sf.SoundBuffer.from_file( os.path.join(os.getcwd(), "res", "dice3.ogg")), sf.SoundBuffer.from_file( os.path.join(os.getcwd(), "res", "dice4.ogg")) ] self.sounds = [] for buffer in buffers: sound = sf.Sound() sound.buffer = buffer self.sounds.append(sound)
def run(): window = sf.RenderWindow(sf.VideoMode(WWIDTH, WHEIGHT), WTITLE) window.framerate_limit = 60 # Background bg_texture = sf.Texture.from_file("assets/images/background.png") background = sf.Sprite(bg_texture) # Ball b_texture = sf.Texture.from_file("assets/images/ball.png") ball = sf.CircleShape(35) ball.texture = b_texture ball.origin = 35, 35 ball.position = WWIDTH / 2.0, WHEIGHT / 2.0 speed = sf.Vector2(randint(-5, 5), randint(-5, 5)) * 50.0 # Paddle 1 paddle_1 = sf.RectangleShape((50, 175)) paddle_1.origin = 25.0, 82.5 paddle_1.position = 50, WHEIGHT / 2.0 # Paddle 2 paddle_2 = sf.RectangleShape((50, 175)) paddle_2.origin = 25.0, 82.5 paddle_2.position = WWIDTH - 50, WHEIGHT / 2.0 # Scores scored = False p1_score, p2_score = 0, 0 # Font font = sf.Font.from_file("assets/fonts/kenvector.ttf") # Texts p1_score_text = sf.Text(str(p1_score)) p1_score_text.font = font p1_score_text.character_size = 72 p1_score_text.color = sf.Color.WHITE p1_score_text.position = 170, 100 p2_score_text = sf.Text(str(p2_score)) p2_score_text.font = font p2_score_text.character_size = 72 p2_score_text.color = sf.Color.WHITE p2_score_text.position = 570, 100 # Sound s_buffer = sf.SoundBuffer.from_file("assets/sounds/tone1.ogg") sound = sf.Sound(s_buffer) # Clock clock = sf.Clock() while window.is_open: for event in window.events: if type(event) is sf.CloseEvent: window.close() # Close if sf.Keyboard.is_key_pressed(sf.Keyboard.ESCAPE): window.close() elapsed = clock.elapsed_time.seconds clock.restart() # Inputs if sf.Keyboard.is_key_pressed(sf.Keyboard.W): paddle_1.move(sf.Vector2(0, -PADDLE_SPEED)) if paddle_1.position.y < paddle_1.origin.y: paddle_1.position = sf.Vector2(paddle_1.position.x, paddle_1.origin.y) if sf.Keyboard.is_key_pressed(sf.Keyboard.S): paddle_1.move(sf.Vector2(0, PADDLE_SPEED)) if paddle_1.position.y > WHEIGHT - paddle_1.origin.y: paddle_1.position = sf.Vector2(paddle_1.position.x, WHEIGHT - paddle_1.origin.y) if sf.Keyboard.is_key_pressed(sf.Keyboard.UP): paddle_2.move(sf.Vector2(0, -PADDLE_SPEED)) if paddle_2.position.y < paddle_2.origin.y: paddle_2.position = sf.Vector2(paddle_2.position.x, paddle_2.origin.y) if sf.Keyboard.is_key_pressed(sf.Keyboard.DOWN): paddle_2.move(sf.Vector2(0, PADDLE_SPEED)) if paddle_2.position.y > WHEIGHT - paddle_2.origin.y: paddle_2.position = sf.Vector2(paddle_2.position.x, WHEIGHT - paddle_2.origin.y) if scored: speed = sf.Vector2(randint(-5, 5), randint(-5, 5)) * 50.0 ball.position = WWIDTH / 2.0, WHEIGHT / 2.0 paddle_1.position = 50, WHEIGHT / 2.0 paddle_2.position = WWIDTH - 50, WHEIGHT / 2.0 scored = False ball.move(speed * elapsed) if ball.position.x < ball.origin.x or ball.position.x > WWIDTH - ball.origin.x: scored = True if ball.position.x < ball.origin.x: p2_score += 1 else: p1_score += 1 p1_score_text.string = str(p1_score) p2_score_text.string = str(p2_score) if ball.position.y < ball.origin.y or ball.position.y > WHEIGHT - ball.origin.y: speed = sf.Vector2(speed.x, speed.y * -1.0) p1_col = ball.global_bounds.intersects(paddle_1.global_bounds) if p1_col: sound.play() if p1_col.top + p1_col.height / 2.0 > paddle_1.position.y: y = (-1.0, 1.0)[speed.y > 0] else: y = (1.0, -1.0)[speed.y > 0] x_factor = (1.0, 1.05)[-MAX_BALL_SPEED < speed.x < MAX_BALL_SPEED] speed = sf.Vector2(speed.x * -1.0 * x_factor, speed.y * y) p2_col = ball.global_bounds.intersects(paddle_2.global_bounds) if p2_col: sound.play() if p2_col.top + p2_col.height / 2.0 > paddle_2.position.y: y = (-1.0, 1.0)[speed.y > 0] else: y = (1.0, -1.0)[speed.y > 0] x_factor = (1.0, 1.05)[-MAX_BALL_SPEED < speed.x < MAX_BALL_SPEED] speed = sf.Vector2(speed.x * -1.0 * x_factor, speed.y * y) # Rendering window.clear(sf.Color.BLACK) window.draw(background) window.draw(ball) window.draw(paddle_1) window.draw(paddle_2) window.draw(p1_score_text) window.draw(p2_score_text) window.display()
import sfml as sf # define some constants game_size = sf.Vector2(800, 600) paddle_size = sf.Vector2(25, 100) ball_radius = 10. # create the window of the application w, h = game_size window = sf.RenderWindow(sf.VideoMode(w, h), "pySFML - Pong") window.vertical_synchronization = True # load the sounds used in the game ball_sound_buffer = sf.SoundBuffer.from_file("data/ball.wav") ball_sound = sf.Sound(ball_sound_buffer) # create the left paddle left_paddle = sf.RectangleShape() left_paddle.size = paddle_size - (3, 3) left_paddle.outline_thickness = 3 left_paddle.outline_color = sf.Color.BLACK left_paddle.fill_color = sf.Color(100, 100, 200) left_paddle.origin = paddle_size / 2 # create the right paddle right_paddle = sf.RectangleShape() right_paddle.size = paddle_size - (3, 3) right_paddle.outline_thickness = 3 right_paddle.outline_color = sf.Color.BLACK right_paddle.fill_color = sf.Color(200, 100, 100)
import sfml as sf import time # define some constants game_size = sf.Vector2(800, 900) tank_size = sf.Vector2(100, 25) # create the window of the application w, h = game_size window = sf.RenderWindow(sf.VideoMode(w, h), "Blizzard Blaster") window.vertical_synchronization = True # load the sounds used in the game dray_sound_buffer = sf.SoundBuffer.from_file("data/laser.wav") dray_sound = sf.Sound(dray_sound_buffer) rampage_sound_buffer = sf.SoundBuffer.from_file("data/rampage.wav") rampage_sound = sf.Sound(rampage_sound_buffer) unstoppable_sound_buffer = sf.SoundBuffer.from_file("data/unstoppable.wav") unstoppable_sound = sf.Sound(unstoppable_sound_buffer) whicked_sick_sound_buffer = sf.SoundBuffer.from_file("data/whickedsick.wav") whicked_sick_sound = sf.Sound(whicked_sick_sound_buffer) play_sound_buffer = sf.SoundBuffer.from_file("data/prepare4.wav") play_sound = sf.Sound(play_sound_buffer) eagle_eye_sound_buffer = sf.SoundBuffer.from_file("data/eagleeye.wav") eagle_eye_sound = sf.Sound(eagle_eye_sound_buffer)
import sfml as sf ambient = sf.Sound(sf.SoundBuffer.from_file("sound/ambiance.ogg")) roar = sf.Sound(sf.SoundBuffer.from_file("sound/roar.ogg")) grue = sf.Sound(sf.SoundBuffer.from_file("sound/monster-1.ogg")) heal = sf.Sound(sf.SoundBuffer.from_file("sound/bell.ogg")) exhausted = sf.Sound(sf.SoundBuffer.from_file("sound/exhausted.ogg")) boss1 = sf.Sound(sf.SoundBuffer.from_file("sound/boss.ogg")) boss2 = sf.Sound(sf.SoundBuffer.from_file("sound/boss2.ogg"))
def main(): # Create the window of the application window = sf.RenderWindow(sf.VideoMode(800, 600, 32), 'PySFML Pong'); # Load the sounds used in the game ball_sound_buffer = sf.SoundBuffer.load_from_file("resources/ball.wav") ball_sound = sf.Sound(ball_sound_buffer); # Load the textures used in the game background_texture = sf.Texture.load_from_file('resources/background.jpg') left_paddle_texture = sf.Texture.load_from_file('resources/paddle_left.png') right_paddle_texture = sf.Texture.load_from_file( 'resources/paddle_right.png') ball_texture = sf.Texture.load_from_file('resources/ball.png') # Load the text font font = sf.Font.load_from_file('resources/sansation.ttf') # Initialize the end text end = sf.Text() end.font = font end.character_size = 60 end.move(150, 200); end.color = sf.Color(50, 50, 250) # Create the sprites of the background, the paddles and the ball background = sf.Sprite(background_texture) left_paddle = sf.Sprite(left_paddle_texture) right_paddle = sf.Sprite(right_paddle_texture) ball = sf.Sprite(ball_texture) left_paddle.move( 10, (window.view.size[1] - left_paddle.global_bounds.height) / 2) right_paddle.move( window.view.size[0] - right_paddle.global_bounds.width - 10, (window.view.size[1] - right_paddle.global_bounds.height) / 2) ball.move((window.view.size[0] - ball.global_bounds.width) / 2, (window.view.size[1] - ball.global_bounds.height) / 2) # Define the paddles properties ai_timer = sf.Clock() ai_time = sf.Time(milliseconds=100) left_paddle_speed = 0.4 right_paddle_speed = 0.4 # Define the ball properties ball_speed = 0.4 ball_angle = 0.0 clock = sf.Clock() while True: # Make sure the ball initial angle is not too much vertical ball_angle = random.uniform(0.0, 2 * math.pi) if abs(math.cos(ball_angle)) < 0.7: break is_playing = True while window.open: # Handle events for event in window.iter_events(): # Window closed or escape key pressed : exit if ((event.type == sf.Event.CLOSED) or (event.type == sf.Event.KEY_PRESSED and event.code == sf.Keyboard.ESCAPE)): window.close() break frame_time = clock.restart().as_milliseconds() if is_playing: # Move the player's paddle if (sf.Keyboard.is_key_pressed(sf.Keyboard.UP) and left_paddle.y > 5.0): left_paddle.move(0.0, -left_paddle_speed * frame_time) if (sf.Keyboard.is_key_pressed(sf.Keyboard.DOWN) and (left_paddle.y < window.view.size[1] - left_paddle.global_bounds.height - 5.0)): left_paddle.move(0.0, left_paddle_speed * frame_time) # Move the computer's paddle if (((right_paddle_speed < 0.0) and (right_paddle.y > 5.0)) or ((right_paddle_speed > 0.0) and (right_paddle.y < window.view.size[1] - right_paddle.global_bounds.height - 5.0))): right_paddle.move(0.0, right_paddle_speed * frame_time) # Update the computer's paddle direction according # to the ball position if ai_timer.elapsed_time > ai_time: ai_timer.restart() if (right_paddle_speed < 0 and (ball.y + ball.global_bounds.height > right_paddle.y + right_paddle.global_bounds.height)): right_paddle_speed = -right_paddle_speed if right_paddle_speed > 0 and ball.y < right_paddle.y: right_paddle_speed = -right_paddle_speed; # Move the ball factor = ball_speed * frame_time ball.move(math.cos(ball_angle) * factor, math.sin(ball_angle) * factor) # Check collisions between the ball and the screen if ball.x < 0.0: is_playing = False end.string = "You lost !\n(press escape to exit)" if ball.x + ball.global_bounds.width > window.view.size[0]: is_playing = False end.string = "You won !\n(press escape to exit)" if ball.y < 0.0: ball_sound.play(); ball_angle = -ball_angle ball.y = 0.1 if ball.y + ball.global_bounds.height > window.view.size[1]: ball_sound.play() ball_angle = -ball_angle ball.y = window.view.size[1] - ball.global_bounds.height - 0.1 # Check the collisions between the ball and the paddles # Left Paddle if (ball.x < left_paddle.x + left_paddle.global_bounds.width and ball.x > left_paddle.x + (left_paddle.global_bounds.width / 2.0) and ball.y + ball.global_bounds.height >= left_paddle.y and ball.y <= left_paddle.y + left_paddle.global_bounds.height): ball_sound.play() ball_angle = math.pi - ball_angle ball.x = left_paddle.x + left_paddle.global_bounds.width + 0.1 # Right Paddle if (ball.x + ball.global_bounds.width > right_paddle.x and ball.x + ball.global_bounds.width < right_paddle.x + (right_paddle.global_bounds.width / 2.0) and ball.y + ball.global_bounds.height >= right_paddle.y and ball.y <= right_paddle.y + right_paddle.global_bounds.height): # ball_sound.play(); ball_angle = math.pi - ball_angle ball.x = right_paddle.x - ball.global_bounds.width - 0.1 # Clear the window window.clear() # Draw the background, paddles and ball sprites window.draw(background) window.draw(left_paddle) window.draw(right_paddle) window.draw(ball) # If the game is over, display the end message if not is_playing: window.draw(end) # Display things on screen window.display()
# Initialize the window window = sfml.RenderWindow(sfml.VideoMode.get_desktop_mode(), 'Rainbow Fire', sfml.window.Style.FULLSCREEN) visible_area = sfml.Rectangle(sfml.Vector2(0, 0), window.size) # Seed random random.seed(time.time()) # Load textures rainbow_dash_tx = sfml.Texture.from_file('rainbow_dash.png') evil_pony_tx = sfml.Texture.from_file('evil_pony.png') blast_tx = sfml.Texture.from_file('blast.png') bullet_tx = sfml.Texture.from_file('bullet.png') # Load sounds blast_sound = sfml.Sound(sfml.SoundBuffer.from_file('blast.wav')) shot_sound = sfml.Sound(sfml.SoundBuffer.from_file('shot.wav')) smash_sound = sfml.Sound(sfml.SoundBuffer.from_file('smash.wav')) # Load font bangers_ft = sfml.Font.from_file('Bangers.ttf') # Score score = 0 score_text = sfml.Text('Score: ' + str(score), bangers_ft, 60) score_text.position = (window.size.x - score_text.global_bounds.width - 20, 10) # Lives lives = 5 lives_text = sfml.Text('Lives: ' + str(lives), bangers_ft, 60) lives_text.position = (20, 10)
bg_texture = sf.Texture.from_file('assets/images/background.png') plane_texture = sf.Texture.from_file('assets/images/planeRed1.png') s_buffer = sf.SoundBuffer.from_file('assets/sounds/tone1.ogg') music = sf.Music.from_file('assets/sounds/spaceTrash3.ogg') font = sf.Font.from_file('assets/fonts/kenvector_future_thin.ttf') except IOError: print("HATA VERDİ!!") sys.exit(-1) background = sf.Sprite(bg_texture) plane = sf.Sprite(plane_texture) plane.position = 100, 100 plane_vel = sf.Vector2(0.0, 0.0) sound = sf.Sound(s_buffer) yazi = sf.Text("Python Oyun Kurs") yazi.font = font yazi.character_size = 25 yazi.position = 100, 100 yazi.color = sf.Color.BLACK count = 0 while window.is_open: for event in window.events: if type(event) is sf.CloseEvent: window.close() if type(event) is sf.KeyEvent: if event.released and event.code is sf.Keyboard.SPACE: